{"thread":{"id":"4179","subject":"[RFC 3/5] Convert update-ref to use ref_lock API.","startedAt":"2006-05-17T09:55:19Z","lastAt":"2006-05-17T10:49:15Z","messageCount":2,"participants":["Shawn Pearce","Junio C Hamano"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"20125","messageId":"20060517095519.GD28529@spearce.org","threadId":"4179","inReplyTo":null,"subject":"[RFC 3/5] Convert update-ref to use ref_lock API.","fromName":"Shawn Pearce","fromEmail":"spearce@spearce.org","sentAt":"2006-05-17T09:55:19Z","receivedAt":"2006-05-17T09:55:19Z","isPatch":false,"sender":{"key":"spearce@spearce.org","avatar":"https://avatars.githubusercontent.com/u/34844?v=4"},"body":"This conversion also adds the '-m' switch to update-ref allowing\nthe caller to record why the ref is changing.  At present this is\nmerely copied down into the ref_lock API.\n\nSigned-off-by: Shawn O. Pearce <spearce@spearce.org>\n\n---\n\n Documentation/git-update-ref.txt |    2 -\n update-ref.c                     |  103 ++++++++++++++------------------------\n 2 files changed, 38 insertions(+), 67 deletions(-)\n\n4f017fce63b3cb56065731967a9684cb36ddd12e\ndiff --git a/Documentation/git-update-ref.txt b/Documentation/git-update-ref.txt\nindex 475237f..f0e710a 100644\n--- a/Documentation/git-update-ref.txt\n+++ b/Documentation/git-update-ref.txt\n@@ -7,7 +7,7 @@ git-update-ref - update the object name \n \n SYNOPSIS\n --------\n-'git-update-ref' <ref> <newvalue> [<oldvalue>]\n+'git-update-ref' <ref> <newvalue> [<oldvalue>] [-m <reason>]\n \n DESCRIPTION\n -----------\ndiff --git a/update-ref.c b/update-ref.c\nindex fd48742..a1e6bb9 100644\n--- a/update-ref.c\n+++ b/update-ref.c\n@@ -1,85 +1,56 @@\n #include \"cache.h\"\n #include \"refs.h\"\n \n-static const char git_update_ref_usage[] = \"git-update-ref <refname> <value> [<oldval>]\";\n-\n-static int re_verify(const char *path, unsigned char *oldsha1, unsigned char *currsha1)\n-{\n-\tchar buf[40];\n-\tint fd = open(path, O_RDONLY), nr;\n-\tif (fd < 0)\n-\t\treturn -1;\n-\tnr = read(fd, buf, 40);\n-\tclose(fd);\n-\tif (nr != 40 || get_sha1_hex(buf, currsha1) < 0)\n-\t\treturn -1;\n-\treturn memcmp(oldsha1, currsha1, 20) ? -1 : 0;\n-}\n+static const char git_update_ref_usage[] =\n+\"git-update-ref <refname> <value> [<oldval>] [-m <reason>]\";\n \n int main(int argc, char **argv)\n {\n-\tchar *hex;\n-\tconst char *refname, *value, *oldval, *path;\n-\tchar *lockpath;\n-\tunsigned char sha1[20], oldsha1[20], currsha1[20];\n-\tint fd, written;\n+\tconst char *refname=NULL, *value=NULL, *oldval=NULL, *msg=NULL;\n+\tstruct ref_lock *lock;\n+\tunsigned char sha1[20], oldsha1[20];\n+\tint i;\n \n \tsetup_git_directory();\n \tgit_config(git_default_config);\n-\tif (argc < 3 || argc > 4)\n+\n+\tfor (i = 1; i < argc; i++) {\n+\t\tif (!strcmp(\"-m\", argv[i])) {\n+\t\t\tif (i+1 >= argc)\n+\t\t\t\tusage(git_update_ref_usage);\n+\t\t\tmsg = argv[++i];\n+\t\t\tif (!*msg)\n+\t\t\t\tdie(\"Refusing to perform update with empty message.\");\n+\t\t\tif (strchr(msg, '\\n'))\n+\t\t\t\tdie(\"Refusing to perform update with \\\\n in message.\");\n+\t\t\tcontinue;\n+\t\t}\n+\t\tif (!refname) {\n+\t\t\trefname = argv[i];\n+\t\t\tcontinue;\n+\t\t}\n+\t\tif (!value) {\n+\t\t\tvalue = argv[i];\n+\t\t\tcontinue;\n+\t\t}\n+\t\tif (!oldval) {\n+\t\t\toldval = argv[i];\n+\t\t\tcontinue;\n+\t\t}\n+\t}\n+\tif (!refname || !value)\n \t\tusage(git_update_ref_usage);\n \n-\trefname = argv[1];\n-\tvalue = argv[2];\n-\toldval = argv[3];\n \tif (get_sha1(value, sha1))\n \t\tdie(\"%s: not a valid SHA1\", value);\n \tmemset(oldsha1, 0, 20);\n \tif (oldval && get_sha1(oldval, oldsha1))\n \t\tdie(\"%s: not a valid old SHA1\", oldval);\n \n-\tpath = resolve_ref(git_path(\"%s\", refname), currsha1, !!oldval);\n-\tif (!path)\n-\t\tdie(\"No such ref: %s\", refname);\n-\n-\tif (oldval) {\n-\t\tif (memcmp(currsha1, oldsha1, 20))\n-\t\t\tdie(\"Ref %s is at %s but expected %s\", refname, sha1_to_hex(currsha1), sha1_to_hex(oldsha1));\n-\t\t/* Nothing to do? */\n-\t\tif (!memcmp(oldsha1, sha1, 20))\n-\t\t\texit(0);\n-\t}\n-\tpath = strdup(path);\n-\tlockpath = mkpath(\"%s.lock\", path);\n-\tif (safe_create_leading_directories(lockpath) < 0)\n-\t\tdie(\"Unable to create all of %s\", lockpath);\n-\n-\tfd = open(lockpath, O_CREAT | O_EXCL | O_WRONLY, 0666);\n-\tif (fd < 0)\n-\t\tdie(\"Unable to create %s\", lockpath);\n-\thex = sha1_to_hex(sha1);\n-\thex[40] = '\\n';\n-\twritten = write(fd, hex, 41);\n-\tclose(fd);\n-\tif (written != 41) {\n-\t\tunlink(lockpath);\n-\t\tdie(\"Unable to write to %s\", lockpath);\n-\t}\n-\n-\t/*\n-\t * Re-read the ref after getting the lock to verify\n-\t */\n-\tif (oldval && re_verify(path, oldsha1, currsha1) < 0) {\n-\t\tunlink(lockpath);\n-\t\tdie(\"Ref lock failed\");\n-\t}\n-\n-\t/*\n-\t * Finally, replace the old ref with the new one\n-\t */\n-\tif (rename(lockpath, path) < 0) {\n-\t\tunlink(lockpath);\n-\t\tdie(\"Unable to create %s\", path);\n-\t}\n+\tlock = lock_any_ref_for_update(refname, oldval ? oldsha1 : NULL, 0);\n+\tif (!lock)\n+\t\treturn 1;\n+\tif (write_ref_sha1(lock, sha1, msg) < 0)\n+\t\treturn 1;\n \treturn 0;\n }\n-- \n1.3.2.g7278\n"},{"id":"20131","messageId":"7vfyj9gc44.fsf@assigned-by-dhcp.cox.net","threadId":"4179","inReplyTo":"20060517095519.GD28529@spearce.org","subject":"Re: [RFC 3/5] Convert update-ref to use ref_lock API.","fromName":"Junio C Hamano","fromEmail":"junkio@cox.net","sentAt":"2006-05-17T10:49:15Z","receivedAt":"2006-05-17T10:49:15Z","isPatch":false,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Shawn Pearce <spearce@spearce.org> writes:\n\n> -'git-update-ref' <ref> <newvalue> [<oldvalue>]\n> +'git-update-ref' <ref> <newvalue> [<oldvalue>] [-m <reason>]\n\nIs it just me who feels \"cmd [-m <reason>] <ref> <new> [<old>]\" would\nlook more natural?\n"}]}