{"thread":{"id":"14339","subject":"[PATCH] avoid null SHA1 in oldest reflog","startedAt":"2008-07-08T04:38:54Z","lastAt":"2008-07-08T04:38:54Z","messageCount":1,"participants":["Jeff King"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"82547","messageId":"20080708043853.GA18130@sigill.intra.peff.net","threadId":"14339","inReplyTo":null,"subject":"[PATCH] avoid null SHA1 in oldest reflog","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2008-07-08T04:38:54Z","receivedAt":"2008-07-08T04:38:54Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"When the user specifies a ref by a reflog entry older than\none we have (e.g., \"HEAD@{20 years ago\"}), we issue a\nwarning and give them the \"from\" value of the oldest reflog\nentry. That is, we say \"we don't know what happened before\nthis entry, but before this we know we had some particular\nSHA1\".\n\nHowever, the oldest reflog entry is often a creation event\nsuch as clone or branch creation. In this case, the entry\nclaims that the ref went from \"00000...\" (the null sha1) to\nthe new value, and the reflog lookup returns the null sha1.\n\nWhile this is technically correct (the entry tells us that\nthe ref didn't exist at the specified time) it is not\nterribly useful to the end user. What they probably want\ninstead is \"the oldest useful sha1 that this ref ever had\".\nThis patch changes the behavior such that if the oldest ref\nwould return the null sha1, it instead returns the first\nvalue the ref ever had.\n\nWe never discovered this problem in the test scripts because\nwe created \"fake\" reflogs that had only a specified segment\nof history. This patch updates the tests with a creation\nevent at the beginning of history.\n\nSigned-off-by: Jeff King <peff@peff.net>\nAcked-by: Shawn O. Pearce <spearce@spearce.org>\n---\nThis patch was generally well-received in the first posting, but didn't\nget applied. I think it was because the previous thread ended with me\nvoicing one concern: should we do this magic other places? I think the\nanswer is no, we don't need to. And even if I am wrong, then this is\nprobably still the right fix for _this_ bit, and we can fix the other if\nand when a bug report comes in.\n\n refs.c                |    4 ++++\n t/t1400-update-ref.sh |    9 ++++++++-\n 2 files changed, 12 insertions(+), 1 deletions(-)\n\ndiff --git a/refs.c b/refs.c\nindex 9e8e858..6c6e9e5 100644\n--- a/refs.c\n+++ b/refs.c\n@@ -1412,6 +1412,10 @@ int read_ref_at(const char *ref, unsigned long at_time, int cnt, unsigned char *\n \ttz = strtoul(tz_c, NULL, 10);\n \tif (get_sha1_hex(logdata, sha1))\n \t\tdie(\"Log %s is corrupt.\", logfile);\n+\tif (is_null_sha1(sha1)) {\n+\t\tif (get_sha1_hex(logdata + 41, sha1))\n+\t\t\tdie(\"Log %s is corrupt.\", logfile);\n+\t}\n \tif (msg)\n \t\t*msg = ref_msg(logdata, logend);\n \tmunmap(log_mapped, mapsz);\ndiff --git a/t/t1400-update-ref.sh b/t/t1400-update-ref.sh\nindex f387d46..ca99d37 100755\n--- a/t/t1400-update-ref.sh\n+++ b/t/t1400-update-ref.sh\n@@ -155,7 +155,8 @@ rm -f .git/$m .git/logs/$m expect\n \n git update-ref $m $D\n cat >.git/logs/$m <<EOF\n-$C $A $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150320 -0500\n+0000000000000000000000000000000000000000 $C $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150320 -0500\n+$C $A $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150350 -0500\n $A $B $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150380 -0500\n $F $Z $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150680 -0500\n $Z $E $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150980 -0500\n@@ -186,6 +187,12 @@ test_expect_success \\\n \t'Query \"master@{May 26 2005 23:32:00}\" (exactly history start)' \\\n \t'rm -f o e\n \t git rev-parse --verify \"master@{May 26 2005 23:32:00}\" >o 2>e &&\n+\t test '\"$C\"' = $(cat o) &&\n+\t test \"\" = \"$(cat e)\"'\n+test_expect_success \\\n+\t'Query \"master@{May 26 2005 23:32:30}\" (first non-creation change)' \\\n+\t'rm -f o e\n+\t git rev-parse --verify \"master@{May 26 2005 23:32:30}\" >o 2>e &&\n \t test '\"$A\"' = $(cat o) &&\n \t test \"\" = \"$(cat e)\"'\n test_expect_success \\\n-- \n1.5.6\n"}]}