Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion xtest/sdk/scripts/cleanup-all.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,47 @@

SCRIPT_DIR=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &>/dev/null && pwd)

# Function to get the bare repo path for each SDK
get_bare_repo_path() {
local sdk=$1
case "$sdk" in
js)
echo "$SCRIPT_DIR/../js/src/web-sdk.git"
;;
java)
echo "$SCRIPT_DIR/../java/src/java-sdk.git"
;;
go)
echo "$SCRIPT_DIR/../go/src/otdfctl.git"
;;
*)
echo ""
;;
esac
}
Comment on lines +7 to +23
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This get_bare_repo_path function duplicates logic from xtest/sdk/scripts/checkout-sdk-branch.sh for determining the bare repository path for each SDK. This duplication can lead to maintenance issues, as changes to SDK paths or names would need to be updated in both files.

To improve maintainability, consider consolidating this logic into a single, shared script or configuration file that both checkout-sdk-branch.sh and this script can use. For example, you could create a helper script that sets environment variables for the paths based on the SDK language.


for sdk in go java js; do
rm -rf "$SCRIPT_DIR/../$sdk/dist"

bare_repo_path=$(get_bare_repo_path "$sdk")

for branch in "$SCRIPT_DIR/../${sdk}/src/"*; do
# Check if the path ends with .git
# Check if the path ends with .git (skip bare repos)
if [[ $branch == *.git ]]; then
continue
fi

if [ -d "$branch" ]; then
if ! git --git-dir="$bare_repo_path" worktree remove "$branch" --force; then
echo "Failed to remove worktree: $sdk#$branch"
fi
rm -rf "$branch"
Comment on lines +37 to 40
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The current logic for removing worktrees can be simplified and made less noisy. The git worktree remove command will fail and print an error if a directory is not a worktree, which creates unnecessary noise in the logs. Additionally, the rm -rf "$branch" command is redundant if git worktree remove succeeds, as it already removes the directory.

A cleaner approach is to attempt worktree remove and, only if it fails (for any reason, including not being a worktree), then fall back to rm -rf. This avoids the redundant call and suppresses unnecessary error messages for non-worktree directories.

Suggested change
if ! git --git-dir="$bare_repo_path" worktree remove "$branch" --force; then
echo "Failed to remove worktree: $sdk#$branch"
fi
rm -rf "$branch"
if ! git --git-dir="$bare_repo_path" worktree remove "$branch" --force 2>/dev/null; then
# If worktree remove fails (e.g., not a worktree), fall back to rm -rf.
rm -rf "$branch"
fi

fi
done

# Clean up any orphaned worktree registrations
if [[ -d "$bare_repo_path" ]]; then
echo "Pruning orphaned worktrees for $sdk..."
git --git-dir="$bare_repo_path" worktree prune 2>/dev/null || true
fi
done