Skip to content

Commit ed49d74

Browse files
jbachorikclaude
andcommitted
Reuse existing worktrees instead of failing
Check for existing worktrees and reuse them if available. Only cleanup worktrees that we created (identified by PID in path). Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
1 parent 1dbf84d commit ed49d74

1 file changed

Lines changed: 20 additions & 7 deletions

File tree

ddprof-lib/benchmarks/branch-prediction/compare_branch_prediction.sh

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,13 @@ cleanup() {
3636
log_info "Cleaning up worktrees..."
3737
cd "${REPO_ROOT}"
3838

39-
if [ -n "${BASELINE_WORKTREE}" ] && [ -d "${BASELINE_WORKTREE}" ]; then
39+
# Only remove worktrees that we created (contain our PID in the path)
40+
if [ -n "${BASELINE_WORKTREE}" ] && [[ "${BASELINE_WORKTREE}" == *"-$$" ]] && [ -d "${BASELINE_WORKTREE}" ]; then
4041
git worktree remove -f "${BASELINE_WORKTREE}" 2>/dev/null || true
4142
fi
4243

43-
# Only cleanup optimized worktree if it's not the current repo
44-
if [ -n "${OPTIMIZED_WORKTREE}" ] && [ "${OPTIMIZED_WORKTREE}" != "${REPO_ROOT}" ] && [ -d "${OPTIMIZED_WORKTREE}" ]; then
44+
# Only cleanup optimized worktree if it's not the current repo and we created it
45+
if [ -n "${OPTIMIZED_WORKTREE}" ] && [ "${OPTIMIZED_WORKTREE}" != "${REPO_ROOT}" ] && [[ "${OPTIMIZED_WORKTREE}" == *"-$$" ]] && [ -d "${OPTIMIZED_WORKTREE}" ]; then
4546
git worktree remove -f "${OPTIMIZED_WORKTREE}" 2>/dev/null || true
4647
fi
4748
}
@@ -174,18 +175,30 @@ main() {
174175
# Get current branch
175176
local current_branch=$(git branch --show-current)
176177

177-
# Create worktrees for both branches
178+
# Check for existing worktrees
179+
local existing_baseline=$(git worktree list | grep "${baseline_branch}" | awk '{print $1}')
180+
local existing_optimized=$(git worktree list | grep "${optimized_branch}" | awk '{print $1}')
181+
182+
# Create or reuse worktrees for both branches
178183
BASELINE_WORKTREE="${REPO_ROOT}/../java-profiler-baseline-$$"
179184
OPTIMIZED_WORKTREE="${REPO_ROOT}/../java-profiler-optimized-$$"
180185

181-
log_step "1/6: Creating worktree for baseline (${baseline_branch})..."
182-
git worktree add "${BASELINE_WORKTREE}" "${baseline_branch}"
186+
log_step "1/6: Setting up worktree for baseline (${baseline_branch})..."
187+
if [ -n "${existing_baseline}" ]; then
188+
log_info "Reusing existing worktree at ${existing_baseline}"
189+
BASELINE_WORKTREE="${existing_baseline}"
190+
else
191+
git worktree add "${BASELINE_WORKTREE}" "${baseline_branch}"
192+
fi
183193

184-
log_step "2/6: Creating worktree for optimized (${optimized_branch})..."
194+
log_step "2/6: Setting up worktree for optimized (${optimized_branch})..."
185195
# If we're already on the optimized branch, use current worktree
186196
if [ "${current_branch}" = "${optimized_branch}" ]; then
187197
log_info "Already on ${optimized_branch}, using current worktree"
188198
OPTIMIZED_WORKTREE="${REPO_ROOT}"
199+
elif [ -n "${existing_optimized}" ]; then
200+
log_info "Reusing existing worktree at ${existing_optimized}"
201+
OPTIMIZED_WORKTREE="${existing_optimized}"
189202
else
190203
git worktree add "${OPTIMIZED_WORKTREE}" "${optimized_branch}"
191204
fi

0 commit comments

Comments
 (0)