{"thread":{"id":"7349","subject":"[PATCH] update HEAD reflog when branch pointed to by HEAD is directly modified","startedAt":"2007-03-21T21:11:44Z","lastAt":"2007-03-21T21:11:44Z","messageCount":1,"participants":["Nicolas Pitre"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"37696","messageId":"alpine.LFD.0.83.0703211650360.18328@xanadu.home","threadId":"7349","inReplyTo":null,"subject":"[PATCH] update HEAD reflog when branch pointed to by HEAD is directly modified","fromName":"Nicolas Pitre","fromEmail":"nico@cam.org","sentAt":"2007-03-21T21:11:44Z","receivedAt":"2007-03-21T21:11:44Z","isPatch":true,"sender":{"key":"nico@fluxnic.net","avatar":"https://avatars.githubusercontent.com/u/702790?v=4"},"body":"The HEAD reflog is updated as well as the reflog for the branch pointed \nto by HEAD whenever it is referenced with \"HEAD\".\n\nThere are some cases where a specific branch may be modified directly.  \nIn those cases, the HEAD reflog should be updated as well if it is a \nsymref to that branch in order to be consistent.\n\nSigned-off-by: Nicolas Pitre <nico@cam.org>\n---\n\ndiff --git a/refs.c b/refs.c\nindex 9f1fb68..435eeca 100644\n--- a/refs.c\n+++ b/refs.c\n@@ -980,6 +980,27 @@ int write_ref_sha1(struct ref_lock *lock,\n \t\tunlock_ref(lock);\n \t\treturn -1;\n \t}\n+\tif (strcmp(lock->orig_ref_name, \"HEAD\") != 0) {\n+\t\t/*\n+\t\t * Special hack: If a branch is updated directly and HEAD\n+\t\t * points to it (may happen on the remote side of a push\n+\t\t * for example) then logically the HEAD reflog should be\n+\t\t * updated too. \n+\t\t * A generic solution implies reverse symref information, \n+\t\t * but finding all symrefs pointing to the given branch\n+\t\t * would be rather costly for this rare event (the direct\n+\t\t * update of a branch) to be worth it.  So let's cheat and\n+\t\t * check with HEAD only which should cover 99% of all usage\n+\t\t * scenarios (even 100% of the default ones).\n+\t\t */\n+\t\tunsigned char head_sha1[20];\n+\t\tint head_flag;\n+\t\tconst char *head_ref;\n+\t\thead_ref = resolve_ref(\"HEAD\", head_sha1, 1, &head_flag);\n+\t\tif (head_ref && (head_flag & REF_ISSYMREF) &&\n+\t\t    !strcmp(head_ref, lock->ref_name))\n+\t\t\tlog_ref_write(\"HEAD\", lock->old_sha1, sha1, logmsg);\n+\t}\n \tif (commit_lock_file(lock->lk)) {\n \t\terror(\"Couldn't set %s\", lock->ref_name);\n \t\tunlock_ref(lock);\n"}]}