{"thread":{"id":"7601","subject":"git fsck and new repos / backup repos","startedAt":"2007-04-10T18:27:05Z","lastAt":"2007-04-11T08:25:43Z","messageCount":2,"participants":["Sergio Callegari","Junio C Hamano"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"39039","messageId":"loom.20070410T200901-466@post.gmane.org","threadId":"7601","inReplyTo":null,"subject":"git fsck and new repos / backup repos","fromName":"Sergio Callegari","fromEmail":"scallegari@arces.unibo.it","sentAt":"2007-04-10T18:27:05Z","receivedAt":"2007-04-10T18:27:05Z","isPatch":false,"sender":{"key":"scallegari@arces.unibo.it","avatar":null},"body":"Hi, \njust posting a very very minor thing, that anyway might be easy to improve...\n\nconsider the following cases:\n\n1) Empty repo\n\nmkdir Foo\ncd Foo\ngit --bare init\ngit --bare fsck\n\nerror: HEAD is not a symbolic ref\nerror: No default references\n\nShould this be an error...?  Of course fsck is not happy: HEAD points to master,\nbut master does not exist.  However, the newbie might find it weird that git\ncomplains over a brand new repo it has just made.\n\nBTW also gitk dies badly in this case.\n\n2) Backup repo\n\nmkdir Foo-Backup\ncd Foo-Backup\ngit --bare init\n\ncd <path>/Foo-Workingtree\ngit config --add remote.foobackup.url <url pointing to Foo Backup>\ngit config --add remote.foobackup.push +refs/heads/*:refs/remotes/workplace1/*\ngit push foobackup\n\ncd <path>/Foo-Backup\ngit --bare fsck\nerror: HEAD is not a symbolic ref\n\nOf course... again head points to master, but master does not exist.\n\n\n\nShould maybe git init not just set up HEAD pointing to master, but also master\npointing to 000000000000000000000000000 and then fsck recognize that\n000000000000000000000000000 is a valid pointer to nothing, namely the starting\npoint of a new branch?\n"},{"id":"39094","messageId":"7vr6qrpbaw.fsf@assigned-by-dhcp.cox.net","threadId":"7601","inReplyTo":"loom.20070410T200901-466@post.gmane.org","subject":"Re: git fsck and new repos / backup repos","fromName":"Junio C Hamano","fromEmail":"junkio@cox.net","sentAt":"2007-04-11T08:25:43Z","receivedAt":"2007-04-11T08:25:43Z","isPatch":false,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Sergio Callegari <scallegari@arces.unibo.it> writes:\n\n> Hi, \n> just posting a very very minor thing, that anyway might be easy to improve...\n>\n> consider the following cases:\n>\n> 1) Empty repo\n>\n> mkdir Foo\n> cd Foo\n> git --bare init\n> git --bare fsck\n>\n> error: HEAD is not a symbolic ref\n> error: No default references\n>\n> Should this be an error...?  Of course fsck is not happy: HEAD points to master,\n> but master does not exist.  However, the newbie might find it weird that git\n> complains over a brand new repo it has just made.\n>\n> BTW also gitk dies badly in this case.\n>\n> 2) Backup repo\n>\n> mkdir Foo-Backup\n> cd Foo-Backup\n> git --bare init\n>\n> cd <path>/Foo-Workingtree\n> git config --add remote.foobackup.url <url pointing to Foo Backup>\n> git config --add remote.foobackup.push +refs/heads/*:refs/remotes/workplace1/*\n> git push foobackup\n>\n> cd <path>/Foo-Backup\n> git --bare fsck\n> error: HEAD is not a symbolic ref\n>\n> Of course... again head points to master, but master does not exist.\n\nI personally do not care too much about empty repository case.\nEither we say error, or we say everything is cruft (if you did\n\"git add; rm -f .git/index\").  So I do not think it matters too\nmuch if the second error from (1) says \"No default references\"\nor did not trigger.  That \"error\" is not about your repository\nbeing corrupt, but is about your use of fsck when you know you\ndo not have anything is, eh, suboptimal ;-).  If you look at the\ncomment before the line that emits that error message you would\nknow.\n\n\"HEAD is not a symbolic ref\" should not even be an error, as we\nsupport detached HEAD, which is another case fsck does not yet\nknow about.  The error message you got should be at least worded\nas an informational message that says it is pointing at a\nyet-to-be-born branch.\n\nI think it *is* an error if:\n\n (1) $GIT_DIR/HEAD does _not_ exist;\n (2) $GIT_DIR/HEAD is a symref, but points outside refs/heads;\n (3) $GIT_DIR/HEAD is _not_ a symref, but does not contain a\n     40-byte object name.\n\nbut (1) or (3) are covered by not even considering such $GIT_DIR\nas a valid repository, and we already check (2).  So I think\nsomething like the following patch to loosen restriction is good\nenough.\n\n---\n\n builtin-fsck.c |   24 ++++++++++++++++--------\n 1 files changed, 16 insertions(+), 8 deletions(-)\n\ndiff --git a/builtin-fsck.c b/builtin-fsck.c\nindex 21f1f9e..7c3b0a5 100644\n--- a/builtin-fsck.c\n+++ b/builtin-fsck.c\n@@ -532,7 +532,7 @@ static void get_default_heads(void)\n \t * \"show_unreachable\" flag.\n \t */\n \tif (!default_refs) {\n-\t\terror(\"No default references\");\n+\t\tfprintf(stderr, \"notice: No default references\\n\");\n \t\tshow_unreachable = 0;\n \t}\n }\n@@ -552,15 +552,23 @@ static int fsck_head_link(void)\n {\n \tunsigned char sha1[20];\n \tint flag;\n-\tconst char *head_points_at = resolve_ref(\"HEAD\", sha1, 1, &flag);\n-\n-\tif (!head_points_at || !(flag & REF_ISSYMREF))\n-\t\treturn error(\"HEAD is not a symbolic ref\");\n-\tif (prefixcmp(head_points_at, \"refs/heads/\"))\n+\tint null_is_error = 0;\n+\tconst char *head_points_at = resolve_ref(\"HEAD\", sha1, 0, &flag);\n+\n+\tif (!head_points_at)\n+\t\treturn error(\"Invalid HEAD\");\n+\tif (!strcmp(head_points_at, \"HEAD\"))\n+\t\t/* detached HEAD */\n+\t\tnull_is_error = 1;\n+\telse if (prefixcmp(head_points_at, \"refs/heads/\"))\n \t\treturn error(\"HEAD points to something strange (%s)\",\n \t\t\t     head_points_at);\n-\tif (is_null_sha1(sha1))\n-\t\treturn error(\"HEAD: not a valid git pointer\");\n+\tif (is_null_sha1(sha1)) {\n+\t\tif (null_is_error)\n+\t\t\treturn error(\"HEAD: detached HEAD points at nothing\");\n+\t\tfprintf(stderr, \"notice: HEAD points to an unborn branch (%s)\\n\",\n+\t\t\thead_points_at + 11);\n+\t}\n \treturn 0;\n }\n \n"}]}