{"thread":{"id":"39691","subject":"[PATCH v3 01/19] delete_ref(): move declaration to refs.h","startedAt":"2015-06-22T14:02:51Z","lastAt":"2015-06-23T17:44:45Z","messageCount":24,"participants":["Michael Haggerty","Junio C Hamano"],"isPatch":true,"patchVersion":3,"patchTotal":19},"messages":[{"id":"264524","messageId":"cover.1434980615.git.mhagger@alum.mit.edu","threadId":"39691","inReplyTo":null,"subject":"[PATCH v3 00/19] Improve \"refs\" encapsulation and speed up deletes","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2015-06-22T14:02:51Z","receivedAt":"2015-06-22T14:02:51Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"This is v3 of the series. I think I have addressed all of the feedback\nfrom v1 [1] and v2 [2]. Thanks to Stefan, Junio, and Peff for their\nfeedback about v2.\n\nThere are three significant changes since v2:\n\n* Add a patch\n\n      delete_refs(): bail early if the packed-refs file cannot be rewritten\n\n  that changes delete_refs() to bail early if repack_without_refs()\n  fails. See the log message for the explanation.\n\n* Add a patch\n\n      prune_refs(): use delete_refs()\n\n  that changes \"git fetch --prune\" to use delete_refs() as well. This\n  is analogous to the existing patch\n\n      prune_remote(): use delete_refs()\n\n  (Both of these changes remove quadratic behavior that can be\n  extremely expensive in repositories with many packed refs.)\n\n* Append four more commits that change how delete_ref() interprets its\n  `old_sha1` parameter, to make it consistent with\n  ref_transaction_delete() and friends:\n\n      check_branch_commit(): make first parameter const\n      update_ref(): don't read old reference value before delete\n      cmd_update_ref(): make logic more straightforward\n      delete_ref(): use the usual convention for old_sha1\n\n  This change was suggested by Junio [3].\n\nThe last change is good for consistency, but less important than\nexpected in the real world. The reason is that the convention of\nref_transaction_delete() is that passing NULL_SHA1 as old_sha1 is a\nbug (because why would somebody delete a reference that he knows not\nto exist?) So we don't really gain a useful third case, though we do\ngain a consistency check that might be useful someday. However, if you\ndon't like this change, the last four commits can be dropped\n(actually, commits N-3 through N-1 are marginally useful cleanups\nanyway so probably only the last commit should be dropped in this\ncase).\n\nOther, minor changes since v2:\n\n* Improve the commit message of \"delete_refs(): make error message\n  more generic\".\n\n* Better explain the change to error output caused by \"prune_remote():\n  use delete_refs()\" in that commit's log message.\n\n* Split the \"add names for function parameters\" changes into a\n  separate patch.\n\n* Fix how die() is invoked in \"initial_ref_transaction_commit():\n  function for initial ref creation\" and remove some superfluous\n  braces.\n\nThis series is also available from my GitHub account [4] as branch\n\"init-delete-refs-api\".\n\n[1] http://thread.gmane.org/gmane.comp.version-control.git/271017\n[2] http://thread.gmane.org/gmane.comp.version-control.git/271552\n[3] http://article.gmane.org/gmane.comp.version-control.git/271697\n[4] https://github.com/mhagger/git\n\nMichael Haggerty (19):\n  delete_ref(): move declaration to refs.h\n  remove_branches(): remove temporary\n  delete_ref(): handle special case more explicitly\n  delete_refs(): new function for the refs API\n  delete_refs(): make error message more generic\n  delete_refs(): bail early if the packed-refs file cannot be rewritten\n  prune_remote(): use delete_refs()\n  prune_refs(): use delete_refs()\n  repack_without_refs(): make function private\n  initial_ref_transaction_commit(): function for initial ref creation\n  refs: remove some functions from the module's public interface\n  initial_ref_transaction_commit(): check for duplicate refs\n  initial_ref_transaction_commit(): check for ref D/F conflicts\n  refs: move the remaining ref module declarations to refs.h\n  refs.h: add some parameter names to function declarations\n  check_branch_commit(): make first parameter const\n  update_ref(): don't read old reference value before delete\n  cmd_update_ref(): make logic more straightforward\n  delete_ref(): use the usual convention for old_sha1\n\n archive.c               |   1 +\n builtin/blame.c         |   1 +\n builtin/branch.c        |   5 +-\n builtin/clone.c         |  18 ++++-\n builtin/fast-export.c   |   1 +\n builtin/fetch.c         |  25 ++++--\n builtin/fmt-merge-msg.c |   1 +\n builtin/init-db.c       |   1 +\n builtin/log.c           |   1 +\n builtin/remote.c        |  33 +-------\n builtin/update-ref.c    |  21 ++++-\n cache.h                 |  68 ----------------\n fast-import.c           |   6 +-\n refs.c                  | 182 ++++++++++++++++++++++++++++++++++++++---\n refs.h                  | 211 +++++++++++++++++++++++++++++++-----------------\n remote-testsvn.c        |   1 +\n 16 files changed, 371 insertions(+), 205 deletions(-)\n\n-- \n2.1.4\n"},{"id":"264516","messageId":"0c49ee45d76303320c197f10270601683ed27cf4.1434980615.git.mhagger@alum.mit.edu","threadId":"39691","inReplyTo":"cover.1434980615.git.mhagger@alum.mit.edu","subject":"[PATCH v3 01/19] delete_ref(): move declaration to refs.h","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2015-06-22T14:02:52Z","receivedAt":"2015-06-22T14:02:52Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"Also\n\n* Add a docstring\n\n* Rename the second parameter to \"old_sha1\", to be consistent with the\n  convention used elsewhere in the refs module\n\nSigned-off-by: Michael Haggerty <mhagger@alum.mit.edu>\n---\n cache.h |  2 --\n refs.c  |  5 +++--\n refs.h  | 10 ++++++++++\n 3 files changed, 13 insertions(+), 4 deletions(-)\n\ndiff --git a/cache.h b/cache.h\nindex 571c98f..be92121 100644\n--- a/cache.h\n+++ b/cache.h\n@@ -585,8 +585,6 @@ extern void update_index_if_able(struct index_state *, struct lock_file *);\n extern int hold_locked_index(struct lock_file *, int);\n extern void set_alternate_index_output(const char *);\n \n-extern int delete_ref(const char *, const unsigned char *sha1, unsigned int flags);\n-\n /* Environment bits from configuration mechanism */\n extern int trust_executable_bit;\n extern int trust_ctime;\ndiff --git a/refs.c b/refs.c\nindex a742d79..b575bb8 100644\n--- a/refs.c\n+++ b/refs.c\n@@ -2789,7 +2789,8 @@ static int delete_ref_loose(struct ref_lock *lock, int flag, struct strbuf *err)\n \treturn 0;\n }\n \n-int delete_ref(const char *refname, const unsigned char *sha1, unsigned int flags)\n+int delete_ref(const char *refname, const unsigned char *old_sha1,\n+\t       unsigned int flags)\n {\n \tstruct ref_transaction *transaction;\n \tstruct strbuf err = STRBUF_INIT;\n@@ -2797,7 +2798,7 @@ int delete_ref(const char *refname, const unsigned char *sha1, unsigned int flag\n \ttransaction = ref_transaction_begin(&err);\n \tif (!transaction ||\n \t    ref_transaction_delete(transaction, refname,\n-\t\t\t\t   (sha1 && !is_null_sha1(sha1)) ? sha1 : NULL,\n+\t\t\t\t   (old_sha1 && !is_null_sha1(old_sha1)) ? old_sha1 : NULL,\n \t\t\t\t   flags, NULL, &err) ||\n \t    ref_transaction_commit(transaction, &err)) {\n \t\terror(\"%s\", err.buf);\ndiff --git a/refs.h b/refs.h\nindex 8c3d433..68b5e81 100644\n--- a/refs.h\n+++ b/refs.h\n@@ -202,6 +202,16 @@ extern int read_ref_at(const char *refname, unsigned int flags,\n /** Check if a particular reflog exists */\n extern int reflog_exists(const char *refname);\n \n+/*\n+ * Delete the specified reference. If old_sha1 is non-NULL and not\n+ * NULL_SHA1, then verify that the current value of the reference is\n+ * old_sha1 before deleting it. If old_sha1 is NULL or NULL_SHA1,\n+ * delete the reference if it exists, regardless of its old value.\n+ * flags is passed through to ref_transaction_delete().\n+ */\n+extern int delete_ref(const char *refname, const unsigned char *old_sha1,\n+\t\t      unsigned int flags);\n+\n /** Delete a reflog */\n extern int delete_reflog(const char *refname);\n \n-- \n2.1.4\n"},{"id":"264534","messageId":"0c78010d9f06e2bfc8136c1ac11d5bc107a7043a.1434980615.git.mhagger@alum.mit.edu","threadId":"39691","inReplyTo":"cover.1434980615.git.mhagger@alum.mit.edu","subject":"[PATCH v3 02/19] remove_branches(): remove temporary","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2015-06-22T14:02:53Z","receivedAt":"2015-06-22T14:02:53Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>\n---\n builtin/remote.c | 3 +--\n 1 file changed, 1 insertion(+), 2 deletions(-)\n\ndiff --git a/builtin/remote.c b/builtin/remote.c\nindex f4a6ec9..53b8e13 100644\n--- a/builtin/remote.c\n+++ b/builtin/remote.c\n@@ -756,8 +756,7 @@ static int remove_branches(struct string_list *branches)\n \tstrbuf_release(&err);\n \n \tfor (i = 0; i < branches->nr; i++) {\n-\t\tstruct string_list_item *item = branches->items + i;\n-\t\tconst char *refname = item->string;\n+\t\tconst char *refname = branches->items[i].string;\n \n \t\tif (delete_ref(refname, NULL, 0))\n \t\t\tresult |= error(_(\"Could not remove branch %s\"), refname);\n-- \n2.1.4\n"},{"id":"264521","messageId":"0c66a6bd6e49b9c28fa3617ee50de746ef4ea9a3.1434980615.git.mhagger@alum.mit.edu","threadId":"39691","inReplyTo":"cover.1434980615.git.mhagger@alum.mit.edu","subject":"[PATCH v3 03/19] delete_ref(): handle special case more explicitly","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2015-06-22T14:02:54Z","receivedAt":"2015-06-22T14:02:54Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"delete_ref() uses a different convention for its old_sha1 parameter\nthan, say, ref_transaction_delete(): NULL_SHA1 means not to check the\nold value. Make this fact a little bit clearer in the code by handling\nit in explicit, commented code rather than burying it in a conditional\nexpression.\n\nSigned-off-by: Michael Haggerty <mhagger@alum.mit.edu>\n---\n refs.c | 11 +++++++++--\n 1 file changed, 9 insertions(+), 2 deletions(-)\n\ndiff --git a/refs.c b/refs.c\nindex b575bb8..f0b6cec 100644\n--- a/refs.c\n+++ b/refs.c\n@@ -2795,10 +2795,17 @@ int delete_ref(const char *refname, const unsigned char *old_sha1,\n \tstruct ref_transaction *transaction;\n \tstruct strbuf err = STRBUF_INIT;\n \n+\t/*\n+\t * Treat NULL_SHA1 and NULL alike, to mean \"we don't care what\n+\t * the old value of the reference was (or even if it didn't\n+\t * exist)\":\n+\t */\n+\tif (old_sha1 && is_null_sha1(old_sha1))\n+\t\told_sha1 = NULL;\n+\n \ttransaction = ref_transaction_begin(&err);\n \tif (!transaction ||\n-\t    ref_transaction_delete(transaction, refname,\n-\t\t\t\t   (old_sha1 && !is_null_sha1(old_sha1)) ? old_sha1 : NULL,\n+\t    ref_transaction_delete(transaction, refname, old_sha1,\n \t\t\t\t   flags, NULL, &err) ||\n \t    ref_transaction_commit(transaction, &err)) {\n \t\terror(\"%s\", err.buf);\n-- \n2.1.4\n"},{"id":"264522","messageId":"9a6c35bd603edfc46da241b8c762f1a269ac44f8.1434980615.git.mhagger@alum.mit.edu","threadId":"39691","inReplyTo":"cover.1434980615.git.mhagger@alum.mit.edu","subject":"[PATCH v3 04/19] delete_refs(): new function for the refs API","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2015-06-22T14:02:55Z","receivedAt":"2015-06-22T14:02:55Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"Move the function remove_branches() from builtin/remote.c to refs.c,\nrename it to delete_refs(), and make it public.\n\nSigned-off-by: Michael Haggerty <mhagger@alum.mit.edu>\n---\n builtin/remote.c | 21 +--------------------\n refs.c           | 19 +++++++++++++++++++\n refs.h           |  7 +++++++\n 3 files changed, 27 insertions(+), 20 deletions(-)\n\ndiff --git a/builtin/remote.c b/builtin/remote.c\nindex 53b8e13..c8dc724 100644\n--- a/builtin/remote.c\n+++ b/builtin/remote.c\n@@ -746,25 +746,6 @@ static int mv(int argc, const char **argv)\n \treturn 0;\n }\n \n-static int remove_branches(struct string_list *branches)\n-{\n-\tstruct strbuf err = STRBUF_INIT;\n-\tint i, result = 0;\n-\n-\tif (repack_without_refs(branches, &err))\n-\t\tresult |= error(\"%s\", err.buf);\n-\tstrbuf_release(&err);\n-\n-\tfor (i = 0; i < branches->nr; i++) {\n-\t\tconst char *refname = branches->items[i].string;\n-\n-\t\tif (delete_ref(refname, NULL, 0))\n-\t\t\tresult |= error(_(\"Could not remove branch %s\"), refname);\n-\t}\n-\n-\treturn result;\n-}\n-\n static int rm(int argc, const char **argv)\n {\n \tstruct option options[] = {\n@@ -821,7 +802,7 @@ static int rm(int argc, const char **argv)\n \tstrbuf_release(&buf);\n \n \tif (!result)\n-\t\tresult = remove_branches(&branches);\n+\t\tresult = delete_refs(&branches);\n \tstring_list_clear(&branches, 0);\n \n \tif (skipped.nr) {\ndiff --git a/refs.c b/refs.c\nindex f0b6cec..6f62bd1 100644\n--- a/refs.c\n+++ b/refs.c\n@@ -2818,6 +2818,25 @@ int delete_ref(const char *refname, const unsigned char *old_sha1,\n \treturn 0;\n }\n \n+int delete_refs(struct string_list *refnames)\n+{\n+\tstruct strbuf err = STRBUF_INIT;\n+\tint i, result = 0;\n+\n+\tif (repack_without_refs(refnames, &err))\n+\t\tresult |= error(\"%s\", err.buf);\n+\tstrbuf_release(&err);\n+\n+\tfor (i = 0; i < refnames->nr; i++) {\n+\t\tconst char *refname = refnames->items[i].string;\n+\n+\t\tif (delete_ref(refname, NULL, 0))\n+\t\t\tresult |= error(_(\"Could not remove branch %s\"), refname);\n+\t}\n+\n+\treturn result;\n+}\n+\n /*\n  * People using contrib's git-new-workdir have .git/logs/refs ->\n  * /some/other/path/.git/logs/refs, and that may live on another device.\ndiff --git a/refs.h b/refs.h\nindex 68b5e81..1a5d44a 100644\n--- a/refs.h\n+++ b/refs.h\n@@ -212,6 +212,13 @@ extern int reflog_exists(const char *refname);\n extern int delete_ref(const char *refname, const unsigned char *old_sha1,\n \t\t      unsigned int flags);\n \n+/*\n+ * Delete the specified references. If there are any problems, emit\n+ * errors but attempt to keep going (i.e., the deletes are not done in\n+ * an all-or-nothing transaction).\n+ */\n+extern int delete_refs(struct string_list *refnames);\n+\n /** Delete a reflog */\n extern int delete_reflog(const char *refname);\n \n-- \n2.1.4\n"},{"id":"264519","messageId":"feb52672f14a4c664478ac65664c3bfd6405e3a8.1434980615.git.mhagger@alum.mit.edu","threadId":"39691","inReplyTo":"cover.1434980615.git.mhagger@alum.mit.edu","subject":"[PATCH v3 05/19] delete_refs(): make error message more generic","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2015-06-22T14:02:56Z","receivedAt":"2015-06-22T14:02:56Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"Change the error message from\n\n    Could not remove branch %s\n\nto\n\n    could not remove reference %s\n\nFirst of all, the old error message referred to \"branch\nrefs/remotes/origin/foo\", which was awkward even for the existing\ncaller. Normally we would refer to a reference like that as either\n\"remote-tracking branch origin/foo\" or \"reference\nrefs/remotes/origin/foo\". Here I take the lazier alternative.\n\nMoreover, now that this function is part of the refs API, it might be\ncalled for refs that are neither branches nor remote-tracking\nbranches.\n\nWhile we're at it, convert the error message to lower case, as per our\nusual convention.\n\nSigned-off-by: Michael Haggerty <mhagger@alum.mit.edu>\n---\n refs.c | 2 +-\n 1 file changed, 1 insertion(+), 1 deletion(-)\n\ndiff --git a/refs.c b/refs.c\nindex 6f62bd1..5386077 100644\n--- a/refs.c\n+++ b/refs.c\n@@ -2831,7 +2831,7 @@ int delete_refs(struct string_list *refnames)\n \t\tconst char *refname = refnames->items[i].string;\n \n \t\tif (delete_ref(refname, NULL, 0))\n-\t\t\tresult |= error(_(\"Could not remove branch %s\"), refname);\n+\t\t\tresult |= error(_(\"could not remove reference %s\"), refname);\n \t}\n \n \treturn result;\n-- \n2.1.4\n"},{"id":"264517","messageId":"28505bdd95dc4da84bef1afc709a51d99718a5ad.1434980615.git.mhagger@alum.mit.edu","threadId":"39691","inReplyTo":"cover.1434980615.git.mhagger@alum.mit.edu","subject":"[PATCH v3 06/19] delete_refs(): bail early if the packed-refs file cannot be rewritten","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2015-06-22T14:02:57Z","receivedAt":"2015-06-22T14:02:57Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"If we fail to delete the doomed references from the packed-refs file,\nthen it is unsafe to delete their loose references, because doing so\nmight expose a value from the packed-refs file that is obsolete and\nperhaps even points at an object that has been garbage collected.\n\nSo if repack_without_refs() fails, emit a more explicit error message\nand bail.\n\nSigned-off-by: Michael Haggerty <mhagger@alum.mit.edu>\n---\n refs.c | 25 ++++++++++++++++++++++---\n 1 file changed, 22 insertions(+), 3 deletions(-)\n\ndiff --git a/refs.c b/refs.c\nindex 5386077..19afc4d 100644\n--- a/refs.c\n+++ b/refs.c\n@@ -2823,9 +2823,26 @@ int delete_refs(struct string_list *refnames)\n \tstruct strbuf err = STRBUF_INIT;\n \tint i, result = 0;\n \n-\tif (repack_without_refs(refnames, &err))\n-\t\tresult |= error(\"%s\", err.buf);\n-\tstrbuf_release(&err);\n+\tif (!refnames->nr)\n+\t\treturn 0;\n+\n+\tresult = repack_without_refs(refnames, &err);\n+\tif (result) {\n+\t\t/*\n+\t\t * If we failed to rewrite the packed-refs file, then\n+\t\t * it is unsafe to try to remove loose refs, because\n+\t\t * doing so might expose an obsolete packed value for\n+\t\t * a reference that might even point at an object that\n+\t\t * has been garbage collected.\n+\t\t */\n+\t\tif (refnames->nr == 1)\n+\t\t\terror(_(\"could not delete reference %s: %s\"),\n+\t\t\t      refnames->items[0].string, err.buf);\n+\t\telse\n+\t\t\terror(_(\"could not delete references: %s\"), err.buf);\n+\n+\t\tgoto out;\n+\t}\n \n \tfor (i = 0; i < refnames->nr; i++) {\n \t\tconst char *refname = refnames->items[i].string;\n@@ -2834,6 +2851,8 @@ int delete_refs(struct string_list *refnames)\n \t\t\tresult |= error(_(\"could not remove reference %s\"), refname);\n \t}\n \n+out:\n+\tstrbuf_release(&err);\n \treturn result;\n }\n \n-- \n2.1.4\n"},{"id":"264518","messageId":"4d2d496d5ac550923110960fcd26a57c4a7b860c.1434980615.git.mhagger@alum.mit.edu","threadId":"39691","inReplyTo":"cover.1434980615.git.mhagger@alum.mit.edu","subject":"[PATCH v3 07/19] prune_remote(): use delete_refs()","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2015-06-22T14:02:58Z","receivedAt":"2015-06-22T14:02:58Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"This slightly changes how errors are reported. The old and new code\nboth report errors that come from repack_without_refs() the same way.\nBut if an error occurs within delete_ref(), the old version only\nemitted an error within delete_ref() without further comment. The new\nversion (in delete_refs()) still emits that error, but then follows it\nup with\n\n    error(_(\"could not remove reference %s\"), refname)\n\nThe \"could not remove reference\" error originally came from a similar\nmessage in remove_branches() (from builtin/remote.c).\n\nThis is an improvement, because the error from delete_ref() (which\nusually comes from ref_transaction_commit()) can be pretty low-level,\nlike\n\n    Cannot lock ref '%s': unable to resolve reference %s: %s\n\nwhere the last \"%s\" is the original strerror.\n\nIn any case, I don't think we need to sweat the details too much,\nbecause these errors should only ever be seen in the case of a broken\nrepository or a race between two processes; i.e., only in pretty rare\nand anomalous situations.\n\nSigned-off-by: Michael Haggerty <mhagger@alum.mit.edu>\n---\n builtin/remote.c | 11 ++---------\n 1 file changed, 2 insertions(+), 9 deletions(-)\n\ndiff --git a/builtin/remote.c b/builtin/remote.c\nindex c8dc724..cc3c741 100644\n--- a/builtin/remote.c\n+++ b/builtin/remote.c\n@@ -1314,19 +1314,12 @@ static int prune_remote(const char *remote, int dry_run)\n \t\tstring_list_append(&refs_to_prune, item->util);\n \tstring_list_sort(&refs_to_prune);\n \n-\tif (!dry_run) {\n-\t\tstruct strbuf err = STRBUF_INIT;\n-\t\tif (repack_without_refs(&refs_to_prune, &err))\n-\t\t\tresult |= error(\"%s\", err.buf);\n-\t\tstrbuf_release(&err);\n-\t}\n+\tif (!dry_run)\n+\t\tresult |= delete_refs(&refs_to_prune);\n \n \tfor_each_string_list_item(item, &states.stale) {\n \t\tconst char *refname = item->util;\n \n-\t\tif (!dry_run)\n-\t\t\tresult |= delete_ref(refname, NULL, 0);\n-\n \t\tif (dry_run)\n \t\t\tprintf_ln(_(\" * [would prune] %s\"),\n \t\t\t       abbrev_ref(refname, \"refs/remotes/\"));\n-- \n2.1.4\n"},{"id":"264530","messageId":"a338a63f4c3225c7faa001a5bdde92c7690eba79.1434980615.git.mhagger@alum.mit.edu","threadId":"39691","inReplyTo":"cover.1434980615.git.mhagger@alum.mit.edu","subject":"[PATCH v3 08/19] prune_refs(): use delete_refs()","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2015-06-22T14:02:59Z","receivedAt":"2015-06-22T14:02:59Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"The old version just looped over the references to delete, calling\ndelete_ref() on each one. But that has quadratic behavior, because\neach call to delete_ref() might have to rewrite the packed-refs file.\nThis can be very expensive in a repository with a large number of\nreferences. In some (admittedly extreme) repositories, we've seen\ncases where the ref-pruning part of fetch takes multiple tens of\nminutes.\n\nInstead call delete_refs(), which (aside from being less code) has the\noptimization that it only rewrites the packed-refs file a single time.\n\nSigned-off-by: Michael Haggerty <mhagger@alum.mit.edu>\n---\n builtin/fetch.c | 25 +++++++++++++++++--------\n 1 file changed, 17 insertions(+), 8 deletions(-)\n\ndiff --git a/builtin/fetch.c b/builtin/fetch.c\nindex 8d5b2db..34b6f5e 100644\n--- a/builtin/fetch.c\n+++ b/builtin/fetch.c\n@@ -790,20 +790,29 @@ static int prune_refs(struct refspec *refs, int ref_count, struct ref *ref_map,\n \tif (4 < i && !strncmp(\".git\", url + i - 3, 4))\n \t\turl_len = i - 3;\n \n-\tfor (ref = stale_refs; ref; ref = ref->next) {\n-\t\tif (!dry_run)\n-\t\t\tresult |= delete_ref(ref->name, NULL, 0);\n-\t\tif (verbosity >= 0 && !shown_url) {\n-\t\t\tfprintf(stderr, _(\"From %.*s\\n\"), url_len, url);\n-\t\t\tshown_url = 1;\n-\t\t}\n-\t\tif (verbosity >= 0) {\n+\tif (!dry_run) {\n+\t\tstruct string_list refnames = STRING_LIST_INIT_NODUP;\n+\n+\t\tfor (ref = stale_refs; ref; ref = ref->next)\n+\t\t\tstring_list_append(&refnames, ref->name);\n+\n+\t\tresult = delete_refs(&refnames);\n+\t\tstring_list_clear(&refnames, 0);\n+\t}\n+\n+\tif (verbosity >= 0) {\n+\t\tfor (ref = stale_refs; ref; ref = ref->next) {\n+\t\t\tif (!shown_url) {\n+\t\t\t\tfprintf(stderr, _(\"From %.*s\\n\"), url_len, url);\n+\t\t\t\tshown_url = 1;\n+\t\t\t}\n \t\t\tfprintf(stderr, \" x %-*s %-*s -> %s\\n\",\n \t\t\t\tTRANSPORT_SUMMARY(_(\"[deleted]\")),\n \t\t\t\tREFCOL_WIDTH, _(\"(none)\"), prettify_refname(ref->name));\n \t\t\twarn_dangling_symref(stderr, dangling_msg, ref->name);\n \t\t}\n \t}\n+\n \tfree(url);\n \tfree_refs(stale_refs);\n \treturn result;\n-- \n2.1.4\n"},{"id":"264533","messageId":"5fa0fb8a71db99b3e96c4ea2946b2efb186aac8b.1434980615.git.mhagger@alum.mit.edu","threadId":"39691","inReplyTo":"cover.1434980615.git.mhagger@alum.mit.edu","subject":"[PATCH v3 09/19] repack_without_refs(): make function private","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2015-06-22T14:03:00Z","receivedAt":"2015-06-22T14:03:00Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"It is no longer called from outside of the refs module. Also move its\ndocstring and change it to imperative voice.\n\nSigned-off-by: Michael Haggerty <mhagger@alum.mit.edu>\n---\n refs.c |  9 ++++++++-\n refs.h | 11 -----------\n 2 files changed, 8 insertions(+), 12 deletions(-)\n\ndiff --git a/refs.c b/refs.c\nindex 19afc4d..b21bb55 100644\n--- a/refs.c\n+++ b/refs.c\n@@ -2724,7 +2724,14 @@ int pack_refs(unsigned int flags)\n \treturn 0;\n }\n \n-int repack_without_refs(struct string_list *refnames, struct strbuf *err)\n+/*\n+ * Rewrite the packed-refs file, omitting any refs listed in\n+ * 'refnames'. On error, leave packed-refs unchanged, write an error\n+ * message to 'err', and return a nonzero value.\n+ *\n+ * The refs in 'refnames' needn't be sorted. `err` must not be NULL.\n+ */\n+static int repack_without_refs(struct string_list *refnames, struct strbuf *err)\n {\n \tstruct ref_dir *packed;\n \tstruct string_list_item *refname;\ndiff --git a/refs.h b/refs.h\nindex 1a5d44a..5f3bea7 100644\n--- a/refs.h\n+++ b/refs.h\n@@ -154,17 +154,6 @@ extern void rollback_packed_refs(void);\n  */\n int pack_refs(unsigned int flags);\n \n-/*\n- * Rewrite the packed-refs file, omitting any refs listed in\n- * 'refnames'. On error, packed-refs will be unchanged, the return\n- * value is nonzero, and a message about the error is written to the\n- * 'err' strbuf.\n- *\n- * The refs in 'refnames' needn't be sorted. `err` must not be NULL.\n- */\n-extern int repack_without_refs(struct string_list *refnames,\n-\t\t\t       struct strbuf *err);\n-\n extern int ref_exists(const char *);\n \n extern int is_branch(const char *refname);\n-- \n2.1.4\n"},{"id":"264535","messageId":"066845f7d6989a34856ae6370f95690f06fbd5f2.1434980615.git.mhagger@alum.mit.edu","threadId":"39691","inReplyTo":"cover.1434980615.git.mhagger@alum.mit.edu","subject":"[PATCH v3 10/19] initial_ref_transaction_commit(): function for initial ref creation","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2015-06-22T14:03:01Z","receivedAt":"2015-06-22T14:03:01Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"\"git clone\" uses shortcuts when creating the initial set of\nreferences:\n\n* It writes them directly to packed-refs.\n\n* It doesn't lock the individual references (though it does lock the\n  packed-refs file).\n\n* It doesn't check for refname conflicts between two new references or\n  between one new reference and any hypothetical old ones.\n\n* It doesn't create reflog entries for the reference creations.\n\nThis functionality was implemented in builtin/clone.c. But really that\nfile shouldn't have such intimate knowledge of how references are\nstored. So provide a new function in the refs API,\ninitial_ref_transaction_commit(), which can be used for initial\nreference creation. The new function is based on the ref_transaction\ninterface.\n\nThis means that we can make some other functions private to the refs\nmodule. That will be done in a followup commit.\n\nIt would seem to make sense to add a test here that there are no\nexisting references, because that is how the function *should* be\nused. But in fact, the \"testgit\" remote helper appears to call it\n*after* having set up refs/remotes/<name>/HEAD and\nrefs/remotes/<name>/master, so we can't be so strict. For now, the\nfunction trusts its caller to only call it when it makes sense. Future\ncommits will add some more limited sanity checks.\n\nSigned-off-by: Michael Haggerty <mhagger@alum.mit.edu>\n---\n builtin/clone.c | 18 ++++++++++++++----\n refs.c          | 47 +++++++++++++++++++++++++++++++++++++++++++++++\n refs.h          | 14 ++++++++++++++\n 3 files changed, 75 insertions(+), 4 deletions(-)\n\ndiff --git a/builtin/clone.c b/builtin/clone.c\nindex b878252..771d450 100644\n--- a/builtin/clone.c\n+++ b/builtin/clone.c\n@@ -500,16 +500,26 @@ static void write_remote_refs(const struct ref *local_refs)\n {\n \tconst struct ref *r;\n \n-\tlock_packed_refs(LOCK_DIE_ON_ERROR);\n+\tstruct ref_transaction *t;\n+\tstruct strbuf err = STRBUF_INIT;\n+\n+\tt = ref_transaction_begin(&err);\n+\tif (!t)\n+\t\tdie(\"%s\", err.buf);\n \n \tfor (r = local_refs; r; r = r->next) {\n \t\tif (!r->peer_ref)\n \t\t\tcontinue;\n-\t\tadd_packed_ref(r->peer_ref->name, r->old_sha1);\n+\t\tif (ref_transaction_create(t, r->peer_ref->name, r->old_sha1,\n+\t\t\t\t\t   0, NULL, &err))\n+\t\t\tdie(\"%s\", err.buf);\n \t}\n \n-\tif (commit_packed_refs())\n-\t\tdie_errno(\"unable to overwrite old ref-pack file\");\n+\tif (initial_ref_transaction_commit(t, &err))\n+\t\tdie(\"%s\", err.buf);\n+\n+\tstrbuf_release(&err);\n+\tref_transaction_free(t);\n }\n \n static void write_followtags(const struct ref *refs, const char *msg)\ndiff --git a/refs.c b/refs.c\nindex b21bb55..2367572 100644\n--- a/refs.c\n+++ b/refs.c\n@@ -4064,6 +4064,53 @@ cleanup:\n \treturn ret;\n }\n \n+int initial_ref_transaction_commit(struct ref_transaction *transaction,\n+\t\t\t\t   struct strbuf *err)\n+{\n+\tint ret = 0, i;\n+\tint n = transaction->nr;\n+\tstruct ref_update **updates = transaction->updates;\n+\n+\tassert(err);\n+\n+\tif (transaction->state != REF_TRANSACTION_OPEN)\n+\t\tdie(\"BUG: commit called for transaction that is not open\");\n+\n+\tfor (i = 0; i < n; i++) {\n+\t\tstruct ref_update *update = updates[i];\n+\n+\t\tif ((update->flags & REF_HAVE_OLD) &&\n+\t\t    !is_null_sha1(update->old_sha1))\n+\t\t\tdie(\"BUG: initial ref transaction with old_sha1 set\");\n+\t}\n+\n+\tif (lock_packed_refs(0)) {\n+\t\tstrbuf_addf(err, \"unable to lock packed-refs file: %s\",\n+\t\t\t    strerror(errno));\n+\t\tret = TRANSACTION_GENERIC_ERROR;\n+\t\tgoto cleanup;\n+\t}\n+\n+\tfor (i = 0; i < n; i++) {\n+\t\tstruct ref_update *update = updates[i];\n+\n+\t\tif ((update->flags & REF_HAVE_NEW) &&\n+\t\t    !is_null_sha1(update->new_sha1))\n+\t\t\tadd_packed_ref(update->refname, update->new_sha1);\n+\t}\n+\n+\tif (commit_packed_refs()) {\n+\t\tstrbuf_addf(err, \"unable to commit packed-refs file: %s\",\n+\t\t\t    strerror(errno));\n+\t\tret = TRANSACTION_GENERIC_ERROR;\n+\t\tgoto cleanup;\n+\t}\n+\n+cleanup:\n+\ttransaction->state = REF_TRANSACTION_CLOSED;\n+\treturn ret;\n+}\n+\n char *shorten_unambiguous_ref(const char *refname, int strict)\n {\n \tint i;\ndiff --git a/refs.h b/refs.h\nindex 5f3bea7..9602889 100644\n--- a/refs.h\n+++ b/refs.h\n@@ -366,6 +366,20 @@ int ref_transaction_commit(struct ref_transaction *transaction,\n \t\t\t   struct strbuf *err);\n \n /*\n+ * Like ref_transaction_commit(), but optimized for creating\n+ * references when originally initializing a repository (e.g., by \"git\n+ * clone\"). It writes the new references directly to packed-refs\n+ * without locking the individual references.\n+ *\n+ * It is a bug to call this function when there might be other\n+ * processes accessing the repository or if there are existing\n+ * references that might conflict with the ones being created. All\n+ * old_sha1 values must either be absent or NULL_SHA1.\n+ */\n+int initial_ref_transaction_commit(struct ref_transaction *transaction,\n+\t\t\t\t   struct strbuf *err);\n+\n+/*\n  * Free an existing transaction and all associated data.\n  */\n void ref_transaction_free(struct ref_transaction *transaction);\n-- \n2.1.4\n"},{"id":"264526","messageId":"5413233024b7efc9081f25ccc983988d48c76c45.1434980615.git.mhagger@alum.mit.edu","threadId":"39691","inReplyTo":"cover.1434980615.git.mhagger@alum.mit.edu","subject":"[PATCH v3 11/19] refs: remove some functions from the module's public interface","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2015-06-22T14:03:02Z","receivedAt":"2015-06-22T14:03:02Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"The following functions are no longer used from outside the refs\nmodule:\n\n* lock_packed_refs()\n* add_packed_ref()\n* commit_packed_refs()\n* rollback_packed_refs()\n\nSo make these functions private.\n\nThis is an important step, because it means that nobody outside of the\nrefs module needs to know the difference between loose and packed\nreferences.\n\nSigned-off-by: Michael Haggerty <mhagger@alum.mit.edu>\n---\n refs.c | 31 ++++++++++++++++++++++++-------\n refs.h | 30 ------------------------------\n 2 files changed, 24 insertions(+), 37 deletions(-)\n\ndiff --git a/refs.c b/refs.c\nindex 2367572..31661c7 100644\n--- a/refs.c\n+++ b/refs.c\n@@ -1314,7 +1314,13 @@ static struct ref_dir *get_packed_refs(struct ref_cache *refs)\n \treturn get_packed_ref_dir(get_packed_ref_cache(refs));\n }\n \n-void add_packed_ref(const char *refname, const unsigned char *sha1)\n+/*\n+ * Add a reference to the in-memory packed reference cache.  This may\n+ * only be called while the packed-refs file is locked (see\n+ * lock_packed_refs()).  To actually write the packed-refs file, call\n+ * commit_packed_refs().\n+ */\n+static void add_packed_ref(const char *refname, const unsigned char *sha1)\n {\n \tstruct packed_ref_cache *packed_ref_cache =\n \t\tget_packed_ref_cache(&ref_cache);\n@@ -2503,8 +2509,12 @@ static int write_packed_entry_fn(struct ref_entry *entry, void *cb_data)\n \treturn 0;\n }\n \n-/* This should return a meaningful errno on failure */\n-int lock_packed_refs(int flags)\n+/*\n+ * Lock the packed-refs file for writing. Flags is passed to\n+ * hold_lock_file_for_update(). Return 0 on success. On errors, set\n+ * errno appropriately and return a nonzero value.\n+ */\n+static int lock_packed_refs(int flags)\n {\n \tstatic int timeout_configured = 0;\n \tstatic int timeout_value = 1000;\n@@ -2534,10 +2544,12 @@ int lock_packed_refs(int flags)\n }\n \n /*\n- * Commit the packed refs changes.\n- * On error we must make sure that errno contains a meaningful value.\n+ * Write the current version of the packed refs cache from memory to\n+ * disk. The packed-refs file must already be locked for writing (see\n+ * lock_packed_refs()). Return zero on success. On errors, set errno\n+ * and return a nonzero value\n  */\n-int commit_packed_refs(void)\n+static int commit_packed_refs(void)\n {\n \tstruct packed_ref_cache *packed_ref_cache =\n \t\tget_packed_ref_cache(&ref_cache);\n@@ -2566,7 +2578,12 @@ int commit_packed_refs(void)\n \treturn error;\n }\n \n-void rollback_packed_refs(void)\n+/*\n+ * Rollback the lockfile for the packed-refs file, and discard the\n+ * in-memory packed reference cache.  (The packed-refs file will be\n+ * read anew if it is needed again after this function is called.)\n+ */\n+static void rollback_packed_refs(void)\n {\n \tstruct packed_ref_cache *packed_ref_cache =\n \t\tget_packed_ref_cache(&ref_cache);\ndiff --git a/refs.h b/refs.h\nindex 9602889..cd87f2f 100644\n--- a/refs.h\n+++ b/refs.h\n@@ -111,36 +111,6 @@ extern void warn_dangling_symref(FILE *fp, const char *msg_fmt, const char *refn\n extern void warn_dangling_symrefs(FILE *fp, const char *msg_fmt, const struct string_list *refnames);\n \n /*\n- * Lock the packed-refs file for writing.  Flags is passed to\n- * hold_lock_file_for_update().  Return 0 on success.\n- * Errno is set to something meaningful on error.\n- */\n-extern int lock_packed_refs(int flags);\n-\n-/*\n- * Add a reference to the in-memory packed reference cache.  This may\n- * only be called while the packed-refs file is locked (see\n- * lock_packed_refs()).  To actually write the packed-refs file, call\n- * commit_packed_refs().\n- */\n-extern void add_packed_ref(const char *refname, const unsigned char *sha1);\n-\n-/*\n- * Write the current version of the packed refs cache from memory to\n- * disk.  The packed-refs file must already be locked for writing (see\n- * lock_packed_refs()).  Return zero on success.\n- * Sets errno to something meaningful on error.\n- */\n-extern int commit_packed_refs(void);\n-\n-/*\n- * Rollback the lockfile for the packed-refs file, and discard the\n- * in-memory packed reference cache.  (The packed-refs file will be\n- * read anew if it is needed again after this function is called.)\n- */\n-extern void rollback_packed_refs(void);\n-\n-/*\n  * Flags for controlling behaviour of pack_refs()\n  * PACK_REFS_PRUNE: Prune loose refs after packing\n  * PACK_REFS_ALL:   Pack _all_ refs, not just tags and already packed refs\n-- \n2.1.4\n"},{"id":"264529","messageId":"63ae59139a3a7b5c85f6d44864eade79a93965fb.1434980615.git.mhagger@alum.mit.edu","threadId":"39691","inReplyTo":"cover.1434980615.git.mhagger@alum.mit.edu","subject":"[PATCH v3 12/19] initial_ref_transaction_commit(): check for duplicate refs","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2015-06-22T14:03:03Z","receivedAt":"2015-06-22T14:03:03Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"Error out if the ref_transaction includes more than one update for any\nrefname.\n\nSigned-off-by: Michael Haggerty <mhagger@alum.mit.edu>\n---\n refs.c | 11 +++++++++++\n 1 file changed, 11 insertions(+)\n\ndiff --git a/refs.c b/refs.c\nindex 31661c7..53d9e45 100644\n--- a/refs.c\n+++ b/refs.c\n@@ -4087,12 +4087,22 @@ int initial_ref_transaction_commit(struct ref_transaction *transaction,\n \tint ret = 0, i;\n \tint n = transaction->nr;\n \tstruct ref_update **updates = transaction->updates;\n+\tstruct string_list affected_refnames = STRING_LIST_INIT_NODUP;\n \n \tassert(err);\n \n \tif (transaction->state != REF_TRANSACTION_OPEN)\n \t\tdie(\"BUG: commit called for transaction that is not open\");\n \n+\t/* Fail if a refname appears more than once in the transaction: */\n+\tfor (i = 0; i < n; i++)\n+\t\tstring_list_append(&affected_refnames, updates[i]->refname);\n+\tstring_list_sort(&affected_refnames);\n+\tif (ref_update_reject_duplicates(&affected_refnames, err)) {\n+\t\tret = TRANSACTION_GENERIC_ERROR;\n+\t\tgoto cleanup;\n+\t}\n+\n \tfor (i = 0; i < n; i++) {\n \t\tstruct ref_update *update = updates[i];\n \n@@ -4125,6 +4135,7 @@ int initial_ref_transaction_commit(struct ref_transaction *transaction,\n \n cleanup:\n \ttransaction->state = REF_TRANSACTION_CLOSED;\n+\tstring_list_clear(&affected_refnames, 0);\n \treturn ret;\n }\n \n-- \n2.1.4\n"},{"id":"264520","messageId":"5320ec28e09bda9cc1f8a1b9748b8de014bd8ffe.1434980615.git.mhagger@alum.mit.edu","threadId":"39691","inReplyTo":"cover.1434980615.git.mhagger@alum.mit.edu","subject":"[PATCH v3 13/19] initial_ref_transaction_commit(): check for ref D/F conflicts","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2015-06-22T14:03:04Z","receivedAt":"2015-06-22T14:03:04Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"In initial_ref_transaction_commit(), check for D/F conflicts (i.e.,\nthe type of conflict that exists between \"refs/foo\" and\n\"refs/foo/bar\") among the references being created and between the\nreferences being created and any hypothetical existing references.\n\nIdeally, there shouldn't *be* any existing references when this\nfunction is called. But, at least in the case of the \"testgit\" remote\nhelper, \"clone\" can be called after the remote-tracking \"HEAD\" and\n\"master\" branches have already been created. So let's just do the\nfull-blown check.\n\nSigned-off-by: Michael Haggerty <mhagger@alum.mit.edu>\n---\n\nNotes (discussion):\n    I'm a bit torn on this commit. Part of me says that we should avoid\n    the overhead of the extra checks against loose_refs and packed_refs\n    because there shouldn't be any references there anyway. Instead, the\n    \"testgit\" remote helper should be fixed.\n    \n    But these tests should be pretty cheap, and we would want to check for\n    ref D/F conflicts among the new references in any case, so it seems\n    safer to include these checks. Also, our documentation suggests that\n    users refer to git-remote-testgit(1) as an example when writing their\n    own remote helpers, so it could be that there is other code out there\n    that has imitated its light misuse of this functionality.\n\n refs.c | 34 ++++++++++++++++++++++++++++++++++\n 1 file changed, 34 insertions(+)\n\ndiff --git a/refs.c b/refs.c\nindex 53d9e45..4513ec7 100644\n--- a/refs.c\n+++ b/refs.c\n@@ -4081,9 +4081,19 @@ cleanup:\n \treturn ret;\n }\n \n+static int ref_present(const char *refname,\n+\t\t       const struct object_id *oid, int flags, void *cb_data)\n+{\n+\tstruct string_list *affected_refnames = cb_data;\n+\n+\treturn string_list_has_string(affected_refnames, refname);\n+}\n+\n int initial_ref_transaction_commit(struct ref_transaction *transaction,\n \t\t\t\t   struct strbuf *err)\n {\n+\tstruct ref_dir *loose_refs = get_loose_refs(&ref_cache);\n+\tstruct ref_dir *packed_refs = get_packed_refs(&ref_cache);\n \tint ret = 0, i;\n \tint n = transaction->nr;\n \tstruct ref_update **updates = transaction->updates;\n@@ -4103,12 +4113,36 @@ int initial_ref_transaction_commit(struct ref_transaction *transaction,\n \t\tgoto cleanup;\n \t}\n \n+\t/*\n+\t * It's really undefined to call this function in an active\n+\t * repository or when there are existing references: we are\n+\t * only locking and changing packed-refs, so (1) any\n+\t * simultaneous processes might try to change a reference at\n+\t * the same time we do, and (2) any existing loose versions of\n+\t * the references that we are setting would have precedence\n+\t * over our values. But some remote helpers create the remote\n+\t * \"HEAD\" and \"master\" branches before calling this function,\n+\t * so here we really only check that none of the references\n+\t * that we are creating already exists.\n+\t */\n+\tif (for_each_rawref(ref_present, &affected_refnames))\n+\t\tdie(\"BUG: initial ref transaction called with existing refs\");\n+\n \tfor (i = 0; i < n; i++) {\n \t\tstruct ref_update *update = updates[i];\n \n \t\tif ((update->flags & REF_HAVE_OLD) &&\n \t\t    !is_null_sha1(update->old_sha1))\n \t\t\tdie(\"BUG: initial ref transaction with old_sha1 set\");\n+\t\tif (verify_refname_available(update->refname,\n+\t\t\t\t\t     &affected_refnames, NULL,\n+\t\t\t\t\t     loose_refs, err) ||\n+\t\t    verify_refname_available(update->refname,\n+\t\t\t\t\t     &affected_refnames, NULL,\n+\t\t\t\t\t     packed_refs, err)) {\n+\t\t\tret = TRANSACTION_NAME_CONFLICT;\n+\t\t\tgoto cleanup;\n+\t\t}\n \t}\n \n \tif (lock_packed_refs(0)) {\n-- \n2.1.4\n"},{"id":"264528","messageId":"f60ff0c29585c637ac47cb2d0b820e5a86c65834.1434980615.git.mhagger@alum.mit.edu","threadId":"39691","inReplyTo":"cover.1434980615.git.mhagger@alum.mit.edu","subject":"[PATCH v3 14/19] refs: move the remaining ref module declarations to refs.h","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2015-06-22T14:03:05Z","receivedAt":"2015-06-22T14:03:05Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"Some functions from the refs module were still declared in cache.h.\nMove them to refs.h.\n\nSigned-off-by: Michael Haggerty <mhagger@alum.mit.edu>\n---\n archive.c               |   1 +\n builtin/blame.c         |   1 +\n builtin/fast-export.c   |   1 +\n builtin/fmt-merge-msg.c |   1 +\n builtin/init-db.c       |   1 +\n builtin/log.c           |   1 +\n cache.h                 |  66 --------------------------\n refs.c                  |   6 ++-\n refs.h                  | 121 +++++++++++++++++++++++++++++++++++++++---------\n remote-testsvn.c        |   1 +\n 10 files changed, 109 insertions(+), 91 deletions(-)\n\ndiff --git a/archive.c b/archive.c\nindex d37c41d..936a594 100644\n--- a/archive.c\n+++ b/archive.c\n@@ -1,4 +1,5 @@\n #include \"cache.h\"\n+#include \"refs.h\"\n #include \"commit.h\"\n #include \"tree-walk.h\"\n #include \"attr.h\"\ndiff --git a/builtin/blame.c b/builtin/blame.c\nindex b3e948e..1c998cb 100644\n--- a/builtin/blame.c\n+++ b/builtin/blame.c\n@@ -6,6 +6,7 @@\n  */\n \n #include \"cache.h\"\n+#include \"refs.h\"\n #include \"builtin.h\"\n #include \"blob.h\"\n #include \"commit.h\"\ndiff --git a/builtin/fast-export.c b/builtin/fast-export.c\nindex b8182c2..d23f3be 100644\n--- a/builtin/fast-export.c\n+++ b/builtin/fast-export.c\n@@ -5,6 +5,7 @@\n  */\n #include \"builtin.h\"\n #include \"cache.h\"\n+#include \"refs.h\"\n #include \"commit.h\"\n #include \"object.h\"\n #include \"tag.h\"\ndiff --git a/builtin/fmt-merge-msg.c b/builtin/fmt-merge-msg.c\nindex 05f4c26..4ba7f28 100644\n--- a/builtin/fmt-merge-msg.c\n+++ b/builtin/fmt-merge-msg.c\n@@ -1,5 +1,6 @@\n #include \"builtin.h\"\n #include \"cache.h\"\n+#include \"refs.h\"\n #include \"commit.h\"\n #include \"diff.h\"\n #include \"revision.h\"\ndiff --git a/builtin/init-db.c b/builtin/init-db.c\nindex 4335738..49df78d 100644\n--- a/builtin/init-db.c\n+++ b/builtin/init-db.c\n@@ -4,6 +4,7 @@\n  * Copyright (C) Linus Torvalds, 2005\n  */\n #include \"cache.h\"\n+#include \"refs.h\"\n #include \"builtin.h\"\n #include \"exec_cmd.h\"\n #include \"parse-options.h\"\ndiff --git a/builtin/log.c b/builtin/log.c\nindex e67671e..3caa917 100644\n--- a/builtin/log.c\n+++ b/builtin/log.c\n@@ -5,6 +5,7 @@\n  *\t\t 2006 Junio Hamano\n  */\n #include \"cache.h\"\n+#include \"refs.h\"\n #include \"color.h\"\n #include \"commit.h\"\n #include \"diff.h\"\ndiff --git a/cache.h b/cache.h\nindex be92121..1c00098 100644\n--- a/cache.h\n+++ b/cache.h\n@@ -1009,76 +1009,10 @@ extern int get_oid_hex(const char *hex, struct object_id *sha1);\n \n extern char *sha1_to_hex(const unsigned char *sha1);\t/* static buffer result! */\n extern char *oid_to_hex(const struct object_id *oid);\t/* same static buffer as sha1_to_hex */\n-extern int read_ref_full(const char *refname, int resolve_flags,\n-\t\t\t unsigned char *sha1, int *flags);\n-extern int read_ref(const char *refname, unsigned char *sha1);\n \n-/*\n- * Resolve a reference, recursively following symbolic refererences.\n- *\n- * Store the referred-to object's name in sha1 and return the name of\n- * the non-symbolic reference that ultimately pointed at it.  The\n- * return value, if not NULL, is a pointer into either a static buffer\n- * or the input ref.\n- *\n- * If the reference cannot be resolved to an object, the behavior\n- * depends on the RESOLVE_REF_READING flag:\n- *\n- * - If RESOLVE_REF_READING is set, return NULL.\n- *\n- * - If RESOLVE_REF_READING is not set, clear sha1 and return the name of\n- *   the last reference name in the chain, which will either be a non-symbolic\n- *   reference or an undefined reference.  If this is a prelude to\n- *   \"writing\" to the ref, the return value is the name of the ref\n- *   that will actually be created or changed.\n- *\n- * If the RESOLVE_REF_NO_RECURSE flag is passed, only resolves one\n- * level of symbolic reference.  The value stored in sha1 for a symbolic\n- * reference will always be null_sha1 in this case, and the return\n- * value is the reference that the symref refers to directly.\n- *\n- * If flags is non-NULL, set the value that it points to the\n- * combination of REF_ISPACKED (if the reference was found among the\n- * packed references), REF_ISSYMREF (if the initial reference was a\n- * symbolic reference), REF_BAD_NAME (if the reference name is ill\n- * formed --- see RESOLVE_REF_ALLOW_BAD_NAME below), and REF_ISBROKEN\n- * (if the ref is malformed or has a bad name). See refs.h for more detail\n- * on each flag.\n- *\n- * If ref is not a properly-formatted, normalized reference, return\n- * NULL.  If more than MAXDEPTH recursive symbolic lookups are needed,\n- * give up and return NULL.\n- *\n- * RESOLVE_REF_ALLOW_BAD_NAME allows resolving refs even when their\n- * name is invalid according to git-check-ref-format(1).  If the name\n- * is bad then the value stored in sha1 will be null_sha1 and the two\n- * flags REF_ISBROKEN and REF_BAD_NAME will be set.\n- *\n- * Even with RESOLVE_REF_ALLOW_BAD_NAME, names that escape the refs/\n- * directory and do not consist of all caps and underscores cannot be\n- * resolved. The function returns NULL for such ref names.\n- * Caps and underscores refers to the special refs, such as HEAD,\n- * FETCH_HEAD and friends, that all live outside of the refs/ directory.\n- */\n-#define RESOLVE_REF_READING 0x01\n-#define RESOLVE_REF_NO_RECURSE 0x02\n-#define RESOLVE_REF_ALLOW_BAD_NAME 0x04\n-extern const char *resolve_ref_unsafe(const char *ref, int resolve_flags, unsigned char *sha1, int *flags);\n-extern char *resolve_refdup(const char *ref, int resolve_flags, unsigned char *sha1, int *flags);\n-\n-extern int dwim_ref(const char *str, int len, unsigned char *sha1, char **ref);\n-extern int dwim_log(const char *str, int len, unsigned char *sha1, char **ref);\n extern int interpret_branch_name(const char *str, int len, struct strbuf *);\n extern int get_sha1_mb(const char *str, unsigned char *sha1);\n \n-/*\n- * Return true iff abbrev_name is a possible abbreviation for\n- * full_name according to the rules defined by ref_rev_parse_rules in\n- * refs.c.\n- */\n-extern int refname_match(const char *abbrev_name, const char *full_name);\n-\n-extern int create_symref(const char *ref, const char *refs_heads_master, const char *logmsg);\n extern int validate_headref(const char *ref);\n \n extern int base_name_compare(const char *name1, int len1, int mode1, const char *name2, int len2, int mode2);\ndiff --git a/refs.c b/refs.c\nindex 4513ec7..2ba618e 100644\n--- a/refs.c\n+++ b/refs.c\n@@ -1732,9 +1732,11 @@ const char *resolve_ref_unsafe(const char *refname, int resolve_flags,\n \treturn ret;\n }\n \n-char *resolve_refdup(const char *ref, int resolve_flags, unsigned char *sha1, int *flags)\n+char *resolve_refdup(const char *refname, int resolve_flags,\n+\t\t     unsigned char *sha1, int *flags)\n {\n-\treturn xstrdup_or_null(resolve_ref_unsafe(ref, resolve_flags, sha1, flags));\n+\treturn xstrdup_or_null(resolve_ref_unsafe(refname, resolve_flags,\n+\t\t\t\t\t\t  sha1, flags));\n }\n \n /* The argument to filter_refs */\ndiff --git a/refs.h b/refs.h\nindex cd87f2f..b22e308 100644\n--- a/refs.h\n+++ b/refs.h\n@@ -2,6 +2,98 @@\n #define REFS_H\n \n /*\n+ * Resolve a reference, recursively following symbolic refererences.\n+ *\n+ * Store the referred-to object's name in sha1 and return the name of\n+ * the non-symbolic reference that ultimately pointed at it.  The\n+ * return value, if not NULL, is a pointer into either a static buffer\n+ * or the input ref.\n+ *\n+ * If the reference cannot be resolved to an object, the behavior\n+ * depends on the RESOLVE_REF_READING flag:\n+ *\n+ * - If RESOLVE_REF_READING is set, return NULL.\n+ *\n+ * - If RESOLVE_REF_READING is not set, clear sha1 and return the name of\n+ *   the last reference name in the chain, which will either be a non-symbolic\n+ *   reference or an undefined reference.  If this is a prelude to\n+ *   \"writing\" to the ref, the return value is the name of the ref\n+ *   that will actually be created or changed.\n+ *\n+ * If the RESOLVE_REF_NO_RECURSE flag is passed, only resolves one\n+ * level of symbolic reference.  The value stored in sha1 for a symbolic\n+ * reference will always be null_sha1 in this case, and the return\n+ * value is the reference that the symref refers to directly.\n+ *\n+ * If flags is non-NULL, set the value that it points to the\n+ * combination of REF_ISPACKED (if the reference was found among the\n+ * packed references), REF_ISSYMREF (if the initial reference was a\n+ * symbolic reference), REF_BAD_NAME (if the reference name is ill\n+ * formed --- see RESOLVE_REF_ALLOW_BAD_NAME below), and REF_ISBROKEN\n+ * (if the ref is malformed or has a bad name). See refs.h for more detail\n+ * on each flag.\n+ *\n+ * If ref is not a properly-formatted, normalized reference, return\n+ * NULL.  If more than MAXDEPTH recursive symbolic lookups are needed,\n+ * give up and return NULL.\n+ *\n+ * RESOLVE_REF_ALLOW_BAD_NAME allows resolving refs even when their\n+ * name is invalid according to git-check-ref-format(1).  If the name\n+ * is bad then the value stored in sha1 will be null_sha1 and the two\n+ * flags REF_ISBROKEN and REF_BAD_NAME will be set.\n+ *\n+ * Even with RESOLVE_REF_ALLOW_BAD_NAME, names that escape the refs/\n+ * directory and do not consist of all caps and underscores cannot be\n+ * resolved. The function returns NULL for such ref names.\n+ * Caps and underscores refers to the special refs, such as HEAD,\n+ * FETCH_HEAD and friends, that all live outside of the refs/ directory.\n+ */\n+#define RESOLVE_REF_READING 0x01\n+#define RESOLVE_REF_NO_RECURSE 0x02\n+#define RESOLVE_REF_ALLOW_BAD_NAME 0x04\n+\n+extern const char *resolve_ref_unsafe(const char *refname, int resolve_flags,\n+\t\t\t\t      unsigned char *sha1, int *flags);\n+\n+extern char *resolve_refdup(const char *refname, int resolve_flags,\n+\t\t\t    unsigned char *sha1, int *flags);\n+\n+extern int read_ref_full(const char *refname, int resolve_flags,\n+\t\t\t unsigned char *sha1, int *flags);\n+extern int read_ref(const char *refname, unsigned char *sha1);\n+\n+extern int ref_exists(const char *);\n+\n+extern int is_branch(const char *refname);\n+\n+/*\n+ * If refname is a non-symbolic reference that refers to a tag object,\n+ * and the tag can be (recursively) dereferenced to a non-tag object,\n+ * store the SHA1 of the referred-to object to sha1 and return 0.  If\n+ * any of these conditions are not met, return a non-zero value.\n+ * Symbolic references are considered unpeelable, even if they\n+ * ultimately resolve to a peelable tag.\n+ */\n+extern int peel_ref(const char *refname, unsigned char *sha1);\n+\n+/**\n+ * Resolve refname in the nested \"gitlink\" repository that is located\n+ * at path.  If the resolution is successful, return 0 and set sha1 to\n+ * the name of the object; otherwise, return a non-zero value.\n+ */\n+extern int resolve_gitlink_ref(const char *path, const char *refname, unsigned char *sha1);\n+\n+/*\n+ * Return true iff abbrev_name is a possible abbreviation for\n+ * full_name according to the rules defined by ref_rev_parse_rules in\n+ * refs.c.\n+ */\n+extern int refname_match(const char *abbrev_name, const char *full_name);\n+\n+extern int dwim_ref(const char *str, int len, unsigned char *sha1, char **ref);\n+extern int dwim_log(const char *str, int len, unsigned char *sha1, char **ref);\n+\n+/*\n  * A ref_transaction represents a collection of ref updates\n  * that should succeed or fail together.\n  *\n@@ -99,14 +191,14 @@ extern int for_each_remote_ref_submodule(const char *submodule, each_ref_fn fn,\n extern int head_ref_namespaced(each_ref_fn fn, void *cb_data);\n extern int for_each_namespaced_ref(each_ref_fn fn, void *cb_data);\n \n+/* can be used to learn about broken ref and symref */\n+extern int for_each_rawref(each_ref_fn, void *);\n+\n static inline const char *has_glob_specials(const char *pattern)\n {\n \treturn strpbrk(pattern, \"?*[\");\n }\n \n-/* can be used to learn about broken ref and symref */\n-extern int for_each_rawref(each_ref_fn, void *);\n-\n extern void warn_dangling_symref(FILE *fp, const char *msg_fmt, const char *refname);\n extern void warn_dangling_symrefs(FILE *fp, const char *msg_fmt, const struct string_list *refnames);\n \n@@ -124,20 +216,6 @@ extern void warn_dangling_symrefs(FILE *fp, const char *msg_fmt, const struct st\n  */\n int pack_refs(unsigned int flags);\n \n-extern int ref_exists(const char *);\n-\n-extern int is_branch(const char *refname);\n-\n-/*\n- * If refname is a non-symbolic reference that refers to a tag object,\n- * and the tag can be (recursively) dereferenced to a non-tag object,\n- * store the SHA1 of the referred-to object to sha1 and return 0.  If\n- * any of these conditions are not met, return a non-zero value.\n- * Symbolic references are considered unpeelable, even if they\n- * ultimately resolve to a peelable tag.\n- */\n-extern int peel_ref(const char *refname, unsigned char *sha1);\n-\n /*\n  * Flags controlling ref_transaction_update(), ref_transaction_create(), etc.\n  * REF_NODEREF: act on the ref directly, instead of dereferencing\n@@ -206,17 +284,13 @@ extern int for_each_reflog(each_ref_fn, void *);\n extern int check_refname_format(const char *refname, int flags);\n \n extern const char *prettify_refname(const char *refname);\n+\n extern char *shorten_unambiguous_ref(const char *refname, int strict);\n \n /** rename ref, return 0 on success **/\n extern int rename_ref(const char *oldref, const char *newref, const char *logmsg);\n \n-/**\n- * Resolve refname in the nested \"gitlink\" repository that is located\n- * at path.  If the resolution is successful, return 0 and set sha1 to\n- * the name of the object; otherwise, return a non-zero value.\n- */\n-extern int resolve_gitlink_ref(const char *path, const char *refname, unsigned char *sha1);\n+extern int create_symref(const char *ref, const char *refs_heads_master, const char *logmsg);\n \n enum action_on_err {\n \tUPDATE_REFS_MSG_ON_ERR,\n@@ -367,6 +441,7 @@ int update_ref(const char *msg, const char *refname,\n \t       unsigned int flags, enum action_on_err onerr);\n \n extern int parse_hide_refs_config(const char *var, const char *value, const char *);\n+\n extern int ref_is_hidden(const char *);\n \n enum expire_reflog_flags {\ndiff --git a/remote-testsvn.c b/remote-testsvn.c\nindex 48bf6eb..f599c37 100644\n--- a/remote-testsvn.c\n+++ b/remote-testsvn.c\n@@ -1,4 +1,5 @@\n #include \"cache.h\"\n+#include \"refs.h\"\n #include \"remote.h\"\n #include \"strbuf.h\"\n #include \"url.h\"\n-- \n2.1.4\n"},{"id":"264531","messageId":"802ff45bc3c789ff60964ceed3e4c14650ad583f.1434980615.git.mhagger@alum.mit.edu","threadId":"39691","inReplyTo":"cover.1434980615.git.mhagger@alum.mit.edu","subject":"[PATCH v3 15/19] refs.h: add some parameter names to function declarations","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2015-06-22T14:03:06Z","receivedAt":"2015-06-22T14:03:06Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>\n---\n refs.h | 22 +++++++++++-----------\n 1 file changed, 11 insertions(+), 11 deletions(-)\n\ndiff --git a/refs.h b/refs.h\nindex b22e308..c9596ea 100644\n--- a/refs.h\n+++ b/refs.h\n@@ -62,7 +62,7 @@ extern int read_ref_full(const char *refname, int resolve_flags,\n \t\t\t unsigned char *sha1, int *flags);\n extern int read_ref(const char *refname, unsigned char *sha1);\n \n-extern int ref_exists(const char *);\n+extern int ref_exists(const char *refname);\n \n extern int is_branch(const char *refname);\n \n@@ -170,15 +170,15 @@ typedef int each_ref_fn(const char *refname,\n  * modifies the reference also returns a nonzero value to immediately\n  * stop the iteration.\n  */\n-extern int head_ref(each_ref_fn, void *);\n-extern int for_each_ref(each_ref_fn, void *);\n-extern int for_each_ref_in(const char *, each_ref_fn, void *);\n-extern int for_each_tag_ref(each_ref_fn, void *);\n-extern int for_each_branch_ref(each_ref_fn, void *);\n-extern int for_each_remote_ref(each_ref_fn, void *);\n-extern int for_each_replace_ref(each_ref_fn, void *);\n-extern int for_each_glob_ref(each_ref_fn, const char *pattern, void *);\n-extern int for_each_glob_ref_in(each_ref_fn, const char *pattern, const char* prefix, void *);\n+extern int head_ref(each_ref_fn fn, void *cb_data);\n+extern int for_each_ref(each_ref_fn fn, void *cb_data);\n+extern int for_each_ref_in(const char *prefix, each_ref_fn fn, void *cb_data);\n+extern int for_each_tag_ref(each_ref_fn fn, void *cb_data);\n+extern int for_each_branch_ref(each_ref_fn fn, void *cb_data);\n+extern int for_each_remote_ref(each_ref_fn fn, void *cb_data);\n+extern int for_each_replace_ref(each_ref_fn fn, void *cb_data);\n+extern int for_each_glob_ref(each_ref_fn fn, const char *pattern, void *cb_data);\n+extern int for_each_glob_ref_in(each_ref_fn fn, const char *pattern, const char *prefix, void *cb_data);\n \n extern int head_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data);\n extern int for_each_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data);\n@@ -192,7 +192,7 @@ extern int head_ref_namespaced(each_ref_fn fn, void *cb_data);\n extern int for_each_namespaced_ref(each_ref_fn fn, void *cb_data);\n \n /* can be used to learn about broken ref and symref */\n-extern int for_each_rawref(each_ref_fn, void *);\n+extern int for_each_rawref(each_ref_fn fn, void *cb_data);\n \n static inline const char *has_glob_specials(const char *pattern)\n {\n-- \n2.1.4\n"},{"id":"264523","messageId":"284c129521a0287fc0f4260c4b4e8751d675419a.1434980615.git.mhagger@alum.mit.edu","threadId":"39691","inReplyTo":"cover.1434980615.git.mhagger@alum.mit.edu","subject":"[PATCH v3 16/19] check_branch_commit(): make first parameter const","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2015-06-22T14:03:07Z","receivedAt":"2015-06-22T14:03:07Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"Make it clear that this function does not overwrite its first\nargument.\n\nSigned-off-by: Michael Haggerty <mhagger@alum.mit.edu>\n---\n builtin/branch.c | 2 +-\n 1 file changed, 1 insertion(+), 1 deletion(-)\n\ndiff --git a/builtin/branch.c b/builtin/branch.c\nindex b42e5b6..47e3eb9 100644\n--- a/builtin/branch.c\n+++ b/builtin/branch.c\n@@ -160,7 +160,7 @@ static int branch_merged(int kind, const char *name,\n }\n \n static int check_branch_commit(const char *branchname, const char *refname,\n-\t\t\t       unsigned char *sha1, struct commit *head_rev,\n+\t\t\t       const unsigned char *sha1, struct commit *head_rev,\n \t\t\t       int kinds, int force)\n {\n \tstruct commit *rev = lookup_commit_reference(sha1);\n-- \n2.1.4\n"},{"id":"264532","messageId":"2104214a318e7bb636507888e4a31c5b739a7f40.1434980615.git.mhagger@alum.mit.edu","threadId":"39691","inReplyTo":"cover.1434980615.git.mhagger@alum.mit.edu","subject":"[PATCH v3 17/19] update_ref(): don't read old reference value before delete","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2015-06-22T14:03:08Z","receivedAt":"2015-06-22T14:03:08Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"If we are deleting the reference, then we don't need to read the\nreference's old value. It doesn't provide any race safety, because the\nvalue read just before the delete is no \"better\" than the value that\nwould be read under lock during the delete. And even if the reference\npreviously didn't exist, we can call delete_ref() on it if we don't\nprovide an old_sha1 value.\n\nSigned-off-by: Michael Haggerty <mhagger@alum.mit.edu>\n---\n fast-import.c | 6 +++---\n 1 file changed, 3 insertions(+), 3 deletions(-)\n\ndiff --git a/fast-import.c b/fast-import.c\nindex 6378726..d7ed065 100644\n--- a/fast-import.c\n+++ b/fast-import.c\n@@ -1692,13 +1692,13 @@ static int update_branch(struct branch *b)\n \tunsigned char old_sha1[20];\n \tstruct strbuf err = STRBUF_INIT;\n \n-\tif (read_ref(b->name, old_sha1))\n-\t\thashclr(old_sha1);\n \tif (is_null_sha1(b->sha1)) {\n \t\tif (b->delete)\n-\t\t\tdelete_ref(b->name, old_sha1, 0);\n+\t\t\tdelete_ref(b->name, NULL, 0);\n \t\treturn 0;\n \t}\n+\tif (read_ref(b->name, old_sha1))\n+\t\thashclr(old_sha1);\n \tif (!force_update && !is_null_sha1(old_sha1)) {\n \t\tstruct commit *old_cmit, *new_cmit;\n \n-- \n2.1.4\n"},{"id":"264527","messageId":"4eaf80adf666bf86beb271779d7226d9eb04d257.1434980615.git.mhagger@alum.mit.edu","threadId":"39691","inReplyTo":"cover.1434980615.git.mhagger@alum.mit.edu","subject":"[PATCH v3 18/19] cmd_update_ref(): make logic more straightforward","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2015-06-22T14:03:09Z","receivedAt":"2015-06-22T14:03:09Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"Restructure the code to avoid clearing oldsha1 when oldval is unset.\nIt's value is not needed in that case, so this change makes it more\nobvious that its initialization is consistent with its later use.\n\nSigned-off-by: Michael Haggerty <mhagger@alum.mit.edu>\n---\n builtin/update-ref.c | 13 ++++++++++---\n 1 file changed, 10 insertions(+), 3 deletions(-)\n\ndiff --git a/builtin/update-ref.c b/builtin/update-ref.c\nindex 3d79a46..160c7ac 100644\n--- a/builtin/update-ref.c\n+++ b/builtin/update-ref.c\n@@ -408,9 +408,16 @@ int cmd_update_ref(int argc, const char **argv, const char *prefix)\n \t\t\tdie(\"%s: not a valid SHA1\", value);\n \t}\n \n-\thashclr(oldsha1); /* all-zero hash in case oldval is the empty string */\n-\tif (oldval && *oldval && get_sha1(oldval, oldsha1))\n-\t\tdie(\"%s: not a valid old SHA1\", oldval);\n+\tif (oldval) {\n+\t\tif (!*oldval)\n+\t\t\t/*\n+\t\t\t * The empty string implies that the reference\n+\t\t\t * must not already exist:\n+\t\t\t */\n+\t\t\thashclr(oldsha1);\n+\t\telse if (get_sha1(oldval, oldsha1))\n+\t\t\tdie(\"%s: not a valid old SHA1\", oldval);\n+\t}\n \n \tif (no_deref)\n \t\tflags = REF_NODEREF;\n-- \n2.1.4\n"},{"id":"264525","messageId":"29ca4c18c045147c5260eb699dad31d0e4036e8b.1434980615.git.mhagger@alum.mit.edu","threadId":"39691","inReplyTo":"cover.1434980615.git.mhagger@alum.mit.edu","subject":"[PATCH v3 19/19] delete_ref(): use the usual convention for old_sha1","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2015-06-22T14:03:10Z","receivedAt":"2015-06-22T14:03:10Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"The ref_transaction_update() family of functions use the following\nconvention for their old_sha1 parameters:\n\n* old_sha1 == NULL: Don't check the old value at all.\n* is_null_sha1(old_sha1): Ensure that the reference didn't exist\n  before the transaction.\n* otherwise: Ensure that the reference had the specified value before\n  the transaction.\n\ndelete_ref() had a different convention, namely treating\nis_null_sha1(old_sha1) as \"don't care\". Change it to adhere to the\nstandard convention to reduce the scope for confusion.\n\nPlease note that it is now a bug to pass old_sha1=NULL_SHA1 to\ndelete_ref() (because it doesn't make sense to delete a reference that\nyou already know doesn't exist). This is consistent with the behavior\nof ref_transaction_delete().\n\nMost of the callers of delete_ref() never pass old_sha1=NULL_SHA1 to\ndelete_ref(), and are therefore unaffected by this change. The\ntwo exceptions are:\n\n* The call in cmd_update_ref(), which passed NULL_SHA1 if the old\n  value passed in on the command line was 0{40} or the empty string.\n  Change that caller to pass NULL in those cases.\n\n  Arguably, it should be an error to call \"update-ref -d\" with the old\n  value set to \"does not exist\", just as it is for the `--stdin`\n  command \"delete\". But since this usage was accepted until now,\n  continue to accept it.\n\n* The call in delete_branches(), which could pass NULL_SHA1 if\n  deleting a broken or symbolic ref. Change it to pass NULL in these\n  cases.\n\nSigned-off-by: Michael Haggerty <mhagger@alum.mit.edu>\n---\n builtin/branch.c     |  3 ++-\n builtin/update-ref.c |  8 +++++++-\n refs.c               |  8 --------\n refs.h               | 10 +++++-----\n 4 files changed, 14 insertions(+), 15 deletions(-)\n\ndiff --git a/builtin/branch.c b/builtin/branch.c\nindex 47e3eb9..58aa84f 100644\n--- a/builtin/branch.c\n+++ b/builtin/branch.c\n@@ -253,7 +253,8 @@ static int delete_branches(int argc, const char **argv, int force, int kinds,\n \t\t\tcontinue;\n \t\t}\n \n-\t\tif (delete_ref(name, sha1, REF_NODEREF)) {\n+\t\tif (delete_ref(name, is_null_sha1(sha1) ? NULL : sha1,\n+\t\t\t       REF_NODEREF)) {\n \t\t\terror(remote_branch\n \t\t\t      ? _(\"Error deleting remote-tracking branch '%s'\")\n \t\t\t      : _(\"Error deleting branch '%s'\"),\ndiff --git a/builtin/update-ref.c b/builtin/update-ref.c\nindex 160c7ac..6763cf1 100644\n--- a/builtin/update-ref.c\n+++ b/builtin/update-ref.c\n@@ -422,7 +422,13 @@ int cmd_update_ref(int argc, const char **argv, const char *prefix)\n \tif (no_deref)\n \t\tflags = REF_NODEREF;\n \tif (delete)\n-\t\treturn delete_ref(refname, oldval ? oldsha1 : NULL, flags);\n+\t\t/*\n+\t\t * For purposes of backwards compatibility, we treat\n+\t\t * NULL_SHA1 as \"don't care\" here:\n+\t\t */\n+\t\treturn delete_ref(refname,\n+\t\t\t\t  (oldval && !is_null_sha1(oldsha1)) ? oldsha1 : NULL,\n+\t\t\t\t  flags);\n \telse\n \t\treturn update_ref(msg, refname, sha1, oldval ? oldsha1 : NULL,\n \t\t\t\t  flags, UPDATE_REFS_DIE_ON_ERR);\ndiff --git a/refs.c b/refs.c\nindex 2ba618e..adf3dc2 100644\n--- a/refs.c\n+++ b/refs.c\n@@ -2821,14 +2821,6 @@ int delete_ref(const char *refname, const unsigned char *old_sha1,\n \tstruct ref_transaction *transaction;\n \tstruct strbuf err = STRBUF_INIT;\n \n-\t/*\n-\t * Treat NULL_SHA1 and NULL alike, to mean \"we don't care what\n-\t * the old value of the reference was (or even if it didn't\n-\t * exist)\":\n-\t */\n-\tif (old_sha1 && is_null_sha1(old_sha1))\n-\t\told_sha1 = NULL;\n-\n \ttransaction = ref_transaction_begin(&err);\n \tif (!transaction ||\n \t    ref_transaction_delete(transaction, refname, old_sha1,\ndiff --git a/refs.h b/refs.h\nindex c9596ea..e82fca5 100644\n--- a/refs.h\n+++ b/refs.h\n@@ -240,11 +240,11 @@ extern int read_ref_at(const char *refname, unsigned int flags,\n extern int reflog_exists(const char *refname);\n \n /*\n- * Delete the specified reference. If old_sha1 is non-NULL and not\n- * NULL_SHA1, then verify that the current value of the reference is\n- * old_sha1 before deleting it. If old_sha1 is NULL or NULL_SHA1,\n- * delete the reference if it exists, regardless of its old value.\n- * flags is passed through to ref_transaction_delete().\n+ * Delete the specified reference. If old_sha1 is non-NULL, then\n+ * verify that the current value of the reference is old_sha1 before\n+ * deleting it. If old_sha1 is NULL, delete the reference if it\n+ * exists, regardless of its old value. It is an error for old_sha1 to\n+ * be NULL_SHA1. flags is passed through to ref_transaction_delete().\n  */\n extern int delete_ref(const char *refname, const unsigned char *old_sha1,\n \t\t      unsigned int flags);\n-- \n2.1.4\n"},{"id":"264589","messageId":"xmqqtwtzfo79.fsf@gitster.dls.corp.google.com","threadId":"39691","inReplyTo":"63ae59139a3a7b5c85f6d44864eade79a93965fb.1434980615.git.mhagger@alum.mit.edu","subject":"Re: [PATCH v3 12/19] initial_ref_transaction_commit(): check for duplicate refs","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2015-06-22T21:06:02Z","receivedAt":"2015-06-22T21:06:02Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Michael Haggerty <mhagger@alum.mit.edu> writes:\n\n> Error out if the ref_transaction includes more than one update for any\n> refname.\n>\n> Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>\n> ---\n>  refs.c | 11 +++++++++++\n>  1 file changed, 11 insertions(+)\n\nThis somehow feels like \"ehh, I now know better and this function\nshould have been like this from the beginning\" to me.\n\nBut that is OK.\n\nIs the initial creation logic too fragile to deserve its own\nfunction to force callers to think about it, by the way?\n\nWhat I am wondering is if we could turn the safety logic that appear\nhere (i.e. no existing refs must be assumed by the set of updates,\netc.)  into an optimization cue and implement this as a special case\nhelper to ref_transaction_commit(), i.e.\n\n\tref_transaction_commit(...)\n        {\n\t\tif (updates are all initial creation &&\n                    no existing refs in repository)\n\t\t\treturn initial_ref_transaction_commit(...);\n\t\t/* otherwise we do the usual thing */\n\t\t...\n\t}\n\nand have \"clone\" call ref_transaction_commit() as usual.\n"},{"id":"264591","messageId":"xmqqpp4nfo0f.fsf@gitster.dls.corp.google.com","threadId":"39691","inReplyTo":"29ca4c18c045147c5260eb699dad31d0e4036e8b.1434980615.git.mhagger@alum.mit.edu","subject":"Re: [PATCH v3 19/19] delete_ref(): use the usual convention for old_sha1","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2015-06-22T21:10:08Z","receivedAt":"2015-06-22T21:10:08Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Michael Haggerty <mhagger@alum.mit.edu> writes:\n\n> The ref_transaction_update() family of functions use the following\n> convention for their old_sha1 parameters:\n>\n> * old_sha1 == NULL: Don't check the old value at all.\n> * is_null_sha1(old_sha1): Ensure that the reference didn't exist\n>   before the transaction.\n> * otherwise: Ensure that the reference had the specified value before\n>   the transaction.\n>\n> delete_ref() had a different convention, namely treating\n> is_null_sha1(old_sha1) as \"don't care\". Change it to adhere to the\n> standard convention to reduce the scope for confusion.\n>\n> Please note that it is now a bug to pass old_sha1=NULL_SHA1 to\n> delete_ref() (because it doesn't make sense to delete a reference that\n> you already know doesn't exist). This is consistent with the behavior\n> of ref_transaction_delete().\n\nNice.\n\nEverything I read in this round of changes makes sense (except for\nthe ones I had minor comments on, which were separately sent).\n\nThanks.\n"},{"id":"264614","messageId":"558906A4.8060106@alum.mit.edu","threadId":"39691","inReplyTo":"xmqqtwtzfo79.fsf@gitster.dls.corp.google.com","subject":"Re: [PATCH v3 12/19] initial_ref_transaction_commit(): check for duplicate refs","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2015-06-23T07:11:32Z","receivedAt":"2015-06-23T07:11:32Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"On 06/22/2015 11:06 PM, Junio C Hamano wrote:\n> Michael Haggerty <mhagger@alum.mit.edu> writes:\n> \n>> Error out if the ref_transaction includes more than one update for any\n>> refname.\n>>\n>> Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>\n>> ---\n>>  refs.c | 11 +++++++++++\n>>  1 file changed, 11 insertions(+)\n> \n> This somehow feels like \"ehh, I now know better and this function\n> should have been like this from the beginning\" to me.\n> \n> But that is OK.\n> \n> Is the initial creation logic too fragile to deserve its own\n> function to force callers to think about it, by the way?\n> \n> What I am wondering is if we could turn the safety logic that appear\n> here (i.e. no existing refs must be assumed by the set of updates,\n> etc.)  into an optimization cue and implement this as a special case\n> helper to ref_transaction_commit(), i.e.\n> \n> \tref_transaction_commit(...)\n>         {\n> \t\tif (updates are all initial creation &&\n>                     no existing refs in repository)\n> \t\t\treturn initial_ref_transaction_commit(...);\n> \t\t/* otherwise we do the usual thing */\n> \t\t...\n> \t}\n> \n> and have \"clone\" call ref_transaction_commit() as usual.\n\nThe safety logic in this function is (approximately) necessary, but not\nsufficient, to guarantee safety. One of the shortcuts that it takes is\nnot locking the references while they are being created. Therefore, it\nwould be unsafe for one process to call ref_transaction_commit() while\nanother is calling initial_ref_transaction_commit(). So the caller has\nto \"know\" somehow that no other processes are working in the repository\nfor this optimization to be safe. It conveys that knowledge by calling\ninitial_ref_transaction_commit() rather than ref_transaction_commit().\n\nOf course the next question is, \"How does `git clone` know that no other\nprocess is working in the new repository?\" Actually, it doesn't. For\nexample, I just verified that I can run\n\n    git clone $URL mygit &\n    sleep 0.1\n    cd mygit\n    git commit --allow-empty -m \"New root commit\"\n\nand thereby \"overwrite\" the upstream `master` without the usual\nnon-fast-forward protection. I guess we are just relying on the user's\ncommon sense not to run Git commands in a new repository before its\ncreation is complete.\n\nI suppose we *could* special-case `git clone` to not finish the\ninitialization of the repository (for example, not write the `config`\nfile) until *after* the packed-refs file is written. This would prevent\nother git processes from recognizing the directory as a Git repository\nand so prevent them from running before the clone is finished.\n\nBut I think if anything it would make more sense to go the other direction:\n\n* Teach ref_transaction_commit() an option that asks it to write\n  references updates to packed-refs instead of loose refs (but\n  locking the references as usual).\n\n* Change clone to use ref_transaction_commit() like everybody\n  else, passing it the new REFS_WRITE_TO_PACKED_REFS option.\n\nThen clone would participate in the normal locking protocol, and it\nwouldn't *matter* if another process runs before the clone is finished.\nThere would also be some consistency benefits. For example, if\ncore.logallrefupdates is set globally or on the command line, the\ninitial reference creations would be reflogged. And other operations\nthat write references in bulk could use the new\nREFS_WRITE_TO_PACKED_REFS option to prevent loose reference proliferation.\n\nBut I don't think any of this is a problem in practice, and I think we\ncan live with using the optimized-but-not-100%-safe\ninitial_ref_transaction_commit() for cloning.\n\nMichael\n\n-- \nMichael Haggerty\nmhagger@alum.mit.edu\n"},{"id":"264645","messageId":"xmqqd20me2uq.fsf@gitster.dls.corp.google.com","threadId":"39691","inReplyTo":"558906A4.8060106@alum.mit.edu","subject":"Re: [PATCH v3 12/19] initial_ref_transaction_commit(): check for duplicate refs","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2015-06-23T17:44:45Z","receivedAt":"2015-06-23T17:44:45Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Michael Haggerty <mhagger@alum.mit.edu> writes:\n\n> On 06/22/2015 11:06 PM, Junio C Hamano wrote:\n> ...\n>> What I am wondering is if we could turn the safety logic that appear\n>> here (i.e. no existing refs must be assumed by the set of updates,\n>> etc.)  into an optimization cue and implement this as a special case\n>> helper to ref_transaction_commit(), i.e.\n>> \n>> \tref_transaction_commit(...)\n>>         {\n>> \t\tif (updates are all initial creation &&\n>>                     no existing refs in repository)\n>> \t\t\treturn initial_ref_transaction_commit(...);\n>> \t\t/* otherwise we do the usual thing */\n>> \t\t...\n>> \t}\n>> \n>> and have \"clone\" call ref_transaction_commit() as usual.\n>\n> The safety logic in this function is (approximately) necessary, but not\n> sufficient, to guarantee safety.\n\nOh, no question about it, and you do not even have to bring up an\ninsane \"user runs random commands while Git is hard working on it\"\nnon use-case ;-)\n\n> One of the shortcuts that it takes is\n> not locking the references while they are being created. Therefore, it\n> would be unsafe for one process to call ref_transaction_commit() while\n> another is calling initial_ref_transaction_commit(). So the caller has\n> to \"know\" somehow that no other processes are working in the repository\n> for this optimization to be safe. It conveys that knowledge by calling\n> initial_ref_transaction_commit() rather than ref_transaction_commit().\n\nOK.  So the answer to my first question \"is the initial creation\nlogic too fragile\" is a resounding \"yes\"; the caller should know\nthat it is too crazy for the user to be competing with what it is\ndoing before deciding to call initial_ref_transaction_commit(),\nhence we cannot automatically detect if it is safe from within\nref_transaction_commit() to use this logic as an optimization.\n\n> But I think if anything it would make more sense to go the other direction:\n>\n> * Teach ref_transaction_commit() an option that asks it to write\n>   references updates to packed-refs instead of loose refs (but\n>   locking the references as usual).\n>\n> * Change clone to use ref_transaction_commit() like everybody\n>   else, passing it the new REFS_WRITE_TO_PACKED_REFS option.\n>\n> Then clone would participate in the normal locking protocol, and it\n> wouldn't *matter* if another process runs before the clone is finished.\n\nYeah, I thought that was actually I was driving at, and doing so\nwithout that write-to-packed-refs option, which I'd prefer to leave\nit as an optimization inside ref_transaction_commit().\n\nExcept that I missed that the initial_* variant is even more\naggressive (i.e. not locking), so no such optimization is safe.\n\n> There would also be some consistency benefits. For example, if\n> core.logallrefupdates is set globally or on the command line, the\n> initial reference creations would be reflogged. And other operations\n> that write references in bulk could use the new\n> REFS_WRITE_TO_PACKED_REFS option to prevent loose reference proliferation.\n>\n> But I don't think any of this is a problem in practice, and I think we\n> can live with using the optimized-but-not-100%-safe\n> initial_ref_transaction_commit() for cloning.\n\nOK.\n"}]}