{"thread":{"id":"36563","subject":"[PATCH v6 01/42] refs.c: constify the sha arguments for ref_transaction_create|delete|update","startedAt":"2014-05-01T20:37:00Z","lastAt":"2014-05-15T17:35:57Z","messageCount":78,"participants":["Ronnie Sahlberg","Eric Sunshine","Michael Haggerty","Jonathan Nieder"],"isPatch":true,"patchVersion":6,"patchTotal":42},"messages":[{"id":"240467","messageId":"1398976662-6962-1-git-send-email-sahlberg@google.com","threadId":"36563","inReplyTo":null,"subject":"[PATCH v6 00/42] Use ref transactions for all ref updates","fromName":"Ronnie Sahlberg","fromEmail":"sahlberg@google.com","sentAt":"2014-05-01T20:37:00Z","receivedAt":"2014-05-01T20:37:00Z","isPatch":true,"sender":{"key":"sahlberg@google.com","avatar":"https://avatars.githubusercontent.com/u/7320636?v=4"},"body":"This patch series is based on next and expands on the transaction API. It\nconverts all ref updates, inside refs.c as well as external, to use the\ntransaction API for updates. This makes most of the ref updates to become\natomic when there are failures locking or writing to a ref.\n\nThis version completes the work to convert all ref updates to use transactions.\nNow that all updates are through transactions I will start working on\ncleaning up the reading of refs and to create an api for managing reflogs but\nall that will go in a different patch series.\n\n\nVersion 6:\n - Convert all updates in refs.c to use transactions too.\n\nVersion 5:\n - Reword commit messages for having _create/_delete/_update returning\n   success/failure. There are no conditions yet that return an error from\n   these failures but there will be in the future. So we still check the\n   return from these functions in the callers in preparation for this.\n - Don't leak memory by just passing a strbuf_detach() pointer to functions.\n   Use <obj>.buf and explicitely strbuf_release the data afterwards.\n - Remove the function update_ref_lock.\n - Remove the function update_ref_write.\n - Track transaction status and die(BUG:) if we call _create/_delete/_update/\n   _commit for a transaction that is not OPEN.\n\nVersion 4:\n - Rename patch series from \"Use ref transactions from most callers\" to\n   \"Use ref transactions for all ref updates\".\n - Convert all external ref writes to use transactions and make write_ref_sha1\n   and lock_ref_sha1 static functions.\n - Change the ref commit and free handling so we no longer pass pointer to\n   pointer to _commit. _commit no longer frees the transaction. The caller\n   MUST call _free itself.\n - Change _commit to take a strbuf pointer instead of a char* for error\n   reporting back to the caller.\n - Re-add the walker patch after fixing it.\n\nVersion 3:\n - Remove the walker patch for now. Walker needs more complex solution\n   so defer it until the basics are done.\n - Remove the onerr argument to ref_transaction_commit(). All callers\n   that need to die() on error now have to do this explicitely.\n - Pass an error string from ref_transaction_commit() back to the callers\n   so that they can craft a nice error message upon failures.\n - Make ref_transaction_rollback() accept NULL as argument.\n - Change ref_transaction_commit() to take a pointer to pointer argument for\n   the transaction and have it clear the callers pointer to NULL when\n   invoked. This allows for much nicer handling of transaction rollback on\n   failure.\n\nVersion 2:\n - Add a patch to ref_transaction_commit to make it honor onerr even if the\n   error triggered in ref_Transaction_commit itself rather than in a call\n   to other functions (that already honor onerr).\n - Add a patch to make the update_ref() helper function use transactions\n   internally.\n - Change ref_transaction_update to die() instead of error() if we pass\n   if a NULL old_sha1 but have have_old == true.\n - Change ref_transaction_create to die() instead of error() if new_sha1\n   is false but we pass it a null_sha1.\n - Change ref_transaction_delete die() instead of error() if we pass\n   if a NULL old_sha1 but have have_old == true.\n - Change several places to do  if(!transaction || ref_transaction_update()\n   || ref_Transaction_commit()) die(generic-message) instead of checking each\n   step separately and having a different message for each failure.\n   Most users are likely not interested in what step of the transaction\n   failed and only whether it failed or not.\n - Change commit.c to only pass a pointer to ref_transaction_update\n   iff current_head is non-NULL.\n   The previous patch used to compute a garbage pointer for\n   current_head->object.sha1 and relied on the fact that ref_transaction_update\n   would not try to dereference this pointer if !!current_head was 0.\n - Updated commit message for the walker_fetch change to try to justify why\n   the change in locking semantics should not be harmful.\n\nRonnie Sahlberg (42):\n  refs.c: constify the sha arguments for\n    ref_transaction_create|delete|update\n  refs.c: allow passing NULL to ref_transaction_free\n  refs.c: add a strbuf argument to ref_transaction_commit for error\n    logging\n  refs.c: make ref_update_reject_duplicates take a strbuf argument for\n    errors\n  update-ref.c: log transaction error from the update_ref\n  refs.c: make update_ref_write update a strbuf on failure\n  refs.c: remove the onerr argument to ref_transaction_commit\n  refs.c: change ref_transaction_update() to do error checking and\n    return status\n  refs.c: change ref_transaction_create to do error checking and return\n    status\n  refs.c: ref_transaction_delete to check for error and return status\n  tag.c: use ref transactions when doing updates\n  replace.c: use the ref transaction functions for updates\n  commit.c: use ref transactions for updates\n  sequencer.c: use ref transactions for all ref updates\n  fast-import.c: change update_branch to use ref transactions\n  branch.c: use ref transaction for all ref updates\n  refs.c: change update_ref to use a transaction\n  refs.c: free the transaction before returning when number of updates\n    is 0\n  refs.c: ref_transaction_commit should not free the transaction\n  fetch.c: clear errno before calling functions that might set it\n  fetch.c: change s_update_ref to use a ref transaction\n  fetch.c: use a single ref transaction for all ref updates\n  receive-pack.c: use a reference transaction for updating the refs\n  fast-import.c: use a ref transaction when dumping tags\n  walker.c: use ref transaction for ref updates\n  refs.c: make write_ref_sha1 static\n  refs.c: make lock_ref_sha1 static\n  refs.c: add transaction.status and track OPEN/CLOSED/ERROR\n  refs.c: remove the update_ref_lock function\n  refs.c: remove the update_ref_write function\n  refs.c: remove lock_ref_sha1\n  refs.c: make prune_ref use a transaction to delete the ref\n  refs.c: make delete_ref use a transaction\n  refs.c: pass the ref log message to _create/delete/update instead of\n    _commit\n  refs.c: pass NULL as *flags to read_ref_full\n  refs.c: pack all refs before we start to rename a ref\n  refs.c: move the check for valid refname to lock_ref_sha1_basic\n  refs.c: call lock_ref_sha1_basic directly from commit\n  refs.c: add a new flag for transaction delete for refs we know are\n    packed only\n  refs.c: pass a skip list to name_conflict_fn\n  refs.c: make rename_ref use a transaction\n  refs.c: remove forward declaraion of write_ref_sha1\n\n branch.c               |  31 ++--\n builtin/commit.c       |  24 ++-\n builtin/fetch.c        |  29 ++--\n builtin/receive-pack.c |  20 +--\n builtin/replace.c      |  15 +-\n builtin/tag.c          |  15 +-\n builtin/update-ref.c   |  32 ++--\n fast-import.c          |  39 +++--\n refs.c                 | 404 +++++++++++++++++++++++++++----------------------\n refs.h                 |  49 +++---\n sequencer.c            |  24 ++-\n t/t3200-branch.sh      |   2 +-\n walker.c               |  51 ++++---\n 13 files changed, 414 insertions(+), 321 deletions(-)\n\n-- \n2.0.0.rc1.351.g4d2c8e4\n"},{"id":"240428","messageId":"1398976662-6962-2-git-send-email-sahlberg@google.com","threadId":"36563","inReplyTo":"1398976662-6962-1-git-send-email-sahlberg@google.com","subject":"[PATCH v6 01/42] refs.c: constify the sha arguments for ref_transaction_create|delete|update","fromName":"Ronnie Sahlberg","fromEmail":"sahlberg@google.com","sentAt":"2014-05-01T20:37:01Z","receivedAt":"2014-05-01T20:37:01Z","isPatch":true,"sender":{"key":"sahlberg@google.com","avatar":"https://avatars.githubusercontent.com/u/7320636?v=4"},"body":"ref_transaction_create|delete|update has no need to modify the sha1\narguments passed to it so it should use const unsigned char* instead\nof unsigned char*.\n\nSome functions, such as fast_forward_to(), already have its old/new\nsha1 arguments as consts. This function will at some point need to\nuse ref_transaction_update() in which case this change is required.\n\nSigned-off-by: Ronnie Sahlberg <sahlberg@google.com>\n---\n refs.c | 7 ++++---\n refs.h | 7 ++++---\n 2 files changed, 8 insertions(+), 6 deletions(-)\n\ndiff --git a/refs.c b/refs.c\nindex 728a761..138ab70 100644\n--- a/refs.c\n+++ b/refs.c\n@@ -3329,7 +3329,8 @@ static struct ref_update *add_update(struct ref_transaction *transaction,\n \n void ref_transaction_update(struct ref_transaction *transaction,\n \t\t\t    const char *refname,\n-\t\t\t    unsigned char *new_sha1, unsigned char *old_sha1,\n+\t\t\t    const unsigned char *new_sha1,\n+\t\t\t    const unsigned char *old_sha1,\n \t\t\t    int flags, int have_old)\n {\n \tstruct ref_update *update = add_update(transaction, refname);\n@@ -3343,7 +3344,7 @@ void ref_transaction_update(struct ref_transaction *transaction,\n \n void ref_transaction_create(struct ref_transaction *transaction,\n \t\t\t    const char *refname,\n-\t\t\t    unsigned char *new_sha1,\n+\t\t\t    const unsigned char *new_sha1,\n \t\t\t    int flags)\n {\n \tstruct ref_update *update = add_update(transaction, refname);\n@@ -3357,7 +3358,7 @@ void ref_transaction_create(struct ref_transaction *transaction,\n \n void ref_transaction_delete(struct ref_transaction *transaction,\n \t\t\t    const char *refname,\n-\t\t\t    unsigned char *old_sha1,\n+\t\t\t    const unsigned char *old_sha1,\n \t\t\t    int flags, int have_old)\n {\n \tstruct ref_update *update = add_update(transaction, refname);\ndiff --git a/refs.h b/refs.h\nindex 0f08def..892c5b6 100644\n--- a/refs.h\n+++ b/refs.h\n@@ -239,7 +239,8 @@ void ref_transaction_rollback(struct ref_transaction *transaction);\n  */\n void ref_transaction_update(struct ref_transaction *transaction,\n \t\t\t    const char *refname,\n-\t\t\t    unsigned char *new_sha1, unsigned char *old_sha1,\n+\t\t\t    const unsigned char *new_sha1,\n+\t\t\t    const unsigned char *old_sha1,\n \t\t\t    int flags, int have_old);\n \n /*\n@@ -250,7 +251,7 @@ void ref_transaction_update(struct ref_transaction *transaction,\n  */\n void ref_transaction_create(struct ref_transaction *transaction,\n \t\t\t    const char *refname,\n-\t\t\t    unsigned char *new_sha1,\n+\t\t\t    const unsigned char *new_sha1,\n \t\t\t    int flags);\n \n /*\n@@ -260,7 +261,7 @@ void ref_transaction_create(struct ref_transaction *transaction,\n  */\n void ref_transaction_delete(struct ref_transaction *transaction,\n \t\t\t    const char *refname,\n-\t\t\t    unsigned char *old_sha1,\n+\t\t\t    const unsigned char *old_sha1,\n \t\t\t    int flags, int have_old);\n \n /*\n-- \n2.0.0.rc1.351.g4d2c8e4\n"},{"id":"240429","messageId":"1398976662-6962-3-git-send-email-sahlberg@google.com","threadId":"36563","inReplyTo":"1398976662-6962-1-git-send-email-sahlberg@google.com","subject":"[PATCH v6 02/42] refs.c: allow passing NULL to ref_transaction_free","fromName":"Ronnie Sahlberg","fromEmail":"sahlberg@google.com","sentAt":"2014-05-01T20:37:02Z","receivedAt":"2014-05-01T20:37:02Z","isPatch":true,"sender":{"key":"sahlberg@google.com","avatar":"https://avatars.githubusercontent.com/u/7320636?v=4"},"body":"Allow ref_transaction_free to be called with NULL and in extension allow\nref_transaction_rollback to be called for a NULL transaction.\n\nThis allows us to write code that will\n  if ( (!transaction ||\n        ref_transaction_update(...))  ||\n      (ref_transaction_commit(...) && !(transaction = NULL)) {\n          ref_transaction_rollback(transaction);\n          ...\n  }\n\nIn this case transaction is reset to NULL IFF ref_transaction_commit() was\ninvoked and thus the rollback becomes ref_transaction_rollback(NULL) which\nis safe. IF the conditional triggered prior to ref_transaction_commit()\nthen transaction is untouched and then ref_transaction_rollback(transaction)\nwill rollback the failed transaction.\n\nSigned-off-by: Ronnie Sahlberg <sahlberg@google.com>\n---\n refs.c | 3 +++\n 1 file changed, 3 insertions(+)\n\ndiff --git a/refs.c b/refs.c\nindex 138ab70..2d83ef6 100644\n--- a/refs.c\n+++ b/refs.c\n@@ -3303,6 +3303,9 @@ static void ref_transaction_free(struct ref_transaction *transaction)\n {\n \tint i;\n \n+\tif (!transaction)\n+\t\treturn;\n+\n \tfor (i = 0; i < transaction->nr; i++)\n \t\tfree(transaction->updates[i]);\n \n-- \n2.0.0.rc1.351.g4d2c8e4\n"},{"id":"240470","messageId":"1398976662-6962-4-git-send-email-sahlberg@google.com","threadId":"36563","inReplyTo":"1398976662-6962-1-git-send-email-sahlberg@google.com","subject":"[PATCH v6 03/42] refs.c: add a strbuf argument to ref_transaction_commit for error logging","fromName":"Ronnie Sahlberg","fromEmail":"sahlberg@google.com","sentAt":"2014-05-01T20:37:03Z","receivedAt":"2014-05-01T20:37:03Z","isPatch":true,"sender":{"key":"sahlberg@google.com","avatar":"https://avatars.githubusercontent.com/u/7320636?v=4"},"body":"Add a strbuf argument to _commit so that we can pass an error string back to\nthe caller. So that we can do error logging from the caller instead of from\n_commit.\n\nLonger term plan is to first convert all callers to use onerr==QUIET_ON_ERR\nand craft any log messages from the callers themselves and finally remove the\nonerr argument completely.\n\nSigned-off-by: Ronnie Sahlberg <sahlberg@google.com>\n---\n builtin/update-ref.c | 2 +-\n refs.c               | 6 +++++-\n refs.h               | 5 ++++-\n 3 files changed, 10 insertions(+), 3 deletions(-)\n\ndiff --git a/builtin/update-ref.c b/builtin/update-ref.c\nindex 405267f..aaa06aa 100644\n--- a/builtin/update-ref.c\n+++ b/builtin/update-ref.c\n@@ -367,7 +367,7 @@ int cmd_update_ref(int argc, const char **argv, const char *prefix)\n \t\tif (end_null)\n \t\t\tline_termination = '\\0';\n \t\tupdate_refs_stdin();\n-\t\tret = ref_transaction_commit(transaction, msg,\n+\t\tret = ref_transaction_commit(transaction, msg, NULL,\n \t\t\t\t\t     UPDATE_REFS_DIE_ON_ERR);\n \t\treturn ret;\n \t}\ndiff --git a/refs.c b/refs.c\nindex 2d83ef6..64e8feb 100644\n--- a/refs.c\n+++ b/refs.c\n@@ -3414,7 +3414,8 @@ static int ref_update_reject_duplicates(struct ref_update **updates, int n,\n }\n \n int ref_transaction_commit(struct ref_transaction *transaction,\n-\t\t\t   const char *msg, enum action_on_err onerr)\n+\t\t\t   const char *msg, struct strbuf *err,\n+\t\t\t   enum action_on_err onerr)\n {\n \tint ret = 0, delnum = 0, i;\n \tconst char **delnames;\n@@ -3443,6 +3444,9 @@ int ref_transaction_commit(struct ref_transaction *transaction,\n \t\t\t\t\t       update->flags,\n \t\t\t\t\t       &update->type, onerr);\n \t\tif (!update->lock) {\n+\t\t\tif (err)\n+\t\t\t\tstrbuf_addf(err ,\"Cannot lock the ref '%s'.\",\n+\t\t\t\t\t    update->refname);\n \t\t\tret = 1;\n \t\t\tgoto cleanup;\n \t\t}\ndiff --git a/refs.h b/refs.h\nindex 892c5b6..ff87e14 100644\n--- a/refs.h\n+++ b/refs.h\n@@ -268,9 +268,12 @@ void ref_transaction_delete(struct ref_transaction *transaction,\n  * Commit all of the changes that have been queued in transaction, as\n  * atomically as possible.  Return a nonzero value if there is a\n  * problem.  The ref_transaction is freed by this function.\n+ * If err is non-NULL we will add an error string to it to explain why\n+ * the transaction failed.\n  */\n int ref_transaction_commit(struct ref_transaction *transaction,\n-\t\t\t   const char *msg, enum action_on_err onerr);\n+\t\t\t   const char *msg, struct strbuf *err,\n+\t\t\t   enum action_on_err onerr);\n \n /** Lock a ref and then write its file */\n int update_ref(const char *action, const char *refname,\n-- \n2.0.0.rc1.351.g4d2c8e4\n"},{"id":"240468","messageId":"1398976662-6962-5-git-send-email-sahlberg@google.com","threadId":"36563","inReplyTo":"1398976662-6962-1-git-send-email-sahlberg@google.com","subject":"[PATCH v6 04/42] refs.c: make ref_update_reject_duplicates take a strbuf argument for errors","fromName":"Ronnie Sahlberg","fromEmail":"sahlberg@google.com","sentAt":"2014-05-01T20:37:04Z","receivedAt":"2014-05-01T20:37:04Z","isPatch":true,"sender":{"key":"sahlberg@google.com","avatar":"https://avatars.githubusercontent.com/u/7320636?v=4"},"body":"Make ref_update_reject_duplicates return any error that occurs through a\nnew strbuf argument.\n\nSigned-off-by: Ronnie Sahlberg <sahlberg@google.com>\n---\n refs.c | 6 +++++-\n 1 file changed, 5 insertions(+), 1 deletion(-)\n\ndiff --git a/refs.c b/refs.c\nindex 64e8feb..21a147b 100644\n--- a/refs.c\n+++ b/refs.c\n@@ -3393,6 +3393,7 @@ static int ref_update_compare(const void *r1, const void *r2)\n }\n \n static int ref_update_reject_duplicates(struct ref_update **updates, int n,\n+\t\t\t\t\tstruct strbuf *err,\n \t\t\t\t\tenum action_on_err onerr)\n {\n \tint i;\n@@ -3400,6 +3401,9 @@ static int ref_update_reject_duplicates(struct ref_update **updates, int n,\n \t\tif (!strcmp(updates[i - 1]->refname, updates[i]->refname)) {\n \t\t\tconst char *str =\n \t\t\t\t\"Multiple updates for ref '%s' not allowed.\";\n+\t\t\tif (err)\n+\t\t\t\tstrbuf_addf(err, str, updates[i]->refname);\n+\n \t\t\tswitch (onerr) {\n \t\t\tcase UPDATE_REFS_MSG_ON_ERR:\n \t\t\t\terror(str, updates[i]->refname); break;\n@@ -3430,7 +3434,7 @@ int ref_transaction_commit(struct ref_transaction *transaction,\n \n \t/* Copy, sort, and reject duplicate refs */\n \tqsort(updates, n, sizeof(*updates), ref_update_compare);\n-\tret = ref_update_reject_duplicates(updates, n, onerr);\n+\tret = ref_update_reject_duplicates(updates, n, err, onerr);\n \tif (ret)\n \t\tgoto cleanup;\n \n-- \n2.0.0.rc1.351.g4d2c8e4\n"},{"id":"240466","messageId":"1398976662-6962-6-git-send-email-sahlberg@google.com","threadId":"36563","inReplyTo":"1398976662-6962-1-git-send-email-sahlberg@google.com","subject":"[PATCH v6 05/42] update-ref.c: log transaction error from the update_ref","fromName":"Ronnie Sahlberg","fromEmail":"sahlberg@google.com","sentAt":"2014-05-01T20:37:05Z","receivedAt":"2014-05-01T20:37:05Z","isPatch":true,"sender":{"key":"sahlberg@google.com","avatar":"https://avatars.githubusercontent.com/u/7320636?v=4"},"body":"Call ref_transaction_commit with QUIET_ON_ERR and use the strbuf that is\nreturned to print a log message if/after the transaction fails.\n\nSigned-off-by: Ronnie Sahlberg <sahlberg@google.com>\n---\n builtin/update-ref.c | 10 +++++-----\n 1 file changed, 5 insertions(+), 5 deletions(-)\n\ndiff --git a/builtin/update-ref.c b/builtin/update-ref.c\nindex aaa06aa..207e24d 100644\n--- a/builtin/update-ref.c\n+++ b/builtin/update-ref.c\n@@ -342,6 +342,7 @@ int cmd_update_ref(int argc, const char **argv, const char *prefix)\n \tconst char *refname, *oldval, *msg = NULL;\n \tunsigned char sha1[20], oldsha1[20];\n \tint delete = 0, no_deref = 0, read_stdin = 0, end_null = 0, flags = 0;\n+\tstruct strbuf err = STRBUF_INIT;\n \tstruct option options[] = {\n \t\tOPT_STRING( 'm', NULL, &msg, N_(\"reason\"), N_(\"reason of the update\")),\n \t\tOPT_BOOL('d', NULL, &delete, N_(\"delete the reference\")),\n@@ -359,17 +360,16 @@ int cmd_update_ref(int argc, const char **argv, const char *prefix)\n \t\tdie(\"Refusing to perform update with empty message.\");\n \n \tif (read_stdin) {\n-\t\tint ret;\n \t\ttransaction = ref_transaction_begin();\n-\n \t\tif (delete || no_deref || argc > 0)\n \t\t\tusage_with_options(git_update_ref_usage, options);\n \t\tif (end_null)\n \t\t\tline_termination = '\\0';\n \t\tupdate_refs_stdin();\n-\t\tret = ref_transaction_commit(transaction, msg, NULL,\n-\t\t\t\t\t     UPDATE_REFS_DIE_ON_ERR);\n-\t\treturn ret;\n+\t\tif (ref_transaction_commit(transaction, msg, &err,\n+\t\t\t\t\t   UPDATE_REFS_QUIET_ON_ERR))\n+\t\t\tdie(\"%s\", err.buf);\n+\t\treturn 0;\n \t}\n \n \tif (end_null)\n-- \n2.0.0.rc1.351.g4d2c8e4\n"},{"id":"240469","messageId":"1398976662-6962-7-git-send-email-sahlberg@google.com","threadId":"36563","inReplyTo":"1398976662-6962-1-git-send-email-sahlberg@google.com","subject":"[PATCH v6 06/42] refs.c: make update_ref_write update a strbuf on failure","fromName":"Ronnie Sahlberg","fromEmail":"sahlberg@google.com","sentAt":"2014-05-01T20:37:06Z","receivedAt":"2014-05-01T20:37:06Z","isPatch":true,"sender":{"key":"sahlberg@google.com","avatar":"https://avatars.githubusercontent.com/u/7320636?v=4"},"body":"Change update_ref_write to also update an error strbuf on failure.\nThis makes the error available to ref_transaction_commit callers if the\ntransaction failed due to update_ref_sha1/write_ref_sha1 failures.\n\nSigned-off-by: Ronnie Sahlberg <sahlberg@google.com>\n---\n refs.c | 9 ++++++---\n 1 file changed, 6 insertions(+), 3 deletions(-)\n\ndiff --git a/refs.c b/refs.c\nindex 21a147b..5e06fd9 100644\n--- a/refs.c\n+++ b/refs.c\n@@ -3253,10 +3253,13 @@ static struct ref_lock *update_ref_lock(const char *refname,\n \n static int update_ref_write(const char *action, const char *refname,\n \t\t\t    const unsigned char *sha1, struct ref_lock *lock,\n-\t\t\t    enum action_on_err onerr)\n+\t\t\t    struct strbuf *err, enum action_on_err onerr)\n {\n \tif (write_ref_sha1(lock, sha1, action) < 0) {\n \t\tconst char *str = \"Cannot update the ref '%s'.\";\n+\t\tif (err)\n+\t\t\tstrbuf_addf(err, str, refname);\n+\n \t\tswitch (onerr) {\n \t\tcase UPDATE_REFS_MSG_ON_ERR: error(str, refname); break;\n \t\tcase UPDATE_REFS_DIE_ON_ERR: die(str, refname); break;\n@@ -3382,7 +3385,7 @@ int update_ref(const char *action, const char *refname,\n \tlock = update_ref_lock(refname, oldval, flags, NULL, onerr);\n \tif (!lock)\n \t\treturn 1;\n-\treturn update_ref_write(action, refname, sha1, lock, onerr);\n+\treturn update_ref_write(action, refname, sha1, lock, NULL, onerr);\n }\n \n static int ref_update_compare(const void *r1, const void *r2)\n@@ -3464,7 +3467,7 @@ int ref_transaction_commit(struct ref_transaction *transaction,\n \t\t\tret = update_ref_write(msg,\n \t\t\t\t\t       update->refname,\n \t\t\t\t\t       update->new_sha1,\n-\t\t\t\t\t       update->lock, onerr);\n+\t\t\t\t\t       update->lock, err, onerr);\n \t\t\tupdate->lock = NULL; /* freed by update_ref_write */\n \t\t\tif (ret)\n \t\t\t\tgoto cleanup;\n-- \n2.0.0.rc1.351.g4d2c8e4\n"},{"id":"240464","messageId":"1398976662-6962-8-git-send-email-sahlberg@google.com","threadId":"36563","inReplyTo":"1398976662-6962-1-git-send-email-sahlberg@google.com","subject":"[PATCH v6 07/42] refs.c: remove the onerr argument to ref_transaction_commit","fromName":"Ronnie Sahlberg","fromEmail":"sahlberg@google.com","sentAt":"2014-05-01T20:37:07Z","receivedAt":"2014-05-01T20:37:07Z","isPatch":true,"sender":{"key":"sahlberg@google.com","avatar":"https://avatars.githubusercontent.com/u/7320636?v=4"},"body":"Since all callers now use QUIET_ON_ERR we no longer need to provide an onerr\nargument any more. Remove the onerr argument from the ref_transaction_commit\nsignature.\n\nSigned-off-by: Ronnie Sahlberg <sahlberg@google.com>\n---\n builtin/update-ref.c |  3 +--\n refs.c               | 22 +++++++---------------\n refs.h               |  3 +--\n 3 files changed, 9 insertions(+), 19 deletions(-)\n\ndiff --git a/builtin/update-ref.c b/builtin/update-ref.c\nindex 207e24d..2bef2a0 100644\n--- a/builtin/update-ref.c\n+++ b/builtin/update-ref.c\n@@ -366,8 +366,7 @@ int cmd_update_ref(int argc, const char **argv, const char *prefix)\n \t\tif (end_null)\n \t\t\tline_termination = '\\0';\n \t\tupdate_refs_stdin();\n-\t\tif (ref_transaction_commit(transaction, msg, &err,\n-\t\t\t\t\t   UPDATE_REFS_QUIET_ON_ERR))\n+\t\tif (ref_transaction_commit(transaction, msg, &err))\n \t\t\tdie(\"%s\", err.buf);\n \t\treturn 0;\n \t}\ndiff --git a/refs.c b/refs.c\nindex 5e06fd9..308e13e 100644\n--- a/refs.c\n+++ b/refs.c\n@@ -3396,8 +3396,7 @@ static int ref_update_compare(const void *r1, const void *r2)\n }\n \n static int ref_update_reject_duplicates(struct ref_update **updates, int n,\n-\t\t\t\t\tstruct strbuf *err,\n-\t\t\t\t\tenum action_on_err onerr)\n+\t\t\t\t\tstruct strbuf *err)\n {\n \tint i;\n \tfor (i = 1; i < n; i++)\n@@ -3407,22 +3406,13 @@ static int ref_update_reject_duplicates(struct ref_update **updates, int n,\n \t\t\tif (err)\n \t\t\t\tstrbuf_addf(err, str, updates[i]->refname);\n \n-\t\t\tswitch (onerr) {\n-\t\t\tcase UPDATE_REFS_MSG_ON_ERR:\n-\t\t\t\terror(str, updates[i]->refname); break;\n-\t\t\tcase UPDATE_REFS_DIE_ON_ERR:\n-\t\t\t\tdie(str, updates[i]->refname); break;\n-\t\t\tcase UPDATE_REFS_QUIET_ON_ERR:\n-\t\t\t\tbreak;\n-\t\t\t}\n \t\t\treturn 1;\n \t\t}\n \treturn 0;\n }\n \n int ref_transaction_commit(struct ref_transaction *transaction,\n-\t\t\t   const char *msg, struct strbuf *err,\n-\t\t\t   enum action_on_err onerr)\n+\t\t\t   const char *msg, struct strbuf *err)\n {\n \tint ret = 0, delnum = 0, i;\n \tconst char **delnames;\n@@ -3437,7 +3427,7 @@ int ref_transaction_commit(struct ref_transaction *transaction,\n \n \t/* Copy, sort, and reject duplicate refs */\n \tqsort(updates, n, sizeof(*updates), ref_update_compare);\n-\tret = ref_update_reject_duplicates(updates, n, err, onerr);\n+\tret = ref_update_reject_duplicates(updates, n, err);\n \tif (ret)\n \t\tgoto cleanup;\n \n@@ -3449,7 +3439,8 @@ int ref_transaction_commit(struct ref_transaction *transaction,\n \t\t\t\t\t       (update->have_old ?\n \t\t\t\t\t\tupdate->old_sha1 : NULL),\n \t\t\t\t\t       update->flags,\n-\t\t\t\t\t       &update->type, onerr);\n+\t\t\t\t\t       &update->type,\n+\t\t\t\t\t       UPDATE_REFS_QUIET_ON_ERR);\n \t\tif (!update->lock) {\n \t\t\tif (err)\n \t\t\t\tstrbuf_addf(err ,\"Cannot lock the ref '%s'.\",\n@@ -3467,7 +3458,8 @@ int ref_transaction_commit(struct ref_transaction *transaction,\n \t\t\tret = update_ref_write(msg,\n \t\t\t\t\t       update->refname,\n \t\t\t\t\t       update->new_sha1,\n-\t\t\t\t\t       update->lock, err, onerr);\n+\t\t\t\t\t       update->lock, err,\n+\t\t\t\t\t       UPDATE_REFS_QUIET_ON_ERR);\n \t\t\tupdate->lock = NULL; /* freed by update_ref_write */\n \t\t\tif (ret)\n \t\t\t\tgoto cleanup;\ndiff --git a/refs.h b/refs.h\nindex ff87e14..bc7715e 100644\n--- a/refs.h\n+++ b/refs.h\n@@ -272,8 +272,7 @@ void ref_transaction_delete(struct ref_transaction *transaction,\n  * the transaction failed.\n  */\n int ref_transaction_commit(struct ref_transaction *transaction,\n-\t\t\t   const char *msg, struct strbuf *err,\n-\t\t\t   enum action_on_err onerr);\n+\t\t\t   const char *msg, struct strbuf *err);\n \n /** Lock a ref and then write its file */\n int update_ref(const char *action, const char *refname,\n-- \n2.0.0.rc1.351.g4d2c8e4\n"},{"id":"240463","messageId":"1398976662-6962-9-git-send-email-sahlberg@google.com","threadId":"36563","inReplyTo":"1398976662-6962-1-git-send-email-sahlberg@google.com","subject":"[PATCH v6 08/42] refs.c: change ref_transaction_update() to do error checking and return status","fromName":"Ronnie Sahlberg","fromEmail":"sahlberg@google.com","sentAt":"2014-05-01T20:37:08Z","receivedAt":"2014-05-01T20:37:08Z","isPatch":true,"sender":{"key":"sahlberg@google.com","avatar":"https://avatars.githubusercontent.com/u/7320636?v=4"},"body":"Update ref_transaction_update() do some basic error checking and return\ntrue on error. Update all callers to check ref_transaction_update() for error.\nThere are currently no conditions in _update that will return error but there\nwill be in the future.\n\nSigned-off-by: Ronnie Sahlberg <sahlberg@google.com>\n---\n builtin/update-ref.c | 10 ++++++----\n refs.c               |  9 +++++++--\n refs.h               | 10 +++++-----\n 3 files changed, 18 insertions(+), 11 deletions(-)\n\ndiff --git a/builtin/update-ref.c b/builtin/update-ref.c\nindex 2bef2a0..59c4d6b 100644\n--- a/builtin/update-ref.c\n+++ b/builtin/update-ref.c\n@@ -197,8 +197,9 @@ static const char *parse_cmd_update(struct strbuf *input, const char *next)\n \tif (*next != line_termination)\n \t\tdie(\"update %s: extra input: %s\", refname, next);\n \n-\tref_transaction_update(transaction, refname, new_sha1, old_sha1,\n-\t\t\t       update_flags, have_old);\n+\tif (ref_transaction_update(transaction, refname, new_sha1, old_sha1,\n+\t\t\t\t   update_flags, have_old))\n+\t\tdie(\"update %s: failed\", refname);\n \n \tupdate_flags = 0;\n \tfree(refname);\n@@ -286,8 +287,9 @@ static const char *parse_cmd_verify(struct strbuf *input, const char *next)\n \tif (*next != line_termination)\n \t\tdie(\"verify %s: extra input: %s\", refname, next);\n \n-\tref_transaction_update(transaction, refname, new_sha1, old_sha1,\n-\t\t\t       update_flags, have_old);\n+\tif (ref_transaction_update(transaction, refname, new_sha1, old_sha1,\n+\t\t\t\t   update_flags, have_old))\n+\t\tdie(\"failed transaction update for %s\", refname);\n \n \tupdate_flags = 0;\n \tfree(refname);\ndiff --git a/refs.c b/refs.c\nindex 308e13e..1a903fb 100644\n--- a/refs.c\n+++ b/refs.c\n@@ -3333,19 +3333,24 @@ static struct ref_update *add_update(struct ref_transaction *transaction,\n \treturn update;\n }\n \n-void ref_transaction_update(struct ref_transaction *transaction,\n+int ref_transaction_update(struct ref_transaction *transaction,\n \t\t\t    const char *refname,\n \t\t\t    const unsigned char *new_sha1,\n \t\t\t    const unsigned char *old_sha1,\n \t\t\t    int flags, int have_old)\n {\n-\tstruct ref_update *update = add_update(transaction, refname);\n+\tstruct ref_update *update;\n+\n+\tif (have_old && !old_sha1)\n+\t\tdie(\"have_old is true but old_sha1 is NULL\");\n \n+\tupdate = add_update(transaction, refname);\n \thashcpy(update->new_sha1, new_sha1);\n \tupdate->flags = flags;\n \tupdate->have_old = have_old;\n \tif (have_old)\n \t\thashcpy(update->old_sha1, old_sha1);\n+\treturn 0;\n }\n \n void ref_transaction_create(struct ref_transaction *transaction,\ndiff --git a/refs.h b/refs.h\nindex bc7715e..0364a3e 100644\n--- a/refs.h\n+++ b/refs.h\n@@ -237,11 +237,11 @@ void ref_transaction_rollback(struct ref_transaction *transaction);\n  * that the reference should have had before the update, or zeros if\n  * it must not have existed beforehand.\n  */\n-void ref_transaction_update(struct ref_transaction *transaction,\n-\t\t\t    const char *refname,\n-\t\t\t    const unsigned char *new_sha1,\n-\t\t\t    const unsigned char *old_sha1,\n-\t\t\t    int flags, int have_old);\n+int ref_transaction_update(struct ref_transaction *transaction,\n+\t\t\t   const char *refname,\n+\t\t\t   const unsigned char *new_sha1,\n+\t\t\t   const unsigned char *old_sha1,\n+\t\t\t   int flags, int have_old);\n \n /*\n  * Add a reference creation to transaction.  new_sha1 is the value\n-- \n2.0.0.rc1.351.g4d2c8e4\n"},{"id":"240465","messageId":"1398976662-6962-10-git-send-email-sahlberg@google.com","threadId":"36563","inReplyTo":"1398976662-6962-1-git-send-email-sahlberg@google.com","subject":"[PATCH v6 09/42] refs.c: change ref_transaction_create to do error checking and return status","fromName":"Ronnie Sahlberg","fromEmail":"sahlberg@google.com","sentAt":"2014-05-01T20:37:09Z","receivedAt":"2014-05-01T20:37:09Z","isPatch":true,"sender":{"key":"sahlberg@google.com","avatar":"https://avatars.githubusercontent.com/u/7320636?v=4"},"body":"Do basic error checking in ref_transaction_create() and make it return\nstatus. Update all callers to check the result of ref_transaction_create()\nThere are currently no conditions in _create that will return error but there\nwill be in the future.\n\nSigned-off-by: Ronnie Sahlberg <sahlberg@google.com>\n---\n builtin/update-ref.c |  4 +++-\n refs.c               | 17 +++++++++++------\n refs.h               |  8 ++++----\n 3 files changed, 18 insertions(+), 11 deletions(-)\n\ndiff --git a/builtin/update-ref.c b/builtin/update-ref.c\nindex 59c4d6b..3fab810 100644\n--- a/builtin/update-ref.c\n+++ b/builtin/update-ref.c\n@@ -225,7 +225,9 @@ static const char *parse_cmd_create(struct strbuf *input, const char *next)\n \tif (*next != line_termination)\n \t\tdie(\"create %s: extra input: %s\", refname, next);\n \n-\tref_transaction_create(transaction, refname, new_sha1, update_flags);\n+\tif (ref_transaction_create(transaction, refname, new_sha1,\n+\t\t\t\t   update_flags))\n+\t\tdie(\"failed transaction create for %s\", refname);\n \n \tupdate_flags = 0;\n \tfree(refname);\ndiff --git a/refs.c b/refs.c\nindex 1a903fb..27db737 100644\n--- a/refs.c\n+++ b/refs.c\n@@ -3353,18 +3353,23 @@ int ref_transaction_update(struct ref_transaction *transaction,\n \treturn 0;\n }\n \n-void ref_transaction_create(struct ref_transaction *transaction,\n-\t\t\t    const char *refname,\n-\t\t\t    const unsigned char *new_sha1,\n-\t\t\t    int flags)\n+int ref_transaction_create(struct ref_transaction *transaction,\n+\t\t\t   const char *refname,\n+\t\t\t   const unsigned char *new_sha1,\n+\t\t\t   int flags)\n {\n-\tstruct ref_update *update = add_update(transaction, refname);\n+\tstruct ref_update *update;\n+\n+\tif (!new_sha1 || is_null_sha1(new_sha1))\n+\t\tdie(\"create ref with null new_sha1\");\n+\n+\tupdate = add_update(transaction, refname);\n \n-\tassert(!is_null_sha1(new_sha1));\n \thashcpy(update->new_sha1, new_sha1);\n \thashclr(update->old_sha1);\n \tupdate->flags = flags;\n \tupdate->have_old = 1;\n+\treturn 0;\n }\n \n void ref_transaction_delete(struct ref_transaction *transaction,\ndiff --git a/refs.h b/refs.h\nindex 0364a3e..01d773c 100644\n--- a/refs.h\n+++ b/refs.h\n@@ -249,10 +249,10 @@ int ref_transaction_update(struct ref_transaction *transaction,\n  * null SHA-1.  It is verified that the reference does not exist\n  * already.\n  */\n-void ref_transaction_create(struct ref_transaction *transaction,\n-\t\t\t    const char *refname,\n-\t\t\t    const unsigned char *new_sha1,\n-\t\t\t    int flags);\n+int ref_transaction_create(struct ref_transaction *transaction,\n+\t\t\t   const char *refname,\n+\t\t\t   const unsigned char *new_sha1,\n+\t\t\t   int flags);\n \n /*\n  * Add a reference deletion to transaction.  If have_old is true, then\n-- \n2.0.0.rc1.351.g4d2c8e4\n"},{"id":"240454","messageId":"1398976662-6962-11-git-send-email-sahlberg@google.com","threadId":"36563","inReplyTo":"1398976662-6962-1-git-send-email-sahlberg@google.com","subject":"[PATCH v6 10/42] refs.c: ref_transaction_delete to check for error and return status","fromName":"Ronnie Sahlberg","fromEmail":"sahlberg@google.com","sentAt":"2014-05-01T20:37:10Z","receivedAt":"2014-05-01T20:37:10Z","isPatch":true,"sender":{"key":"sahlberg@google.com","avatar":"https://avatars.githubusercontent.com/u/7320636?v=4"},"body":"Change ref_transaction_delete() to do basic error checking and return\nstatus. Update all callers to check the return for ref_transaction_delete()\nThere are currently no conditions in _delete that will return error but there\nwill be in the future.\n\nSigned-off-by: Ronnie Sahlberg <sahlberg@google.com>\n---\n builtin/update-ref.c |  5 +++--\n refs.c               | 15 ++++++++++-----\n refs.h               |  8 ++++----\n 3 files changed, 17 insertions(+), 11 deletions(-)\n\ndiff --git a/builtin/update-ref.c b/builtin/update-ref.c\nindex 3fab810..fc3512f 100644\n--- a/builtin/update-ref.c\n+++ b/builtin/update-ref.c\n@@ -257,8 +257,9 @@ static const char *parse_cmd_delete(struct strbuf *input, const char *next)\n \tif (*next != line_termination)\n \t\tdie(\"delete %s: extra input: %s\", refname, next);\n \n-\tref_transaction_delete(transaction, refname, old_sha1,\n-\t\t\t       update_flags, have_old);\n+\tif (ref_transaction_delete(transaction, refname, old_sha1,\n+\t\t\t\t   update_flags, have_old))\n+\t\tdie(\"failed transaction delete for %s\", refname);\n \n \tupdate_flags = 0;\n \tfree(refname);\ndiff --git a/refs.c b/refs.c\nindex 27db737..0a4e28e 100644\n--- a/refs.c\n+++ b/refs.c\n@@ -3372,19 +3372,24 @@ int ref_transaction_create(struct ref_transaction *transaction,\n \treturn 0;\n }\n \n-void ref_transaction_delete(struct ref_transaction *transaction,\n-\t\t\t    const char *refname,\n-\t\t\t    const unsigned char *old_sha1,\n-\t\t\t    int flags, int have_old)\n+int ref_transaction_delete(struct ref_transaction *transaction,\n+\t\t\t   const char *refname,\n+\t\t\t   const unsigned char *old_sha1,\n+\t\t\t   int flags, int have_old)\n {\n-\tstruct ref_update *update = add_update(transaction, refname);\n+\tstruct ref_update *update;\n \n+\tif (have_old && !old_sha1)\n+\t\tdie(\"have_old is true but old_sha1 is NULL\");\n+\n+\tupdate = add_update(transaction, refname);\n \tupdate->flags = flags;\n \tupdate->have_old = have_old;\n \tif (have_old) {\n \t\tassert(!is_null_sha1(old_sha1));\n \t\thashcpy(update->old_sha1, old_sha1);\n \t}\n+\treturn 0;\n }\n \n int update_ref(const char *action, const char *refname,\ndiff --git a/refs.h b/refs.h\nindex 01d773c..5eb500e 100644\n--- a/refs.h\n+++ b/refs.h\n@@ -259,10 +259,10 @@ int ref_transaction_create(struct ref_transaction *transaction,\n  * old_sha1 holds the value that the reference should have had before\n  * the update (which must not be the null SHA-1).\n  */\n-void ref_transaction_delete(struct ref_transaction *transaction,\n-\t\t\t    const char *refname,\n-\t\t\t    const unsigned char *old_sha1,\n-\t\t\t    int flags, int have_old);\n+int ref_transaction_delete(struct ref_transaction *transaction,\n+\t\t\t   const char *refname,\n+\t\t\t   const unsigned char *old_sha1,\n+\t\t\t   int flags, int have_old);\n \n /*\n  * Commit all of the changes that have been queued in transaction, as\n-- \n2.0.0.rc1.351.g4d2c8e4\n"},{"id":"240459","messageId":"1398976662-6962-12-git-send-email-sahlberg@google.com","threadId":"36563","inReplyTo":"1398976662-6962-1-git-send-email-sahlberg@google.com","subject":"[PATCH v6 11/42] tag.c: use ref transactions when doing updates","fromName":"Ronnie Sahlberg","fromEmail":"sahlberg@google.com","sentAt":"2014-05-01T20:37:11Z","receivedAt":"2014-05-01T20:37:11Z","isPatch":true,"sender":{"key":"sahlberg@google.com","avatar":"https://avatars.githubusercontent.com/u/7320636?v=4"},"body":"Change tag.c to use ref transactions for all ref updates.\n\nSigned-off-by: Ronnie Sahlberg <sahlberg@google.com>\n---\n builtin/tag.c | 14 ++++++++------\n 1 file changed, 8 insertions(+), 6 deletions(-)\n\ndiff --git a/builtin/tag.c b/builtin/tag.c\nindex f344002..1cf5650 100644\n--- a/builtin/tag.c\n+++ b/builtin/tag.c\n@@ -548,7 +548,6 @@ int cmd_tag(int argc, const char **argv, const char *prefix)\n \tstruct strbuf ref = STRBUF_INIT;\n \tunsigned char object[20], prev[20];\n \tconst char *object_ref, *tag;\n-\tstruct ref_lock *lock;\n \tstruct create_tag_options opt;\n \tchar *cleanup_arg = NULL;\n \tint annotate = 0, force = 0, lines = -1;\n@@ -556,6 +555,8 @@ int cmd_tag(int argc, const char **argv, const char *prefix)\n \tconst char *msgfile = NULL, *keyid = NULL;\n \tstruct msg_arg msg = { 0, STRBUF_INIT };\n \tstruct commit_list *with_commit = NULL;\n+\tstruct ref_transaction *transaction;\n+\tstruct strbuf err = STRBUF_INIT;\n \tstruct option options[] = {\n \t\tOPT_CMDMODE('l', \"list\", &cmdmode, N_(\"list tag names\"), 'l'),\n \t\t{ OPTION_INTEGER, 'n', NULL, &lines, N_(\"n\"),\n@@ -701,11 +702,12 @@ int cmd_tag(int argc, const char **argv, const char *prefix)\n \tif (annotate)\n \t\tcreate_tag(object, tag, &buf, &opt, prev, object);\n \n-\tlock = lock_any_ref_for_update(ref.buf, prev, 0, NULL);\n-\tif (!lock)\n-\t\tdie(_(\"%s: cannot lock the ref\"), ref.buf);\n-\tif (write_ref_sha1(lock, object, NULL) < 0)\n-\t\tdie(_(\"%s: cannot update the ref\"), ref.buf);\n+\ttransaction = ref_transaction_begin();\n+\tif (!transaction ||\n+\t    ref_transaction_update(transaction, ref.buf, object, prev,\n+\t\t\t\t   0, !is_null_sha1(prev)) ||\n+\t    ref_transaction_commit(transaction, NULL, &err))\n+\t\tdie(_(\"%s: cannot update the ref: %s\"), ref.buf, err.buf);\n \tif (force && !is_null_sha1(prev) && hashcmp(prev, object))\n \t\tprintf(_(\"Updated tag '%s' (was %s)\\n\"), tag, find_unique_abbrev(prev, DEFAULT_ABBREV));\n \n-- \n2.0.0.rc1.351.g4d2c8e4\n"},{"id":"240460","messageId":"1398976662-6962-13-git-send-email-sahlberg@google.com","threadId":"36563","inReplyTo":"1398976662-6962-1-git-send-email-sahlberg@google.com","subject":"[PATCH v6 12/42] replace.c: use the ref transaction functions for updates","fromName":"Ronnie Sahlberg","fromEmail":"sahlberg@google.com","sentAt":"2014-05-01T20:37:12Z","receivedAt":"2014-05-01T20:37:12Z","isPatch":true,"sender":{"key":"sahlberg@google.com","avatar":"https://avatars.githubusercontent.com/u/7320636?v=4"},"body":"Update replace.c to use ref transactions for updates.\n\nSigned-off-by: Ronnie Sahlberg <sahlberg@google.com>\n---\n builtin/replace.c | 14 ++++++++------\n 1 file changed, 8 insertions(+), 6 deletions(-)\n\ndiff --git a/builtin/replace.c b/builtin/replace.c\nindex b62420a..b037b29 100644\n--- a/builtin/replace.c\n+++ b/builtin/replace.c\n@@ -129,7 +129,8 @@ static int replace_object(const char *object_ref, const char *replace_ref,\n \tunsigned char object[20], prev[20], repl[20];\n \tenum object_type obj_type, repl_type;\n \tchar ref[PATH_MAX];\n-\tstruct ref_lock *lock;\n+\tstruct ref_transaction *transaction;\n+\tstruct strbuf err = STRBUF_INIT;\n \n \tif (get_sha1(object_ref, object))\n \t\tdie(\"Failed to resolve '%s' as a valid ref.\", object_ref);\n@@ -157,11 +158,12 @@ static int replace_object(const char *object_ref, const char *replace_ref,\n \telse if (!force)\n \t\tdie(\"replace ref '%s' already exists\", ref);\n \n-\tlock = lock_any_ref_for_update(ref, prev, 0, NULL);\n-\tif (!lock)\n-\t\tdie(\"%s: cannot lock the ref\", ref);\n-\tif (write_ref_sha1(lock, repl, NULL) < 0)\n-\t\tdie(\"%s: cannot update the ref\", ref);\n+\ttransaction = ref_transaction_begin();\n+\tif (!transaction ||\n+\t    ref_transaction_update(transaction, ref, repl, prev,\n+\t\t\t\t   0, !is_null_sha1(prev)) ||\n+\t    ref_transaction_commit(transaction, NULL, &err))\n+\t\tdie(_(\"%s: failed to replace ref: %s\"), ref, err.buf);\n \n \treturn 0;\n }\n-- \n2.0.0.rc1.351.g4d2c8e4\n"},{"id":"240458","messageId":"1398976662-6962-14-git-send-email-sahlberg@google.com","threadId":"36563","inReplyTo":"1398976662-6962-1-git-send-email-sahlberg@google.com","subject":"[PATCH v6 13/42] commit.c: use ref transactions for updates","fromName":"Ronnie Sahlberg","fromEmail":"sahlberg@google.com","sentAt":"2014-05-01T20:37:13Z","receivedAt":"2014-05-01T20:37:13Z","isPatch":true,"sender":{"key":"sahlberg@google.com","avatar":"https://avatars.githubusercontent.com/u/7320636?v=4"},"body":"Change commit.c to use ref transactions for all ref updates.\nMake sure we pass a NULL pointer to ref_transaction_update if have_old\nis false.\n\nSigned-off-by: Ronnie Sahlberg <sahlberg@google.com>\n---\n builtin/commit.c | 23 ++++++++++-------------\n 1 file changed, 10 insertions(+), 13 deletions(-)\n\ndiff --git a/builtin/commit.c b/builtin/commit.c\nindex f0b7906..6117123 100644\n--- a/builtin/commit.c\n+++ b/builtin/commit.c\n@@ -1541,11 +1541,12 @@ int cmd_commit(int argc, const char **argv, const char *prefix)\n \tconst char *index_file, *reflog_msg;\n \tchar *nl;\n \tunsigned char sha1[20];\n-\tstruct ref_lock *ref_lock;\n \tstruct commit_list *parents = NULL, **pptr = &parents;\n \tstruct stat statbuf;\n \tstruct commit *current_head = NULL;\n \tstruct commit_extra_header *extra = NULL;\n+\tstruct ref_transaction *transaction;\n+\tstruct strbuf err = STRBUF_INIT;\n \n \tif (argc == 2 && !strcmp(argv[1], \"-h\"))\n \t\tusage_with_options(builtin_commit_usage, builtin_commit_options);\n@@ -1667,16 +1668,6 @@ int cmd_commit(int argc, const char **argv, const char *prefix)\n \tstrbuf_release(&author_ident);\n \tfree_commit_extra_headers(extra);\n \n-\tref_lock = lock_any_ref_for_update(\"HEAD\",\n-\t\t\t\t\t   !current_head\n-\t\t\t\t\t   ? NULL\n-\t\t\t\t\t   : current_head->object.sha1,\n-\t\t\t\t\t   0, NULL);\n-\tif (!ref_lock) {\n-\t\trollback_index_files();\n-\t\tdie(_(\"cannot lock HEAD ref\"));\n-\t}\n-\n \tnl = strchr(sb.buf, '\\n');\n \tif (nl)\n \t\tstrbuf_setlen(&sb, nl + 1 - sb.buf);\n@@ -1685,9 +1676,15 @@ int cmd_commit(int argc, const char **argv, const char *prefix)\n \tstrbuf_insert(&sb, 0, reflog_msg, strlen(reflog_msg));\n \tstrbuf_insert(&sb, strlen(reflog_msg), \": \", 2);\n \n-\tif (write_ref_sha1(ref_lock, sha1, sb.buf) < 0) {\n+\ttransaction = ref_transaction_begin();\n+\tif (!transaction ||\n+\t    ref_transaction_update(transaction, \"HEAD\", sha1,\n+\t\t\t\t   current_head ?\n+\t\t\t\t   current_head->object.sha1 : NULL,\n+\t\t\t\t   0, !!current_head) ||\n+\t    ref_transaction_commit(transaction, sb.buf, &err)) {\n \t\trollback_index_files();\n-\t\tdie(_(\"cannot update HEAD ref\"));\n+\t\tdie(_(\"HEAD: cannot update ref: %s\"), err.buf);\n \t}\n \n \tunlink(git_path(\"CHERRY_PICK_HEAD\"));\n-- \n2.0.0.rc1.351.g4d2c8e4\n"},{"id":"240461","messageId":"1398976662-6962-15-git-send-email-sahlberg@google.com","threadId":"36563","inReplyTo":"1398976662-6962-1-git-send-email-sahlberg@google.com","subject":"[PATCH v6 14/42] sequencer.c: use ref transactions for all ref updates","fromName":"Ronnie Sahlberg","fromEmail":"sahlberg@google.com","sentAt":"2014-05-01T20:37:14Z","receivedAt":"2014-05-01T20:37:14Z","isPatch":true,"sender":{"key":"sahlberg@google.com","avatar":"https://avatars.githubusercontent.com/u/7320636?v=4"},"body":"Change to use ref transactions for all updates to refs.\n\nSigned-off-by: Ronnie Sahlberg <sahlberg@google.com>\n---\n sequencer.c | 24 ++++++++++++++++--------\n 1 file changed, 16 insertions(+), 8 deletions(-)\n\ndiff --git a/sequencer.c b/sequencer.c\nindex 0a80c58..9282a12 100644\n--- a/sequencer.c\n+++ b/sequencer.c\n@@ -272,23 +272,31 @@ static int error_dirty_index(struct replay_opts *opts)\n static int fast_forward_to(const unsigned char *to, const unsigned char *from,\n \t\t\tint unborn, struct replay_opts *opts)\n {\n-\tstruct ref_lock *ref_lock;\n+\tstruct ref_transaction *transaction;\n \tstruct strbuf sb = STRBUF_INIT;\n-\tint ret;\n+\tstruct strbuf err = STRBUF_INIT;\n \n \tread_cache();\n \tif (checkout_fast_forward(from, to, 1))\n \t\texit(1); /* the callee should have complained already */\n-\tref_lock = lock_any_ref_for_update(\"HEAD\", unborn ? null_sha1 : from,\n-\t\t\t\t\t   0, NULL);\n-\tif (!ref_lock)\n-\t\treturn error(_(\"Failed to lock HEAD during fast_forward_to\"));\n \n \tstrbuf_addf(&sb, \"%s: fast-forward\", action_name(opts));\n-\tret = write_ref_sha1(ref_lock, to, sb.buf);\n+\n+\ttransaction = ref_transaction_begin();\n+\tif ((!transaction ||\n+\t    ref_transaction_update(transaction, \"HEAD\", to, from,\n+\t\t\t\t   0, !unborn)) ||\n+\t    (ref_transaction_commit(transaction, sb.buf, &err) &&\n+\t     !(transaction = NULL))) {\n+\t\tref_transaction_rollback(transaction);\n+\t\terror(_(\"HEAD: Could not fast-forward: %s\\n\"), err.buf);\n+\t\tstrbuf_release(&sb);\n+\t\tstrbuf_release(&err);\n+\t\treturn -1;\n+\t}\n \n \tstrbuf_release(&sb);\n-\treturn ret;\n+\treturn 0;\n }\n \n static int do_recursive_merge(struct commit *base, struct commit *next,\n-- \n2.0.0.rc1.351.g4d2c8e4\n"},{"id":"240462","messageId":"1398976662-6962-16-git-send-email-sahlberg@google.com","threadId":"36563","inReplyTo":"1398976662-6962-1-git-send-email-sahlberg@google.com","subject":"[PATCH v6 15/42] fast-import.c: change update_branch to use ref transactions","fromName":"Ronnie Sahlberg","fromEmail":"sahlberg@google.com","sentAt":"2014-05-01T20:37:15Z","receivedAt":"2014-05-01T20:37:15Z","isPatch":true,"sender":{"key":"sahlberg@google.com","avatar":"https://avatars.githubusercontent.com/u/7320636?v=4"},"body":"Change update_branch() to use ref transactions for updates.\n\nSigned-off-by: Ronnie Sahlberg <sahlberg@google.com>\n---\n fast-import.c | 22 ++++++++++++++--------\n 1 file changed, 14 insertions(+), 8 deletions(-)\n\ndiff --git a/fast-import.c b/fast-import.c\nindex 6707a66..79d219b 100644\n--- a/fast-import.c\n+++ b/fast-import.c\n@@ -1679,39 +1679,45 @@ found_entry:\n static int update_branch(struct branch *b)\n {\n \tstatic const char *msg = \"fast-import\";\n-\tstruct ref_lock *lock;\n+\tstruct ref_transaction *transaction;\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+\n \tif (is_null_sha1(b->sha1)) {\n \t\tif (b->delete)\n \t\t\tdelete_ref(b->name, old_sha1, 0);\n \t\treturn 0;\n \t}\n-\tlock = lock_any_ref_for_update(b->name, old_sha1, 0, NULL);\n-\tif (!lock)\n-\t\treturn error(\"Unable to lock %s\", b->name);\n \tif (!force_update && !is_null_sha1(old_sha1)) {\n \t\tstruct commit *old_cmit, *new_cmit;\n \n \t\told_cmit = lookup_commit_reference_gently(old_sha1, 0);\n \t\tnew_cmit = lookup_commit_reference_gently(b->sha1, 0);\n \t\tif (!old_cmit || !new_cmit) {\n-\t\t\tunlock_ref(lock);\n \t\t\treturn error(\"Branch %s is missing commits.\", b->name);\n \t\t}\n \n \t\tif (!in_merge_bases(old_cmit, new_cmit)) {\n-\t\t\tunlock_ref(lock);\n \t\t\twarning(\"Not updating %s\"\n \t\t\t\t\" (new tip %s does not contain %s)\",\n \t\t\t\tb->name, sha1_to_hex(b->sha1), sha1_to_hex(old_sha1));\n \t\t\treturn -1;\n \t\t}\n \t}\n-\tif (write_ref_sha1(lock, b->sha1, msg) < 0)\n-\t\treturn error(\"Unable to update %s\", b->name);\n+\ttransaction = ref_transaction_begin();\n+\tif ((!transaction ||\n+\t    ref_transaction_update(transaction, b->name, b->sha1, old_sha1,\n+\t\t\t\t   0, 1)) ||\n+\t    (ref_transaction_commit(transaction, msg, &err) &&\n+\t     !(transaction = NULL))) {\n+\t\tref_transaction_rollback(transaction);\n+\t\terror(\"Unable to update branch %s: %s\", b->name, err.buf);\n+\t\tstrbuf_release(&err);\n+\t\treturn -1;\n+\t}\n \treturn 0;\n }\n \n-- \n2.0.0.rc1.351.g4d2c8e4\n"},{"id":"240456","messageId":"1398976662-6962-17-git-send-email-sahlberg@google.com","threadId":"36563","inReplyTo":"1398976662-6962-1-git-send-email-sahlberg@google.com","subject":"[PATCH v6 16/42] branch.c: use ref transaction for all ref updates","fromName":"Ronnie Sahlberg","fromEmail":"sahlberg@google.com","sentAt":"2014-05-01T20:37:16Z","receivedAt":"2014-05-01T20:37:16Z","isPatch":true,"sender":{"key":"sahlberg@google.com","avatar":"https://avatars.githubusercontent.com/u/7320636?v=4"},"body":"Change create_branch to use a ref transaction when creating the new branch.\nref_transaction_create will check that the ref does not already exist and fail\notherwise meaning that we no longer need to keep a lock on the ref during the\nsetup_tracking. This simplifies the code since we can now do the transaction\nin one single step.\n\nIf the forcing flag is false then use ref_transaction_create since this will\nfail if the ref already exist. Otherwise use ref_transaction_update.\n\nThis also fixes a race condition in the old code where two concurrent\ncreate_branch could race since the lock_any_ref_for_update/write_ref_sha1\ndid not protect against the ref already existsing. I.e. one thread could end up\noverwriting a branch even if the forcing flag is false.\n\nSigned-off-by: Ronnie Sahlberg <sahlberg@google.com>\n---\n branch.c | 30 ++++++++++++++++--------------\n 1 file changed, 16 insertions(+), 14 deletions(-)\n\ndiff --git a/branch.c b/branch.c\nindex 660097b..2aa5c82 100644\n--- a/branch.c\n+++ b/branch.c\n@@ -226,7 +226,6 @@ void create_branch(const char *head,\n \t\t   int force, int reflog, int clobber_head,\n \t\t   int quiet, enum branch_track track)\n {\n-\tstruct ref_lock *lock = NULL;\n \tstruct commit *commit;\n \tunsigned char sha1[20];\n \tchar *real_ref, msg[PATH_MAX + 20];\n@@ -285,15 +284,6 @@ void create_branch(const char *head,\n \t\tdie(_(\"Not a valid branch point: '%s'.\"), start_name);\n \thashcpy(sha1, commit->object.sha1);\n \n-\tif (!dont_change_ref) {\n-\t\tlock = lock_any_ref_for_update(ref.buf, NULL, 0, NULL);\n-\t\tif (!lock)\n-\t\t\tdie_errno(_(\"Failed to lock ref for update\"));\n-\t}\n-\n-\tif (reflog)\n-\t\tlog_all_ref_updates = 1;\n-\n \tif (forcing)\n \t\tsnprintf(msg, sizeof msg, \"branch: Reset to %s\",\n \t\t\t start_name);\n@@ -301,13 +291,25 @@ void create_branch(const char *head,\n \t\tsnprintf(msg, sizeof msg, \"branch: Created from %s\",\n \t\t\t start_name);\n \n+\tif (reflog)\n+\t\tlog_all_ref_updates = 1;\n+\n+\tif (!dont_change_ref) {\n+\t\tstruct ref_transaction *transaction;\n+\t\tstruct strbuf err = STRBUF_INIT;\n+\n+\t\ttransaction = ref_transaction_begin();\n+\t\tif (!transaction ||\n+\t\t\tref_transaction_update(transaction, ref.buf, sha1,\n+\t\t\t\t\t       null_sha1, 0, !forcing) ||\n+\t\t\tref_transaction_commit(transaction, msg, &err))\n+\t\t\t\tdie_errno(_(\"%s: failed to write ref: %s\"),\n+\t\t\t\t\t  ref.buf, err.buf);\n+\t}\n+\n \tif (real_ref && track)\n \t\tsetup_tracking(ref.buf + 11, real_ref, track, quiet);\n \n-\tif (!dont_change_ref)\n-\t\tif (write_ref_sha1(lock, sha1, msg) < 0)\n-\t\t\tdie_errno(_(\"Failed to write ref\"));\n-\n \tstrbuf_release(&ref);\n \tfree(real_ref);\n }\n-- \n2.0.0.rc1.351.g4d2c8e4\n"},{"id":"240457","messageId":"1398976662-6962-18-git-send-email-sahlberg@google.com","threadId":"36563","inReplyTo":"1398976662-6962-1-git-send-email-sahlberg@google.com","subject":"[PATCH v6 17/42] refs.c: change update_ref to use a transaction","fromName":"Ronnie Sahlberg","fromEmail":"sahlberg@google.com","sentAt":"2014-05-01T20:37:17Z","receivedAt":"2014-05-01T20:37:17Z","isPatch":true,"sender":{"key":"sahlberg@google.com","avatar":"https://avatars.githubusercontent.com/u/7320636?v=4"},"body":"Change the update_ref helper function to use a ref transaction internally.\n\nSigned-off-by: Ronnie Sahlberg <sahlberg@google.com>\n---\n refs.c | 25 +++++++++++++++++++++----\n 1 file changed, 21 insertions(+), 4 deletions(-)\n\ndiff --git a/refs.c b/refs.c\nindex 0a4e28e..563f336 100644\n--- a/refs.c\n+++ b/refs.c\n@@ -3396,11 +3396,28 @@ int update_ref(const char *action, const char *refname,\n \t       const unsigned char *sha1, const unsigned char *oldval,\n \t       int flags, enum action_on_err onerr)\n {\n-\tstruct ref_lock *lock;\n-\tlock = update_ref_lock(refname, oldval, flags, NULL, onerr);\n-\tif (!lock)\n+\tstruct ref_transaction *t;\n+\tstruct strbuf err = STRBUF_INIT;\n+\n+\tt = ref_transaction_begin();\n+\tif ((!t ||\n+\t    ref_transaction_update(t, refname, sha1, oldval, flags,\n+\t\t\t\t   !!oldval)) ||\n+\t    (ref_transaction_commit(t, action, &err) && !(t = NULL))) {\n+\t\tconst char *str = \"update_ref failed for ref '%s': %s\";\n+\n+\t\tref_transaction_rollback(t);\n+\t\tswitch (onerr) {\n+\t\tcase UPDATE_REFS_MSG_ON_ERR:\n+\t\t\terror(str, refname, err.buf); break;\n+\t\tcase UPDATE_REFS_DIE_ON_ERR:\n+\t\t\tdie(str, refname, err.buf); break;\n+\t\tcase UPDATE_REFS_QUIET_ON_ERR: break;\n+\t\t}\n+\t\tstrbuf_release(&err);\n \t\treturn 1;\n-\treturn update_ref_write(action, refname, sha1, lock, NULL, onerr);\n+\t}\n+\treturn 0;\n }\n \n static int ref_update_compare(const void *r1, const void *r2)\n-- \n2.0.0.rc1.351.g4d2c8e4\n"},{"id":"240455","messageId":"1398976662-6962-19-git-send-email-sahlberg@google.com","threadId":"36563","inReplyTo":"1398976662-6962-1-git-send-email-sahlberg@google.com","subject":"[PATCH v6 18/42] refs.c: free the transaction before returning when number of updates is 0","fromName":"Ronnie Sahlberg","fromEmail":"sahlberg@google.com","sentAt":"2014-05-01T20:37:18Z","receivedAt":"2014-05-01T20:37:18Z","isPatch":true,"sender":{"key":"sahlberg@google.com","avatar":"https://avatars.githubusercontent.com/u/7320636?v=4"},"body":"We have to free the transaction before returning in the early check for\n'return early if number of updates == 0' or else the following code would\ncreate a memory leak with the transaction never being freed :\n   t = ref_transaction_begin()\n   ref_transaction_commit(t)\n\nSigned-off-by: Ronnie Sahlberg <sahlberg@google.com>\n---\n refs.c | 4 +++-\n 1 file changed, 3 insertions(+), 1 deletion(-)\n\ndiff --git a/refs.c b/refs.c\nindex 563f336..c6ee43c 100644\n--- a/refs.c\n+++ b/refs.c\n@@ -3451,8 +3451,10 @@ int ref_transaction_commit(struct ref_transaction *transaction,\n \tint n = transaction->nr;\n \tstruct ref_update **updates = transaction->updates;\n \n-\tif (!n)\n+\tif (!n) {\n+\t\tref_transaction_free(transaction);\n \t\treturn 0;\n+\t}\n \n \t/* Allocate work space */\n \tdelnames = xmalloc(sizeof(*delnames) * n);\n-- \n2.0.0.rc1.351.g4d2c8e4\n"},{"id":"240430","messageId":"1398976662-6962-20-git-send-email-sahlberg@google.com","threadId":"36563","inReplyTo":"1398976662-6962-1-git-send-email-sahlberg@google.com","subject":"[PATCH v6 19/42] refs.c: ref_transaction_commit should not free the transaction","fromName":"Ronnie Sahlberg","fromEmail":"sahlberg@google.com","sentAt":"2014-05-01T20:37:19Z","receivedAt":"2014-05-01T20:37:19Z","isPatch":true,"sender":{"key":"sahlberg@google.com","avatar":"https://avatars.githubusercontent.com/u/7320636?v=4"},"body":"Change ref_transaction_commit so that it does not free the transaction.\nInstead require that a caller will end a transaction by either calling\nref_transaction_rollback or ref_transaction_free.\n\nBy having the transaction object remaining valid after _commit returns allows\nus to write much nicer code and still be able to call ref_transaction_rollback\nsafely. Instead of this horribleness\n\tt = ref_transaction_begin();\n\tif ((!t ||\n\t    ref_transaction_update(t, refname, sha1, oldval, flags,\n\t\t\t\t   !!oldval)) ||\n\t    (ref_transaction_commit(t, action, &err) && !(t = NULL))) {\n\t\tref_transaction_rollback(t);\n\nwe can now just do the much nicer\n\tt = ref_transaction_begin();\n\tif (!t ||\n\t    ref_transaction_update(t, refname, sha1, oldval, flags,\n\t\t\t\t   !!oldval) ||\n\t    ref_transaction_commit(&t, action, &err)) {\n\t\tref_transaction_rollback(t);\n\t\t... die/return ...\n\n\tref_transaction_free(transaction);\n\nSigned-off-by: Ronnie Sahlberg <sahlberg@google.com>\n---\n branch.c             |  1 +\n builtin/commit.c     |  1 +\n builtin/replace.c    |  1 +\n builtin/tag.c        |  1 +\n builtin/update-ref.c |  1 +\n fast-import.c        |  8 ++++----\n refs.c               | 14 ++++++--------\n refs.h               | 14 +++++++++-----\n sequencer.c          |  8 ++++----\n 9 files changed, 28 insertions(+), 21 deletions(-)\n\ndiff --git a/branch.c b/branch.c\nindex 2aa5c82..8e58908 100644\n--- a/branch.c\n+++ b/branch.c\n@@ -305,6 +305,7 @@ void create_branch(const char *head,\n \t\t\tref_transaction_commit(transaction, msg, &err))\n \t\t\t\tdie_errno(_(\"%s: failed to write ref: %s\"),\n \t\t\t\t\t  ref.buf, err.buf);\n+\t\tref_transaction_free(transaction);\n \t}\n \n \tif (real_ref && track)\ndiff --git a/builtin/commit.c b/builtin/commit.c\nindex 6117123..5bdcf9e 100644\n--- a/builtin/commit.c\n+++ b/builtin/commit.c\n@@ -1686,6 +1686,7 @@ int cmd_commit(int argc, const char **argv, const char *prefix)\n \t\trollback_index_files();\n \t\tdie(_(\"HEAD: cannot update ref: %s\"), err.buf);\n \t}\n+\tref_transaction_free(transaction);\n \n \tunlink(git_path(\"CHERRY_PICK_HEAD\"));\n \tunlink(git_path(\"REVERT_HEAD\"));\ndiff --git a/builtin/replace.c b/builtin/replace.c\nindex b037b29..5999f7d 100644\n--- a/builtin/replace.c\n+++ b/builtin/replace.c\n@@ -165,6 +165,7 @@ static int replace_object(const char *object_ref, const char *replace_ref,\n \t    ref_transaction_commit(transaction, NULL, &err))\n \t\tdie(_(\"%s: failed to replace ref: %s\"), ref, err.buf);\n \n+\tref_transaction_free(transaction);\n \treturn 0;\n }\n \ndiff --git a/builtin/tag.c b/builtin/tag.c\nindex 1cf5650..d59154a 100644\n--- a/builtin/tag.c\n+++ b/builtin/tag.c\n@@ -708,6 +708,7 @@ int cmd_tag(int argc, const char **argv, const char *prefix)\n \t\t\t\t   0, !is_null_sha1(prev)) ||\n \t    ref_transaction_commit(transaction, NULL, &err))\n \t\tdie(_(\"%s: cannot update the ref: %s\"), ref.buf, err.buf);\n+\tref_transaction_free(transaction);\n \tif (force && !is_null_sha1(prev) && hashcmp(prev, object))\n \t\tprintf(_(\"Updated tag '%s' (was %s)\\n\"), tag, find_unique_abbrev(prev, DEFAULT_ABBREV));\n \ndiff --git a/builtin/update-ref.c b/builtin/update-ref.c\nindex fc3512f..7fe9c11 100644\n--- a/builtin/update-ref.c\n+++ b/builtin/update-ref.c\n@@ -373,6 +373,7 @@ int cmd_update_ref(int argc, const char **argv, const char *prefix)\n \t\tupdate_refs_stdin();\n \t\tif (ref_transaction_commit(transaction, msg, &err))\n \t\t\tdie(\"%s\", err.buf);\n+\t\tref_transaction_free(transaction);\n \t\treturn 0;\n \t}\n \ndiff --git a/fast-import.c b/fast-import.c\nindex 79d219b..3e356da 100644\n--- a/fast-import.c\n+++ b/fast-import.c\n@@ -1708,16 +1708,16 @@ static int update_branch(struct branch *b)\n \t\t}\n \t}\n \ttransaction = ref_transaction_begin();\n-\tif ((!transaction ||\n+\tif (!transaction ||\n \t    ref_transaction_update(transaction, b->name, b->sha1, old_sha1,\n-\t\t\t\t   0, 1)) ||\n-\t    (ref_transaction_commit(transaction, msg, &err) &&\n-\t     !(transaction = NULL))) {\n+\t\t\t\t   0, 1) ||\n+\t    ref_transaction_commit(transaction, msg, &err)) {\n \t\tref_transaction_rollback(transaction);\n \t\terror(\"Unable to update branch %s: %s\", b->name, err.buf);\n \t\tstrbuf_release(&err);\n \t\treturn -1;\n \t}\n+\tref_transaction_free(transaction);\n \treturn 0;\n }\n \ndiff --git a/refs.c b/refs.c\nindex c6ee43c..fcbdf9b 100644\n--- a/refs.c\n+++ b/refs.c\n@@ -3302,7 +3302,7 @@ struct ref_transaction *ref_transaction_begin(void)\n \treturn xcalloc(1, sizeof(struct ref_transaction));\n }\n \n-static void ref_transaction_free(struct ref_transaction *transaction)\n+void ref_transaction_free(struct ref_transaction *transaction)\n {\n \tint i;\n \n@@ -3400,10 +3400,10 @@ int update_ref(const char *action, const char *refname,\n \tstruct strbuf err = STRBUF_INIT;\n \n \tt = ref_transaction_begin();\n-\tif ((!t ||\n+\tif (!t ||\n \t    ref_transaction_update(t, refname, sha1, oldval, flags,\n-\t\t\t\t   !!oldval)) ||\n-\t    (ref_transaction_commit(t, action, &err) && !(t = NULL))) {\n+\t\t\t\t   !!oldval) ||\n+\t    ref_transaction_commit(t, action, &err)) {\n \t\tconst char *str = \"update_ref failed for ref '%s': %s\";\n \n \t\tref_transaction_rollback(t);\n@@ -3417,6 +3417,7 @@ int update_ref(const char *action, const char *refname,\n \t\tstrbuf_release(&err);\n \t\treturn 1;\n \t}\n+\tref_transaction_free(t);\n \treturn 0;\n }\n \n@@ -3451,10 +3452,8 @@ int ref_transaction_commit(struct ref_transaction *transaction,\n \tint n = transaction->nr;\n \tstruct ref_update **updates = transaction->updates;\n \n-\tif (!n) {\n-\t\tref_transaction_free(transaction);\n+\tif (!n)\n \t\treturn 0;\n-\t}\n \n \t/* Allocate work space */\n \tdelnames = xmalloc(sizeof(*delnames) * n);\n@@ -3520,7 +3519,6 @@ cleanup:\n \t\tif (updates[i]->lock)\n \t\t\tunlock_ref(updates[i]->lock);\n \tfree(delnames);\n-\tref_transaction_free(transaction);\n \treturn ret;\n }\n \ndiff --git a/refs.h b/refs.h\nindex 5eb500e..fcde43e 100644\n--- a/refs.h\n+++ b/refs.h\n@@ -210,8 +210,8 @@ enum action_on_err {\n \n /*\n  * Begin a reference transaction.  The reference transaction must\n- * eventually be commited using ref_transaction_commit() or rolled\n- * back using ref_transaction_rollback().\n+ * eventually be freed by either calling ref_transaction_rollback()\n+ * or ref_transaction_free().\n  */\n struct ref_transaction *ref_transaction_begin(void);\n \n@@ -267,13 +267,17 @@ int ref_transaction_delete(struct ref_transaction *transaction,\n /*\n  * Commit all of the changes that have been queued in transaction, as\n  * atomically as possible.  Return a nonzero value if there is a\n- * problem.  The ref_transaction is freed by this function.\n- * If err is non-NULL we will add an error string to it to explain why\n- * the transaction failed.\n+ * problem. If err is non-NULL we will add an error string to it to explain\n+ * why the transaction failed.\n  */\n int ref_transaction_commit(struct ref_transaction *transaction,\n \t\t\t   const char *msg, struct strbuf *err);\n \n+/*\n+ * Free an existing transaction.\n+ */\n+void ref_transaction_free(struct ref_transaction *transaction);\n+\n /** Lock a ref and then write its file */\n int update_ref(const char *action, const char *refname,\n \t\tconst unsigned char *sha1, const unsigned char *oldval,\ndiff --git a/sequencer.c b/sequencer.c\nindex 9282a12..0cfdaf0 100644\n--- a/sequencer.c\n+++ b/sequencer.c\n@@ -283,17 +283,17 @@ static int fast_forward_to(const unsigned char *to, const unsigned char *from,\n \tstrbuf_addf(&sb, \"%s: fast-forward\", action_name(opts));\n \n \ttransaction = ref_transaction_begin();\n-\tif ((!transaction ||\n+\tif (!transaction ||\n \t    ref_transaction_update(transaction, \"HEAD\", to, from,\n-\t\t\t\t   0, !unborn)) ||\n-\t    (ref_transaction_commit(transaction, sb.buf, &err) &&\n-\t     !(transaction = NULL))) {\n+\t\t\t\t   0, !unborn) ||\n+\t    ref_transaction_commit(transaction, sb.buf, &err)) {\n \t\tref_transaction_rollback(transaction);\n \t\terror(_(\"HEAD: Could not fast-forward: %s\\n\"), err.buf);\n \t\tstrbuf_release(&sb);\n \t\tstrbuf_release(&err);\n \t\treturn -1;\n \t}\n+\tref_transaction_free(transaction);\n \n \tstrbuf_release(&sb);\n \treturn 0;\n-- \n2.0.0.rc1.351.g4d2c8e4\n"},{"id":"240431","messageId":"1398976662-6962-21-git-send-email-sahlberg@google.com","threadId":"36563","inReplyTo":"1398976662-6962-1-git-send-email-sahlberg@google.com","subject":"[PATCH v6 20/42] fetch.c: clear errno before calling functions that might set it","fromName":"Ronnie Sahlberg","fromEmail":"sahlberg@google.com","sentAt":"2014-05-01T20:37:20Z","receivedAt":"2014-05-01T20:37:20Z","isPatch":true,"sender":{"key":"sahlberg@google.com","avatar":"https://avatars.githubusercontent.com/u/7320636?v=4"},"body":"In s_update_ref there are two calls that when they fail we return an error\nbased on the errno value. In particular we want to return a specific error\nif ENOTDIR happened. Both these functions do have failure modes where they\nmay return an error without updating errno, in which case a previous and\nunrelated ENOTDIT may cause us to return the wrong error. Clear errno before\ncalling any functions if we check errno afterwards.\n\nAlso skip initializing a static variable to 0. Statics live in .bss and\nare all automatically initialized to 0.\n\nSigned-off-by: Ronnie Sahlberg <sahlberg@google.com>\n---\n builtin/fetch.c | 4 +++-\n 1 file changed, 3 insertions(+), 1 deletion(-)\n\ndiff --git a/builtin/fetch.c b/builtin/fetch.c\nindex 55f457c..a93c893 100644\n--- a/builtin/fetch.c\n+++ b/builtin/fetch.c\n@@ -44,7 +44,7 @@ static struct transport *gtransport;\n static struct transport *gsecondary;\n static const char *submodule_prefix = \"\";\n static const char *recurse_submodules_default;\n-static int shown_url = 0;\n+static int shown_url;\n \n static int option_parse_recurse_submodules(const struct option *opt,\n \t\t\t\t   const char *arg, int unset)\n@@ -382,6 +382,8 @@ static int s_update_ref(const char *action,\n \tif (!rla)\n \t\trla = default_rla.buf;\n \tsnprintf(msg, sizeof(msg), \"%s: %s\", rla, action);\n+\n+\terrno = 0;\n \tlock = lock_any_ref_for_update(ref->name,\n \t\t\t\t       check_old ? ref->old_sha1 : NULL,\n \t\t\t\t       0, NULL);\n-- \n2.0.0.rc1.351.g4d2c8e4\n"},{"id":"240436","messageId":"1398976662-6962-22-git-send-email-sahlberg@google.com","threadId":"36563","inReplyTo":"1398976662-6962-1-git-send-email-sahlberg@google.com","subject":"[PATCH v6 21/42] fetch.c: change s_update_ref to use a ref transaction","fromName":"Ronnie Sahlberg","fromEmail":"sahlberg@google.com","sentAt":"2014-05-01T20:37:21Z","receivedAt":"2014-05-01T20:37:21Z","isPatch":true,"sender":{"key":"sahlberg@google.com","avatar":"https://avatars.githubusercontent.com/u/7320636?v=4"},"body":"Change s_update_ref to use a ref transaction for the ref update.\n\nSigned-off-by: Ronnie Sahlberg <sahlberg@google.com>\n---\n builtin/fetch.c | 17 +++++++++--------\n 1 file changed, 9 insertions(+), 8 deletions(-)\n\ndiff --git a/builtin/fetch.c b/builtin/fetch.c\nindex a93c893..b41d7b7 100644\n--- a/builtin/fetch.c\n+++ b/builtin/fetch.c\n@@ -375,7 +375,7 @@ static int s_update_ref(const char *action,\n {\n \tchar msg[1024];\n \tchar *rla = getenv(\"GIT_REFLOG_ACTION\");\n-\tstatic struct ref_lock *lock;\n+\tstruct ref_transaction *transaction;\n \n \tif (dry_run)\n \t\treturn 0;\n@@ -384,15 +384,16 @@ static int s_update_ref(const char *action,\n \tsnprintf(msg, sizeof(msg), \"%s: %s\", rla, action);\n \n \terrno = 0;\n-\tlock = lock_any_ref_for_update(ref->name,\n-\t\t\t\t       check_old ? ref->old_sha1 : NULL,\n-\t\t\t\t       0, NULL);\n-\tif (!lock)\n-\t\treturn errno == ENOTDIR ? STORE_REF_ERROR_DF_CONFLICT :\n-\t\t\t\t\t  STORE_REF_ERROR_OTHER;\n-\tif (write_ref_sha1(lock, ref->new_sha1, msg) < 0)\n+\ttransaction = ref_transaction_begin();\n+\tif (!transaction ||\n+\t    ref_transaction_update(transaction, ref->name, ref->new_sha1,\n+\t\t\t\t   ref->old_sha1, 0, check_old) ||\n+\t    ref_transaction_commit(transaction, msg, NULL)) {\n+\t\tref_transaction_rollback(transaction);\n \t\treturn errno == ENOTDIR ? STORE_REF_ERROR_DF_CONFLICT :\n \t\t\t\t\t  STORE_REF_ERROR_OTHER;\n+\t}\n+\tref_transaction_free(transaction);\n \treturn 0;\n }\n \n-- \n2.0.0.rc1.351.g4d2c8e4\n"},{"id":"240434","messageId":"1398976662-6962-23-git-send-email-sahlberg@google.com","threadId":"36563","inReplyTo":"1398976662-6962-1-git-send-email-sahlberg@google.com","subject":"[PATCH v6 22/42] fetch.c: use a single ref transaction for all ref updates","fromName":"Ronnie Sahlberg","fromEmail":"sahlberg@google.com","sentAt":"2014-05-01T20:37:22Z","receivedAt":"2014-05-01T20:37:22Z","isPatch":true,"sender":{"key":"sahlberg@google.com","avatar":"https://avatars.githubusercontent.com/u/7320636?v=4"},"body":"Change store_updated_refs to use a single ref transaction for all refs that\nare updated during the fetch. This makes the fetch more atomic when update\nfailures occur.\n\nSince ref update failures will now no longer occur in the code path for\nupdating a single ref in s_update_ref, we no longer have as detailed error\nmessage logging the exact reference and the ref log action as in the old cod\nInstead since we fail the entire transaction we log a much more generic\nmessage. But since we commit the transaction using MSG_ON_ERR we will log\nan error containing the ref name if either locking of writing the ref would\nso the regression in the log message is minor.\n\nThis will also change the order in which errors are checked for and logged\nwhich may alter which error will be logged if there are multiple errors\noccuring during a fetch.\n\nFor example, assume we have a fetch for two refs that both would fail.\nWhere the first ref would fail with ENOTDIR due to a directory in the ref\npath not existing, and the second ref in the fetch would fail due to\nthe check in update_logical_ref():\n        if (current_branch &&\n            !strcmp(ref->name, current_branch->name) &&\n            !(update_head_ok || is_bare_repository()) &&\n            !is_null_sha1(ref->old_sha1)) {\n                /*\n                 * If this is the head, and it's not okay to update\n                 * the head, and the old value of the head isn't empty...\n                 */\n\nIn the old code since we would update the refs one ref at a time we would\nfirst fail the ENOTDIR and then fail the second update of HEAD as well.\nBut since the first ref failed with ENOTDIR we would eventually fail the who\nfetch with STORE_REF_ERROR_DF_CONFLICT\n\nIn the new code, since we defer committing the transaction until all refs\nhave been processed, we would now detect that the second ref was bad and\nrollback the transaction before we would even try start writing the update t\ndisk and thus we would not return STORE_REF_ERROR_DF_CONFLICT for this case.\n\nI think this new behaviour is more correct, since if there was a problem\nwe would not even try to commit the transaction but need to highlight this\nchange in how/what errors are reported.\nThis change in what error is returned only occurs if there are multiple\nrefs that fail to update and only some, but not all, of them fail due to\nENOTDIR.\n\nSigned-off-by: Ronnie Sahlberg <sahlberg@google.com>\n---\n builtin/fetch.c | 34 ++++++++++++++++------------------\n 1 file changed, 16 insertions(+), 18 deletions(-)\n\ndiff --git a/builtin/fetch.c b/builtin/fetch.c\nindex b41d7b7..5dbd0f0 100644\n--- a/builtin/fetch.c\n+++ b/builtin/fetch.c\n@@ -45,6 +45,7 @@ static struct transport *gsecondary;\n static const char *submodule_prefix = \"\";\n static const char *recurse_submodules_default;\n static int shown_url;\n+struct ref_transaction *transaction;\n \n static int option_parse_recurse_submodules(const struct option *opt,\n \t\t\t\t   const char *arg, int unset)\n@@ -373,27 +374,13 @@ static int s_update_ref(const char *action,\n \t\t\tstruct ref *ref,\n \t\t\tint check_old)\n {\n-\tchar msg[1024];\n-\tchar *rla = getenv(\"GIT_REFLOG_ACTION\");\n-\tstruct ref_transaction *transaction;\n-\n \tif (dry_run)\n \t\treturn 0;\n-\tif (!rla)\n-\t\trla = default_rla.buf;\n-\tsnprintf(msg, sizeof(msg), \"%s: %s\", rla, action);\n \n-\terrno = 0;\n-\ttransaction = ref_transaction_begin();\n-\tif (!transaction ||\n-\t    ref_transaction_update(transaction, ref->name, ref->new_sha1,\n-\t\t\t\t   ref->old_sha1, 0, check_old) ||\n-\t    ref_transaction_commit(transaction, msg, NULL)) {\n-\t\tref_transaction_rollback(transaction);\n-\t\treturn errno == ENOTDIR ? STORE_REF_ERROR_DF_CONFLICT :\n-\t\t\t\t\t  STORE_REF_ERROR_OTHER;\n-\t}\n-\tref_transaction_free(transaction);\n+\tif (ref_transaction_update(transaction, ref->name, ref->new_sha1,\n+\t\t\t       ref->old_sha1, 0, check_old))\n+\t\treturn STORE_REF_ERROR_OTHER;\n+\n \treturn 0;\n }\n \n@@ -565,6 +552,13 @@ static int store_updated_refs(const char *raw_url, const char *remote_name,\n \t\tgoto abort;\n \t}\n \n+\terrno = 0;\n+\ttransaction = ref_transaction_begin();\n+\tif (!transaction) {\n+\t\trc = error(_(\"cannot start ref transaction\\n\"));\n+\t\tgoto abort;\n+\t}\n+\n \t/*\n \t * We do a pass for each fetch_head_status type in their enum order, so\n \t * merged entries are written before not-for-merge. That lets readers\n@@ -676,6 +670,10 @@ static int store_updated_refs(const char *raw_url, const char *remote_name,\n \t\t\t}\n \t\t}\n \t}\n+\tif (ref_transaction_commit(transaction, \"fetch_ref transaction\", NULL))\n+\t\trc |= errno == ENOTDIR ? STORE_REF_ERROR_DF_CONFLICT :\n+\t\t  STORE_REF_ERROR_OTHER;\n+\tref_transaction_free(transaction);\n \n \tif (rc & STORE_REF_ERROR_DF_CONFLICT)\n \t\terror(_(\"some local refs could not be updated; try running\\n\"\n-- \n2.0.0.rc1.351.g4d2c8e4\n"},{"id":"240435","messageId":"1398976662-6962-24-git-send-email-sahlberg@google.com","threadId":"36563","inReplyTo":"1398976662-6962-1-git-send-email-sahlberg@google.com","subject":"[PATCH v6 23/42] receive-pack.c: use a reference transaction for updating the refs","fromName":"Ronnie Sahlberg","fromEmail":"sahlberg@google.com","sentAt":"2014-05-01T20:37:23Z","receivedAt":"2014-05-01T20:37:23Z","isPatch":true,"sender":{"key":"sahlberg@google.com","avatar":"https://avatars.githubusercontent.com/u/7320636?v=4"},"body":"Wrap all the ref updates inside a transaction to make the update atomic.\n\nSigned-off-by: Ronnie Sahlberg <sahlberg@google.com>\n---\n builtin/receive-pack.c | 20 ++++++++++----------\n 1 file changed, 10 insertions(+), 10 deletions(-)\n\ndiff --git a/builtin/receive-pack.c b/builtin/receive-pack.c\nindex c323081..d580176 100644\n--- a/builtin/receive-pack.c\n+++ b/builtin/receive-pack.c\n@@ -46,6 +46,8 @@ static void *head_name_to_free;\n static int sent_capabilities;\n static int shallow_update;\n static const char *alt_shallow_file;\n+struct strbuf err = STRBUF_INIT;\n+struct ref_transaction *transaction;\n \n static enum deny_action parse_deny_action(const char *var, const char *value)\n {\n@@ -475,7 +477,6 @@ static const char *update(struct command *cmd, struct shallow_info *si)\n \tconst char *namespaced_name;\n \tunsigned char *old_sha1 = cmd->old_sha1;\n \tunsigned char *new_sha1 = cmd->new_sha1;\n-\tstruct ref_lock *lock;\n \n \t/* only refs/... are allowed */\n \tif (!starts_with(name, \"refs/\") || check_refname_format(name + 5, 0)) {\n@@ -580,15 +581,9 @@ static const char *update(struct command *cmd, struct shallow_info *si)\n \t\t    update_shallow_ref(cmd, si))\n \t\t\treturn \"shallow error\";\n \n-\t\tlock = lock_any_ref_for_update(namespaced_name, old_sha1,\n-\t\t\t\t\t       0, NULL);\n-\t\tif (!lock) {\n-\t\t\trp_error(\"failed to lock %s\", name);\n-\t\t\treturn \"failed to lock\";\n-\t\t}\n-\t\tif (write_ref_sha1(lock, new_sha1, \"push\")) {\n-\t\t\treturn \"failed to write\"; /* error() already called */\n-\t\t}\n+\t\tif (ref_transaction_update(transaction, namespaced_name,\n+\t\t\t\t\t   new_sha1, old_sha1, 0, 1))\n+\t\t\treturn \"failed to update\";\n \t\treturn NULL; /* good */\n \t}\n }\n@@ -812,6 +807,7 @@ static void execute_commands(struct command *commands,\n \thead_name = head_name_to_free = resolve_refdup(\"HEAD\", sha1, 0, NULL);\n \n \tchecked_connectivity = 1;\n+\ttransaction = ref_transaction_begin();\n \tfor (cmd = commands; cmd; cmd = cmd->next) {\n \t\tif (cmd->error_string)\n \t\t\tcontinue;\n@@ -827,6 +823,10 @@ static void execute_commands(struct command *commands,\n \t\t\tchecked_connectivity = 0;\n \t\t}\n \t}\n+\tif (ref_transaction_commit(transaction, \"push\", &err))\n+\t\terror(\"%s\", err.buf);\n+\tref_transaction_free(transaction);\n+\tstrbuf_release(&err);\n \n \tif (shallow_update && !checked_connectivity)\n \t\terror(\"BUG: run 'git fsck' for safety.\\n\"\n-- \n2.0.0.rc1.351.g4d2c8e4\n"},{"id":"240452","messageId":"1398976662-6962-25-git-send-email-sahlberg@google.com","threadId":"36563","inReplyTo":"1398976662-6962-1-git-send-email-sahlberg@google.com","subject":"[PATCH v6 24/42] fast-import.c: use a ref transaction when dumping tags","fromName":"Ronnie Sahlberg","fromEmail":"sahlberg@google.com","sentAt":"2014-05-01T20:37:24Z","receivedAt":"2014-05-01T20:37:24Z","isPatch":true,"sender":{"key":"sahlberg@google.com","avatar":"https://avatars.githubusercontent.com/u/7320636?v=4"},"body":"Signed-off-by: Ronnie Sahlberg <sahlberg@google.com>\n---\n fast-import.c | 17 ++++++++++++-----\n 1 file changed, 12 insertions(+), 5 deletions(-)\n\ndiff --git a/fast-import.c b/fast-import.c\nindex 3e356da..5587cf6 100644\n--- a/fast-import.c\n+++ b/fast-import.c\n@@ -1736,15 +1736,22 @@ static void dump_tags(void)\n {\n \tstatic const char *msg = \"fast-import\";\n \tstruct tag *t;\n-\tstruct ref_lock *lock;\n \tchar ref_name[PATH_MAX];\n+\tstruct strbuf err = STRBUF_INIT;\n+\tstruct ref_transaction *transaction;\n \n+\ttransaction = ref_transaction_begin();\n \tfor (t = first_tag; t; t = t->next_tag) {\n-\t\tsprintf(ref_name, \"tags/%s\", t->name);\n-\t\tlock = lock_ref_sha1(ref_name, NULL);\n-\t\tif (!lock || write_ref_sha1(lock, t->sha1, msg) < 0)\n-\t\t\tfailure |= error(\"Unable to update %s\", ref_name);\n+\t\tsprintf(ref_name, \"refs/tags/%s\", t->name);\n+\n+\t\tif (ref_transaction_update(transaction, ref_name, t->sha1,\n+\t\t\t\t\t   NULL, 0, 0))\n+\t\t\tfailure |= error(\"Unable to update %s\", err.buf);\n \t}\n+\tif (failure || ref_transaction_commit(transaction, msg, &err))\n+\t\tfailure |= error(\"Unable to update %s\", err.buf);\n+\tref_transaction_free(transaction);\n+\tstrbuf_release(&err);\n }\n \n static void dump_marks_helper(FILE *f,\n-- \n2.0.0.rc1.351.g4d2c8e4\n"},{"id":"240447","messageId":"1398976662-6962-26-git-send-email-sahlberg@google.com","threadId":"36563","inReplyTo":"1398976662-6962-1-git-send-email-sahlberg@google.com","subject":"[PATCH v6 25/42] walker.c: use ref transaction for ref updates","fromName":"Ronnie Sahlberg","fromEmail":"sahlberg@google.com","sentAt":"2014-05-01T20:37:25Z","receivedAt":"2014-05-01T20:37:25Z","isPatch":true,"sender":{"key":"sahlberg@google.com","avatar":"https://avatars.githubusercontent.com/u/7320636?v=4"},"body":"Switch to using ref transactions in walker_fetch(). As part of the refactoring\nto use ref transactions we also fix a potential memory leak where in the\noriginal code if write_ref_sha1() would fail we would end up returning from\nthe function without free()ing the msg string.\n\nThis changes the locking slightly for walker_fetch. Previously the code would\nlock all refs before writing them but now we do not lock the refs until the\ncommit stage. There is thus a very short window where changes could be done\nlocally during the fetch which would be overwritten when the fetch completes\nand commits its transaction. But this window should be reasonably short.\nEven if this race does trigger, since both the old code and the new code\njust overwrites the refs to the new values without checking or comparing\nthem with the previous value, this is not too dissimilar to a similar scenario\nwhere you first do a ref change locally and then later do a fetch that\noverwrites the local change. With this in mind I do not see the change in\nlocking semantics to be critical.\n\nNote that this function is only called when fetching from a remote HTTP\nrepository onto the local (most of the time single-user) repository which\nlikely means that the type of collissions that the previous locking would\nprotect against and cause the fetch to fail for to be even more rare.\n\nSigned-off-by: Ronnie Sahlberg <sahlberg@google.com>\n---\n walker.c | 51 ++++++++++++++++++++++++++-------------------------\n 1 file changed, 26 insertions(+), 25 deletions(-)\n\ndiff --git a/walker.c b/walker.c\nindex 1dd86b8..6044ccf 100644\n--- a/walker.c\n+++ b/walker.c\n@@ -251,24 +251,18 @@ void walker_targets_free(int targets, char **target, const char **write_ref)\n int walker_fetch(struct walker *walker, int targets, char **target,\n \t\t const char **write_ref, const char *write_ref_log_details)\n {\n-\tstruct ref_lock **lock = xcalloc(targets, sizeof(struct ref_lock *));\n+\tchar ref_name[PATH_MAX];\n+\tstruct strbuf err = STRBUF_INIT;\n+\tstruct ref_transaction *transaction;\n \tunsigned char *sha1 = xmalloc(targets * 20);\n-\tchar *msg;\n-\tint ret;\n+\tchar *msg = NULL;\n \tint i;\n \n \tsave_commit_buffer = 0;\n \n-\tfor (i = 0; i < targets; i++) {\n-\t\tif (!write_ref || !write_ref[i])\n-\t\t\tcontinue;\n-\n-\t\tlock[i] = lock_ref_sha1(write_ref[i], NULL);\n-\t\tif (!lock[i]) {\n-\t\t\terror(\"Can't lock ref %s\", write_ref[i]);\n-\t\t\tgoto unlock_and_fail;\n-\t\t}\n-\t}\n+\ttransaction = ref_transaction_begin();\n+\tif (!transaction)\n+\t\treturn -1;\n \n \tif (!walker->get_recover)\n \t\tfor_each_ref(mark_complete, NULL);\n@@ -276,14 +270,14 @@ int walker_fetch(struct walker *walker, int targets, char **target,\n \tfor (i = 0; i < targets; i++) {\n \t\tif (interpret_target(walker, target[i], &sha1[20 * i])) {\n \t\t\terror(\"Could not interpret response from server '%s' as something to pull\", target[i]);\n-\t\t\tgoto unlock_and_fail;\n+\t\t\tgoto rollback_and_fail;\n \t\t}\n \t\tif (process(walker, lookup_unknown_object(&sha1[20 * i])))\n-\t\t\tgoto unlock_and_fail;\n+\t\t\tgoto rollback_and_fail;\n \t}\n \n \tif (loop(walker))\n-\t\tgoto unlock_and_fail;\n+\t\tgoto rollback_and_fail;\n \n \tif (write_ref_log_details) {\n \t\tmsg = xmalloc(strlen(write_ref_log_details) + 12);\n@@ -294,19 +288,26 @@ int walker_fetch(struct walker *walker, int targets, char **target,\n \tfor (i = 0; i < targets; i++) {\n \t\tif (!write_ref || !write_ref[i])\n \t\t\tcontinue;\n-\t\tret = write_ref_sha1(lock[i], &sha1[20 * i], msg ? msg : \"fetch (unknown)\");\n-\t\tlock[i] = NULL;\n-\t\tif (ret)\n-\t\t\tgoto unlock_and_fail;\n+\t\tsprintf(ref_name, \"refs/%s\", write_ref[i]);\n+\t\tif (ref_transaction_update(transaction, ref_name,\n+\t\t\t\t\t   &sha1[20 * i], NULL,\n+\t\t\t\t\t   0, 0))\n+\t\t\tgoto rollback_and_fail;\n+\t}\n+\n+\tif (ref_transaction_commit(transaction, msg ? msg : \"fetch (unknown)\",\n+\t\t\t\t   &err)) {\n+\t\terror(\"%s\", err.buf);\n+\t\tgoto rollback_and_fail;\n \t}\n-\tfree(msg);\n \n+\tfree(msg);\n \treturn 0;\n \n-unlock_and_fail:\n-\tfor (i = 0; i < targets; i++)\n-\t\tif (lock[i])\n-\t\t\tunlock_ref(lock[i]);\n+rollback_and_fail:\n+\tfree(msg);\n+\tstrbuf_release(&err);\n+\tref_transaction_free(transaction);\n \n \treturn -1;\n }\n-- \n2.0.0.rc1.351.g4d2c8e4\n"},{"id":"240446","messageId":"1398976662-6962-27-git-send-email-sahlberg@google.com","threadId":"36563","inReplyTo":"1398976662-6962-1-git-send-email-sahlberg@google.com","subject":"[PATCH v6 26/42] refs.c: make write_ref_sha1 static","fromName":"Ronnie Sahlberg","fromEmail":"sahlberg@google.com","sentAt":"2014-05-01T20:37:26Z","receivedAt":"2014-05-01T20:37:26Z","isPatch":true,"sender":{"key":"sahlberg@google.com","avatar":"https://avatars.githubusercontent.com/u/7320636?v=4"},"body":"No external users call write_ref_sha1 any more so lets declare it static.\n\nSigned-off-by: Ronnie Sahlberg <sahlberg@google.com>\n---\n refs.c | 4 +++-\n refs.h | 3 ---\n 2 files changed, 3 insertions(+), 4 deletions(-)\n\ndiff --git a/refs.c b/refs.c\nindex fcbdf9b..020eadf 100644\n--- a/refs.c\n+++ b/refs.c\n@@ -251,6 +251,8 @@ struct ref_entry {\n };\n \n static void read_loose_refs(const char *dirname, struct ref_dir *dir);\n+static int write_ref_sha1(struct ref_lock *lock,\n+\t\t\t  const unsigned char *sha1, const char *logmsg);\n \n static struct ref_dir *get_ref_dir(struct ref_entry *entry)\n {\n@@ -2798,7 +2800,7 @@ static int is_branch(const char *refname)\n \treturn !strcmp(refname, \"HEAD\") || starts_with(refname, \"refs/heads/\");\n }\n \n-int write_ref_sha1(struct ref_lock *lock,\n+static int write_ref_sha1(struct ref_lock *lock,\n \tconst unsigned char *sha1, const char *logmsg)\n {\n \tstatic char term = '\\n';\ndiff --git a/refs.h b/refs.h\nindex fcde43e..bc8a95b 100644\n--- a/refs.h\n+++ b/refs.h\n@@ -150,9 +150,6 @@ extern int commit_ref(struct ref_lock *lock);\n /** Release any lock taken but not written. **/\n extern void unlock_ref(struct ref_lock *lock);\n \n-/** Writes sha1 into the ref specified by the lock. **/\n-extern int write_ref_sha1(struct ref_lock *lock, const unsigned char *sha1, const char *msg);\n-\n /** Setup reflog before using. **/\n int log_ref_setup(const char *refname, char *logfile, int bufsize);\n \n-- \n2.0.0.rc1.351.g4d2c8e4\n"},{"id":"240449","messageId":"1398976662-6962-28-git-send-email-sahlberg@google.com","threadId":"36563","inReplyTo":"1398976662-6962-1-git-send-email-sahlberg@google.com","subject":"[PATCH v6 27/42] refs.c: make lock_ref_sha1 static","fromName":"Ronnie Sahlberg","fromEmail":"sahlberg@google.com","sentAt":"2014-05-01T20:37:27Z","receivedAt":"2014-05-01T20:37:27Z","isPatch":true,"sender":{"key":"sahlberg@google.com","avatar":"https://avatars.githubusercontent.com/u/7320636?v=4"},"body":"No external callers reference lock_ref_sha1 any more so lets declare it static.\n\nSigned-off-by: Ronnie Sahlberg <sahlberg@google.com>\n---\n refs.c | 2 +-\n refs.h | 3 ---\n 2 files changed, 1 insertion(+), 4 deletions(-)\n\ndiff --git a/refs.c b/refs.c\nindex 020eadf..a82d6ba 100644\n--- a/refs.c\n+++ b/refs.c\n@@ -2130,7 +2130,7 @@ static struct ref_lock *lock_ref_sha1_basic(const char *refname,\n \treturn NULL;\n }\n \n-struct ref_lock *lock_ref_sha1(const char *refname, const unsigned char *old_sha1)\n+static struct ref_lock *lock_ref_sha1(const char *refname, const unsigned char *old_sha1)\n {\n \tchar refpath[PATH_MAX];\n \tif (check_refname_format(refname, 0))\ndiff --git a/refs.h b/refs.h\nindex bc8a95b..b14fe86 100644\n--- a/refs.h\n+++ b/refs.h\n@@ -132,9 +132,6 @@ extern int ref_exists(const char *);\n  */\n extern int peel_ref(const char *refname, unsigned char *sha1);\n \n-/** Locks a \"refs/\" ref returning the lock on success and NULL on failure. **/\n-extern struct ref_lock *lock_ref_sha1(const char *refname, const unsigned char *old_sha1);\n-\n /** Locks any ref (for 'HEAD' type refs). */\n #define REF_NODEREF\t0x01\n extern struct ref_lock *lock_any_ref_for_update(const char *refname,\n-- \n2.0.0.rc1.351.g4d2c8e4\n"},{"id":"240445","messageId":"1398976662-6962-29-git-send-email-sahlberg@google.com","threadId":"36563","inReplyTo":"1398976662-6962-1-git-send-email-sahlberg@google.com","subject":"[PATCH v6 28/42] refs.c: add transaction.status and track OPEN/CLOSED/ERROR","fromName":"Ronnie Sahlberg","fromEmail":"sahlberg@google.com","sentAt":"2014-05-01T20:37:28Z","receivedAt":"2014-05-01T20:37:28Z","isPatch":true,"sender":{"key":"sahlberg@google.com","avatar":"https://avatars.githubusercontent.com/u/7320636?v=4"},"body":"Track the status of a transaction in a new status field. Check the field for\nsanity, i.e. that status must be OPEN when _commit/_create/_delete or\n_update is called or else die(BUG:...)\n\nSigned-off-by: Ronnie Sahlberg <sahlberg@google.com>\n---\n refs.c | 37 +++++++++++++++++++++++++++++++++----\n 1 file changed, 33 insertions(+), 4 deletions(-)\n\ndiff --git a/refs.c b/refs.c\nindex a82d6ba..206e1a2 100644\n--- a/refs.c\n+++ b/refs.c\n@@ -3288,6 +3288,12 @@ struct ref_update {\n \tconst char refname[FLEX_ARRAY];\n };\n \n+enum ref_transaction_status {\n+\tREF_TRANSACTION_OPEN   = 0,\n+\tREF_TRANSACTION_CLOSED = 1,\n+\tREF_TRANSACTION_ERROR  = 2,\n+};\n+\n /*\n  * Data structure for holding a reference transaction, which can\n  * consist of checks and updates to multiple references, carried out\n@@ -3297,6 +3303,7 @@ struct ref_transaction {\n \tstruct ref_update **updates;\n \tsize_t alloc;\n \tsize_t nr;\n+\tenum ref_transaction_status status;\n };\n \n struct ref_transaction *ref_transaction_begin(void)\n@@ -3320,6 +3327,11 @@ void ref_transaction_free(struct ref_transaction *transaction)\n \n void ref_transaction_rollback(struct ref_transaction *transaction)\n {\n+\tif (!transaction)\n+\t\treturn;\n+\n+\ttransaction->status = REF_TRANSACTION_ERROR;\n+\n \tref_transaction_free(transaction);\n }\n \n@@ -3344,7 +3356,10 @@ int ref_transaction_update(struct ref_transaction *transaction,\n \tstruct ref_update *update;\n \n \tif (have_old && !old_sha1)\n-\t\tdie(\"have_old is true but old_sha1 is NULL\");\n+\t\tdie(\"BUG: have_old is true but old_sha1 is NULL\");\n+\n+\tif (transaction->status != REF_TRANSACTION_OPEN)\n+\t\tdie(\"BUG: update on transaction that is not open\");\n \n \tupdate = add_update(transaction, refname);\n \thashcpy(update->new_sha1, new_sha1);\n@@ -3363,7 +3378,10 @@ int ref_transaction_create(struct ref_transaction *transaction,\n \tstruct ref_update *update;\n \n \tif (!new_sha1 || is_null_sha1(new_sha1))\n-\t\tdie(\"create ref with null new_sha1\");\n+\t\tdie(\"BUG: create ref with null new_sha1\");\n+\n+\tif (transaction->status != REF_TRANSACTION_OPEN)\n+\t\tdie(\"BUG: create on transaction that is not open\");\n \n \tupdate = add_update(transaction, refname);\n \n@@ -3382,7 +3400,10 @@ int ref_transaction_delete(struct ref_transaction *transaction,\n \tstruct ref_update *update;\n \n \tif (have_old && !old_sha1)\n-\t\tdie(\"have_old is true but old_sha1 is NULL\");\n+\t\tdie(\"BUG: have_old is true but old_sha1 is NULL\");\n+\n+\tif (transaction->status != REF_TRANSACTION_OPEN)\n+\t\tdie(\"BUG: delete on transaction that is not open\");\n \n \tupdate = add_update(transaction, refname);\n \tupdate->flags = flags;\n@@ -3454,8 +3475,13 @@ int ref_transaction_commit(struct ref_transaction *transaction,\n \tint n = transaction->nr;\n \tstruct ref_update **updates = transaction->updates;\n \n-\tif (!n)\n+\tif (transaction->status != REF_TRANSACTION_OPEN)\n+\t\tdie(\"BUG: commit on transaction that is not open\");\n+\n+\tif (!n) {\n+\t\ttransaction->status = REF_TRANSACTION_CLOSED;\n \t\treturn 0;\n+\t}\n \n \t/* Allocate work space */\n \tdelnames = xmalloc(sizeof(*delnames) * n);\n@@ -3517,6 +3543,9 @@ int ref_transaction_commit(struct ref_transaction *transaction,\n \tclear_loose_ref_cache(&ref_cache);\n \n cleanup:\n+\ttransaction->status = ret ? REF_TRANSACTION_ERROR\n+\t  : REF_TRANSACTION_CLOSED;\n+\n \tfor (i = 0; i < n; i++)\n \t\tif (updates[i]->lock)\n \t\t\tunlock_ref(updates[i]->lock);\n-- \n2.0.0.rc1.351.g4d2c8e4\n"},{"id":"240444","messageId":"1398976662-6962-30-git-send-email-sahlberg@google.com","threadId":"36563","inReplyTo":"1398976662-6962-1-git-send-email-sahlberg@google.com","subject":"[PATCH v6 29/42] refs.c: remove the update_ref_lock function","fromName":"Ronnie Sahlberg","fromEmail":"sahlberg@google.com","sentAt":"2014-05-01T20:37:29Z","receivedAt":"2014-05-01T20:37:29Z","isPatch":true,"sender":{"key":"sahlberg@google.com","avatar":"https://avatars.githubusercontent.com/u/7320636?v=4"},"body":"Since we now only call update_ref_lock with onerr==QUIET_ON_ERR we no longer\nneed this function and can replace it with just calling lock_any_ref_for_update\ndirectly.\n\nSigned-off-by: Ronnie Sahlberg <sahlberg@google.com>\n---\n refs.c | 30 ++++++------------------------\n 1 file changed, 6 insertions(+), 24 deletions(-)\n\ndiff --git a/refs.c b/refs.c\nindex 206e1a2..9f5435d 100644\n--- a/refs.c\n+++ b/refs.c\n@@ -3235,24 +3235,6 @@ int for_each_reflog(each_ref_fn fn, void *cb_data)\n \treturn retval;\n }\n \n-static struct ref_lock *update_ref_lock(const char *refname,\n-\t\t\t\t\tconst unsigned char *oldval,\n-\t\t\t\t\tint flags, int *type_p,\n-\t\t\t\t\tenum action_on_err onerr)\n-{\n-\tstruct ref_lock *lock;\n-\tlock = lock_any_ref_for_update(refname, oldval, flags, type_p);\n-\tif (!lock) {\n-\t\tconst char *str = \"Cannot lock the ref '%s'.\";\n-\t\tswitch (onerr) {\n-\t\tcase UPDATE_REFS_MSG_ON_ERR: error(str, refname); break;\n-\t\tcase UPDATE_REFS_DIE_ON_ERR: die(str, refname); break;\n-\t\tcase UPDATE_REFS_QUIET_ON_ERR: break;\n-\t\t}\n-\t}\n-\treturn lock;\n-}\n-\n static int update_ref_write(const char *action, const char *refname,\n \t\t\t    const unsigned char *sha1, struct ref_lock *lock,\n \t\t\t    struct strbuf *err, enum action_on_err onerr)\n@@ -3496,12 +3478,12 @@ int ref_transaction_commit(struct ref_transaction *transaction,\n \tfor (i = 0; i < n; i++) {\n \t\tstruct ref_update *update = updates[i];\n \n-\t\tupdate->lock = update_ref_lock(update->refname,\n-\t\t\t\t\t       (update->have_old ?\n-\t\t\t\t\t\tupdate->old_sha1 : NULL),\n-\t\t\t\t\t       update->flags,\n-\t\t\t\t\t       &update->type,\n-\t\t\t\t\t       UPDATE_REFS_QUIET_ON_ERR);\n+\t\tupdate->lock = lock_any_ref_for_update(update->refname,\n+\t\t\t\t\t\t       (update->have_old ?\n+\t\t\t\t\t\t\tupdate->old_sha1 :\n+\t\t\t\t\t\t\tNULL),\n+\t\t\t\t\t\t       update->flags,\n+\t\t\t\t\t\t       &update->type);\n \t\tif (!update->lock) {\n \t\t\tif (err)\n \t\t\t\tstrbuf_addf(err ,\"Cannot lock the ref '%s'.\",\n-- \n2.0.0.rc1.351.g4d2c8e4\n"},{"id":"240448","messageId":"1398976662-6962-31-git-send-email-sahlberg@google.com","threadId":"36563","inReplyTo":"1398976662-6962-1-git-send-email-sahlberg@google.com","subject":"[PATCH v6 30/42] refs.c: remove the update_ref_write function","fromName":"Ronnie Sahlberg","fromEmail":"sahlberg@google.com","sentAt":"2014-05-01T20:37:30Z","receivedAt":"2014-05-01T20:37:30Z","isPatch":true,"sender":{"key":"sahlberg@google.com","avatar":"https://avatars.githubusercontent.com/u/7320636?v=4"},"body":"Since we only call update_ref_write from a single place and we only call it\nwith onerr==QUIET_ON_ERR we can just as well get rid of it and just call\nwrite_ref_sha1 directly.\n\nSigned-off-by: Ronnie Sahlberg <sahlberg@google.com>\n---\n refs.c | 35 +++++++++--------------------------\n 1 file changed, 9 insertions(+), 26 deletions(-)\n\ndiff --git a/refs.c b/refs.c\nindex 9f5435d..875178b 100644\n--- a/refs.c\n+++ b/refs.c\n@@ -3235,25 +3235,6 @@ int for_each_reflog(each_ref_fn fn, void *cb_data)\n \treturn retval;\n }\n \n-static int update_ref_write(const char *action, const char *refname,\n-\t\t\t    const unsigned char *sha1, struct ref_lock *lock,\n-\t\t\t    struct strbuf *err, enum action_on_err onerr)\n-{\n-\tif (write_ref_sha1(lock, sha1, action) < 0) {\n-\t\tconst char *str = \"Cannot update the ref '%s'.\";\n-\t\tif (err)\n-\t\t\tstrbuf_addf(err, str, refname);\n-\n-\t\tswitch (onerr) {\n-\t\tcase UPDATE_REFS_MSG_ON_ERR: error(str, refname); break;\n-\t\tcase UPDATE_REFS_DIE_ON_ERR: die(str, refname); break;\n-\t\tcase UPDATE_REFS_QUIET_ON_ERR: break;\n-\t\t}\n-\t\treturn 1;\n-\t}\n-\treturn 0;\n-}\n-\n /**\n  * Information needed for a single ref update.  Set new_sha1 to the\n  * new value or to zero to delete the ref.  To check the old value\n@@ -3498,14 +3479,16 @@ int ref_transaction_commit(struct ref_transaction *transaction,\n \t\tstruct ref_update *update = updates[i];\n \n \t\tif (!is_null_sha1(update->new_sha1)) {\n-\t\t\tret = update_ref_write(msg,\n-\t\t\t\t\t       update->refname,\n-\t\t\t\t\t       update->new_sha1,\n-\t\t\t\t\t       update->lock, err,\n-\t\t\t\t\t       UPDATE_REFS_QUIET_ON_ERR);\n-\t\t\tupdate->lock = NULL; /* freed by update_ref_write */\n-\t\t\tif (ret)\n+\t\t\tret = write_ref_sha1(update->lock, update->new_sha1,\n+\t\t\t\t\t     msg);\n+\t\t\tupdate->lock = NULL; /* freed by write_ref_sha1 */\n+\t\t\tif (ret) {\n+\t\t\t\tconst char *str = \"Cannot update the ref '%s'.\";\n+\n+\t\t\t\tif (err)\n+\t\t\t\t\tstrbuf_addf(err, str, update->refname);\n \t\t\t\tgoto cleanup;\n+\t\t\t}\n \t\t}\n \t}\n \n-- \n2.0.0.rc1.351.g4d2c8e4\n"},{"id":"240451","messageId":"1398976662-6962-32-git-send-email-sahlberg@google.com","threadId":"36563","inReplyTo":"1398976662-6962-1-git-send-email-sahlberg@google.com","subject":"[PATCH v6 31/42] refs.c: remove lock_ref_sha1","fromName":"Ronnie Sahlberg","fromEmail":"sahlberg@google.com","sentAt":"2014-05-01T20:37:31Z","receivedAt":"2014-05-01T20:37:31Z","isPatch":true,"sender":{"key":"sahlberg@google.com","avatar":"https://avatars.githubusercontent.com/u/7320636?v=4"},"body":"lock_ref_sha1 was only called from one place in refc.c and only provided\na check that the refname was sane before adding back the initial \"refs/\"\npart of the ref path name, the initial \"refs/\" that this caller had already\nstripped off before calling lock_ref_sha1.\n\nSigned-off-by: Ronnie Sahlberg <sahlberg@google.com>\n---\n refs.c | 15 +++++----------\n 1 file changed, 5 insertions(+), 10 deletions(-)\n\ndiff --git a/refs.c b/refs.c\nindex 875178b..a55cc38 100644\n--- a/refs.c\n+++ b/refs.c\n@@ -2130,15 +2130,6 @@ static struct ref_lock *lock_ref_sha1_basic(const char *refname,\n \treturn NULL;\n }\n \n-static struct ref_lock *lock_ref_sha1(const char *refname, const unsigned char *old_sha1)\n-{\n-\tchar refpath[PATH_MAX];\n-\tif (check_refname_format(refname, 0))\n-\t\treturn NULL;\n-\tstrcpy(refpath, mkpath(\"refs/%s\", refname));\n-\treturn lock_ref_sha1_basic(refpath, old_sha1, 0, NULL);\n-}\n-\n struct ref_lock *lock_any_ref_for_update(const char *refname,\n \t\t\t\t\t const unsigned char *old_sha1,\n \t\t\t\t\t int flags, int *type_p)\n@@ -2339,8 +2330,12 @@ static void try_remove_empty_parents(char *name)\n /* make sure nobody touched the ref, and unlink */\n static void prune_ref(struct ref_to_prune *r)\n {\n-\tstruct ref_lock *lock = lock_ref_sha1(r->name + 5, r->sha1);\n+\tstruct ref_lock *lock;\n+\n+\tif (check_refname_format(r->name + 5, 0))\n+\t\treturn;\n \n+\tlock = lock_ref_sha1_basic(r->name, r->sha1, 0, NULL);\n \tif (lock) {\n \t\tunlink_or_warn(git_path(\"%s\", r->name));\n \t\tunlock_ref(lock);\n-- \n2.0.0.rc1.351.g4d2c8e4\n"},{"id":"240443","messageId":"1398976662-6962-33-git-send-email-sahlberg@google.com","threadId":"36563","inReplyTo":"1398976662-6962-1-git-send-email-sahlberg@google.com","subject":"[PATCH v6 32/42] refs.c: make prune_ref use a transaction to delete the ref","fromName":"Ronnie Sahlberg","fromEmail":"sahlberg@google.com","sentAt":"2014-05-01T20:37:32Z","receivedAt":"2014-05-01T20:37:32Z","isPatch":true,"sender":{"key":"sahlberg@google.com","avatar":"https://avatars.githubusercontent.com/u/7320636?v=4"},"body":"Change prune_ref to delete the ref using a ref transaction. To do this we also\nneed to add a new flag REF_ISPRUNING that will tell the transaction that we\ndo not want to delete this ref from the packed refs.\n\nSigned-off-by: Ronnie Sahlberg <sahlberg@google.com>\n---\n refs.c | 22 +++++++++++++++-------\n refs.h |  2 ++\n 2 files changed, 17 insertions(+), 7 deletions(-)\n\ndiff --git a/refs.c b/refs.c\nindex a55cc38..2b82dce 100644\n--- a/refs.c\n+++ b/refs.c\n@@ -2330,17 +2330,24 @@ static void try_remove_empty_parents(char *name)\n /* make sure nobody touched the ref, and unlink */\n static void prune_ref(struct ref_to_prune *r)\n {\n-\tstruct ref_lock *lock;\n+\tstruct ref_transaction *transaction;\n+\tstruct strbuf err = STRBUF_INIT;\n \n \tif (check_refname_format(r->name + 5, 0))\n \t\treturn;\n \n-\tlock = lock_ref_sha1_basic(r->name, r->sha1, 0, NULL);\n-\tif (lock) {\n-\t\tunlink_or_warn(git_path(\"%s\", r->name));\n-\t\tunlock_ref(lock);\n-\t\ttry_remove_empty_parents(r->name);\n+\ttransaction = ref_transaction_begin();\n+\tif (!transaction ||\n+\t    ref_transaction_delete(transaction, r->name, r->sha1,\n+\t\t\t\t   REF_ISPRUNING, 1) ||\n+\t    ref_transaction_commit(transaction, NULL, &err)) {\n+\t\tref_transaction_rollback(transaction);\n+\t\twarning(\"prune_ref: %s\", err.buf);\n+\t\tstrbuf_release(&err);\n+\t\treturn;\n \t}\n+\tref_transaction_free(transaction);\n+\ttry_remove_empty_parents(r->name);\n }\n \n static void prune_refs(struct ref_to_prune *r)\n@@ -3492,8 +3499,9 @@ int ref_transaction_commit(struct ref_transaction *transaction,\n \t\tstruct ref_update *update = updates[i];\n \n \t\tif (update->lock) {\n-\t\t\tdelnames[delnum++] = update->lock->ref_name;\n \t\t\tret |= delete_ref_loose(update->lock, update->type);\n+\t\t\tif (!(update->flags & REF_ISPRUNING))\n+\t\t\t\tdelnames[delnum++] = update->lock->ref_name;\n \t\t}\n \t}\n \ndiff --git a/refs.h b/refs.h\nindex b14fe86..340e11a 100644\n--- a/refs.h\n+++ b/refs.h\n@@ -134,6 +134,8 @@ extern int peel_ref(const char *refname, unsigned char *sha1);\n \n /** Locks any ref (for 'HEAD' type refs). */\n #define REF_NODEREF\t0x01\n+/** Deleting a loose ref during prune */\n+#define REF_ISPRUNING\t0x02\n extern struct ref_lock *lock_any_ref_for_update(const char *refname,\n \t\t\t\t\t\tconst unsigned char *old_sha1,\n \t\t\t\t\t\tint flags, int *type_p);\n-- \n2.0.0.rc1.351.g4d2c8e4\n"},{"id":"240450","messageId":"1398976662-6962-34-git-send-email-sahlberg@google.com","threadId":"36563","inReplyTo":"1398976662-6962-1-git-send-email-sahlberg@google.com","subject":"[PATCH v6 33/42] refs.c: make delete_ref use a transaction","fromName":"Ronnie Sahlberg","fromEmail":"sahlberg@google.com","sentAt":"2014-05-01T20:37:33Z","receivedAt":"2014-05-01T20:37:33Z","isPatch":true,"sender":{"key":"sahlberg@google.com","avatar":"https://avatars.githubusercontent.com/u/7320636?v=4"},"body":"Change delete_ref to use a ref transaction for the deletion. At the same time\nsince we no longer have any callers of repack_without_ref we can now delete\nthis function.\n\nSigned-off-by: Ronnie Sahlberg <sahlberg@google.com>\n---\n refs.c | 31 ++++++++++---------------------\n 1 file changed, 10 insertions(+), 21 deletions(-)\n\ndiff --git a/refs.c b/refs.c\nindex 2b82dce..96b03e5 100644\n--- a/refs.c\n+++ b/refs.c\n@@ -2481,11 +2481,6 @@ static int repack_without_refs(const char **refnames, int n)\n \treturn commit_packed_refs();\n }\n \n-static int repack_without_ref(const char *refname)\n-{\n-\treturn repack_without_refs(&refname, 1);\n-}\n-\n static int delete_ref_loose(struct ref_lock *lock, int flag)\n {\n \tif (!(flag & REF_ISPACKED) || flag & REF_ISSYMREF) {\n@@ -2503,24 +2498,18 @@ static int delete_ref_loose(struct ref_lock *lock, int flag)\n \n int delete_ref(const char *refname, const unsigned char *sha1, int delopt)\n {\n-\tstruct ref_lock *lock;\n-\tint ret = 0, flag = 0;\n+\tstruct ref_transaction *transaction;\n \n-\tlock = lock_ref_sha1_basic(refname, sha1, delopt, &flag);\n-\tif (!lock)\n+\ttransaction = ref_transaction_begin();\n+\tif (!transaction ||\n+\t    ref_transaction_delete(transaction, refname, sha1, delopt,\n+\t\t\t\t   sha1 && !is_null_sha1(sha1)) ||\n+\t    ref_transaction_commit(transaction, NULL, NULL)) {\n+\t\tref_transaction_rollback(transaction);\n \t\treturn 1;\n-\tret |= delete_ref_loose(lock, flag);\n-\n-\t/* removing the loose one could have resurrected an earlier\n-\t * packed one.  Also, if it was not loose we need to repack\n-\t * without it.\n-\t */\n-\tret |= repack_without_ref(lock->ref_name);\n-\n-\tunlink_or_warn(git_path(\"logs/%s\", lock->ref_name));\n-\tclear_loose_ref_cache(&ref_cache);\n-\tunlock_ref(lock);\n-\treturn ret;\n+\t}\n+\tref_transaction_free(transaction);\n+\treturn 0;\n }\n \n /*\n-- \n2.0.0.rc1.351.g4d2c8e4\n"},{"id":"240442","messageId":"1398976662-6962-35-git-send-email-sahlberg@google.com","threadId":"36563","inReplyTo":"1398976662-6962-1-git-send-email-sahlberg@google.com","subject":"[PATCH v6 34/42] refs.c: pass the ref log message to _create/delete/update instead of _commit","fromName":"Ronnie Sahlberg","fromEmail":"sahlberg@google.com","sentAt":"2014-05-01T20:37:34Z","receivedAt":"2014-05-01T20:37:34Z","isPatch":true,"sender":{"key":"sahlberg@google.com","avatar":"https://avatars.githubusercontent.com/u/7320636?v=4"},"body":"Change the reference transactions so that we pass the reflog message\nthrough to the create/delete/update function instead of the commit message.\nThis allows for individual messages for each change in a multi ref\ntransaction.\n\nSigned-off-by: Ronnie Sahlberg <sahlberg@google.com>\n---\n branch.c               |  6 +++---\n builtin/commit.c       |  4 ++--\n builtin/fetch.c        | 10 ++++++++--\n builtin/receive-pack.c |  4 ++--\n builtin/replace.c      |  4 ++--\n builtin/tag.c          |  4 ++--\n builtin/update-ref.c   | 13 +++++++------\n fast-import.c          |  8 ++++----\n refs.c                 | 34 +++++++++++++++++++++-------------\n refs.h                 |  8 ++++----\n sequencer.c            |  4 ++--\n walker.c               |  6 +++---\n 12 files changed, 60 insertions(+), 45 deletions(-)\n\ndiff --git a/branch.c b/branch.c\nindex 8e58908..74d55e7 100644\n--- a/branch.c\n+++ b/branch.c\n@@ -300,9 +300,9 @@ void create_branch(const char *head,\n \n \t\ttransaction = ref_transaction_begin();\n \t\tif (!transaction ||\n-\t\t\tref_transaction_update(transaction, ref.buf, sha1,\n-\t\t\t\t\t       null_sha1, 0, !forcing) ||\n-\t\t\tref_transaction_commit(transaction, msg, &err))\n+\t\t    ref_transaction_update(transaction, ref.buf, sha1,\n+\t\t\t\t\t   null_sha1, 0, !forcing, msg) ||\n+\t\t    ref_transaction_commit(transaction, &err))\n \t\t\t\tdie_errno(_(\"%s: failed to write ref: %s\"),\n \t\t\t\t\t  ref.buf, err.buf);\n \t\tref_transaction_free(transaction);\ndiff --git a/builtin/commit.c b/builtin/commit.c\nindex 5bdcf9e..0f4e1fc 100644\n--- a/builtin/commit.c\n+++ b/builtin/commit.c\n@@ -1681,8 +1681,8 @@ int cmd_commit(int argc, const char **argv, const char *prefix)\n \t    ref_transaction_update(transaction, \"HEAD\", sha1,\n \t\t\t\t   current_head ?\n \t\t\t\t   current_head->object.sha1 : NULL,\n-\t\t\t\t   0, !!current_head) ||\n-\t    ref_transaction_commit(transaction, sb.buf, &err)) {\n+\t\t\t\t   0, !!current_head, sb.buf) ||\n+\t    ref_transaction_commit(transaction, &err)) {\n \t\trollback_index_files();\n \t\tdie(_(\"HEAD: cannot update ref: %s\"), err.buf);\n \t}\ndiff --git a/builtin/fetch.c b/builtin/fetch.c\nindex 5dbd0f0..3a849b0 100644\n--- a/builtin/fetch.c\n+++ b/builtin/fetch.c\n@@ -374,11 +374,17 @@ static int s_update_ref(const char *action,\n \t\t\tstruct ref *ref,\n \t\t\tint check_old)\n {\n+\tchar msg[1024];\n+\tchar *rla = getenv(\"GIT_REFLOG_ACTION\");\n+\n \tif (dry_run)\n \t\treturn 0;\n+\tif (!rla)\n+\t\trla = default_rla.buf;\n+\tsnprintf(msg, sizeof(msg), \"%s: %s\", rla, action);\n \n \tif (ref_transaction_update(transaction, ref->name, ref->new_sha1,\n-\t\t\t       ref->old_sha1, 0, check_old))\n+\t\t\t\t   ref->old_sha1, 0, check_old, msg))\n \t\treturn STORE_REF_ERROR_OTHER;\n \n \treturn 0;\n@@ -670,7 +676,7 @@ static int store_updated_refs(const char *raw_url, const char *remote_name,\n \t\t\t}\n \t\t}\n \t}\n-\tif (ref_transaction_commit(transaction, \"fetch_ref transaction\", NULL))\n+\tif (ref_transaction_commit(transaction, NULL))\n \t\trc |= errno == ENOTDIR ? STORE_REF_ERROR_DF_CONFLICT :\n \t\t  STORE_REF_ERROR_OTHER;\n \tref_transaction_free(transaction);\ndiff --git a/builtin/receive-pack.c b/builtin/receive-pack.c\nindex d580176..991c659 100644\n--- a/builtin/receive-pack.c\n+++ b/builtin/receive-pack.c\n@@ -582,7 +582,7 @@ static const char *update(struct command *cmd, struct shallow_info *si)\n \t\t\treturn \"shallow error\";\n \n \t\tif (ref_transaction_update(transaction, namespaced_name,\n-\t\t\t\t\t   new_sha1, old_sha1, 0, 1))\n+\t\t\t\t\t   new_sha1, old_sha1, 0, 1, \"push\"))\n \t\t\treturn \"failed to update\";\n \t\treturn NULL; /* good */\n \t}\n@@ -823,7 +823,7 @@ static void execute_commands(struct command *commands,\n \t\t\tchecked_connectivity = 0;\n \t\t}\n \t}\n-\tif (ref_transaction_commit(transaction, \"push\", &err))\n+\tif (ref_transaction_commit(transaction, &err))\n \t\terror(\"%s\", err.buf);\n \tref_transaction_free(transaction);\n \tstrbuf_release(&err);\ndiff --git a/builtin/replace.c b/builtin/replace.c\nindex 5999f7d..47c360c 100644\n--- a/builtin/replace.c\n+++ b/builtin/replace.c\n@@ -161,8 +161,8 @@ static int replace_object(const char *object_ref, const char *replace_ref,\n \ttransaction = ref_transaction_begin();\n \tif (!transaction ||\n \t    ref_transaction_update(transaction, ref, repl, prev,\n-\t\t\t\t   0, !is_null_sha1(prev)) ||\n-\t    ref_transaction_commit(transaction, NULL, &err))\n+\t\t\t\t   0, !is_null_sha1(prev), NULL) ||\n+\t    ref_transaction_commit(transaction, &err))\n \t\tdie(_(\"%s: failed to replace ref: %s\"), ref, err.buf);\n \n \tref_transaction_free(transaction);\ndiff --git a/builtin/tag.c b/builtin/tag.c\nindex d59154a..c039355 100644\n--- a/builtin/tag.c\n+++ b/builtin/tag.c\n@@ -705,8 +705,8 @@ int cmd_tag(int argc, const char **argv, const char *prefix)\n \ttransaction = ref_transaction_begin();\n \tif (!transaction ||\n \t    ref_transaction_update(transaction, ref.buf, object, prev,\n-\t\t\t\t   0, !is_null_sha1(prev)) ||\n-\t    ref_transaction_commit(transaction, NULL, &err))\n+\t\t\t\t   0, !is_null_sha1(prev), NULL) ||\n+\t    ref_transaction_commit(transaction, &err))\n \t\tdie(_(\"%s: cannot update the ref: %s\"), ref.buf, err.buf);\n \tref_transaction_free(transaction);\n \tif (force && !is_null_sha1(prev) && hashcmp(prev, object))\ndiff --git a/builtin/update-ref.c b/builtin/update-ref.c\nindex 7fe9c11..c5aff92 100644\n--- a/builtin/update-ref.c\n+++ b/builtin/update-ref.c\n@@ -16,6 +16,7 @@ static struct ref_transaction *transaction;\n \n static char line_termination = '\\n';\n static int update_flags;\n+static const char *msg;\n \n /*\n  * Parse one whitespace- or NUL-terminated, possibly C-quoted argument\n@@ -198,7 +199,7 @@ static const char *parse_cmd_update(struct strbuf *input, const char *next)\n \t\tdie(\"update %s: extra input: %s\", refname, next);\n \n \tif (ref_transaction_update(transaction, refname, new_sha1, old_sha1,\n-\t\t\t\t   update_flags, have_old))\n+\t\t\t\t   update_flags, have_old, msg))\n \t\tdie(\"update %s: failed\", refname);\n \n \tupdate_flags = 0;\n@@ -226,7 +227,7 @@ static const char *parse_cmd_create(struct strbuf *input, const char *next)\n \t\tdie(\"create %s: extra input: %s\", refname, next);\n \n \tif (ref_transaction_create(transaction, refname, new_sha1,\n-\t\t\t\t   update_flags))\n+\t\t\t\t   update_flags, msg))\n \t\tdie(\"failed transaction create for %s\", refname);\n \n \tupdate_flags = 0;\n@@ -258,7 +259,7 @@ static const char *parse_cmd_delete(struct strbuf *input, const char *next)\n \t\tdie(\"delete %s: extra input: %s\", refname, next);\n \n \tif (ref_transaction_delete(transaction, refname, old_sha1,\n-\t\t\t\t   update_flags, have_old))\n+\t\t\t\t   update_flags, have_old, msg))\n \t\tdie(\"failed transaction delete for %s\", refname);\n \n \tupdate_flags = 0;\n@@ -291,7 +292,7 @@ static const char *parse_cmd_verify(struct strbuf *input, const char *next)\n \t\tdie(\"verify %s: extra input: %s\", refname, next);\n \n \tif (ref_transaction_update(transaction, refname, new_sha1, old_sha1,\n-\t\t\t\t   update_flags, have_old))\n+\t\t\t\t   update_flags, have_old, msg))\n \t\tdie(\"failed transaction update for %s\", refname);\n \n \tupdate_flags = 0;\n@@ -344,7 +345,7 @@ static void update_refs_stdin(void)\n \n int cmd_update_ref(int argc, const char **argv, const char *prefix)\n {\n-\tconst char *refname, *oldval, *msg = NULL;\n+\tconst char *refname, *oldval;\n \tunsigned char sha1[20], oldsha1[20];\n \tint delete = 0, no_deref = 0, read_stdin = 0, end_null = 0, flags = 0;\n \tstruct strbuf err = STRBUF_INIT;\n@@ -371,7 +372,7 @@ int cmd_update_ref(int argc, const char **argv, const char *prefix)\n \t\tif (end_null)\n \t\t\tline_termination = '\\0';\n \t\tupdate_refs_stdin();\n-\t\tif (ref_transaction_commit(transaction, msg, &err))\n+\t\tif (ref_transaction_commit(transaction, &err))\n \t\t\tdie(\"%s\", err.buf);\n \t\tref_transaction_free(transaction);\n \t\treturn 0;\ndiff --git a/fast-import.c b/fast-import.c\nindex 5587cf6..099e71b 100644\n--- a/fast-import.c\n+++ b/fast-import.c\n@@ -1710,8 +1710,8 @@ static int update_branch(struct branch *b)\n \ttransaction = ref_transaction_begin();\n \tif (!transaction ||\n \t    ref_transaction_update(transaction, b->name, b->sha1, old_sha1,\n-\t\t\t\t   0, 1) ||\n-\t    ref_transaction_commit(transaction, msg, &err)) {\n+\t\t\t\t   0, 1, msg) ||\n+\t    ref_transaction_commit(transaction, &err)) {\n \t\tref_transaction_rollback(transaction);\n \t\terror(\"Unable to update branch %s: %s\", b->name, err.buf);\n \t\tstrbuf_release(&err);\n@@ -1745,10 +1745,10 @@ static void dump_tags(void)\n \t\tsprintf(ref_name, \"refs/tags/%s\", t->name);\n \n \t\tif (ref_transaction_update(transaction, ref_name, t->sha1,\n-\t\t\t\t\t   NULL, 0, 0))\n+\t\t\t\t\t   NULL, 0, 0, msg))\n \t\t\tfailure |= error(\"Unable to update %s\", err.buf);\n \t}\n-\tif (failure || ref_transaction_commit(transaction, msg, &err))\n+\tif (failure || ref_transaction_commit(transaction, &err))\n \t\tfailure |= error(\"Unable to update %s\", err.buf);\n \tref_transaction_free(transaction);\n \tstrbuf_release(&err);\ndiff --git a/refs.c b/refs.c\nindex 96b03e5..7e58693 100644\n--- a/refs.c\n+++ b/refs.c\n@@ -2339,8 +2339,8 @@ static void prune_ref(struct ref_to_prune *r)\n \ttransaction = ref_transaction_begin();\n \tif (!transaction ||\n \t    ref_transaction_delete(transaction, r->name, r->sha1,\n-\t\t\t\t   REF_ISPRUNING, 1) ||\n-\t    ref_transaction_commit(transaction, NULL, &err)) {\n+\t\t\t\t   REF_ISPRUNING, 1, NULL) ||\n+\t    ref_transaction_commit(transaction, &err)) {\n \t\tref_transaction_rollback(transaction);\n \t\twarning(\"prune_ref: %s\", err.buf);\n \t\tstrbuf_release(&err);\n@@ -2503,8 +2503,8 @@ int delete_ref(const char *refname, const unsigned char *sha1, int delopt)\n \ttransaction = ref_transaction_begin();\n \tif (!transaction ||\n \t    ref_transaction_delete(transaction, refname, sha1, delopt,\n-\t\t\t\t   sha1 && !is_null_sha1(sha1)) ||\n-\t    ref_transaction_commit(transaction, NULL, NULL)) {\n+\t\t\t\t   sha1 && !is_null_sha1(sha1), NULL) ||\n+\t    ref_transaction_commit(transaction, NULL)) {\n \t\tref_transaction_rollback(transaction);\n \t\treturn 1;\n \t}\n@@ -3239,6 +3239,7 @@ struct ref_update {\n \tint have_old; /* 1 if old_sha1 is valid, 0 otherwise */\n \tstruct ref_lock *lock;\n \tint type;\n+\tconst char *msg;\n \tconst char refname[FLEX_ARRAY];\n };\n \n@@ -3272,9 +3273,10 @@ void ref_transaction_free(struct ref_transaction *transaction)\n \tif (!transaction)\n \t\treturn;\n \n-\tfor (i = 0; i < transaction->nr; i++)\n+\tfor (i = 0; i < transaction->nr; i++) {\n+\t  free((char *)transaction->updates[i]->msg);\n \t\tfree(transaction->updates[i]);\n-\n+\t}\n \tfree(transaction->updates);\n \tfree(transaction);\n }\n@@ -3305,7 +3307,7 @@ int ref_transaction_update(struct ref_transaction *transaction,\n \t\t\t    const char *refname,\n \t\t\t    const unsigned char *new_sha1,\n \t\t\t    const unsigned char *old_sha1,\n-\t\t\t    int flags, int have_old)\n+\t\t\t   int flags, int have_old, const char *msg)\n {\n \tstruct ref_update *update;\n \n@@ -3321,13 +3323,15 @@ int ref_transaction_update(struct ref_transaction *transaction,\n \tupdate->have_old = have_old;\n \tif (have_old)\n \t\thashcpy(update->old_sha1, old_sha1);\n+\tif (msg)\n+\t\tupdate->msg = xstrdup(msg);\n \treturn 0;\n }\n \n int ref_transaction_create(struct ref_transaction *transaction,\n \t\t\t   const char *refname,\n \t\t\t   const unsigned char *new_sha1,\n-\t\t\t   int flags)\n+\t\t\t   int flags, const char *msg)\n {\n \tstruct ref_update *update;\n \n@@ -3343,13 +3347,15 @@ int ref_transaction_create(struct ref_transaction *transaction,\n \thashclr(update->old_sha1);\n \tupdate->flags = flags;\n \tupdate->have_old = 1;\n+\tif (msg)\n+\t\tupdate->msg = xstrdup(msg);\n \treturn 0;\n }\n \n int ref_transaction_delete(struct ref_transaction *transaction,\n \t\t\t   const char *refname,\n \t\t\t   const unsigned char *old_sha1,\n-\t\t\t   int flags, int have_old)\n+\t\t\t   int flags, int have_old, const char *msg)\n {\n \tstruct ref_update *update;\n \n@@ -3366,6 +3372,8 @@ int ref_transaction_delete(struct ref_transaction *transaction,\n \t\tassert(!is_null_sha1(old_sha1));\n \t\thashcpy(update->old_sha1, old_sha1);\n \t}\n+\tif (msg)\n+\t\tupdate->msg = xstrdup(msg);\n \treturn 0;\n }\n \n@@ -3379,8 +3387,8 @@ int update_ref(const char *action, const char *refname,\n \tt = ref_transaction_begin();\n \tif (!t ||\n \t    ref_transaction_update(t, refname, sha1, oldval, flags,\n-\t\t\t\t   !!oldval) ||\n-\t    ref_transaction_commit(t, action, &err)) {\n+\t\t\t\t   !!oldval, action) ||\n+\t    ref_transaction_commit(t, &err)) {\n \t\tconst char *str = \"update_ref failed for ref '%s': %s\";\n \n \t\tref_transaction_rollback(t);\n@@ -3422,7 +3430,7 @@ static int ref_update_reject_duplicates(struct ref_update **updates, int n,\n }\n \n int ref_transaction_commit(struct ref_transaction *transaction,\n-\t\t\t   const char *msg, struct strbuf *err)\n+\t\t\t   struct strbuf *err)\n {\n \tint ret = 0, delnum = 0, i;\n \tconst char **delnames;\n@@ -3471,7 +3479,7 @@ int ref_transaction_commit(struct ref_transaction *transaction,\n \n \t\tif (!is_null_sha1(update->new_sha1)) {\n \t\t\tret = write_ref_sha1(update->lock, update->new_sha1,\n-\t\t\t\t\t     msg);\n+\t\t\t\t\t     update->msg);\n \t\t\tupdate->lock = NULL; /* freed by write_ref_sha1 */\n \t\t\tif (ret) {\n \t\t\t\tconst char *str = \"Cannot update the ref '%s'.\";\ndiff --git a/refs.h b/refs.h\nindex 340e11a..7a89415 100644\n--- a/refs.h\n+++ b/refs.h\n@@ -237,7 +237,7 @@ int ref_transaction_update(struct ref_transaction *transaction,\n \t\t\t   const char *refname,\n \t\t\t   const unsigned char *new_sha1,\n \t\t\t   const unsigned char *old_sha1,\n-\t\t\t   int flags, int have_old);\n+\t\t\t   int flags, int have_old, const char *msg);\n \n /*\n  * Add a reference creation to transaction.  new_sha1 is the value\n@@ -248,7 +248,7 @@ int ref_transaction_update(struct ref_transaction *transaction,\n int ref_transaction_create(struct ref_transaction *transaction,\n \t\t\t   const char *refname,\n \t\t\t   const unsigned char *new_sha1,\n-\t\t\t   int flags);\n+\t\t\t   int flags, const char *msg);\n \n /*\n  * Add a reference deletion to transaction.  If have_old is true, then\n@@ -258,7 +258,7 @@ int ref_transaction_create(struct ref_transaction *transaction,\n int ref_transaction_delete(struct ref_transaction *transaction,\n \t\t\t   const char *refname,\n \t\t\t   const unsigned char *old_sha1,\n-\t\t\t   int flags, int have_old);\n+\t\t\t   int flags, int have_old, const char *msg);\n \n /*\n  * Commit all of the changes that have been queued in transaction, as\n@@ -267,7 +267,7 @@ int ref_transaction_delete(struct ref_transaction *transaction,\n  * why the transaction failed.\n  */\n int ref_transaction_commit(struct ref_transaction *transaction,\n-\t\t\t   const char *msg, struct strbuf *err);\n+\t\t\t   struct strbuf *err);\n \n /*\n  * Free an existing transaction.\ndiff --git a/sequencer.c b/sequencer.c\nindex 0cfdaf0..3a0ee09 100644\n--- a/sequencer.c\n+++ b/sequencer.c\n@@ -285,8 +285,8 @@ static int fast_forward_to(const unsigned char *to, const unsigned char *from,\n \ttransaction = ref_transaction_begin();\n \tif (!transaction ||\n \t    ref_transaction_update(transaction, \"HEAD\", to, from,\n-\t\t\t\t   0, !unborn) ||\n-\t    ref_transaction_commit(transaction, sb.buf, &err)) {\n+\t\t\t\t   0, !unborn, sb.buf) ||\n+\t    ref_transaction_commit(transaction, &err)) {\n \t\tref_transaction_rollback(transaction);\n \t\terror(_(\"HEAD: Could not fast-forward: %s\\n\"), err.buf);\n \t\tstrbuf_release(&sb);\ndiff --git a/walker.c b/walker.c\nindex 6044ccf..c2a1266 100644\n--- a/walker.c\n+++ b/walker.c\n@@ -291,12 +291,12 @@ int walker_fetch(struct walker *walker, int targets, char **target,\n \t\tsprintf(ref_name, \"refs/%s\", write_ref[i]);\n \t\tif (ref_transaction_update(transaction, ref_name,\n \t\t\t\t\t   &sha1[20 * i], NULL,\n-\t\t\t\t\t   0, 0))\n+\t\t\t\t\t   0, 0,\n+\t\t\t\t\t   msg ? msg : \"fetch (unknown)\"))\n \t\t\tgoto rollback_and_fail;\n \t}\n \n-\tif (ref_transaction_commit(transaction, msg ? msg : \"fetch (unknown)\",\n-\t\t\t\t   &err)) {\n+\tif (ref_transaction_commit(transaction, &err)) {\n \t\terror(\"%s\", err.buf);\n \t\tgoto rollback_and_fail;\n \t}\n-- \n2.0.0.rc1.351.g4d2c8e4\n"},{"id":"240453","messageId":"1398976662-6962-36-git-send-email-sahlberg@google.com","threadId":"36563","inReplyTo":"1398976662-6962-1-git-send-email-sahlberg@google.com","subject":"[PATCH v6 35/42] refs.c: pass NULL as *flags to read_ref_full","fromName":"Ronnie Sahlberg","fromEmail":"sahlberg@google.com","sentAt":"2014-05-01T20:37:35Z","receivedAt":"2014-05-01T20:37:35Z","isPatch":true,"sender":{"key":"sahlberg@google.com","avatar":"https://avatars.githubusercontent.com/u/7320636?v=4"},"body":"We call read_ref_full with a pointer to flags from rename_ref but since\nwe never actually use the returned flags we can just pass NULL here instead.\n\nSigned-off-by: Ronnie Sahlberg <sahlberg@google.com>\n---\n refs.c | 2 +-\n 1 file changed, 1 insertion(+), 1 deletion(-)\n\ndiff --git a/refs.c b/refs.c\nindex 7e58693..e1f919e 100644\n--- a/refs.c\n+++ b/refs.c\n@@ -2600,7 +2600,7 @@ int rename_ref(const char *oldrefname, const char *newrefname, const char *logms\n \t\tgoto rollback;\n \t}\n \n-\tif (!read_ref_full(newrefname, sha1, 1, &flag) &&\n+\tif (!read_ref_full(newrefname, sha1, 1, NULL) &&\n \t    delete_ref(newrefname, sha1, REF_NODEREF)) {\n \t\tif (errno==EISDIR) {\n \t\t\tif (remove_empty_directories(git_path(\"%s\", newrefname))) {\n-- \n2.0.0.rc1.351.g4d2c8e4\n"},{"id":"240439","messageId":"1398976662-6962-37-git-send-email-sahlberg@google.com","threadId":"36563","inReplyTo":"1398976662-6962-1-git-send-email-sahlberg@google.com","subject":"[PATCH v6 36/42] refs.c: pack all refs before we start to rename a ref","fromName":"Ronnie Sahlberg","fromEmail":"sahlberg@google.com","sentAt":"2014-05-01T20:37:36Z","receivedAt":"2014-05-01T20:37:36Z","isPatch":true,"sender":{"key":"sahlberg@google.com","avatar":"https://avatars.githubusercontent.com/u/7320636?v=4"},"body":"This means that most loose refs will no longer be present after the rename\nwhich triggered a test failure since it assumes the file for an unrelated\nref would still be present after the rename.\n\nSigned-off-by: Ronnie Sahlberg <sahlberg@google.com>\n---\n refs.c            | 3 +++\n t/t3200-branch.sh | 2 +-\n 2 files changed, 4 insertions(+), 1 deletion(-)\n\ndiff --git a/refs.c b/refs.c\nindex e1f919e..14d1573 100644\n--- a/refs.c\n+++ b/refs.c\n@@ -2595,6 +2595,9 @@ int rename_ref(const char *oldrefname, const char *newrefname, const char *logms\n \t\treturn error(\"unable to move logfile logs/%s to \"TMP_RENAMED_LOG\": %s\",\n \t\t\toldrefname, strerror(errno));\n \n+\tif (pack_refs(PACK_REFS_ALL | PACK_REFS_PRUNE))\n+\t\treturn error(\"unable to pack refs\");\n+\n \tif (delete_ref(oldrefname, orig_sha1, REF_NODEREF)) {\n \t\terror(\"unable to delete old %s\", oldrefname);\n \t\tgoto rollback;\ndiff --git a/t/t3200-branch.sh b/t/t3200-branch.sh\nindex ac31b71..fafd38a 100755\n--- a/t/t3200-branch.sh\n+++ b/t/t3200-branch.sh\n@@ -289,7 +289,7 @@ test_expect_success 'renaming a symref is not allowed' '\n \tgit symbolic-ref refs/heads/master2 refs/heads/master &&\n \ttest_must_fail git branch -m master2 master3 &&\n \tgit symbolic-ref refs/heads/master2 &&\n-\ttest_path_is_file .git/refs/heads/master &&\n+\ttest_path_is_missing .git/refs/heads/master &&\n \ttest_path_is_missing .git/refs/heads/master3\n '\n \n-- \n2.0.0.rc1.351.g4d2c8e4\n"},{"id":"240440","messageId":"1398976662-6962-38-git-send-email-sahlberg@google.com","threadId":"36563","inReplyTo":"1398976662-6962-1-git-send-email-sahlberg@google.com","subject":"[PATCH v6 37/42] refs.c: move the check for valid refname to lock_ref_sha1_basic","fromName":"Ronnie Sahlberg","fromEmail":"sahlberg@google.com","sentAt":"2014-05-01T20:37:37Z","receivedAt":"2014-05-01T20:37:37Z","isPatch":true,"sender":{"key":"sahlberg@google.com","avatar":"https://avatars.githubusercontent.com/u/7320636?v=4"},"body":"Move the check for check_refname_format from lock_any_ref_for_update\nto lock_ref_sha1_basic. At some later stage we will get rid of\nlock_any_ref_for_update completely.\n\nThis leaves lock_any_ref_for_updates as a no-op wrapper which could be removed.\nBut this wrapper is also called from an external caller and we will soon\nmake changes to the signature to lock_ref_sha1_basic that we do not want to\nexpose to that caller.\n\nSigned-off-by: Ronnie Sahlberg <sahlberg@google.com>\n---\n refs.c | 5 +++--\n 1 file changed, 3 insertions(+), 2 deletions(-)\n\ndiff --git a/refs.c b/refs.c\nindex 14d1573..8d82742 100644\n--- a/refs.c\n+++ b/refs.c\n@@ -2043,6 +2043,9 @@ static struct ref_lock *lock_ref_sha1_basic(const char *refname,\n \tint missing = 0;\n \tint attempts_remaining = 3;\n \n+\tif (check_refname_format(refname, REFNAME_ALLOW_ONELEVEL))\n+\t\treturn NULL;\n+\n \tlock = xcalloc(1, sizeof(struct ref_lock));\n \tlock->lock_fd = -1;\n \n@@ -2134,8 +2137,6 @@ struct ref_lock *lock_any_ref_for_update(const char *refname,\n \t\t\t\t\t const unsigned char *old_sha1,\n \t\t\t\t\t int flags, int *type_p)\n {\n-\tif (check_refname_format(refname, REFNAME_ALLOW_ONELEVEL))\n-\t\treturn NULL;\n \treturn lock_ref_sha1_basic(refname, old_sha1, flags, type_p);\n }\n \n-- \n2.0.0.rc1.351.g4d2c8e4\n"},{"id":"240441","messageId":"1398976662-6962-39-git-send-email-sahlberg@google.com","threadId":"36563","inReplyTo":"1398976662-6962-1-git-send-email-sahlberg@google.com","subject":"[PATCH v6 38/42] refs.c: call lock_ref_sha1_basic directly from commit","fromName":"Ronnie Sahlberg","fromEmail":"sahlberg@google.com","sentAt":"2014-05-01T20:37:38Z","receivedAt":"2014-05-01T20:37:38Z","isPatch":true,"sender":{"key":"sahlberg@google.com","avatar":"https://avatars.githubusercontent.com/u/7320636?v=4"},"body":"Skip using the lock_any_ref_for_update wrapper and call lock_ref_sha1_basic\ndirectly from the commit function.\n\nSigned-off-by: Ronnie Sahlberg <sahlberg@google.com>\n---\n refs.c | 12 ++++++------\n 1 file changed, 6 insertions(+), 6 deletions(-)\n\ndiff --git a/refs.c b/refs.c\nindex 8d82742..51cd41e 100644\n--- a/refs.c\n+++ b/refs.c\n@@ -3462,12 +3462,12 @@ int ref_transaction_commit(struct ref_transaction *transaction,\n \tfor (i = 0; i < n; i++) {\n \t\tstruct ref_update *update = updates[i];\n \n-\t\tupdate->lock = lock_any_ref_for_update(update->refname,\n-\t\t\t\t\t\t       (update->have_old ?\n-\t\t\t\t\t\t\tupdate->old_sha1 :\n-\t\t\t\t\t\t\tNULL),\n-\t\t\t\t\t\t       update->flags,\n-\t\t\t\t\t\t       &update->type);\n+\t\tupdate->lock = lock_ref_sha1_basic(update->refname,\n+\t\t\t\t\t\t   (update->have_old ?\n+\t\t\t\t\t\t    update->old_sha1 :\n+\t\t\t\t\t\t    NULL),\n+\t\t\t\t\t\t   update->flags,\n+\t\t\t\t\t\t   &update->type);\n \t\tif (!update->lock) {\n \t\t\tif (err)\n \t\t\t\tstrbuf_addf(err ,\"Cannot lock the ref '%s'.\",\n-- \n2.0.0.rc1.351.g4d2c8e4\n"},{"id":"240438","messageId":"1398976662-6962-40-git-send-email-sahlberg@google.com","threadId":"36563","inReplyTo":"1398976662-6962-1-git-send-email-sahlberg@google.com","subject":"[PATCH v6 39/42] refs.c: add a new flag for transaction delete for refs we know are packed only","fromName":"Ronnie Sahlberg","fromEmail":"sahlberg@google.com","sentAt":"2014-05-01T20:37:39Z","receivedAt":"2014-05-01T20:37:39Z","isPatch":true,"sender":{"key":"sahlberg@google.com","avatar":"https://avatars.githubusercontent.com/u/7320636?v=4"},"body":"Add a new flag REF_ISPACKONLY that we can use in ref_transaction_delete.\nThis flag indicates that the ref does not exist as a loose ref andf only as\na packed ref. If this is the case we then change the commit code so that\nwe skip taking out a lock file and we skip calling delete_ref_loose.\nCheck for this flag and die(BUG:...) if used with _update or _create.\n\nAt the start of the transaction, before we even start locking any refs,\nwe add all such REF_ISPACKONLY refs to delnames so that we have a list of\nall pack only refs that we will be deleting during this transaction.\n\nSigned-off-by: Ronnie Sahlberg <sahlberg@google.com>\n---\n refs.c | 19 +++++++++++++++++++\n refs.h |  2 ++\n 2 files changed, 21 insertions(+)\n\ndiff --git a/refs.c b/refs.c\nindex 51cd41e..b525076 100644\n--- a/refs.c\n+++ b/refs.c\n@@ -3321,6 +3321,9 @@ int ref_transaction_update(struct ref_transaction *transaction,\n \tif (transaction->status != REF_TRANSACTION_OPEN)\n \t\tdie(\"BUG: update on transaction that is not open\");\n \n+\tif (flags & REF_ISPACKONLY)\n+\t\tdie(\"BUG: REF_ISPACKONLY can not be used with updates\");\n+\n \tupdate = add_update(transaction, refname);\n \thashcpy(update->new_sha1, new_sha1);\n \tupdate->flags = flags;\n@@ -3345,6 +3348,9 @@ int ref_transaction_create(struct ref_transaction *transaction,\n \tif (transaction->status != REF_TRANSACTION_OPEN)\n \t\tdie(\"BUG: create on transaction that is not open\");\n \n+\tif (flags & REF_ISPACKONLY)\n+\t\tdie(\"BUG: REF_ISPACKONLY can not be used with creates\");\n+\n \tupdate = add_update(transaction, refname);\n \n \thashcpy(update->new_sha1, new_sha1);\n@@ -3458,10 +3464,20 @@ int ref_transaction_commit(struct ref_transaction *transaction,\n \tif (ret)\n \t\tgoto cleanup;\n \n+\tfor (i = 0; i < n; i++) {\n+\t\tstruct ref_update *update = updates[i];\n+\n+\t\tif (update->flags & REF_ISPACKONLY)\n+\t\t\tdelnames[delnum++] = update->refname;\n+\t}\n+\n \t/* Acquire all locks while verifying old values */\n \tfor (i = 0; i < n; i++) {\n \t\tstruct ref_update *update = updates[i];\n \n+\t\tif (update->flags & REF_ISPACKONLY)\n+\t\t\tcontinue;\n+\n \t\tupdate->lock = lock_ref_sha1_basic(update->refname,\n \t\t\t\t\t\t   (update->have_old ?\n \t\t\t\t\t\t    update->old_sha1 :\n@@ -3499,6 +3515,9 @@ int ref_transaction_commit(struct ref_transaction *transaction,\n \tfor (i = 0; i < n; i++) {\n \t\tstruct ref_update *update = updates[i];\n \n+\t\tif (update->flags & REF_ISPACKONLY)\n+\t\t\tcontinue;\n+\n \t\tif (update->lock) {\n \t\t\tret |= delete_ref_loose(update->lock, update->type);\n \t\t\tif (!(update->flags & REF_ISPRUNING))\ndiff --git a/refs.h b/refs.h\nindex 7a89415..71e39b9 100644\n--- a/refs.h\n+++ b/refs.h\n@@ -136,6 +136,8 @@ extern int peel_ref(const char *refname, unsigned char *sha1);\n #define REF_NODEREF\t0x01\n /** Deleting a loose ref during prune */\n #define REF_ISPRUNING\t0x02\n+/** Deletion of a ref that only exists as a packed ref */\n+#define REF_ISPACKONLY\t0x04\n extern struct ref_lock *lock_any_ref_for_update(const char *refname,\n \t\t\t\t\t\tconst unsigned char *old_sha1,\n \t\t\t\t\t\tint flags, int *type_p);\n-- \n2.0.0.rc1.351.g4d2c8e4\n"},{"id":"240432","messageId":"1398976662-6962-41-git-send-email-sahlberg@google.com","threadId":"36563","inReplyTo":"1398976662-6962-1-git-send-email-sahlberg@google.com","subject":"[PATCH v6 40/42] refs.c: pass a skip list to name_conflict_fn","fromName":"Ronnie Sahlberg","fromEmail":"sahlberg@google.com","sentAt":"2014-05-01T20:37:40Z","receivedAt":"2014-05-01T20:37:40Z","isPatch":true,"sender":{"key":"sahlberg@google.com","avatar":"https://avatars.githubusercontent.com/u/7320636?v=4"},"body":"Allow passing a list of refs to ckip checking to name_conflict_fn.\nThere are some conditions where we want to allow a temporary conflict and skip\nchecking those refs. For example if we have a transaction that\n1, guarantees that m is a packed refs and there is no loose ref for m\n2, the transaction will delete m from the packed ref\n3, the transaction will create conflicting m/m\n\nFor this case we want to be able to lock anc create m/m since we know that the\nconflict is only transient. I.e. the conflict will be automatically resolved\nby the transaction when it deletes m.\n\nSigned-off-by: Ronnie Sahlberg <sahlberg@google.com>\n---\n refs.c | 43 +++++++++++++++++++++++++++++++++----------\n 1 file changed, 33 insertions(+), 10 deletions(-)\n\ndiff --git a/refs.c b/refs.c\nindex b525076..eb75927 100644\n--- a/refs.c\n+++ b/refs.c\n@@ -789,11 +789,19 @@ struct name_conflict_cb {\n \tconst char *refname;\n \tconst char *oldrefname;\n \tconst char *conflicting_refname;\n+\tconst char **skip;\n+\tint skipnum;\n };\n \n static int name_conflict_fn(struct ref_entry *entry, void *cb_data)\n {\n \tstruct name_conflict_cb *data = (struct name_conflict_cb *)cb_data;\n+\tint i;\n+\tfor(i = 0; i < data->skipnum; i++) {\n+\t\tif (!strcmp(entry->name, data->skip[i])) {\n+\t\t\treturn 0;\n+\t\t}\n+\t}\n \tif (data->oldrefname && !strcmp(data->oldrefname, entry->name))\n \t\treturn 0;\n \tif (names_conflict(data->refname, entry->name)) {\n@@ -808,15 +816,21 @@ static int name_conflict_fn(struct ref_entry *entry, void *cb_data)\n  * conflicting with the name of an existing reference in dir.  If\n  * oldrefname is non-NULL, ignore potential conflicts with oldrefname\n  * (e.g., because oldrefname is scheduled for deletion in the same\n- * operation).\n+ * operation). skip contains a list of refs we want to skip checking for\n+ * conflicts with. Refs may be skipped due to us knowing that it will\n+ * be deleted later during a transaction that deletes one reference and then\n+ * creates a new conflicting reference. For example a rename from m to m/m.\n  */\n static int is_refname_available(const char *refname, const char *oldrefname,\n-\t\t\t\tstruct ref_dir *dir)\n+\t\t\t\tstruct ref_dir *dir,\n+\t\t\t\tconst char **skip, int skipnum)\n {\n \tstruct name_conflict_cb data;\n \tdata.refname = refname;\n \tdata.oldrefname = oldrefname;\n \tdata.conflicting_refname = NULL;\n+\tdata.skip = skip;\n+\tdata.skipnum = skipnum;\n \n \tsort_ref_dir(dir);\n \tif (do_for_each_entry_in_dir(dir, 0, name_conflict_fn, &data)) {\n@@ -2032,7 +2046,8 @@ int dwim_log(const char *str, int len, unsigned char *sha1, char **log)\n \n static struct ref_lock *lock_ref_sha1_basic(const char *refname,\n \t\t\t\t\t    const unsigned char *old_sha1,\n-\t\t\t\t\t    int flags, int *type_p)\n+\t\t\t\t\t    int flags, int *type_p,\n+\t\t\t\t\t    const char **skip, int skipnum)\n {\n \tchar *ref_file;\n \tconst char *orig_refname = refname;\n@@ -2079,7 +2094,9 @@ static struct ref_lock *lock_ref_sha1_basic(const char *refname,\n \t * name is a proper prefix of our refname.\n \t */\n \tif (missing &&\n-\t     !is_refname_available(refname, NULL, get_packed_refs(&ref_cache))) {\n+\t     !is_refname_available(refname, NULL,\n+\t\t\t\t   get_packed_refs(&ref_cache),\n+\t\t\t\t   skip, skipnum)) {\n \t\tlast_errno = ENOTDIR;\n \t\tgoto error_return;\n \t}\n@@ -2137,7 +2154,7 @@ struct ref_lock *lock_any_ref_for_update(const char *refname,\n \t\t\t\t\t const unsigned char *old_sha1,\n \t\t\t\t\t int flags, int *type_p)\n {\n-\treturn lock_ref_sha1_basic(refname, old_sha1, flags, type_p);\n+\treturn lock_ref_sha1_basic(refname, old_sha1, flags, type_p, NULL, 0);\n }\n \n /*\n@@ -2576,6 +2593,9 @@ int rename_ref(const char *oldrefname, const char *newrefname, const char *logms\n \tint log = !lstat(git_path(\"logs/%s\", oldrefname), &loginfo);\n \tconst char *symref = NULL;\n \n+\tif (!strcmp(oldrefname, newrefname))\n+\t\treturn 0;\n+\n \tif (log && S_ISLNK(loginfo.st_mode))\n \t\treturn error(\"reflog for %s is a symlink\", oldrefname);\n \n@@ -2586,10 +2606,12 @@ int rename_ref(const char *oldrefname, const char *newrefname, const char *logms\n \tif (!symref)\n \t\treturn error(\"refname %s not found\", oldrefname);\n \n-\tif (!is_refname_available(newrefname, oldrefname, get_packed_refs(&ref_cache)))\n+\tif (!is_refname_available(newrefname, oldrefname,\n+\t\t\t\t  get_packed_refs(&ref_cache), NULL, 0))\n \t\treturn 1;\n \n-\tif (!is_refname_available(newrefname, oldrefname, get_loose_refs(&ref_cache)))\n+\tif (!is_refname_available(newrefname, oldrefname,\n+\t\t\t\t  get_loose_refs(&ref_cache), NULL, 0))\n \t\treturn 1;\n \n \tif (log && rename(git_path(\"logs/%s\", oldrefname), git_path(TMP_RENAMED_LOG)))\n@@ -2622,7 +2644,7 @@ int rename_ref(const char *oldrefname, const char *newrefname, const char *logms\n \n \tlogmoved = log;\n \n-\tlock = lock_ref_sha1_basic(newrefname, NULL, 0, NULL);\n+\tlock = lock_ref_sha1_basic(newrefname, NULL, 0, NULL, NULL, 0);\n \tif (!lock) {\n \t\terror(\"unable to lock %s for update\", newrefname);\n \t\tgoto rollback;\n@@ -2637,7 +2659,7 @@ int rename_ref(const char *oldrefname, const char *newrefname, const char *logms\n \treturn 0;\n \n  rollback:\n-\tlock = lock_ref_sha1_basic(oldrefname, NULL, 0, NULL);\n+\tlock = lock_ref_sha1_basic(oldrefname, NULL, 0, NULL, NULL, 0);\n \tif (!lock) {\n \t\terror(\"unable to lock %s for rollback\", oldrefname);\n \t\tgoto rollbacklog;\n@@ -3483,7 +3505,8 @@ int ref_transaction_commit(struct ref_transaction *transaction,\n \t\t\t\t\t\t    update->old_sha1 :\n \t\t\t\t\t\t    NULL),\n \t\t\t\t\t\t   update->flags,\n-\t\t\t\t\t\t   &update->type);\n+\t\t\t\t\t\t   &update->type,\n+\t\t\t\t\t\t   delnames, delnum);\n \t\tif (!update->lock) {\n \t\t\tif (err)\n \t\t\t\tstrbuf_addf(err ,\"Cannot lock the ref '%s'.\",\n-- \n2.0.0.rc1.351.g4d2c8e4\n"},{"id":"240437","messageId":"1398976662-6962-42-git-send-email-sahlberg@google.com","threadId":"36563","inReplyTo":"1398976662-6962-1-git-send-email-sahlberg@google.com","subject":"[PATCH v6 41/42] refs.c: make rename_ref use a transaction","fromName":"Ronnie Sahlberg","fromEmail":"sahlberg@google.com","sentAt":"2014-05-01T20:37:41Z","receivedAt":"2014-05-01T20:37:41Z","isPatch":true,"sender":{"key":"sahlberg@google.com","avatar":"https://avatars.githubusercontent.com/u/7320636?v=4"},"body":"Change rename_ref to use a single transaction to perform the ref rename.\n\nSigned-off-by: Ronnie Sahlberg <sahlberg@google.com>\n---\n refs.c | 73 ++++++++++++++++++------------------------------------------------\n 1 file changed, 20 insertions(+), 53 deletions(-)\n\ndiff --git a/refs.c b/refs.c\nindex eb75927..810a4db 100644\n--- a/refs.c\n+++ b/refs.c\n@@ -2586,9 +2586,10 @@ static int rename_tmp_log(const char *newrefname)\n \n int rename_ref(const char *oldrefname, const char *newrefname, const char *logmsg)\n {\n-\tunsigned char sha1[20], orig_sha1[20];\n-\tint flag = 0, logmoved = 0;\n-\tstruct ref_lock *lock;\n+\tunsigned char sha1[20];\n+\tint flag = 0;\n+\tstruct ref_transaction *transaction;\n+\tstruct strbuf err = STRBUF_INIT;\n \tstruct stat loginfo;\n \tint log = !lstat(git_path(\"logs/%s\", oldrefname), &loginfo);\n \tconst char *symref = NULL;\n@@ -2599,7 +2600,7 @@ int rename_ref(const char *oldrefname, const char *newrefname, const char *logms\n \tif (log && S_ISLNK(loginfo.st_mode))\n \t\treturn error(\"reflog for %s is a symlink\", oldrefname);\n \n-\tsymref = resolve_ref_unsafe(oldrefname, orig_sha1, 1, &flag);\n+\tsymref = resolve_ref_unsafe(oldrefname, sha1, 1, &flag);\n \tif (flag & REF_ISSYMREF)\n \t\treturn error(\"refname %s is a symbolic ref, renaming it is not supported\",\n \t\t\toldrefname);\n@@ -2621,62 +2622,28 @@ int rename_ref(const char *oldrefname, const char *newrefname, const char *logms\n \tif (pack_refs(PACK_REFS_ALL | PACK_REFS_PRUNE))\n \t\treturn error(\"unable to pack refs\");\n \n-\tif (delete_ref(oldrefname, orig_sha1, REF_NODEREF)) {\n-\t\terror(\"unable to delete old %s\", oldrefname);\n-\t\tgoto rollback;\n-\t}\n-\n-\tif (!read_ref_full(newrefname, sha1, 1, NULL) &&\n-\t    delete_ref(newrefname, sha1, REF_NODEREF)) {\n-\t\tif (errno==EISDIR) {\n-\t\t\tif (remove_empty_directories(git_path(\"%s\", newrefname))) {\n-\t\t\t\terror(\"Directory not empty: %s\", newrefname);\n-\t\t\t\tgoto rollback;\n-\t\t\t}\n-\t\t} else {\n-\t\t\terror(\"unable to delete existing %s\", newrefname);\n-\t\t\tgoto rollback;\n-\t\t}\n+\ttransaction = ref_transaction_begin();\n+\tif (!transaction ||\n+\t    ref_transaction_delete(transaction, oldrefname, sha1,\n+\t\t\t\t   REF_NODEREF | REF_ISPACKONLY,\n+\t\t\t\t   1, NULL) ||\n+\t    ref_transaction_update(transaction, newrefname, sha1,\n+\t\t\t\t   NULL, 0, 0, logmsg) ||\n+\t    ref_transaction_commit(transaction, &err)) {\n+\t\tref_transaction_rollback(transaction);\n+\t\terror(\"rename_ref failed: %s\", err.buf);\n+\t\tstrbuf_release(&err);\n+\t\tgoto rollbacklog;\n \t}\n+\tref_transaction_free(transaction);\n \n \tif (log && rename_tmp_log(newrefname))\n-\t\tgoto rollback;\n-\n-\tlogmoved = log;\n-\n-\tlock = lock_ref_sha1_basic(newrefname, NULL, 0, NULL, NULL, 0);\n-\tif (!lock) {\n-\t\terror(\"unable to lock %s for update\", newrefname);\n-\t\tgoto rollback;\n-\t}\n-\tlock->force_write = 1;\n-\thashcpy(lock->old_sha1, orig_sha1);\n-\tif (write_ref_sha1(lock, orig_sha1, logmsg)) {\n-\t\terror(\"unable to write current sha1 into %s\", newrefname);\n-\t\tgoto rollback;\n-\t}\n-\n-\treturn 0;\n-\n- rollback:\n-\tlock = lock_ref_sha1_basic(oldrefname, NULL, 0, NULL, NULL, 0);\n-\tif (!lock) {\n-\t\terror(\"unable to lock %s for rollback\", oldrefname);\n \t\tgoto rollbacklog;\n-\t}\n \n-\tlock->force_write = 1;\n-\tflag = log_all_ref_updates;\n-\tlog_all_ref_updates = 0;\n-\tif (write_ref_sha1(lock, orig_sha1, NULL))\n-\t\terror(\"unable to write current sha1 into %s\", oldrefname);\n-\tlog_all_ref_updates = flag;\n+\treturn 0;\n \n  rollbacklog:\n-\tif (logmoved && rename(git_path(\"logs/%s\", newrefname), git_path(\"logs/%s\", oldrefname)))\n-\t\terror(\"unable to restore logfile %s from %s: %s\",\n-\t\t\toldrefname, newrefname, strerror(errno));\n-\tif (!logmoved && log &&\n+\tif (log &&\n \t    rename(git_path(TMP_RENAMED_LOG), git_path(\"logs/%s\", oldrefname)))\n \t\terror(\"unable to restore logfile %s from \"TMP_RENAMED_LOG\": %s\",\n \t\t\toldrefname, strerror(errno));\n-- \n2.0.0.rc1.351.g4d2c8e4\n"},{"id":"240433","messageId":"1398976662-6962-43-git-send-email-sahlberg@google.com","threadId":"36563","inReplyTo":"1398976662-6962-1-git-send-email-sahlberg@google.com","subject":"[PATCH v6 42/42] refs.c: remove forward declaraion of write_ref_sha1","fromName":"Ronnie Sahlberg","fromEmail":"sahlberg@google.com","sentAt":"2014-05-01T20:37:42Z","receivedAt":"2014-05-01T20:37:42Z","isPatch":true,"sender":{"key":"sahlberg@google.com","avatar":"https://avatars.githubusercontent.com/u/7320636?v=4"},"body":"Signed-off-by: Ronnie Sahlberg <sahlberg@google.com>\n---\n refs.c | 2 --\n 1 file changed, 2 deletions(-)\n\ndiff --git a/refs.c b/refs.c\nindex 810a4db..e59bc18 100644\n--- a/refs.c\n+++ b/refs.c\n@@ -251,8 +251,6 @@ struct ref_entry {\n };\n \n static void read_loose_refs(const char *dirname, struct ref_dir *dir);\n-static int write_ref_sha1(struct ref_lock *lock,\n-\t\t\t  const unsigned char *sha1, const char *logmsg);\n \n static struct ref_dir *get_ref_dir(struct ref_entry *entry)\n {\n-- \n2.0.0.rc1.351.g4d2c8e4\n"},{"id":"240513","messageId":"CAPig+cRa55472sncNsBnweR9Dj2HwTttu4t3GLP8buoTK59DyA@mail.gmail.com","threadId":"36563","inReplyTo":"1398976662-6962-21-git-send-email-sahlberg@google.com","subject":"Re: [PATCH v6 20/42] fetch.c: clear errno before calling functions that might set it","fromName":"Eric Sunshine","fromEmail":"sunshine@sunshineco.com","sentAt":"2014-05-02T04:11:55Z","receivedAt":"2014-05-02T04:11:55Z","isPatch":true,"sender":{"key":"sunshine@sunshineco.com","avatar":"https://avatars.githubusercontent.com/u/163641?v=4"},"body":"On Thu, May 1, 2014 at 4:37 PM, Ronnie Sahlberg <sahlberg@google.com> wrote:\n> In s_update_ref there are two calls that when they fail we return an error\n> based on the errno value. In particular we want to return a specific error\n> if ENOTDIR happened. Both these functions do have failure modes where they\n> may return an error without updating errno, in which case a previous and\n> unrelated ENOTDIT may cause us to return the wrong error. Clear errno before\n\ns/ENOTDIT/ENOTDIR/\n\n> calling any functions if we check errno afterwards.\n>\n> Also skip initializing a static variable to 0. Statics live in .bss and\n> are all automatically initialized to 0.\n>\n> Signed-off-by: Ronnie Sahlberg <sahlberg@google.com>\n> ---\n>  builtin/fetch.c | 4 +++-\n>  1 file changed, 3 insertions(+), 1 deletion(-)\n>\n> diff --git a/builtin/fetch.c b/builtin/fetch.c\n> index 55f457c..a93c893 100644\n> --- a/builtin/fetch.c\n> +++ b/builtin/fetch.c\n> @@ -44,7 +44,7 @@ static struct transport *gtransport;\n>  static struct transport *gsecondary;\n>  static const char *submodule_prefix = \"\";\n>  static const char *recurse_submodules_default;\n> -static int shown_url = 0;\n> +static int shown_url;\n>\n>  static int option_parse_recurse_submodules(const struct option *opt,\n>                                    const char *arg, int unset)\n> @@ -382,6 +382,8 @@ static int s_update_ref(const char *action,\n>         if (!rla)\n>                 rla = default_rla.buf;\n>         snprintf(msg, sizeof(msg), \"%s: %s\", rla, action);\n> +\n> +       errno = 0;\n>         lock = lock_any_ref_for_update(ref->name,\n>                                        check_old ? ref->old_sha1 : NULL,\n>                                        0, NULL);\n> --\n> 2.0.0.rc1.351.g4d2c8e4\n"},{"id":"240514","messageId":"CAPig+cQj92paVQDb14Wi602NME4dvJzpVeB_SVeJKVe32kqF4w@mail.gmail.com","threadId":"36563","inReplyTo":"1398976662-6962-41-git-send-email-sahlberg@google.com","subject":"Re: [PATCH v6 40/42] refs.c: pass a skip list to name_conflict_fn","fromName":"Eric Sunshine","fromEmail":"sunshine@sunshineco.com","sentAt":"2014-05-02T04:22:53Z","receivedAt":"2014-05-02T04:22:53Z","isPatch":true,"sender":{"key":"sunshine@sunshineco.com","avatar":"https://avatars.githubusercontent.com/u/163641?v=4"},"body":"On Thu, May 1, 2014 at 4:37 PM, Ronnie Sahlberg <sahlberg@google.com> wrote:\n> Allow passing a list of refs to ckip checking to name_conflict_fn.\n\ns/ckip/skip/\n\n> There are some conditions where we want to allow a temporary conflict and skip\n> checking those refs. For example if we have a transaction that\n> 1, guarantees that m is a packed refs and there is no loose ref for m\n> 2, the transaction will delete m from the packed ref\n> 3, the transaction will create conflicting m/m\n>\n> For this case we want to be able to lock anc create m/m since we know that the\n\ns/anc/and/\n\n> conflict is only transient. I.e. the conflict will be automatically resolved\n> by the transaction when it deletes m.\n>\n> Signed-off-by: Ronnie Sahlberg <sahlberg@google.com>\n> ---\n>  refs.c | 43 +++++++++++++++++++++++++++++++++----------\n>  1 file changed, 33 insertions(+), 10 deletions(-)\n>\n> diff --git a/refs.c b/refs.c\n> index b525076..eb75927 100644\n> --- a/refs.c\n> +++ b/refs.c\n> @@ -789,11 +789,19 @@ struct name_conflict_cb {\n>         const char *refname;\n>         const char *oldrefname;\n>         const char *conflicting_refname;\n> +       const char **skip;\n> +       int skipnum;\n>  };\n>\n>  static int name_conflict_fn(struct ref_entry *entry, void *cb_data)\n>  {\n>         struct name_conflict_cb *data = (struct name_conflict_cb *)cb_data;\n> +       int i;\n> +       for(i = 0; i < data->skipnum; i++) {\n> +               if (!strcmp(entry->name, data->skip[i])) {\n> +                       return 0;\n> +               }\n> +       }\n>         if (data->oldrefname && !strcmp(data->oldrefname, entry->name))\n>                 return 0;\n>         if (names_conflict(data->refname, entry->name)) {\n> @@ -808,15 +816,21 @@ static int name_conflict_fn(struct ref_entry *entry, void *cb_data)\n>   * conflicting with the name of an existing reference in dir.  If\n>   * oldrefname is non-NULL, ignore potential conflicts with oldrefname\n>   * (e.g., because oldrefname is scheduled for deletion in the same\n> - * operation).\n> + * operation). skip contains a list of refs we want to skip checking for\n> + * conflicts with. Refs may be skipped due to us knowing that it will\n> + * be deleted later during a transaction that deletes one reference and then\n> + * creates a new conflicting reference. For example a rename from m to m/m.\n>   */\n>  static int is_refname_available(const char *refname, const char *oldrefname,\n> -                               struct ref_dir *dir)\n> +                               struct ref_dir *dir,\n> +                               const char **skip, int skipnum)\n>  {\n>         struct name_conflict_cb data;\n>         data.refname = refname;\n>         data.oldrefname = oldrefname;\n>         data.conflicting_refname = NULL;\n> +       data.skip = skip;\n> +       data.skipnum = skipnum;\n>\n>         sort_ref_dir(dir);\n>         if (do_for_each_entry_in_dir(dir, 0, name_conflict_fn, &data)) {\n> @@ -2032,7 +2046,8 @@ int dwim_log(const char *str, int len, unsigned char *sha1, char **log)\n>\n>  static struct ref_lock *lock_ref_sha1_basic(const char *refname,\n>                                             const unsigned char *old_sha1,\n> -                                           int flags, int *type_p)\n> +                                           int flags, int *type_p,\n> +                                           const char **skip, int skipnum)\n>  {\n>         char *ref_file;\n>         const char *orig_refname = refname;\n> @@ -2079,7 +2094,9 @@ static struct ref_lock *lock_ref_sha1_basic(const char *refname,\n>          * name is a proper prefix of our refname.\n>          */\n>         if (missing &&\n> -            !is_refname_available(refname, NULL, get_packed_refs(&ref_cache))) {\n> +            !is_refname_available(refname, NULL,\n> +                                  get_packed_refs(&ref_cache),\n> +                                  skip, skipnum)) {\n>                 last_errno = ENOTDIR;\n>                 goto error_return;\n>         }\n> @@ -2137,7 +2154,7 @@ struct ref_lock *lock_any_ref_for_update(const char *refname,\n>                                          const unsigned char *old_sha1,\n>                                          int flags, int *type_p)\n>  {\n> -       return lock_ref_sha1_basic(refname, old_sha1, flags, type_p);\n> +       return lock_ref_sha1_basic(refname, old_sha1, flags, type_p, NULL, 0);\n>  }\n>\n>  /*\n> @@ -2576,6 +2593,9 @@ int rename_ref(const char *oldrefname, const char *newrefname, const char *logms\n>         int log = !lstat(git_path(\"logs/%s\", oldrefname), &loginfo);\n>         const char *symref = NULL;\n>\n> +       if (!strcmp(oldrefname, newrefname))\n> +               return 0;\n> +\n>         if (log && S_ISLNK(loginfo.st_mode))\n>                 return error(\"reflog for %s is a symlink\", oldrefname);\n>\n> @@ -2586,10 +2606,12 @@ int rename_ref(const char *oldrefname, const char *newrefname, const char *logms\n>         if (!symref)\n>                 return error(\"refname %s not found\", oldrefname);\n>\n> -       if (!is_refname_available(newrefname, oldrefname, get_packed_refs(&ref_cache)))\n> +       if (!is_refname_available(newrefname, oldrefname,\n> +                                 get_packed_refs(&ref_cache), NULL, 0))\n>                 return 1;\n>\n> -       if (!is_refname_available(newrefname, oldrefname, get_loose_refs(&ref_cache)))\n> +       if (!is_refname_available(newrefname, oldrefname,\n> +                                 get_loose_refs(&ref_cache), NULL, 0))\n>                 return 1;\n>\n>         if (log && rename(git_path(\"logs/%s\", oldrefname), git_path(TMP_RENAMED_LOG)))\n> @@ -2622,7 +2644,7 @@ int rename_ref(const char *oldrefname, const char *newrefname, const char *logms\n>\n>         logmoved = log;\n>\n> -       lock = lock_ref_sha1_basic(newrefname, NULL, 0, NULL);\n> +       lock = lock_ref_sha1_basic(newrefname, NULL, 0, NULL, NULL, 0);\n>         if (!lock) {\n>                 error(\"unable to lock %s for update\", newrefname);\n>                 goto rollback;\n> @@ -2637,7 +2659,7 @@ int rename_ref(const char *oldrefname, const char *newrefname, const char *logms\n>         return 0;\n>\n>   rollback:\n> -       lock = lock_ref_sha1_basic(oldrefname, NULL, 0, NULL);\n> +       lock = lock_ref_sha1_basic(oldrefname, NULL, 0, NULL, NULL, 0);\n>         if (!lock) {\n>                 error(\"unable to lock %s for rollback\", oldrefname);\n>                 goto rollbacklog;\n> @@ -3483,7 +3505,8 @@ int ref_transaction_commit(struct ref_transaction *transaction,\n>                                                     update->old_sha1 :\n>                                                     NULL),\n>                                                    update->flags,\n> -                                                  &update->type);\n> +                                                  &update->type,\n> +                                                  delnames, delnum);\n>                 if (!update->lock) {\n>                         if (err)\n>                                 strbuf_addf(err ,\"Cannot lock the ref '%s'.\",\n> --\n> 2.0.0.rc1.351.g4d2c8e4\n>\n> --\n> To unsubscribe from this list: send the line \"unsubscribe git\" in\n> the body of a message to majordomo@vger.kernel.org\n> More majordomo info at  http://vger.kernel.org/majordomo-info.html\n"},{"id":"240526","messageId":"CAL=YDW=CEQedqJ2S=MrPG3YnavJVyVYg_X7FbE3QwpNDQ3Ofhw@mail.gmail.com","threadId":"36563","inReplyTo":"CAPig+cRa55472sncNsBnweR9Dj2HwTttu4t3GLP8buoTK59DyA@mail.gmail.com","subject":"Re: [PATCH v6 20/42] fetch.c: clear errno before calling functions that might set it","fromName":"Ronnie Sahlberg","fromEmail":"sahlberg@google.com","sentAt":"2014-05-02T14:48:16Z","receivedAt":"2014-05-02T14:48:16Z","isPatch":true,"sender":{"key":"sahlberg@google.com","avatar":"https://avatars.githubusercontent.com/u/7320636?v=4"},"body":"Fixed. Thanks.\n\nOn Thu, May 1, 2014 at 9:11 PM, Eric Sunshine <sunshine@sunshineco.com> wrote:\n> On Thu, May 1, 2014 at 4:37 PM, Ronnie Sahlberg <sahlberg@google.com> wrote:\n>> In s_update_ref there are two calls that when they fail we return an error\n>> based on the errno value. In particular we want to return a specific error\n>> if ENOTDIR happened. Both these functions do have failure modes where they\n>> may return an error without updating errno, in which case a previous and\n>> unrelated ENOTDIT may cause us to return the wrong error. Clear errno before\n>\n> s/ENOTDIT/ENOTDIR/\n>\n>> calling any functions if we check errno afterwards.\n>>\n>> Also skip initializing a static variable to 0. Statics live in .bss and\n>> are all automatically initialized to 0.\n>>\n>> Signed-off-by: Ronnie Sahlberg <sahlberg@google.com>\n>> ---\n>>  builtin/fetch.c | 4 +++-\n>>  1 file changed, 3 insertions(+), 1 deletion(-)\n>>\n>> diff --git a/builtin/fetch.c b/builtin/fetch.c\n>> index 55f457c..a93c893 100644\n>> --- a/builtin/fetch.c\n>> +++ b/builtin/fetch.c\n>> @@ -44,7 +44,7 @@ static struct transport *gtransport;\n>>  static struct transport *gsecondary;\n>>  static const char *submodule_prefix = \"\";\n>>  static const char *recurse_submodules_default;\n>> -static int shown_url = 0;\n>> +static int shown_url;\n>>\n>>  static int option_parse_recurse_submodules(const struct option *opt,\n>>                                    const char *arg, int unset)\n>> @@ -382,6 +382,8 @@ static int s_update_ref(const char *action,\n>>         if (!rla)\n>>                 rla = default_rla.buf;\n>>         snprintf(msg, sizeof(msg), \"%s: %s\", rla, action);\n>> +\n>> +       errno = 0;\n>>         lock = lock_any_ref_for_update(ref->name,\n>>                                        check_old ? ref->old_sha1 : NULL,\n>>                                        0, NULL);\n>> --\n>> 2.0.0.rc1.351.g4d2c8e4\n"},{"id":"240527","messageId":"CAL=YDWm=Qc0RHEBnnd5SDBKJ=1T9=yKWoQeBUWY1m7TtmX5B1g@mail.gmail.com","threadId":"36563","inReplyTo":"CAPig+cQj92paVQDb14Wi602NME4dvJzpVeB_SVeJKVe32kqF4w@mail.gmail.com","subject":"Re: [PATCH v6 40/42] refs.c: pass a skip list to name_conflict_fn","fromName":"Ronnie Sahlberg","fromEmail":"sahlberg@google.com","sentAt":"2014-05-02T14:49:49Z","receivedAt":"2014-05-02T14:49:49Z","isPatch":true,"sender":{"key":"sahlberg@google.com","avatar":"https://avatars.githubusercontent.com/u/7320636?v=4"},"body":"Fixed. Thanks.\n\nOn Thu, May 1, 2014 at 9:22 PM, Eric Sunshine <sunshine@sunshineco.com> wrote:\n> On Thu, May 1, 2014 at 4:37 PM, Ronnie Sahlberg <sahlberg@google.com> wrote:\n>> Allow passing a list of refs to ckip checking to name_conflict_fn.\n>\n> s/ckip/skip/\n>\n>> There are some conditions where we want to allow a temporary conflict and skip\n>> checking those refs. For example if we have a transaction that\n>> 1, guarantees that m is a packed refs and there is no loose ref for m\n>> 2, the transaction will delete m from the packed ref\n>> 3, the transaction will create conflicting m/m\n>>\n>> For this case we want to be able to lock anc create m/m since we know that the\n>\n> s/anc/and/\n>\n>> conflict is only transient. I.e. the conflict will be automatically resolved\n>> by the transaction when it deletes m.\n>>\n>> Signed-off-by: Ronnie Sahlberg <sahlberg@google.com>\n>> ---\n>>  refs.c | 43 +++++++++++++++++++++++++++++++++----------\n>>  1 file changed, 33 insertions(+), 10 deletions(-)\n>>\n>> diff --git a/refs.c b/refs.c\n>> index b525076..eb75927 100644\n>> --- a/refs.c\n>> +++ b/refs.c\n>> @@ -789,11 +789,19 @@ struct name_conflict_cb {\n>>         const char *refname;\n>>         const char *oldrefname;\n>>         const char *conflicting_refname;\n>> +       const char **skip;\n>> +       int skipnum;\n>>  };\n>>\n>>  static int name_conflict_fn(struct ref_entry *entry, void *cb_data)\n>>  {\n>>         struct name_conflict_cb *data = (struct name_conflict_cb *)cb_data;\n>> +       int i;\n>> +       for(i = 0; i < data->skipnum; i++) {\n>> +               if (!strcmp(entry->name, data->skip[i])) {\n>> +                       return 0;\n>> +               }\n>> +       }\n>>         if (data->oldrefname && !strcmp(data->oldrefname, entry->name))\n>>                 return 0;\n>>         if (names_conflict(data->refname, entry->name)) {\n>> @@ -808,15 +816,21 @@ static int name_conflict_fn(struct ref_entry *entry, void *cb_data)\n>>   * conflicting with the name of an existing reference in dir.  If\n>>   * oldrefname is non-NULL, ignore potential conflicts with oldrefname\n>>   * (e.g., because oldrefname is scheduled for deletion in the same\n>> - * operation).\n>> + * operation). skip contains a list of refs we want to skip checking for\n>> + * conflicts with. Refs may be skipped due to us knowing that it will\n>> + * be deleted later during a transaction that deletes one reference and then\n>> + * creates a new conflicting reference. For example a rename from m to m/m.\n>>   */\n>>  static int is_refname_available(const char *refname, const char *oldrefname,\n>> -                               struct ref_dir *dir)\n>> +                               struct ref_dir *dir,\n>> +                               const char **skip, int skipnum)\n>>  {\n>>         struct name_conflict_cb data;\n>>         data.refname = refname;\n>>         data.oldrefname = oldrefname;\n>>         data.conflicting_refname = NULL;\n>> +       data.skip = skip;\n>> +       data.skipnum = skipnum;\n>>\n>>         sort_ref_dir(dir);\n>>         if (do_for_each_entry_in_dir(dir, 0, name_conflict_fn, &data)) {\n>> @@ -2032,7 +2046,8 @@ int dwim_log(const char *str, int len, unsigned char *sha1, char **log)\n>>\n>>  static struct ref_lock *lock_ref_sha1_basic(const char *refname,\n>>                                             const unsigned char *old_sha1,\n>> -                                           int flags, int *type_p)\n>> +                                           int flags, int *type_p,\n>> +                                           const char **skip, int skipnum)\n>>  {\n>>         char *ref_file;\n>>         const char *orig_refname = refname;\n>> @@ -2079,7 +2094,9 @@ static struct ref_lock *lock_ref_sha1_basic(const char *refname,\n>>          * name is a proper prefix of our refname.\n>>          */\n>>         if (missing &&\n>> -            !is_refname_available(refname, NULL, get_packed_refs(&ref_cache))) {\n>> +            !is_refname_available(refname, NULL,\n>> +                                  get_packed_refs(&ref_cache),\n>> +                                  skip, skipnum)) {\n>>                 last_errno = ENOTDIR;\n>>                 goto error_return;\n>>         }\n>> @@ -2137,7 +2154,7 @@ struct ref_lock *lock_any_ref_for_update(const char *refname,\n>>                                          const unsigned char *old_sha1,\n>>                                          int flags, int *type_p)\n>>  {\n>> -       return lock_ref_sha1_basic(refname, old_sha1, flags, type_p);\n>> +       return lock_ref_sha1_basic(refname, old_sha1, flags, type_p, NULL, 0);\n>>  }\n>>\n>>  /*\n>> @@ -2576,6 +2593,9 @@ int rename_ref(const char *oldrefname, const char *newrefname, const char *logms\n>>         int log = !lstat(git_path(\"logs/%s\", oldrefname), &loginfo);\n>>         const char *symref = NULL;\n>>\n>> +       if (!strcmp(oldrefname, newrefname))\n>> +               return 0;\n>> +\n>>         if (log && S_ISLNK(loginfo.st_mode))\n>>                 return error(\"reflog for %s is a symlink\", oldrefname);\n>>\n>> @@ -2586,10 +2606,12 @@ int rename_ref(const char *oldrefname, const char *newrefname, const char *logms\n>>         if (!symref)\n>>                 return error(\"refname %s not found\", oldrefname);\n>>\n>> -       if (!is_refname_available(newrefname, oldrefname, get_packed_refs(&ref_cache)))\n>> +       if (!is_refname_available(newrefname, oldrefname,\n>> +                                 get_packed_refs(&ref_cache), NULL, 0))\n>>                 return 1;\n>>\n>> -       if (!is_refname_available(newrefname, oldrefname, get_loose_refs(&ref_cache)))\n>> +       if (!is_refname_available(newrefname, oldrefname,\n>> +                                 get_loose_refs(&ref_cache), NULL, 0))\n>>                 return 1;\n>>\n>>         if (log && rename(git_path(\"logs/%s\", oldrefname), git_path(TMP_RENAMED_LOG)))\n>> @@ -2622,7 +2644,7 @@ int rename_ref(const char *oldrefname, const char *newrefname, const char *logms\n>>\n>>         logmoved = log;\n>>\n>> -       lock = lock_ref_sha1_basic(newrefname, NULL, 0, NULL);\n>> +       lock = lock_ref_sha1_basic(newrefname, NULL, 0, NULL, NULL, 0);\n>>         if (!lock) {\n>>                 error(\"unable to lock %s for update\", newrefname);\n>>                 goto rollback;\n>> @@ -2637,7 +2659,7 @@ int rename_ref(const char *oldrefname, const char *newrefname, const char *logms\n>>         return 0;\n>>\n>>   rollback:\n>> -       lock = lock_ref_sha1_basic(oldrefname, NULL, 0, NULL);\n>> +       lock = lock_ref_sha1_basic(oldrefname, NULL, 0, NULL, NULL, 0);\n>>         if (!lock) {\n>>                 error(\"unable to lock %s for rollback\", oldrefname);\n>>                 goto rollbacklog;\n>> @@ -3483,7 +3505,8 @@ int ref_transaction_commit(struct ref_transaction *transaction,\n>>                                                     update->old_sha1 :\n>>                                                     NULL),\n>>                                                    update->flags,\n>> -                                                  &update->type);\n>> +                                                  &update->type,\n>> +                                                  delnames, delnum);\n>>                 if (!update->lock) {\n>>                         if (err)\n>>                                 strbuf_addf(err ,\"Cannot lock the ref '%s'.\",\n>> --\n>> 2.0.0.rc1.351.g4d2c8e4\n>>\n>> --\n>> To unsubscribe from this list: send the line \"unsubscribe git\" in\n>> the body of a message to majordomo@vger.kernel.org\n>> More majordomo info at  http://vger.kernel.org/majordomo-info.html\n"},{"id":"240770","messageId":"53678ABA.4010104@alum.mit.edu","threadId":"36563","inReplyTo":"1398976662-6962-1-git-send-email-sahlberg@google.com","subject":"Re: [PATCH v6 00/42] Use ref transactions for all ref updates","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2014-05-05T12:57:30Z","receivedAt":"2014-05-05T12:57:30Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"On 05/01/2014 10:37 PM, Ronnie Sahlberg wrote:\n> This patch series is based on next and expands on the transaction API. [...]\n\nMeta-comment:\n\nRonnie,\n\nIt seems like successive versions of this patch series are growing not\nonly in maturity but also in breadth.  That makes it harder to review them.\n\nI, for one, would prefer that a patch series cover a roughly fixed set\nof changes [1], so that all of the patches in a version of the series\nare at roughly the same level of maturity.  That way, the whole series\ncan progress from \"is this a good idea?\" to \"is the implementation\ncorrect?\" to \"are all the details right?\" at roughly the same time, and\nthen Junio can merge the branch, locking in that bit of progress.  While\nthis is happening, other series can be making their way through other\nstages of the pipeline.\n\nWhen new patches are added to an old series, then they delay the merge\nof the older patches, even if those are ripe.  Plus, it makes it harder\nfor reviewers to keep track of the maturity level of each patch and to\nread off how the older patches have changed.  It makes the patch series\na moving target.\n\nThere's no need to re-split this patch series, but please take this wish\ninto account in the future.\n\nThanks,\nMichael\n\n[1] Of course, if a patch series has to grow to make the *existing*\nchanges correct, then that's perfectly OK.\n\n-- \nMichael Haggerty\nmhagger@alum.mit.edu\nhttp://softwareswirl.blogspot.com/\n"},{"id":"240735","messageId":"53678D41.8010100@alum.mit.edu","threadId":"36563","inReplyTo":"1398976662-6962-9-git-send-email-sahlberg@google.com","subject":"Re: [PATCH v6 08/42] refs.c: change ref_transaction_update() to do error checking and return status","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2014-05-05T13:08:17Z","receivedAt":"2014-05-05T13:08:17Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"On 05/01/2014 10:37 PM, Ronnie Sahlberg wrote:\n> Update ref_transaction_update() do some basic error checking and return\n> true on error. Update all callers to check ref_transaction_update() for error.\n> There are currently no conditions in _update that will return error but there\n> will be in the future.\n\nI would change s/true/non-zero/, because error return values are not\njust boolean values; the error values sometimes encode the type of error\nthat occurred.\n\n> \n> Signed-off-by: Ronnie Sahlberg <sahlberg@google.com>\n> ---\n>  builtin/update-ref.c | 10 ++++++----\n>  refs.c               |  9 +++++++--\n>  refs.h               | 10 +++++-----\n>  3 files changed, 18 insertions(+), 11 deletions(-)\n> \n> diff --git a/builtin/update-ref.c b/builtin/update-ref.c\n> index 2bef2a0..59c4d6b 100644\n> --- a/builtin/update-ref.c\n> +++ b/builtin/update-ref.c\n> @@ -197,8 +197,9 @@ static const char *parse_cmd_update(struct strbuf *input, const char *next)\n>  \tif (*next != line_termination)\n>  \t\tdie(\"update %s: extra input: %s\", refname, next);\n>  \n> -\tref_transaction_update(transaction, refname, new_sha1, old_sha1,\n> -\t\t\t       update_flags, have_old);\n> +\tif (ref_transaction_update(transaction, refname, new_sha1, old_sha1,\n> +\t\t\t\t   update_flags, have_old))\n> +\t\tdie(\"update %s: failed\", refname);\n>  \n>  \tupdate_flags = 0;\n>  \tfree(refname);\n> @@ -286,8 +287,9 @@ static const char *parse_cmd_verify(struct strbuf *input, const char *next)\n>  \tif (*next != line_termination)\n>  \t\tdie(\"verify %s: extra input: %s\", refname, next);\n>  \n> -\tref_transaction_update(transaction, refname, new_sha1, old_sha1,\n> -\t\t\t       update_flags, have_old);\n> +\tif (ref_transaction_update(transaction, refname, new_sha1, old_sha1,\n> +\t\t\t\t   update_flags, have_old))\n> +\t\tdie(\"failed transaction update for %s\", refname);\n>  \n>  \tupdate_flags = 0;\n>  \tfree(refname);\n> diff --git a/refs.c b/refs.c\n> index 308e13e..1a903fb 100644\n> --- a/refs.c\n> +++ b/refs.c\n> @@ -3333,19 +3333,24 @@ static struct ref_update *add_update(struct ref_transaction *transaction,\n>  \treturn update;\n>  }\n>  \n> -void ref_transaction_update(struct ref_transaction *transaction,\n> +int ref_transaction_update(struct ref_transaction *transaction,\n>  \t\t\t    const char *refname,\n>  \t\t\t    const unsigned char *new_sha1,\n>  \t\t\t    const unsigned char *old_sha1,\n>  \t\t\t    int flags, int have_old)\n>  {\n> -\tstruct ref_update *update = add_update(transaction, refname);\n> +\tstruct ref_update *update;\n> +\n> +\tif (have_old && !old_sha1)\n> +\t\tdie(\"have_old is true but old_sha1 is NULL\");\n\nThis new check is orthogonal to the rest of the patch, isn't it?\n\n>  \n> +\tupdate = add_update(transaction, refname);\n>  \thashcpy(update->new_sha1, new_sha1);\n>  \tupdate->flags = flags;\n>  \tupdate->have_old = have_old;\n>  \tif (have_old)\n>  \t\thashcpy(update->old_sha1, old_sha1);\n> +\treturn 0;\n>  }\n>  \n>  void ref_transaction_create(struct ref_transaction *transaction,\n> diff --git a/refs.h b/refs.h\n> index bc7715e..0364a3e 100644\n> --- a/refs.h\n> +++ b/refs.h\n> @@ -237,11 +237,11 @@ void ref_transaction_rollback(struct ref_transaction *transaction);\n>   * that the reference should have had before the update, or zeros if\n>   * it must not have existed beforehand.\n>   */\n\nPlease update the docstring above to explain the return value.\n\n> -void ref_transaction_update(struct ref_transaction *transaction,\n> -\t\t\t    const char *refname,\n> -\t\t\t    const unsigned char *new_sha1,\n> -\t\t\t    const unsigned char *old_sha1,\n> -\t\t\t    int flags, int have_old);\n> +int ref_transaction_update(struct ref_transaction *transaction,\n> +\t\t\t   const char *refname,\n> +\t\t\t   const unsigned char *new_sha1,\n> +\t\t\t   const unsigned char *old_sha1,\n> +\t\t\t   int flags, int have_old);\n>  \n>  /*\n>   * Add a reference creation to transaction.  new_sha1 is the value\n> \n\nMichael\n\n-- \nMichael Haggerty\nmhagger@alum.mit.edu\nhttp://softwareswirl.blogspot.com/\n"},{"id":"240752","messageId":"CAL=YDWkQEMwOaxQHHGC9g+kJjVTnTGNt4KxKFY9cRwcudK4i_A@mail.gmail.com","threadId":"36563","inReplyTo":"53678ABA.4010104@alum.mit.edu","subject":"Re: [PATCH v6 00/42] Use ref transactions for all ref updates","fromName":"Ronnie Sahlberg","fromEmail":"sahlberg@google.com","sentAt":"2014-05-05T15:09:31Z","receivedAt":"2014-05-05T15:09:31Z","isPatch":true,"sender":{"key":"sahlberg@google.com","avatar":"https://avatars.githubusercontent.com/u/7320636?v=4"},"body":"On Mon, May 5, 2014 at 5:57 AM, Michael Haggerty <mhagger@alum.mit.edu> wrote:\n> On 05/01/2014 10:37 PM, Ronnie Sahlberg wrote:\n>> This patch series is based on next and expands on the transaction API. [...]\n>\n> Meta-comment:\n>\n> Ronnie,\n>\n> It seems like successive versions of this patch series are growing not\n> only in maturity but also in breadth.  That makes it harder to review them.\n>\n> I, for one, would prefer that a patch series cover a roughly fixed set\n> of changes [1], so that all of the patches in a version of the series\n> are at roughly the same level of maturity.  That way, the whole series\n> can progress from \"is this a good idea?\" to \"is the implementation\n> correct?\" to \"are all the details right?\" at roughly the same time, and\n> then Junio can merge the branch, locking in that bit of progress.  While\n> this is happening, other series can be making their way through other\n> stages of the pipeline.\n>\n> When new patches are added to an old series, then they delay the merge\n> of the older patches, even if those are ripe.  Plus, it makes it harder\n> for reviewers to keep track of the maturity level of each patch and to\n> read off how the older patches have changed.  It makes the patch series\n> a moving target.\n>\n> There's no need to re-split this patch series, but please take this wish\n> into account in the future.\n>\n\nUnderstood.\n"},{"id":"240768","messageId":"CAL=YDW=YYHGMsVSUsUKZeWwQ=ScPx66U72tcj8GNApnSopi6Uw@mail.gmail.com","threadId":"36563","inReplyTo":"53678D41.8010100@alum.mit.edu","subject":"Re: [PATCH v6 08/42] refs.c: change ref_transaction_update() to do error checking and return status","fromName":"Ronnie Sahlberg","fromEmail":"sahlberg@google.com","sentAt":"2014-05-05T23:09:59Z","receivedAt":"2014-05-05T23:09:59Z","isPatch":true,"sender":{"key":"sahlberg@google.com","avatar":"https://avatars.githubusercontent.com/u/7320636?v=4"},"body":"Thanks!\n\nOn Mon, May 5, 2014 at 6:08 AM, Michael Haggerty <mhagger@alum.mit.edu> wrote:\n> On 05/01/2014 10:37 PM, Ronnie Sahlberg wrote:\n>> Update ref_transaction_update() do some basic error checking and return\n>> true on error. Update all callers to check ref_transaction_update() for error.\n>> There are currently no conditions in _update that will return error but there\n>> will be in the future.\n>\n> I would change s/true/non-zero/, because error return values are not\n> just boolean values; the error values sometimes encode the type of error\n> that occurred.\n\nDone.\n\n>\n>>\n>> Signed-off-by: Ronnie Sahlberg <sahlberg@google.com>\n>> ---\n>>  builtin/update-ref.c | 10 ++++++----\n>>  refs.c               |  9 +++++++--\n>>  refs.h               | 10 +++++-----\n>>  3 files changed, 18 insertions(+), 11 deletions(-)\n>>\n>> diff --git a/builtin/update-ref.c b/builtin/update-ref.c\n>> index 2bef2a0..59c4d6b 100644\n>> --- a/builtin/update-ref.c\n>> +++ b/builtin/update-ref.c\n>> @@ -197,8 +197,9 @@ static const char *parse_cmd_update(struct strbuf *input, const char *next)\n>>       if (*next != line_termination)\n>>               die(\"update %s: extra input: %s\", refname, next);\n>>\n>> -     ref_transaction_update(transaction, refname, new_sha1, old_sha1,\n>> -                            update_flags, have_old);\n>> +     if (ref_transaction_update(transaction, refname, new_sha1, old_sha1,\n>> +                                update_flags, have_old))\n>> +             die(\"update %s: failed\", refname);\n>>\n>>       update_flags = 0;\n>>       free(refname);\n>> @@ -286,8 +287,9 @@ static const char *parse_cmd_verify(struct strbuf *input, const char *next)\n>>       if (*next != line_termination)\n>>               die(\"verify %s: extra input: %s\", refname, next);\n>>\n>> -     ref_transaction_update(transaction, refname, new_sha1, old_sha1,\n>> -                            update_flags, have_old);\n>> +     if (ref_transaction_update(transaction, refname, new_sha1, old_sha1,\n>> +                                update_flags, have_old))\n>> +             die(\"failed transaction update for %s\", refname);\n>>\n>>       update_flags = 0;\n>>       free(refname);\n>> diff --git a/refs.c b/refs.c\n>> index 308e13e..1a903fb 100644\n>> --- a/refs.c\n>> +++ b/refs.c\n>> @@ -3333,19 +3333,24 @@ static struct ref_update *add_update(struct ref_transaction *transaction,\n>>       return update;\n>>  }\n>>\n>> -void ref_transaction_update(struct ref_transaction *transaction,\n>> +int ref_transaction_update(struct ref_transaction *transaction,\n>>                           const char *refname,\n>>                           const unsigned char *new_sha1,\n>>                           const unsigned char *old_sha1,\n>>                           int flags, int have_old)\n>>  {\n>> -     struct ref_update *update = add_update(transaction, refname);\n>> +     struct ref_update *update;\n>> +\n>> +     if (have_old && !old_sha1)\n>> +             die(\"have_old is true but old_sha1 is NULL\");\n>\n> This new check is orthogonal to the rest of the patch, isn't it?\n\nYes.\n\nI have updated the commit message to mention that we also check and\ndie(BUG:...) for this condition.\n\n\n>\n>>\n>> +     update = add_update(transaction, refname);\n>>       hashcpy(update->new_sha1, new_sha1);\n>>       update->flags = flags;\n>>       update->have_old = have_old;\n>>       if (have_old)\n>>               hashcpy(update->old_sha1, old_sha1);\n>> +     return 0;\n>>  }\n>>\n>>  void ref_transaction_create(struct ref_transaction *transaction,\n>> diff --git a/refs.h b/refs.h\n>> index bc7715e..0364a3e 100644\n>> --- a/refs.h\n>> +++ b/refs.h\n>> @@ -237,11 +237,11 @@ void ref_transaction_rollback(struct ref_transaction *transaction);\n>>   * that the reference should have had before the update, or zeros if\n>>   * it must not have existed beforehand.\n>>   */\n>\n> Please update the docstring above to explain the return value.\n\nDone.\n\n>\n>> -void ref_transaction_update(struct ref_transaction *transaction,\n>> -                         const char *refname,\n>> -                         const unsigned char *new_sha1,\n>> -                         const unsigned char *old_sha1,\n>> -                         int flags, int have_old);\n>> +int ref_transaction_update(struct ref_transaction *transaction,\n>> +                        const char *refname,\n>> +                        const unsigned char *new_sha1,\n>> +                        const unsigned char *old_sha1,\n>> +                        int flags, int have_old);\n>>\n>>  /*\n>>   * Add a reference creation to transaction.  new_sha1 is the value\n>>\n>\n> Michael\n>\n> --\n> Michael Haggerty\n> mhagger@alum.mit.edu\n> http://softwareswirl.blogspot.com/\n"},{"id":"241399","messageId":"20140513202510.GW9218@google.com","threadId":"36563","inReplyTo":"1398976662-6962-1-git-send-email-sahlberg@google.com","subject":"Re: [PATCH v6 00/42] Use ref transactions for all ref updates","fromName":"Jonathan Nieder","fromEmail":"jrnieder@gmail.com","sentAt":"2014-05-13T20:25:10Z","receivedAt":"2014-05-13T20:25:10Z","isPatch":true,"sender":{"key":"jrnieder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/281595?v=4"},"body":"Hi,\n\nRonnie Sahlberg wrote:\n\n> This patch series is based on next and expands on the transaction API.\n\nSorry to take so long to get to this.\n\nFor the future, it's easier to review patches based on some particular\nbranch that got merged into next, since next is a moving target\n(series come and go from there depending on what seems to need testing\nat a given moment).  Is mh/ref-transaction the relevant branch to\nbuild on?\n\nTrying to apply the series to mh/ref-transaction, I get conflicts in\npatch 13 due to absence of rs/ref-update-check-errors-early.\n\nTrying to apply the series to a merge of mh/ref-transaction and\nrs/ref-update-check-errors-early, I get a minor conflict in patch 15\nbut it is easy to resolve and the rest goes smoothly.\n\nLooking forward to reading the rest.  Thanks.\nJonathan\n"},{"id":"241464","messageId":"20140513224422.GX9218@google.com","threadId":"36563","inReplyTo":"1398976662-6962-3-git-send-email-sahlberg@google.com","subject":"Re: [PATCH v6 02/42] refs.c: allow passing NULL to ref_transaction_free","fromName":"Jonathan Nieder","fromEmail":"jrnieder@gmail.com","sentAt":"2014-05-13T22:44:22Z","receivedAt":"2014-05-13T22:44:22Z","isPatch":true,"sender":{"key":"jrnieder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/281595?v=4"},"body":"Ronnie Sahlberg wrote:\n\n> Allow ref_transaction_free to be called with NULL and in extension allow\n> ref_transaction_rollback to be called for a NULL transaction.\n\nIn extension = as a result?\n\nMakes sense.  It lets someone do the usual\n\n\tstruct ref_transaction *transaction;\n\tint ret = 0;\n\n\tif (something_fails()) {\n\t\tret = -1;\n\t\tgoto cleanup;\n\t}\n\t...\n\n cleanup:\n\tref_transaction_free(transaction);\n\treturn ret;\n\njust like you can already do with free().\n\n> This allows us to write code that will\n>\n>   if ( (!transaction ||\n>         ref_transaction_update(...))  ||\n>       (ref_transaction_commit(...) && !(transaction = NULL)) {\n>           ref_transaction_rollback(transaction);\n>           ...\n>   }\n\nSomewhere in the whitespace and parentheses I'm lost.\n\nIs the idea that when ref_transaction_commit fails it will have\nfreed the transaction so we need not to roll back to prevent a\ndouble free?  I think it would be simpler for the caller to\nunconditionally set transaction to NULL after calling\nref_transaction_commit in such a case to avoid use-after-free.\n\nEven better if it is the caller's responsibility to free\nthe transaction.  At any rate, it doesn't seem related to this\npatch.\n\n> --- a/refs.c\n> +++ b/refs.c\n> @@ -3303,6 +3303,9 @@ static void ref_transaction_free(struct ref_transaction *transaction)\n>  {\n>  \tint i;\n>  \n> +\tif (!transaction)\n> +\t\treturn;\n\nExcept for the unclear commit message,\nReviewed-by: Jonathan Nieder <jrnieder@gmail.com>\n"},{"id":"241465","messageId":"CAL=YDWniCYby1BZSS5yjrormi93-bVJiF4RV2FXT44KMrJXpPA@mail.gmail.com","threadId":"36563","inReplyTo":"20140513224422.GX9218@google.com","subject":"Re: [PATCH v6 02/42] refs.c: allow passing NULL to ref_transaction_free","fromName":"Ronnie Sahlberg","fromEmail":"sahlberg@google.com","sentAt":"2014-05-13T22:52:20Z","receivedAt":"2014-05-13T22:52:20Z","isPatch":true,"sender":{"key":"sahlberg@google.com","avatar":"https://avatars.githubusercontent.com/u/7320636?v=4"},"body":"On Tue, May 13, 2014 at 3:44 PM, Jonathan Nieder <jrnieder@gmail.com> wrote:\n> Ronnie Sahlberg wrote:\n>\n>> Allow ref_transaction_free to be called with NULL and in extension allow\n>> ref_transaction_rollback to be called for a NULL transaction.\n>\n> In extension = as a result?\n>\n> Makes sense.  It lets someone do the usual\n>\n>         struct ref_transaction *transaction;\n>         int ret = 0;\n>\n>         if (something_fails()) {\n>                 ret = -1;\n>                 goto cleanup;\n>         }\n>         ...\n>\n>  cleanup:\n>         ref_transaction_free(transaction);\n>         return ret;\n>\n> just like you can already do with free().\n>\n>> This allows us to write code that will\n>>\n>>   if ( (!transaction ||\n>>         ref_transaction_update(...))  ||\n>>       (ref_transaction_commit(...) && !(transaction = NULL)) {\n>>           ref_transaction_rollback(transaction);\n>>           ...\n>>   }\n>\n> Somewhere in the whitespace and parentheses I'm lost.\n>\n> Is the idea that when ref_transaction_commit fails it will have\n> freed the transaction so we need not to roll back to prevent a\n> double free?\n\nYes. But also, this horribleness is also to illustrate a weak point in the API\nin that ref_transaction_commit actually frees the transaction if\nsuccessful, so the\n&& !(transaction = NULL) kludge is to avoid a double free in the\nref_transaction_rollback.\n\nThis is horrible,  but all this is going away later in the patch\nseries when _commit is fixed so that\nit does not free the transaction anymore.\nWhen that patch comes in later in this series, this horribleness will go away.\n\n\n I think it would be simpler for the caller to\n> unconditionally set transaction to NULL after calling\n> ref_transaction_commit in such a case to avoid use-after-free.\n\nYes. Later patches does that by having ref_transaction_commit not free\nthe transaction\nand instead requiring the caller to explicitely free it by calling\nref_transaction_free.\n\n\nMaybe see this as this is how ugly rollback is by the current _commit\nsemantics. Then see how beautiful it\nall gets once _commit is repaired and the  && !(transaction = NULL)\nkludge is removed. :-)\n\n\n>\n> Even better if it is the caller's responsibility to free\n> the transaction.  At any rate, it doesn't seem related to this\n> patch.\n>\n>> --- a/refs.c\n>> +++ b/refs.c\n>> @@ -3303,6 +3303,9 @@ static void ref_transaction_free(struct ref_transaction *transaction)\n>>  {\n>>       int i;\n>>\n>> +     if (!transaction)\n>> +             return;\n>\n> Except for the unclear commit message,\n> Reviewed-by: Jonathan Nieder <jrnieder@gmail.com>\n"},{"id":"241470","messageId":"20140513231012.GY9218@google.com","threadId":"36563","inReplyTo":"1398976662-6962-4-git-send-email-sahlberg@google.com","subject":"Re: [PATCH v6 03/42] refs.c: add a strbuf argument to ref_transaction_commit for error logging","fromName":"Jonathan Nieder","fromEmail":"jrnieder@gmail.com","sentAt":"2014-05-13T23:10:12Z","receivedAt":"2014-05-13T23:10:12Z","isPatch":true,"sender":{"key":"jrnieder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/281595?v=4"},"body":"Ronnie Sahlberg wrote:\n\n> Add a strbuf argument to _commit so that we can pass an error string back to\n> the caller. So that we can do error logging from the caller instead of from\n> _commit.\n>\n> Longer term plan is to first convert all callers to use onerr==QUIET_ON_ERR\n> and craft any log messages from the callers themselves and finally remove the\n> onerr argument completely.\n\nVery nice.\n\n[...]\n> +++ b/refs.c\n[...]\n> @@ -3443,6 +3444,9 @@ int ref_transaction_commit(struct ref_transaction *transaction,\n>  \t\t\t\t\t       update->flags,\n>  \t\t\t\t\t       &update->type, onerr);\n>  \t\tif (!update->lock) {\n> +\t\t\tif (err)\n> +\t\t\t\tstrbuf_addf(err ,\"Cannot lock the ref '%s'.\",\n> +\t\t\t\t\t    update->refname);\n\nTiny nit: whitespace.\n\n[...]\n> --- a/refs.h\n> +++ b/refs.h\n> @@ -268,9 +268,12 @@ void ref_transaction_delete(struct ref_transaction *transaction,\n>   * Commit all of the changes that have been queued in transaction, as\n>   * atomically as possible.  Return a nonzero value if there is a\n>   * problem.  The ref_transaction is freed by this function.\n> + * If err is non-NULL we will add an error string to it to explain why\n> + * the transaction failed.\n\nProbably worth mentioning the error string doesn't end with a newline\nso the caller knows how to use it.\n\nWith the whitespace fix and with or without the comment tweak,\nReviewed-by: Jonathan Nieder <jrnieder@gmail.com>\n\ndiff --git i/refs.c w/refs.c\nindex 64e8feb..2ca3169 100644\n--- i/refs.c\n+++ w/refs.c\n@@ -3445,7 +3445,7 @@ int ref_transaction_commit(struct ref_transaction *transaction,\n \t\t\t\t\t       &update->type, onerr);\n \t\tif (!update->lock) {\n \t\t\tif (err)\n-\t\t\t\tstrbuf_addf(err ,\"Cannot lock the ref '%s'.\",\n+\t\t\t\tstrbuf_addf(err, \"Cannot lock the ref '%s'.\",\n \t\t\t\t\t    update->refname);\n \t\t\tret = 1;\n \t\t\tgoto cleanup;\ndiff --git i/refs.h w/refs.h\nindex ff87e14..d45212f 100644\n--- i/refs.h\n+++ w/refs.h\n@@ -268,8 +268,8 @@ void ref_transaction_delete(struct ref_transaction *transaction,\n  * Commit all of the changes that have been queued in transaction, as\n  * atomically as possible.  Return a nonzero value if there is a\n  * problem.  The ref_transaction is freed by this function.\n- * If err is non-NULL we will add an error string to it to explain why\n- * the transaction failed.\n+ * If err is non-NULL we will append an error string (with no trailing\n+ * newline) to it to explain why the transaction failed.\n  */\n int ref_transaction_commit(struct ref_transaction *transaction,\n \t\t\t   const char *msg, struct strbuf *err,\n"},{"id":"241477","messageId":"20140514000402.GZ9218@google.com","threadId":"36563","inReplyTo":"1398976662-6962-5-git-send-email-sahlberg@google.com","subject":"Re: [PATCH v6 04/42] refs.c: make ref_update_reject_duplicates take a strbuf argument for errors","fromName":"Jonathan Nieder","fromEmail":"jrnieder@gmail.com","sentAt":"2014-05-14T00:04:02Z","receivedAt":"2014-05-14T00:04:02Z","isPatch":true,"sender":{"key":"jrnieder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/281595?v=4"},"body":"Ronnie Sahlberg wrote:\n\n> Make ref_update_reject_duplicates return any error that occurs through a\n> new strbuf argument.\n\nSensible.  The caller-visible effect would be that now\nref_transaction_commit() can pass back a helpful error message through\nits \"err\" argument when asked to make multiple updates for the same\nref.\n\nReviewed-by: Jonathan Nieder <jrnieder@gmail.com>\n"},{"id":"241502","messageId":"CAL=YDWkV1QbhEB=LSX6mvJWonMz81xE4j1xd2O+J413po968ag@mail.gmail.com","threadId":"36563","inReplyTo":"20140513224422.GX9218@google.com","subject":"Re: [PATCH v6 02/42] refs.c: allow passing NULL to ref_transaction_free","fromName":"Ronnie Sahlberg","fromEmail":"sahlberg@google.com","sentAt":"2014-05-14T15:14:11Z","receivedAt":"2014-05-14T15:14:11Z","isPatch":true,"sender":{"key":"sahlberg@google.com","avatar":"https://avatars.githubusercontent.com/u/7320636?v=4"},"body":"Thanks.\nI changed the commit message.\n\nOn Tue, May 13, 2014 at 3:44 PM, Jonathan Nieder <jrnieder@gmail.com> wrote:\n> Ronnie Sahlberg wrote:\n>\n>> Allow ref_transaction_free to be called with NULL and in extension allow\n>> ref_transaction_rollback to be called for a NULL transaction.\n>\n> In extension = as a result?\n>\n> Makes sense.  It lets someone do the usual\n>\n>         struct ref_transaction *transaction;\n>         int ret = 0;\n>\n>         if (something_fails()) {\n>                 ret = -1;\n>                 goto cleanup;\n>         }\n>         ...\n>\n>  cleanup:\n>         ref_transaction_free(transaction);\n>         return ret;\n>\n> just like you can already do with free().\n>\n>> This allows us to write code that will\n>>\n>>   if ( (!transaction ||\n>>         ref_transaction_update(...))  ||\n>>       (ref_transaction_commit(...) && !(transaction = NULL)) {\n>>           ref_transaction_rollback(transaction);\n>>           ...\n>>   }\n>\n> Somewhere in the whitespace and parentheses I'm lost.\n>\n> Is the idea that when ref_transaction_commit fails it will have\n> freed the transaction so we need not to roll back to prevent a\n> double free?  I think it would be simpler for the caller to\n> unconditionally set transaction to NULL after calling\n> ref_transaction_commit in such a case to avoid use-after-free.\n>\n> Even better if it is the caller's responsibility to free\n> the transaction.  At any rate, it doesn't seem related to this\n> patch.\n>\n>> --- a/refs.c\n>> +++ b/refs.c\n>> @@ -3303,6 +3303,9 @@ static void ref_transaction_free(struct ref_transaction *transaction)\n>>  {\n>>       int i;\n>>\n>> +     if (!transaction)\n>> +             return;\n>\n> Except for the unclear commit message,\n> Reviewed-by: Jonathan Nieder <jrnieder@gmail.com>\n"},{"id":"241503","messageId":"CAL=YDWmvqC-Yz-nM0DEQUi_FkBESVWMwEh7oNfUqtGRiNg1EPA@mail.gmail.com","threadId":"36563","inReplyTo":"20140513231012.GY9218@google.com","subject":"Re: [PATCH v6 03/42] refs.c: add a strbuf argument to ref_transaction_commit for error logging","fromName":"Ronnie Sahlberg","fromEmail":"sahlberg@google.com","sentAt":"2014-05-14T15:20:48Z","receivedAt":"2014-05-14T15:20:48Z","isPatch":true,"sender":{"key":"sahlberg@google.com","avatar":"https://avatars.githubusercontent.com/u/7320636?v=4"},"body":"Thanks.\nComment added and whitespace fixed.\n\nOn Tue, May 13, 2014 at 4:10 PM, Jonathan Nieder <jrnieder@gmail.com> wrote:\n> Ronnie Sahlberg wrote:\n>\n>> Add a strbuf argument to _commit so that we can pass an error string back to\n>> the caller. So that we can do error logging from the caller instead of from\n>> _commit.\n>>\n>> Longer term plan is to first convert all callers to use onerr==QUIET_ON_ERR\n>> and craft any log messages from the callers themselves and finally remove the\n>> onerr argument completely.\n>\n> Very nice.\n>\n> [...]\n>> +++ b/refs.c\n> [...]\n>> @@ -3443,6 +3444,9 @@ int ref_transaction_commit(struct ref_transaction *transaction,\n>>                                              update->flags,\n>>                                              &update->type, onerr);\n>>               if (!update->lock) {\n>> +                     if (err)\n>> +                             strbuf_addf(err ,\"Cannot lock the ref '%s'.\",\n>> +                                         update->refname);\n>\n> Tiny nit: whitespace.\n>\n> [...]\n>> --- a/refs.h\n>> +++ b/refs.h\n>> @@ -268,9 +268,12 @@ void ref_transaction_delete(struct ref_transaction *transaction,\n>>   * Commit all of the changes that have been queued in transaction, as\n>>   * atomically as possible.  Return a nonzero value if there is a\n>>   * problem.  The ref_transaction is freed by this function.\n>> + * If err is non-NULL we will add an error string to it to explain why\n>> + * the transaction failed.\n>\n> Probably worth mentioning the error string doesn't end with a newline\n> so the caller knows how to use it.\n>\n> With the whitespace fix and with or without the comment tweak,\n> Reviewed-by: Jonathan Nieder <jrnieder@gmail.com>\n>\n> diff --git i/refs.c w/refs.c\n> index 64e8feb..2ca3169 100644\n> --- i/refs.c\n> +++ w/refs.c\n> @@ -3445,7 +3445,7 @@ int ref_transaction_commit(struct ref_transaction *transaction,\n>                                                &update->type, onerr);\n>                 if (!update->lock) {\n>                         if (err)\n> -                               strbuf_addf(err ,\"Cannot lock the ref '%s'.\",\n> +                               strbuf_addf(err, \"Cannot lock the ref '%s'.\",\n>                                             update->refname);\n>                         ret = 1;\n>                         goto cleanup;\n> diff --git i/refs.h w/refs.h\n> index ff87e14..d45212f 100644\n> --- i/refs.h\n> +++ w/refs.h\n> @@ -268,8 +268,8 @@ void ref_transaction_delete(struct ref_transaction *transaction,\n>   * Commit all of the changes that have been queued in transaction, as\n>   * atomically as possible.  Return a nonzero value if there is a\n>   * problem.  The ref_transaction is freed by this function.\n> - * If err is non-NULL we will add an error string to it to explain why\n> - * the transaction failed.\n> + * If err is non-NULL we will append an error string (with no trailing\n> + * newline) to it to explain why the transaction failed.\n>   */\n>  int ref_transaction_commit(struct ref_transaction *transaction,\n>                            const char *msg, struct strbuf *err,\n"},{"id":"241505","messageId":"CAL=YDW=pN=L0v2RsiaanuAOR==_RENX0xnbwnWbJXKfih_oeVQ@mail.gmail.com","threadId":"36563","inReplyTo":"20140514000402.GZ9218@google.com","subject":"Re: [PATCH v6 04/42] refs.c: make ref_update_reject_duplicates take a strbuf argument for errors","fromName":"Ronnie Sahlberg","fromEmail":"sahlberg@google.com","sentAt":"2014-05-14T15:24:48Z","receivedAt":"2014-05-14T15:24:48Z","isPatch":true,"sender":{"key":"sahlberg@google.com","avatar":"https://avatars.githubusercontent.com/u/7320636?v=4"},"body":"Thanks.\n\nI updated the commit message to highlight that this means the error\nstring can now be passed all the way back to the caller.\n\nOn Tue, May 13, 2014 at 5:04 PM, Jonathan Nieder <jrnieder@gmail.com> wrote:\n> Ronnie Sahlberg wrote:\n>\n>> Make ref_update_reject_duplicates return any error that occurs through a\n>> new strbuf argument.\n>\n> Sensible.  The caller-visible effect would be that now\n> ref_transaction_commit() can pass back a helpful error message through\n> its \"err\" argument when asked to make multiple updates for the same\n> ref.\n>\n> Reviewed-by: Jonathan Nieder <jrnieder@gmail.com>\n"},{"id":"241589","messageId":"20140514220831.GB9218@google.com","threadId":"36563","inReplyTo":"1398976662-6962-6-git-send-email-sahlberg@google.com","subject":"Re: [PATCH v6 05/42] update-ref.c: log transaction error from the update_ref","fromName":"Jonathan Nieder","fromEmail":"jrnieder@gmail.com","sentAt":"2014-05-14T22:08:31Z","receivedAt":"2014-05-14T22:08:31Z","isPatch":true,"sender":{"key":"jrnieder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/281595?v=4"},"body":"Hi,\n\nRonnie Sahlberg wrote:\n\n> --- a/builtin/update-ref.c\n> +++ b/builtin/update-ref.c\n> @@ -342,6 +342,7 @@ int cmd_update_ref(int argc, const char **argv, const char *prefix)\n[...]\n> @@ -359,17 +360,16 @@ int cmd_update_ref(int argc, const char **argv, const char *prefix)\n[...]\n>  \t\tif (delete || no_deref || argc > 0)\n>  \t\t\tusage_with_options(git_update_ref_usage, options);\n>  \t\tif (end_null)\n>  \t\t\tline_termination = '\\0';\n>  \t\tupdate_refs_stdin();\n> -\t\tret = ref_transaction_commit(transaction, msg, NULL,\n> -\t\t\t\t\t     UPDATE_REFS_DIE_ON_ERR);\n> -\t\treturn ret;\n> +\t\tif (ref_transaction_commit(transaction, msg, &err,\n> +\t\t\t\t\t   UPDATE_REFS_QUIET_ON_ERR))\n> +\t\t\tdie(\"%s\", err.buf);\n\nNice.  I like this much more than passing a flag to each function to\ntell it how to handle errors. :)\n\nref_transaction_commit didn't have any stray codepaths that return\nsome other exit code instead of die()-ing with UPDATE_REFS_DIE_ON_ERR,\nso this should be safe as far as the exit code is concerned.\n\nThe only danger would be that some codepath leaves 'err' alone and\nforgets to write a messages, so we die with\n\n\t\"fatal: \"\n\nAlas, it looks like this patch can do that.\n\n i. The call to update_ref_write can error out without updating the\n    error string.\n\n ii. delete_ref_loose can print a message and then fail without updating\n     the error string so the output looks like\n\n\twarning: unable to unlink .git/refs/heads/master.lock: Permission denied\n\tfatal:\n\t$\n\n iii. repack_without_refs can similarly return an error\n\n\terror: Unable to create '/home/jrn/test/.git/packed-refs.lock: Permission denied\n\terror: cannot delete 'refs/heads/master' from packed refs\n\tfatal:\n\t$\n\n iv. commit_lock_file in commit_packed_refs is silent on error.\n     repack_without_refs probably intends to write a message in that\n     case but doesn't :(\n\nI wish there were some way to automatically detect missed spots or\nmake them stand out (like with the current \"return error()\" idiom a\nbare \"return -1\" stands out).\n\n(i) is fixed by a later patch.  It would be better to put that before\nthis one for bisectability.\n\nI don't see fixes to (ii), (iii), and (iv) in the series yet from a\nquick glance.\n\nThanks,\nJonathan\n"},{"id":"241631","messageId":"20140514230424.GC9218@google.com","threadId":"36563","inReplyTo":"1398976662-6962-7-git-send-email-sahlberg@google.com","subject":"Re: [PATCH v6 06/42] refs.c: make update_ref_write update a strbuf on failure","fromName":"Jonathan Nieder","fromEmail":"jrnieder@gmail.com","sentAt":"2014-05-14T23:04:24Z","receivedAt":"2014-05-14T23:04:24Z","isPatch":true,"sender":{"key":"jrnieder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/281595?v=4"},"body":"Ronnie Sahlberg wrote:\n\n> Change update_ref_write to also update an error strbuf on failure.\n> This makes the error available to ref_transaction_commit callers if the\n> transaction failed due to update_ref_sha1/write_ref_sha1 failures.\n\nReviewed-by: Jonathan Nieder <jrnieder@gmail.com>\n"},{"id":"241632","messageId":"20140514230604.GD9218@google.com","threadId":"36563","inReplyTo":"1398976662-6962-8-git-send-email-sahlberg@google.com","subject":"Re: [PATCH v6 07/42] refs.c: remove the onerr argument to ref_transaction_commit","fromName":"Jonathan Nieder","fromEmail":"jrnieder@gmail.com","sentAt":"2014-05-14T23:06:04Z","receivedAt":"2014-05-14T23:06:04Z","isPatch":true,"sender":{"key":"jrnieder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/281595?v=4"},"body":"Ronnie Sahlberg wrote:\n\n> Since all callers now use QUIET_ON_ERR we no longer need to provide an onerr\n> argument any more. Remove the onerr argument from the ref_transaction_commit\n> signature.\n\nNice, and obviously correct.\n\nReviewed-by: Jonathan Nieder <jrnieder@gmail.com>\n"},{"id":"241634","messageId":"20140514234015.GE9218@google.com","threadId":"36563","inReplyTo":"1398976662-6962-9-git-send-email-sahlberg@google.com","subject":"Re: [PATCH v6 08/42] refs.c: change ref_transaction_update() to do error checking and return status","fromName":"Jonathan Nieder","fromEmail":"jrnieder@gmail.com","sentAt":"2014-05-14T23:40:15Z","receivedAt":"2014-05-14T23:40:15Z","isPatch":true,"sender":{"key":"jrnieder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/281595?v=4"},"body":"Ronnie Sahlberg wrote:\n\n> Update ref_transaction_update() do some basic error checking and return\n> true on error. Update all callers to check ref_transaction_update() for error.\n> There are currently no conditions in _update that will return error but there\n> will be in the future.\n>\n> Signed-off-by: Ronnie Sahlberg <sahlberg@google.com>\n> ---\n>  builtin/update-ref.c | 10 ++++++----\n>  refs.c               |  9 +++++++--\n>  refs.h               | 10 +++++-----\n>  3 files changed, 18 insertions(+), 11 deletions(-)\n\nRevisiting comments from [1]:\n\n * When I call ref_transaction_update, what does it mean that I get\n   a nonzero return value?  Does it mean the _update failed and had\n   no effect?  What will I want to do next: should I try again or\n   print an error and exit?\n\n   Ideally I should be able to answer these questions by reading\n   the signature of ref_transaction_update and the comment documenting\n   it.  The comment doesn't say anything about what errors\n   mean here.\n\n * the error message change for the have_old && !old_sha1 case (to add\n   \"BUG:\" so users know the impossible has happened and translators\n   know not to bother with it) seems to have snuck ahead into patch 28\n   (refs.c: add transaction.status and track OPEN/CLOSED/ERROR).\n\n * It would be easier to make sense of the error path (does the error\n   message have enough information?  Will the user be bewildered?)\n   if there were an example of how ref_transaction_update can fail.\n\n   There still doesn't seem to be one by the end of the series.\n\nThe general idea still seems sensible.\n\nThanks,\nJonathan\n\n[1] http://thread.gmane.org/gmane.comp.version-control.git/246437/focus=247115\n"},{"id":"241637","messageId":"20140515000409.GF9218@google.com","threadId":"36563","inReplyTo":"1398976662-6962-10-git-send-email-sahlberg@google.com","subject":"Re: [PATCH v6 09/42] refs.c: change ref_transaction_create to do error checking and return status","fromName":"Jonathan Nieder","fromEmail":"jrnieder@gmail.com","sentAt":"2014-05-15T00:04:09Z","receivedAt":"2014-05-15T00:04:09Z","isPatch":true,"sender":{"key":"jrnieder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/281595?v=4"},"body":"Ronnie Sahlberg wrote:\n\n> Do basic error checking in ref_transaction_create() and make it return\n> status. Update all callers to check the result of ref_transaction_create()\n> There are currently no conditions in _create that will return error but there\n> will be in the future.\n\nSame concerns as with _update:\n\n[...]\n> +++ b/builtin/update-ref.c\n> @@ -225,7 +225,9 @@ static const char *parse_cmd_create(struct strbuf *input, const char *next)\n>  \tif (*next != line_termination)\n>  \t\tdie(\"create %s: extra input: %s\", refname, next);\n>  \n> -\tref_transaction_create(transaction, refname, new_sha1, update_flags);\n> +\tif (ref_transaction_create(transaction, refname, new_sha1,\n> +\t\t\t\t   update_flags))\n> +\t\tdie(\"failed transaction create for %s\", refname);\n\nIf it were ever triggered, the message\n\n\terror: some bad thing\n\tfatal: failed transaction create for refs/heads/master\n\nlooks overly verbose and unclear.  Something like\n\n\tfatal: cannot create ref refs/heads/master: some bad thing\n\nmight work better.  It's hard to tell without an example in mind.\n\n[...]\n> @@ -3353,18 +3353,23 @@ void ref_transaction_create(struct ref_transaction *transaction,\n[...]\n> -\tassert(!is_null_sha1(new_sha1));\n> +\tif (!new_sha1 || is_null_sha1(new_sha1))\n> +\t\tdie(\"create ref with null new_sha1\");\n\nOne less 'assert' is nice. :)\n\nAs with _update, the message should start with \"BUG:\" to make it clear\nto users and translators that this should never happen, even with\nmalformed user input.  That gets corrected in patch 28 but it's\nclearer to include it from the start.\n"},{"id":"241638","messageId":"20140515001904.GG9218@google.com","threadId":"36563","inReplyTo":"1398976662-6962-11-git-send-email-sahlberg@google.com","subject":"Re: [PATCH v6 10/42] refs.c: ref_transaction_delete to check for error and return status","fromName":"Jonathan Nieder","fromEmail":"jrnieder@gmail.com","sentAt":"2014-05-15T00:19:04Z","receivedAt":"2014-05-15T00:19:04Z","isPatch":true,"sender":{"key":"jrnieder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/281595?v=4"},"body":"Ronnie Sahlberg wrote:\n\n> Change ref_transaction_delete() to do basic error checking and return\n> status. Update all callers to check the return for ref_transaction_delete()\n> There are currently no conditions in _delete that will return error but there\n> will be in the future.\n>\n> Signed-off-by: Ronnie Sahlberg <sahlberg@google.com>\n\nSame comments as _delete and _update. :)\n"},{"id":"241639","messageId":"20140515002738.GH9218@google.com","threadId":"36563","inReplyTo":"1398976662-6962-12-git-send-email-sahlberg@google.com","subject":"Re: [PATCH v6 11/42] tag.c: use ref transactions when doing updates","fromName":"Jonathan Nieder","fromEmail":"jrnieder@gmail.com","sentAt":"2014-05-15T00:27:38Z","receivedAt":"2014-05-15T00:27:38Z","isPatch":true,"sender":{"key":"jrnieder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/281595?v=4"},"body":"Ronnie Sahlberg wrote:\n\n> --- a/builtin/tag.c\n> +++ b/builtin/tag.c\n> @@ -701,11 +702,12 @@ int cmd_tag(int argc, const char **argv, const char *prefix)\n>  \tif (annotate)\n>  \t\tcreate_tag(object, tag, &buf, &opt, prev, object);\n>  \n> -\tlock = lock_any_ref_for_update(ref.buf, prev, 0, NULL);\n> -\tif (!lock)\n> -\t\tdie(_(\"%s: cannot lock the ref\"), ref.buf);\n> -\tif (write_ref_sha1(lock, object, NULL) < 0)\n> -\t\tdie(_(\"%s: cannot update the ref\"), ref.buf);\n> +\ttransaction = ref_transaction_begin();\n> +\tif (!transaction ||\n> +\t    ref_transaction_update(transaction, ref.buf, object, prev,\n> +\t\t\t\t   0, !is_null_sha1(prev)) ||\n> +\t    ref_transaction_commit(transaction, NULL, &err))\n> +\t\tdie(_(\"%s: cannot update the ref: %s\"), ref.buf, err.buf);\n\nMakes sense for the _update and _commit case.  (BTW, why is have_old\na separate boolean instead of a bit in flags?)\n\nFor the _begin() case, can ref_transaction_begin() ever fail?  xcalloc\ndie()s on allocation failure.  So I think it's fine to assume\ntransaction is non-null (i.e., drop the !transaction condition), or if\nyou want to be defensive, then label it as a bug --- e.g.:\n\n\tif (!transaction)\n\t\tdie(\"BUG: ref_transaction_begin() returned NULL?\");\n\nOtherwise if ref_transaction_begin regresses in the future and this\ncase is tripped then the message would be\n\n\tfatal: refs/tags/v1.0: cannot update the ref:\n\nwhich is not as obvious an indicator that the user should contact\nthe mailing list.\n\nThanks,\nJonathan\n"},{"id":"241640","messageId":"20140515003031.GI9218@google.com","threadId":"36563","inReplyTo":"1398976662-6962-13-git-send-email-sahlberg@google.com","subject":"Re: [PATCH v6 12/42] replace.c: use the ref transaction functions for updates","fromName":"Jonathan Nieder","fromEmail":"jrnieder@gmail.com","sentAt":"2014-05-15T00:30:31Z","receivedAt":"2014-05-15T00:30:31Z","isPatch":true,"sender":{"key":"jrnieder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/281595?v=4"},"body":"Ronnie Sahlberg wrote:\n\n[...]\n> +++ b/builtin/replace.c\n[...]\n> @@ -157,11 +158,12 @@ static int replace_object(const char *object_ref, const char *replace_ref,\n>  \telse if (!force)\n>  \t\tdie(\"replace ref '%s' already exists\", ref);\n>  \n> -\tlock = lock_any_ref_for_update(ref, prev, 0, NULL);\n> -\tif (!lock)\n> -\t\tdie(\"%s: cannot lock the ref\", ref);\n> -\tif (write_ref_sha1(lock, repl, NULL) < 0)\n> -\t\tdie(\"%s: cannot update the ref\", ref);\n> +\ttransaction = ref_transaction_begin();\n> +\tif (!transaction ||\n> +\t    ref_transaction_update(transaction, ref, repl, prev,\n> +\t\t\t\t   0, !is_null_sha1(prev)) ||\n> +\t    ref_transaction_commit(transaction, NULL, &err))\n> +\t\tdie(_(\"%s: failed to replace ref: %s\"), ref, err.buf);\n\nSame question about the !transaction case.\n\nThis makes the message translated, which is a nice change but not\nmentioned in the commit message.  (Generally speaking, I don't mind\neither way about adding or not adding _() to new messages in files\nthat have not already undergone a pass of marking everything for\ntranslation.)\n"},{"id":"241641","messageId":"20140515011134.GJ9218@google.com","threadId":"36563","inReplyTo":"1398976662-6962-14-git-send-email-sahlberg@google.com","subject":"Re: [PATCH v6 13/42] commit.c: use ref transactions for updates","fromName":"Jonathan Nieder","fromEmail":"jrnieder@gmail.com","sentAt":"2014-05-15T01:11:34Z","receivedAt":"2014-05-15T01:11:34Z","isPatch":true,"sender":{"key":"jrnieder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/281595?v=4"},"body":"Ronnie Sahlberg wrote:\n\n[...]\n> +++ b/builtin/commit.c\n> @@ -1541,11 +1541,12 @@ int cmd_commit(int argc, const char **argv, const char *prefix)\n[...]\n> @@ -1667,16 +1668,6 @@ int cmd_commit(int argc, const char **argv, const char *prefix)\n>  \tstrbuf_release(&author_ident);\n>  \tfree_commit_extra_headers(extra);\n>  \n> -\tref_lock = lock_any_ref_for_update(\"HEAD\",\n> -\t\t\t\t\t   !current_head\n> -\t\t\t\t\t   ? NULL\n> -\t\t\t\t\t   : current_head->object.sha1,\n> -\t\t\t\t\t   0, NULL);\n> -\tif (!ref_lock) {\n> -\t\trollback_index_files();\n> -\t\tdie(_(\"cannot lock HEAD ref\"));\n> -\t}\n> -\n>  \tnl = strchr(sb.buf, '\\n');\n>  \tif (nl)\n>  \t\tstrbuf_setlen(&sb, nl + 1 - sb.buf);\n> \telse\n> \t\tstrbuf_addch(&sb, '\\n');\n>  \tstrbuf_insert(&sb, 0, reflog_msg, strlen(reflog_msg));\n>  \tstrbuf_insert(&sb, strlen(reflog_msg), \": \", 2);\n>  \n> -\tif (write_ref_sha1(ref_lock, sha1, sb.buf) < 0) {\n> +\ttransaction = ref_transaction_begin();\n> +\tif (!transaction ||\n> +\t    ref_transaction_update(transaction, \"HEAD\", sha1,\n> +\t\t\t\t   current_head ?\n> +\t\t\t\t   current_head->object.sha1 : NULL,\n> +\t\t\t\t   0, !!current_head) ||\n> +\t    ref_transaction_commit(transaction, sb.buf, &err)) {\n>  \t\trollback_index_files();\n> -\t\tdie(_(\"cannot update HEAD ref\"));\n> +\t\tdie(_(\"HEAD: cannot update ref: %s\"), err.buf);\n\nSame question about !transaction (it also applies to later patches but I\nwon't mention it any more).\n\nThe error message changed from\n\n\tfatal: cannot lock HEAD ref\n\nto\n\n\tfatal: HEAD: cannot update ref: Cannot lock the ref 'HEAD'.\n\nDoes the message from ref_transaction_commit always say what ref\nwas being updated when it failed?  If so, it's tempting to just use\nthe message as-is:\n\n\tfatal: Cannot lock the ref 'HEAD'\n\nIf the caller should add to the message, it could say something about\nthe context --- e.g.,\n\n\tfatal: cannot update HEAD with new commit: cannot lock the ref 'HEAD'\n\nLooking at that,\n\n\tdie(\"%s\", err.buf)\n\nseems simplest since even if \"git commit\" was being called in a loop,\nit's already clear that git was trying to lock HEAD to advance it.\n\nThanks,\nJonathan\n"},{"id":"241685","messageId":"CAL=YDWmsD6ajY0f+yfqW+8Tp7NOE6HvYFGgrUGRFswwXBvP6-w@mail.gmail.com","threadId":"36563","inReplyTo":"20140514220831.GB9218@google.com","subject":"Re: [PATCH v6 05/42] update-ref.c: log transaction error from the update_ref","fromName":"Ronnie Sahlberg","fromEmail":"sahlberg@google.com","sentAt":"2014-05-15T15:47:59Z","receivedAt":"2014-05-15T15:47:59Z","isPatch":true,"sender":{"key":"sahlberg@google.com","avatar":"https://avatars.githubusercontent.com/u/7320636?v=4"},"body":"On Wed, May 14, 2014 at 3:08 PM, Jonathan Nieder <jrnieder@gmail.com> wrote:\n> Hi,\n>\n> Ronnie Sahlberg wrote:\n>\n>> --- a/builtin/update-ref.c\n>> +++ b/builtin/update-ref.c\n>> @@ -342,6 +342,7 @@ int cmd_update_ref(int argc, const char **argv, const char *prefix)\n> [...]\n>> @@ -359,17 +360,16 @@ int cmd_update_ref(int argc, const char **argv, const char *prefix)\n> [...]\n>>               if (delete || no_deref || argc > 0)\n>>                       usage_with_options(git_update_ref_usage, options);\n>>               if (end_null)\n>>                       line_termination = '\\0';\n>>               update_refs_stdin();\n>> -             ret = ref_transaction_commit(transaction, msg, NULL,\n>> -                                          UPDATE_REFS_DIE_ON_ERR);\n>> -             return ret;\n>> +             if (ref_transaction_commit(transaction, msg, &err,\n>> +                                        UPDATE_REFS_QUIET_ON_ERR))\n>> +                     die(\"%s\", err.buf);\n>\n> Nice.  I like this much more than passing a flag to each function to\n> tell it how to handle errors. :)\n>\n> ref_transaction_commit didn't have any stray codepaths that return\n> some other exit code instead of die()-ing with UPDATE_REFS_DIE_ON_ERR,\n> so this should be safe as far as the exit code is concerned.\n>\n> The only danger would be that some codepath leaves 'err' alone and\n> forgets to write a messages, so we die with\n>\n>         \"fatal: \"\n>\n> Alas, it looks like this patch can do that.\n>\n>  i. The call to update_ref_write can error out without updating the\n>     error string.\n\nFixed.\nI reordered the patches so the change to update_ref_write to take an\nerr argument will come before the change to update-ref.c as you\nsuggested.\n\n>\n>  ii. delete_ref_loose can print a message and then fail without updating\n>      the error string so the output looks like\n>\n>         warning: unable to unlink .git/refs/heads/master.lock: Permission denied\n>         fatal:\n>         $\n>\n\nFixed.\nI have added a new patch before the change to update-ref.c to add err\nto delete_ref_loose.\n\n>  iii. repack_without_refs can similarly return an error\n>\n>         error: Unable to create '/home/jrn/test/.git/packed-refs.lock: Permission denied\n>         error: cannot delete 'refs/heads/master' from packed refs\n>         fatal:\n>         $\n>\n>  iv. commit_lock_file in commit_packed_refs is silent on error.\n>      repack_without_refs probably intends to write a message in that\n>      case but doesn't :(\n\nFixed.\nI added a patch to take an err argument to repack_without_refs and\nupdate it for both\nconditions iii and iv.\n\n\n\n>\n> I wish there were some way to automatically detect missed spots or\n> make them stand out (like with the current \"return error()\" idiom a\n> bare \"return -1\" stands out).\n>\n> (i) is fixed by a later patch.  It would be better to put that before\n> this one for bisectability.\n>\n> I don't see fixes to (ii), (iii), and (iv) in the series yet from a\n> quick glance.\n\nFixed in the next version of the patch series I will send out.\nThanks.\n"},{"id":"241687","messageId":"CAL=YDW=e1FdVn5cHvRHPQrA8ywPne-uZk_iYNO4EmxJEA=fHSw@mail.gmail.com","threadId":"36563","inReplyTo":"20140514234015.GE9218@google.com","subject":"Re: [PATCH v6 08/42] refs.c: change ref_transaction_update() to do error checking and return status","fromName":"Ronnie Sahlberg","fromEmail":"sahlberg@google.com","sentAt":"2014-05-15T16:06:50Z","receivedAt":"2014-05-15T16:06:50Z","isPatch":true,"sender":{"key":"sahlberg@google.com","avatar":"https://avatars.githubusercontent.com/u/7320636?v=4"},"body":"On Wed, May 14, 2014 at 4:40 PM, Jonathan Nieder <jrnieder@gmail.com> wrote:\n> Ronnie Sahlberg wrote:\n>\n>> Update ref_transaction_update() do some basic error checking and return\n>> true on error. Update all callers to check ref_transaction_update() for error.\n>> There are currently no conditions in _update that will return error but there\n>> will be in the future.\n>>\n>> Signed-off-by: Ronnie Sahlberg <sahlberg@google.com>\n>> ---\n>>  builtin/update-ref.c | 10 ++++++----\n>>  refs.c               |  9 +++++++--\n>>  refs.h               | 10 +++++-----\n>>  3 files changed, 18 insertions(+), 11 deletions(-)\n>\n> Revisiting comments from [1]:\n>\n>  * When I call ref_transaction_update, what does it mean that I get\n>    a nonzero return value?  Does it mean the _update failed and had\n>    no effect?  What will I want to do next: should I try again or\n>    print an error and exit?\n\nIt means the transaction will no longer work and must be rolled back.\nSee below for the updated text I added to refs.h\n\n>\n>    Ideally I should be able to answer these questions by reading\n>    the signature of ref_transaction_update and the comment documenting\n>    it.  The comment doesn't say anything about what errors\n>    mean here.\n\nI have updated the description to include :\n * Function returns 0 on success and non-zero on failure. A failure to update\n * means that the transaction as a whole has failed and will need to be\n * rolled back.\n\n\n>\n>  * the error message change for the have_old && !old_sha1 case (to add\n>    \"BUG:\" so users know the impossible has happened and translators\n>    know not to bother with it) seems to have snuck ahead into patch 28\n>    (refs.c: add transaction.status and track OPEN/CLOSED/ERROR).\n\nDone.\n\n>\n>  * It would be easier to make sense of the error path (does the error\n>    message have enough information?  Will the user be bewildered?)\n>    if there were an example of how ref_transaction_update can fail.\n>\n>    There still doesn't seem to be one by the end of the series.\n\nThis patch series got a lot longer than I initially thought so I did\nnot get to the point where we it would make sense\nto start returning !0. :-(\n\nThe next patchseries I sent out for review does add things to _update\nthat will cause it to return failures.\nFor example, locking the ref there happens in _update instead of\n_commit and then it starts make sense to\nreturn failures back to the caller for things such as \"Multiple\nupdates for ref '%s' not allowed.\"\n\nUnfortunate but since this patch series reached >40 patches I did not\nwant to continue expanding on it.\nThis means that actually starting to use \"let _update return error\"\ndid not actually start becomming used until the second\npatch series, which now is well over 30 patches in size :-(\n\nI just felt I had to stop growing this series or it would never be finished.\n\n\n\n>\n> The general idea still seems sensible.\n>\n> Thanks,\n> Jonathan\n>\n> [1] http://thread.gmane.org/gmane.comp.version-control.git/246437/focus=247115\n"},{"id":"241690","messageId":"CAL=YDWn5+_jA55KGx74We=41ngxSDA6yKJbuZ=3zy8gZTYPYnQ@mail.gmail.com","threadId":"36563","inReplyTo":"20140515000409.GF9218@google.com","subject":"Re: [PATCH v6 09/42] refs.c: change ref_transaction_create to do error checking and return status","fromName":"Ronnie Sahlberg","fromEmail":"sahlberg@google.com","sentAt":"2014-05-15T16:23:45Z","receivedAt":"2014-05-15T16:23:45Z","isPatch":true,"sender":{"key":"sahlberg@google.com","avatar":"https://avatars.githubusercontent.com/u/7320636?v=4"},"body":"On Wed, May 14, 2014 at 5:04 PM, Jonathan Nieder <jrnieder@gmail.com> wrote:\n> Ronnie Sahlberg wrote:\n>\n>> Do basic error checking in ref_transaction_create() and make it return\n>> status. Update all callers to check the result of ref_transaction_create()\n>> There are currently no conditions in _create that will return error but there\n>> will be in the future.\n>\n> Same concerns as with _update:\n\nDone.\n\n>\n> [...]\n>> +++ b/builtin/update-ref.c\n>> @@ -225,7 +225,9 @@ static const char *parse_cmd_create(struct strbuf *input, const char *next)\n>>       if (*next != line_termination)\n>>               die(\"create %s: extra input: %s\", refname, next);\n>>\n>> -     ref_transaction_create(transaction, refname, new_sha1, update_flags);\n>> +     if (ref_transaction_create(transaction, refname, new_sha1,\n>> +                                update_flags))\n>> +             die(\"failed transaction create for %s\", refname);\n>\n> If it were ever triggered, the message\n>\n>         error: some bad thing\n>         fatal: failed transaction create for refs/heads/master\n>\n> looks overly verbose and unclear.  Something like\n>\n>         fatal: cannot create ref refs/heads/master: some bad thing\n\nI changed it to :\n   die(\"cannot create ref '%s'\", refname);\n\nBut it would still mean you would have\n         error: some bad thing\n         fatal: cannot create 'refs/heads/master'\n\n\nTo make it better we have to wait until the end of the second patch\nseries, ref-transactions-next\nwhere we will have an err argument to _update/_create/_delete too and\nthus we can do this from update-ref.c :\n\n   if (transaction_create_sha1(transaction, refname, new_sha1,\n       update_flags, msg, &err))\n   die(\"%s\", err.buf);\n\n\n>\n> might work better.  It's hard to tell without an example in mind.\n>\n> [...]\n>> @@ -3353,18 +3353,23 @@ void ref_transaction_create(struct ref_transaction *transaction,\n> [...]\n>> -     assert(!is_null_sha1(new_sha1));\n>> +     if (!new_sha1 || is_null_sha1(new_sha1))\n>> +             die(\"create ref with null new_sha1\");\n>\n> One less 'assert' is nice. :)\n>\n> As with _update, the message should start with \"BUG:\" to make it clear\n> to users and translators that this should never happen, even with\n> malformed user input.  That gets corrected in patch 28 but it's\n> clearer to include it from the start.\n"},{"id":"241691","messageId":"CAL=YDWnkbyWk4iTaOey7bsoQc+Maqu7z5tOy3Vj-jX-3OjCakQ@mail.gmail.com","threadId":"36563","inReplyTo":"20140515001904.GG9218@google.com","subject":"Re: [PATCH v6 10/42] refs.c: ref_transaction_delete to check for error and return status","fromName":"Ronnie Sahlberg","fromEmail":"sahlberg@google.com","sentAt":"2014-05-15T16:26:19Z","receivedAt":"2014-05-15T16:26:19Z","isPatch":true,"sender":{"key":"sahlberg@google.com","avatar":"https://avatars.githubusercontent.com/u/7320636?v=4"},"body":"On Wed, May 14, 2014 at 5:19 PM, Jonathan Nieder <jrnieder@gmail.com> wrote:\n> Ronnie Sahlberg wrote:\n>\n>> Change ref_transaction_delete() to do basic error checking and return\n>> status. Update all callers to check the return for ref_transaction_delete()\n>> There are currently no conditions in _delete that will return error but there\n>> will be in the future.\n>>\n>> Signed-off-by: Ronnie Sahlberg <sahlberg@google.com>\n>\n> Same comments as _delete and _update. :)\n\nDone.\nThanks.\n"},{"id":"241694","messageId":"CAL=YDWk8uZzJyex0YoBx81KhJZjS99qW1nV2AOe=rZhgoYRAcg@mail.gmail.com","threadId":"36563","inReplyTo":"20140515002738.GH9218@google.com","subject":"Re: [PATCH v6 11/42] tag.c: use ref transactions when doing updates","fromName":"Ronnie Sahlberg","fromEmail":"sahlberg@google.com","sentAt":"2014-05-15T16:45:01Z","receivedAt":"2014-05-15T16:45:01Z","isPatch":true,"sender":{"key":"sahlberg@google.com","avatar":"https://avatars.githubusercontent.com/u/7320636?v=4"},"body":"On Wed, May 14, 2014 at 5:27 PM, Jonathan Nieder <jrnieder@gmail.com> wrote:\n> Ronnie Sahlberg wrote:\n>\n>> --- a/builtin/tag.c\n>> +++ b/builtin/tag.c\n>> @@ -701,11 +702,12 @@ int cmd_tag(int argc, const char **argv, const char *prefix)\n>>       if (annotate)\n>>               create_tag(object, tag, &buf, &opt, prev, object);\n>>\n>> -     lock = lock_any_ref_for_update(ref.buf, prev, 0, NULL);\n>> -     if (!lock)\n>> -             die(_(\"%s: cannot lock the ref\"), ref.buf);\n>> -     if (write_ref_sha1(lock, object, NULL) < 0)\n>> -             die(_(\"%s: cannot update the ref\"), ref.buf);\n>> +     transaction = ref_transaction_begin();\n>> +     if (!transaction ||\n>> +         ref_transaction_update(transaction, ref.buf, object, prev,\n>> +                                0, !is_null_sha1(prev)) ||\n>> +         ref_transaction_commit(transaction, NULL, &err))\n>> +             die(_(\"%s: cannot update the ref: %s\"), ref.buf, err.buf);\n>\n> Makes sense for the _update and _commit case.  (BTW, why is have_old\n> a separate boolean instead of a bit in flags?)\n>\n> For the _begin() case, can ref_transaction_begin() ever fail?  xcalloc\n> die()s on allocation failure.  So I think it's fine to assume\n> transaction is non-null (i.e., drop the !transaction condition), or if\n> you want to be defensive, then label it as a bug --- e.g.:\n>\n>         if (!transaction)\n>                 die(\"BUG: ref_transaction_begin() returned NULL?\");\n>\n> Otherwise if ref_transaction_begin regresses in the future and this\n> case is tripped then the message would be\n>\n>         fatal: refs/tags/v1.0: cannot update the ref:\n>\n> which is not as obvious an indicator that the user should contact\n> the mailing list.\n\nFor the current refs implementation, _begin can never return NULL\nsince the only failure mode would be OOM in which case we die().\nAnd then for that case we could remove the !transaction check since transaction\ncan never be NULL here.\n\n(I am not a big fan of die() in general)\n\nHowever, if we implement a different datastore for refs in the future\nit is likely that\nthe ref_transaction_begin equivalent for that backend could well start returning\nfailures for a lot other reasons than just OOM.\n\nI could imagine that tdb_transaction_start() could fail for a\ncorrupted database.\nAn SQL based backend could fail due to the client library failing to\nopen a socket to the db,\netc.\n\n\nBut you bring a good point about the error message.\n\nInstead of the suggestions above, would you accept an alternative\napproach where I would\nadd an err argument to ref_transaction_begin() instead?\n\nFor a hypothetical mysql backend, this could then do something like :\n\n>> +     transaction = ref_transaction_begin();\n>> +     if (!transaction ||\n>> +         ref_transaction_update(transaction, ref.buf, object, prev,\n>> +                                0, !is_null_sha1(prev)) ||\n>> +         ref_transaction_commit(transaction, NULL, &err))\n>> +             die(_(\"%s: cannot update the ref: %s\"), ref.buf, err.buf);\n\nWhich could then result in output like\n\nfatal: refs/heads/master: cannot update the ref: failed to connect to\nmysql database ...\n\n\n\nSo I suggest that instead of doing these changes I will add an err\nargument to ref_transaction_begin.\nDoes that sound ok with you?\n\n\n>\n> Thanks,\n> Jonathan\n"},{"id":"241696","messageId":"CAL=YDWkOz2Xa_ZA4TJ2sh9oG9m18yDkEd=-252h+u7w3nG1ZVQ@mail.gmail.com","threadId":"36563","inReplyTo":"20140515003031.GI9218@google.com","subject":"Re: [PATCH v6 12/42] replace.c: use the ref transaction functions for updates","fromName":"Ronnie Sahlberg","fromEmail":"sahlberg@google.com","sentAt":"2014-05-15T16:50:25Z","receivedAt":"2014-05-15T16:50:25Z","isPatch":true,"sender":{"key":"sahlberg@google.com","avatar":"https://avatars.githubusercontent.com/u/7320636?v=4"},"body":"On Wed, May 14, 2014 at 5:30 PM, Jonathan Nieder <jrnieder@gmail.com> wrote:\n> Ronnie Sahlberg wrote:\n>\n> [...]\n>> +++ b/builtin/replace.c\n> [...]\n>> @@ -157,11 +158,12 @@ static int replace_object(const char *object_ref, const char *replace_ref,\n>>       else if (!force)\n>>               die(\"replace ref '%s' already exists\", ref);\n>>\n>> -     lock = lock_any_ref_for_update(ref, prev, 0, NULL);\n>> -     if (!lock)\n>> -             die(\"%s: cannot lock the ref\", ref);\n>> -     if (write_ref_sha1(lock, repl, NULL) < 0)\n>> -             die(\"%s: cannot update the ref\", ref);\n>> +     transaction = ref_transaction_begin();\n>> +     if (!transaction ||\n>> +         ref_transaction_update(transaction, ref, repl, prev,\n>> +                                0, !is_null_sha1(prev)) ||\n>> +         ref_transaction_commit(transaction, NULL, &err))\n>> +             die(_(\"%s: failed to replace ref: %s\"), ref, err.buf);\n>\n> Same question about the !transaction case.\n>\n> This makes the message translated, which is a nice change but not\n> mentioned in the commit message.  (Generally speaking, I don't mind\n> either way about adding or not adding _() to new messages in files\n> that have not already undergone a pass of marking everything for\n> translation.)\n\nRemoved the _.  This series is long enough as is so lets not start\nworrying about translations too.\n\nSame opinion about the ref_transaction_begin() case as before. I think\nit will be better to just add err to it\nsince it is likely this will be useful for future non-file based ref backends.\n"},{"id":"241697","messageId":"20140515165322.GB27279@google.com","threadId":"36563","inReplyTo":"CAL=YDWk8uZzJyex0YoBx81KhJZjS99qW1nV2AOe=rZhgoYRAcg@mail.gmail.com","subject":"Re: [PATCH v6 11/42] tag.c: use ref transactions when doing updates","fromName":"Jonathan Nieder","fromEmail":"jrnieder@gmail.com","sentAt":"2014-05-15T16:53:22Z","receivedAt":"2014-05-15T16:53:22Z","isPatch":true,"sender":{"key":"jrnieder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/281595?v=4"},"body":"Ronnie Sahlberg wrote:\n\n> Instead of the suggestions above, would you accept an alternative\n> approach where I would\n> add an err argument to ref_transaction_begin() instead?\n>\n> For a hypothetical mysql backend, this could then do something like :\n[...]\n> fatal: refs/heads/master: cannot update the ref: failed to connect to mysql database ...\n\nYes, sounds like a good thing to do.\n\nThanks,\nJonathan\n"},{"id":"241698","messageId":"CAL=YDWkve_vXpmuL7qdsmyhEys+PdfHT3RN1g0oriBQxvVC7wg@mail.gmail.com","threadId":"36563","inReplyTo":"20140515011134.GJ9218@google.com","subject":"Re: [PATCH v6 13/42] commit.c: use ref transactions for updates","fromName":"Ronnie Sahlberg","fromEmail":"sahlberg@google.com","sentAt":"2014-05-15T16:53:32Z","receivedAt":"2014-05-15T16:53:32Z","isPatch":true,"sender":{"key":"sahlberg@google.com","avatar":"https://avatars.githubusercontent.com/u/7320636?v=4"},"body":"On Wed, May 14, 2014 at 6:11 PM, Jonathan Nieder <jrnieder@gmail.com> wrote:\n> Ronnie Sahlberg wrote:\n>\n> [...]\n>> +++ b/builtin/commit.c\n>> @@ -1541,11 +1541,12 @@ int cmd_commit(int argc, const char **argv, const char *prefix)\n> [...]\n>> @@ -1667,16 +1668,6 @@ int cmd_commit(int argc, const char **argv, const char *prefix)\n>>       strbuf_release(&author_ident);\n>>       free_commit_extra_headers(extra);\n>>\n>> -     ref_lock = lock_any_ref_for_update(\"HEAD\",\n>> -                                        !current_head\n>> -                                        ? NULL\n>> -                                        : current_head->object.sha1,\n>> -                                        0, NULL);\n>> -     if (!ref_lock) {\n>> -             rollback_index_files();\n>> -             die(_(\"cannot lock HEAD ref\"));\n>> -     }\n>> -\n>>       nl = strchr(sb.buf, '\\n');\n>>       if (nl)\n>>               strbuf_setlen(&sb, nl + 1 - sb.buf);\n>>       else\n>>               strbuf_addch(&sb, '\\n');\n>>       strbuf_insert(&sb, 0, reflog_msg, strlen(reflog_msg));\n>>       strbuf_insert(&sb, strlen(reflog_msg), \": \", 2);\n>>\n>> -     if (write_ref_sha1(ref_lock, sha1, sb.buf) < 0) {\n>> +     transaction = ref_transaction_begin();\n>> +     if (!transaction ||\n>> +         ref_transaction_update(transaction, \"HEAD\", sha1,\n>> +                                current_head ?\n>> +                                current_head->object.sha1 : NULL,\n>> +                                0, !!current_head) ||\n>> +         ref_transaction_commit(transaction, sb.buf, &err)) {\n>>               rollback_index_files();\n>> -             die(_(\"cannot update HEAD ref\"));\n>> +             die(_(\"HEAD: cannot update ref: %s\"), err.buf);\n>\n> Same question about !transaction (it also applies to later patches but I\n> won't mention it any more).\n>\n> The error message changed from\n>\n>         fatal: cannot lock HEAD ref\n>\n> to\n>\n>         fatal: HEAD: cannot update ref: Cannot lock the ref 'HEAD'.\n>\n> Does the message from ref_transaction_commit always say what ref\n> was being updated when it failed?  If so, it's tempting to just use\n> the message as-is:\n>\n>         fatal: Cannot lock the ref 'HEAD'\n>\n> If the caller should add to the message, it could say something about\n> the context --- e.g.,\n>\n>         fatal: cannot update HEAD with new commit: cannot lock the ref 'HEAD'\n>\n> Looking at that,\n>\n>         die(\"%s\", err.buf)\n>\n> seems simplest since even if \"git commit\" was being called in a loop,\n> it's already clear that git was trying to lock HEAD to advance it.\n\nChanged it to\n>         die(\"%s\", err.buf)\n\nas you suggested.\n\n\n\nMany thanks for the reviews so far!\n\nregards\nronnie sahlberg\n"},{"id":"241699","messageId":"20140515165631.GC27279@google.com","threadId":"36563","inReplyTo":"CAL=YDWn5+_jA55KGx74We=41ngxSDA6yKJbuZ=3zy8gZTYPYnQ@mail.gmail.com","subject":"Re: [PATCH v6 09/42] refs.c: change ref_transaction_create to do error checking and return status","fromName":"Jonathan Nieder","fromEmail":"jrnieder@gmail.com","sentAt":"2014-05-15T16:56:31Z","receivedAt":"2014-05-15T16:56:31Z","isPatch":true,"sender":{"key":"jrnieder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/281595?v=4"},"body":"Ronnie Sahlberg wrote:\n> On Wed, May 14, 2014 at 5:04 PM, Jonathan Nieder <jrnieder@gmail.com> wrote:\n\n>> If it were ever triggered, the message\n>>\n>>         error: some bad thing\n>>         fatal: failed transaction create for refs/heads/master\n>>\n>> looks overly verbose and unclear.  Something like\n>>\n>>         fatal: cannot create ref refs/heads/master: some bad thing\n>\n> I changed it to :\n>    die(\"cannot create ref '%s'\", refname);\n>\n> But it would still mean you would have\n>          error: some bad thing\n>          fatal: cannot create 'refs/heads/master'\n>\n> To make it better we have to wait until the end of the second patch\n> series, ref-transactions-next\n> where we will have an err argument to _update/_create/_delete too and\n> thus we can do this from update-ref.c :\n>\n>    if (transaction_create_sha1(transaction, refname, new_sha1,\n>        update_flags, msg, &err))\n>    die(\"%s\", err.buf);\n\nThanks.  Sounds good.\n"},{"id":"241749","messageId":"20140515173557.GL9218@google.com","threadId":"36563","inReplyTo":"1398976662-6962-15-git-send-email-sahlberg@google.com","subject":"Re: [PATCH v6 14/42] sequencer.c: use ref transactions for all ref updates","fromName":"Jonathan Nieder","fromEmail":"jrnieder@gmail.com","sentAt":"2014-05-15T17:35:57Z","receivedAt":"2014-05-15T17:35:57Z","isPatch":true,"sender":{"key":"jrnieder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/281595?v=4"},"body":"Ronnie Sahlberg wrote:\n\n> --- a/sequencer.c\n> +++ b/sequencer.c\n> @@ -272,23 +272,31 @@ static int error_dirty_index(struct replay_opts *opts)\n>  static int fast_forward_to(const unsigned char *to, const unsigned char *from,\n>  \t\t\tint unborn, struct replay_opts *opts)\n>  {\n> -\tstruct ref_lock *ref_lock;\n> +\tstruct ref_transaction *transaction;\n>  \tstruct strbuf sb = STRBUF_INIT;\n> -\tint ret;\n> +\tstruct strbuf err = STRBUF_INIT;\n>  \n>  \tread_cache();\n>  \tif (checkout_fast_forward(from, to, 1))\n>  \t\texit(1); /* the callee should have complained already */\n> -\tref_lock = lock_any_ref_for_update(\"HEAD\", unborn ? null_sha1 : from,\n> -\t\t\t\t\t   0, NULL);\n> -\tif (!ref_lock)\n> -\t\treturn error(_(\"Failed to lock HEAD during fast_forward_to\"));\n>  \n>  \tstrbuf_addf(&sb, \"%s: fast-forward\", action_name(opts));\n> -\tret = write_ref_sha1(ref_lock, to, sb.buf);\n> +\n> +\ttransaction = ref_transaction_begin();\n> +\tif ((!transaction ||\n> +\t    ref_transaction_update(transaction, \"HEAD\", to, from,\n> +\t\t\t\t   0, !unborn)) ||\n\nThese parentheses make it harder to parse.  Other patches in this\nseries do\n\n\tif (!transaction ||\n\t    ref_transaction_update(...) ||\n\t    ref_transaction_commit(...)) {\n\nso this could do\n\n\tif (!transaction ||\n\t    ref_transaction_update(...) ||\n\t    (ref_transaction_commit(...) && !(transaction = NULL))) {\n\n> +\t    (ref_transaction_commit(transaction, sb.buf, &err) &&\n> +\t     !(transaction = NULL))) {\n> +\t\tref_transaction_rollback(transaction);\n\nEarlier patches in the series didn't bother rolling back.  Should they\nhave?\n\nThanks,\nJonathan\n"}]}