git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v2 2/2] checkout.c: use ref_exists instead of file_exist

From
Ronnie Sahlberg <sahlberg@google.com>
Date
May 6, 2014, 22:45 UTC
Message-ID
<1399416353-31817-3-git-send-email-sahlberg@google.com>
In-Reply-To
<1399416353-31817-1-git-send-email-sahlberg@google.com>

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 <sahlberg@google.com>
---
 builtin/checkout.c | 5 +----
 t/t1410-reflog.sh  | 8 ++++++++
 2 files changed, 9 insertions(+), 4 deletions(-)
diff --git a/builtin/checkout.c b/builtin/checkout.c
index 929f5bd..f1dc56e 100644
--- a/builtin/checkout.c
+++ b/builtin/checkout.c
@@ -651,10 +651,7 @@ static void update_refs_for_switch(const struct checkout_opts *opts,
 			}
 		}
 		if (old->path && old->name) {
-			char ref_file[PATH_MAX];
-
-			git_snpath(ref_file, sizeof(ref_file), "%s", old->path);
-			if (!file_exists(ref_file) && reflog_exists(old->path))
+			if (!ref_exists(old->path) && reflog_exists(old->path))
 				delete_reflog(old->path);
 		}
 	}
diff --git a/t/t1410-reflog.sh b/t/t1410-reflog.sh
index 236b13a..8cab06f 100755
--- a/t/t1410-reflog.sh
+++ b/t/t1410-reflog.sh
@@ -245,4 +245,12 @@ test_expect_success 'gc.reflogexpire=false' '
 
 '
 
+test_expect_success 'checkout should not delete log for packed ref' '
+	test $(git reflog master | wc -l) = 4 &&
+	git branch foo &&
+	git pack-refs --all &&
+	git checkout foo &&
+	test $(git reflog master | wc -l) = 4
+'
+
 test_done
-- 
2.0.0.rc1.354.g7561c2b.dirty
Previous: Ronnie SahlbergNext: Michael Haggerty
Message 3 of 5 in “add a reflog_exists and delete_reflog abstraction”
  1. 0/2 add a reflog_exists and delete_reflog abstractionRonnie Sahlberg, May 6, 2014
  2. 1/2 refs.c: add new functions reflog_exists and delete_reflogRonnie Sahlberg, May 6, 2014
  3. 2/2 checkout.c: use ref_exists instead of file_existRonnie Sahlberg, May 6, 2014
  4. Michael HaggertyMay 7, 2014
  5. Junio C HamanoMay 7, 2014

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.