Skip to content

Commit

Permalink
fix: infinite bad loop caused by unexpected worktree directory removal
Browse files Browse the repository at this point in the history
  • Loading branch information
bakome authored and thockin committed Oct 13, 2023
1 parent 6426efe commit ff51ca9
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2038,7 +2038,7 @@ func (git *repoSync) SyncRepo(ctx context.Context, refreshCreds func(context.Con
// Regular cleanup will happen in the outer loop, to catch stale
// worktrees.

if currentWorktree != git.worktreeFor(currentHash) {
if currentHash != "" && currentWorktree != git.worktreeFor(currentHash) {
// The old worktree might have come from a prior version, and so
// not get caught by the normal cleanup.
os.RemoveAll(currentWorktree.Path().String())
Expand Down
45 changes: 45 additions & 0 deletions test_e2e.sh
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,51 @@ function e2e::worktree_cleanup() {
assert_file_absent "$ROOT/.worktrees/not_a_directory"
}

##############################################
# Test worktree-unexpected-removal
##############################################
function e2e::worktree_unexpected_removal() {
GIT_SYNC \
--period=100ms \
--repo="file://$REPO" \
--root="$ROOT" \
--link="link" \
&

# wait for first sync
wait_for_sync "${MAXWAIT}"
assert_link_exists "$ROOT/link"
assert_file_exists "$ROOT/link/file"
assert_file_eq "$ROOT/link/file" "$FUNCNAME"
assert_metric_eq "${METRIC_GOOD_SYNC_COUNT}" 1
assert_metric_eq "${METRIC_FETCH_COUNT}" 1

# suspend time so we can fake corruption
docker ps --filter label="git-sync-e2e=$RUNID" --format="{{.ID}}" \
| while read CTR; do
docker pause "$CTR" >/dev/null
done

# make a unexpected removal
WT=$(git -C "$REPO" rev-list -n1 HEAD)
rm -r "$ROOT/.worktrees/$WT"

# resume time
docker ps --filter label="git-sync-e2e=$RUNID" --format="{{.ID}}" \
| while read CTR; do
docker unpause "$CTR" >/dev/null
done

echo "$METRIC_GOOD_SYNC_COUNT"

wait_for_sync "${MAXWAIT}"
assert_link_exists "$ROOT/link"
assert_file_exists "$ROOT/link/file"
assert_file_eq "$ROOT/link/file" "$FUNCNAME"
assert_metric_eq "${METRIC_GOOD_SYNC_COUNT}" 2
assert_metric_eq "${METRIC_FETCH_COUNT}" 2
}

##############################################
# Test stale-worktree-timeout
##############################################
Expand Down

0 comments on commit ff51ca9

Please sign in to comment.