diff --git a/main.go b/main.go index 2129474..fdad469 100644 --- a/main.go +++ b/main.go @@ -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()) diff --git a/test_e2e.sh b/test_e2e.sh index 53717a5..9ac063f 100755 --- a/test_e2e.sh +++ b/test_e2e.sh @@ -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 ##############################################