fix: infinite bad loop caused by unexpected worktree directory removal

This commit is contained in:
Aleksandar Markovski 2023-10-12 00:53:07 +02:00 committed by Tim Hockin
parent 6426efe346
commit ff51ca92dc
2 changed files with 46 additions and 1 deletions

View File

@ -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())

View File

@ -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
##############################################