Skip to content

Commit 482b8f3

Browse files
rsahlberggitster
authored andcommitted
checkout.c: use ref_exists instead of file_exist
Change checkout.c to check if a ref exists instead of checking if a loose ref file exists when deciding if to delete an orphaned log file. Otherwise, if a ref only exists as a packed ref without a corresponding loose ref for the currently checked out branch, we risk that the reflog will be deleted when we switch to a different branch. Update the reflog tests to check for this bug. The following reproduces the bug: $ git init-db $ git config core.logallrefupdates true $ git commit -m Initial --allow-empty [master (root-commit) bb11abe] Initial $ git reflog master [8561dcb master@{0}: commit (initial): Initial] $ find .git/{refs,logs} -type f | grep master [.git/refs/heads/master] [.git/logs/refs/heads/master] $ git branch foo $ git pack-refs --all $ find .git/{refs,logs} -type f | grep master [.git/logs/refs/heads/master] $ git checkout foo $ find .git/{refs,logs} -type f | grep master ... reflog file is missing ... $ git reflog master ... nothing ... Signed-off-by: Ronnie Sahlberg <[email protected]> Acked-by: Michael Haggerty <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 4da5883 commit 482b8f3

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

builtin/checkout.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -651,10 +651,7 @@ static void update_refs_for_switch(const struct checkout_opts *opts,
651651
}
652652
}
653653
if (old->path && old->name) {
654-
char ref_file[PATH_MAX];
655-
656-
git_snpath(ref_file, sizeof(ref_file), "%s", old->path);
657-
if (!file_exists(ref_file) && reflog_exists(old->path))
654+
if (!ref_exists(old->path) && reflog_exists(old->path))
658655
delete_reflog(old->path);
659656
}
660657
}

t/t1410-reflog.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,4 +245,12 @@ test_expect_success 'gc.reflogexpire=false' '
245245
246246
'
247247

248+
test_expect_success 'checkout should not delete log for packed ref' '
249+
test $(git reflog master | wc -l) = 4 &&
250+
git branch foo &&
251+
git pack-refs --all &&
252+
git checkout foo &&
253+
test $(git reflog master | wc -l) = 4
254+
'
255+
248256
test_done

0 commit comments

Comments
 (0)