{"thread":{"id":"36928","subject":"[PATCH v2 10/14] refs.c: allow multiple reflog updates during a single transaction","startedAt":"2014-06-16T16:51:31Z","lastAt":"2014-06-16T16:51:45Z","messageCount":15,"participants":["Ronnie Sahlberg"],"isPatch":true,"patchVersion":2,"patchTotal":14},"messages":[{"id":"244267","messageId":"1402937505-6091-1-git-send-email-sahlberg@google.com","threadId":"36928","inReplyTo":null,"subject":"[PATCH v2 00/14] ref-transactions-reflog","fromName":"Ronnie Sahlberg","fromEmail":"sahlberg@google.com","sentAt":"2014-06-16T16:51:31Z","receivedAt":"2014-06-16T16:51:31Z","isPatch":true,"sender":{"key":"sahlberg@google.com","avatar":"https://avatars.githubusercontent.com/u/7320636?v=4"},"body":"This patch series builds on the ref-transactions series added to origin/pu.\nIt adds support for updating the reflog during a transaction and changes\nbuiltin/reflog.c to use a transaction.\nWith this series in place we now have only one single place where we\nwrite reflog entries and only two places where we read and parse the entries\n(the two interators).\n\nThis is version 2 of the series and is mainly rebased and tweaked to apply\nontop of origin/pu and all the changes/conflicts between it and the previous\nbase.\n\nRonnie Sahlberg (14):\n  refs.c make ref_transaction_create a wrapper to ref_transaction_update\n  refs.c: make ref_transaction_delete a wrapper for\n    ref_transaction_update\n  refs.c: rename the transaction functions\n  refs.c: add a new update_type field to ref_update\n  refs.c: add a function to append a reflog entry to a fd\n  lockfile.c: make hold_lock_file_for_append preserve meaningful errno\n  refs.c: add a transaction function to append a reflog entry\n  refs.c: add a flag to allow reflog updates to truncate the log\n  refs.c: only write reflog update if msg is non-NULL\n  refs.c: allow multiple reflog updates during a single transaction\n  reflog.c: use a reflog transaction when writing during expire\n  refs.c: rename log_ref_setup to create_reflog\n  refs.c: make unlock_ref/close_ref/commit_ref static\n  refs.c: remove lock_any_ref_for_update\n\n branch.c               |  11 +-\n builtin/checkout.c     |   8 +-\n builtin/commit.c       |  14 +-\n builtin/fetch.c        |  12 +-\n builtin/receive-pack.c |  14 +-\n builtin/reflog.c       |  85 +++++-----\n builtin/replace.c      |  10 +-\n builtin/tag.c          |  10 +-\n builtin/update-ref.c   |  22 +--\n copy.c                 |  20 ++-\n fast-import.c          |  22 +--\n lockfile.c             |   7 +-\n refs.c                 | 428 +++++++++++++++++++++++++++++++++----------------\n refs.h                 | 109 +++++++------\n sequencer.c            |  12 +-\n walker.c               |  16 +-\n 16 files changed, 475 insertions(+), 325 deletions(-)\n\n-- \n2.0.0.770.gd892650.dirty\n"},{"id":"244263","messageId":"1402937505-6091-2-git-send-email-sahlberg@google.com","threadId":"36928","inReplyTo":"1402937505-6091-1-git-send-email-sahlberg@google.com","subject":"[PATCH v2 01/14] refs.c make ref_transaction_create a wrapper to ref_transaction_update","fromName":"Ronnie Sahlberg","fromEmail":"sahlberg@google.com","sentAt":"2014-06-16T16:51:32Z","receivedAt":"2014-06-16T16:51:32Z","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 | 13 ++-----------\n refs.h |  7 ++++---\n 2 files changed, 6 insertions(+), 14 deletions(-)\n\ndiff --git a/refs.c b/refs.c\nindex fbb34c0..c62df4c 100644\n--- a/refs.c\n+++ b/refs.c\n@@ -3509,23 +3509,14 @@ int ref_transaction_create(struct ref_transaction *transaction,\n \t\t\t   int flags, const char *msg,\n \t\t\t   struct strbuf *err)\n {\n-\tstruct ref_update *update;\n-\n \tif (transaction->state != REF_TRANSACTION_OPEN)\n \t\tdie(\"BUG: create called for transaction that is not open\");\n \n \tif (!new_sha1 || is_null_sha1(new_sha1))\n \t\tdie(\"BUG: create ref with null new_sha1\");\n \n-\tupdate = add_update(transaction, refname);\n-\n-\thashcpy(update->new_sha1, new_sha1);\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+\treturn ref_transaction_update(transaction, refname, new_sha1,\n+\t\t\t\t      null_sha1, flags, 1, msg, err);\n }\n \n int ref_transaction_delete(struct ref_transaction *transaction,\ndiff --git a/refs.h b/refs.h\nindex 0766a9e..4843706 100644\n--- a/refs.h\n+++ b/refs.h\n@@ -282,9 +282,10 @@ struct ref_transaction *ref_transaction_begin(struct strbuf *err);\n /*\n  * Add a reference update to transaction.  new_sha1 is the value that\n  * the reference should have after the update, or zeros if it should\n- * be deleted.  If have_old is true, then old_sha1 holds the value\n- * that the reference should have had before the update, or zeros if\n- * it must not have existed beforehand.\n+ * be deleted.  If have_old is true and old_sha is not the null_sha1\n+ * then the previous value of the ref must match or the update will fail.\n+ * If have_old is true and old_sha1 is the null_sha1 then the ref must not\n+ * already exist and a new ref will be created with new_sha1.\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-- \n2.0.0.770.gd892650.dirty\n"},{"id":"244264","messageId":"1402937505-6091-3-git-send-email-sahlberg@google.com","threadId":"36928","inReplyTo":"1402937505-6091-1-git-send-email-sahlberg@google.com","subject":"[PATCH v2 02/14] refs.c: make ref_transaction_delete a wrapper for ref_transaction_update","fromName":"Ronnie Sahlberg","fromEmail":"sahlberg@google.com","sentAt":"2014-06-16T16:51:33Z","receivedAt":"2014-06-16T16:51:33Z","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 | 17 +++++------------\n refs.h |  2 +-\n 2 files changed, 6 insertions(+), 13 deletions(-)\n\ndiff --git a/refs.c b/refs.c\nindex c62df4c..76732a4 100644\n--- a/refs.c\n+++ b/refs.c\n@@ -3525,24 +3525,17 @@ int ref_transaction_delete(struct ref_transaction *transaction,\n \t\t\t   int flags, int have_old, const char *msg,\n \t\t\t   struct strbuf *err)\n {\n-\tstruct ref_update *update;\n-\n \tif (transaction->state != REF_TRANSACTION_OPEN)\n \t\tdie(\"BUG: delete called for transaction that is not open\");\n \n \tif (have_old && !old_sha1)\n \t\tdie(\"BUG: 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-\tif (msg)\n-\t\tupdate->msg = xstrdup(msg);\n-\treturn 0;\n+\tif (have_old && is_null_sha1(old_sha1))\n+\t\tdie(\"BUG: have_old is true but old_sha1 is null_sha1\");\n+\n+\treturn ref_transaction_update(transaction, refname, null_sha1,\n+\t\t\t\t      old_sha1, flags, have_old, msg, err);\n }\n \n int update_ref(const char *action, const char *refname,\ndiff --git a/refs.h b/refs.h\nindex 4843706..43aafb7 100644\n--- a/refs.h\n+++ b/refs.h\n@@ -281,7 +281,7 @@ struct ref_transaction *ref_transaction_begin(struct strbuf *err);\n \n /*\n  * Add a reference update to transaction.  new_sha1 is the value that\n- * the reference should have after the update, or zeros if it should\n+ * the reference should have after the update, or null_sha1 if it should\n  * be deleted.  If have_old is true and old_sha is not the null_sha1\n  * then the previous value of the ref must match or the update will fail.\n  * If have_old is true and old_sha1 is the null_sha1 then the ref must not\n-- \n2.0.0.770.gd892650.dirty\n"},{"id":"244266","messageId":"1402937505-6091-4-git-send-email-sahlberg@google.com","threadId":"36928","inReplyTo":"1402937505-6091-1-git-send-email-sahlberg@google.com","subject":"[PATCH v2 03/14] refs.c: rename the transaction functions","fromName":"Ronnie Sahlberg","fromEmail":"sahlberg@google.com","sentAt":"2014-06-16T16:51:34Z","receivedAt":"2014-06-16T16:51:34Z","isPatch":true,"sender":{"key":"sahlberg@google.com","avatar":"https://avatars.githubusercontent.com/u/7320636?v=4"},"body":"Rename the transaction functions. Remove the leading ref_ from the\nnames and append _sha1 to the names for functions that create/delete/\nupdate sha1 refs.\n\nSigned-off-by: Ronnie Sahlberg <sahlberg@google.com>\n---\n branch.c               | 11 +++----\n builtin/commit.c       | 14 ++++-----\n builtin/fetch.c        | 12 ++++----\n builtin/receive-pack.c | 14 ++++-----\n builtin/replace.c      | 10 +++----\n builtin/tag.c          | 10 +++----\n builtin/update-ref.c   | 22 +++++++-------\n fast-import.c          | 22 +++++++-------\n refs.c                 | 80 +++++++++++++++++++++++++-------------------------\n refs.h                 | 56 +++++++++++++++++------------------\n sequencer.c            | 12 ++++----\n walker.c               | 16 +++++-----\n 12 files changed, 140 insertions(+), 139 deletions(-)\n\ndiff --git a/branch.c b/branch.c\nindex e0439af..6fa6d78 100644\n--- a/branch.c\n+++ b/branch.c\n@@ -298,13 +298,14 @@ void create_branch(const char *head,\n \t\tstruct ref_transaction *transaction;\n \t\tstruct strbuf err = STRBUF_INIT;\n \n-\t\ttransaction = ref_transaction_begin(&err);\n+\t\ttransaction = transaction_begin(&err);\n \t\tif (!transaction ||\n-\t\t    ref_transaction_update(transaction, ref.buf, sha1,\n-\t\t\t\t\t   null_sha1, 0, !forcing, msg, &err) ||\n-\t\t    ref_transaction_commit(transaction, &err))\n+\t\t    transaction_update_sha1(transaction, ref.buf, sha1,\n+\t\t\t\t\t    null_sha1, 0, !forcing, msg,\n+\t\t\t\t\t    &err) ||\n+\t\t    transaction_commit(transaction, &err))\n \t\t\tdie(\"%s\", err.buf);\n-\t\tref_transaction_free(transaction);\n+\t\ttransaction_free(transaction);\n \t}\n \n \tif (real_ref && track)\ndiff --git a/builtin/commit.c b/builtin/commit.c\nindex 2346953..1558242 100644\n--- a/builtin/commit.c\n+++ b/builtin/commit.c\n@@ -1757,17 +1757,17 @@ 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-\ttransaction = ref_transaction_begin(&err);\n+\ttransaction = transaction_begin(&err);\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, sb.buf, &err) ||\n-\t    ref_transaction_commit(transaction, &err)) {\n+\t    transaction_update_sha1(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, sb.buf, &err) ||\n+\t    transaction_commit(transaction, &err)) {\n \t\trollback_index_files();\n \t\tdie(\"%s\", err.buf);\n \t}\n-\tref_transaction_free(transaction);\n+\ttransaction_free(transaction);\n \n \tunlink(git_path(\"CHERRY_PICK_HEAD\"));\n \tunlink(git_path(\"REVERT_HEAD\"));\ndiff --git a/builtin/fetch.c b/builtin/fetch.c\nindex 7cd1221..78abc14 100644\n--- a/builtin/fetch.c\n+++ b/builtin/fetch.c\n@@ -414,22 +414,22 @@ static int s_update_ref(const char *action,\n \t\trla = default_rla.buf;\n \tsnprintf(msg, sizeof(msg), \"%s: %s\", rla, action);\n \n-\ttransaction = ref_transaction_begin(&err);\n+\ttransaction = transaction_begin(&err);\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, msg, &err))\n+\t    transaction_update_sha1(transaction, ref->name, ref->new_sha1,\n+\t\t\t\t    ref->old_sha1, 0, check_old, msg, &err))\n \t\tgoto fail;\n \n-\tret = ref_transaction_commit(transaction, &err);\n+\tret = transaction_commit(transaction, &err);\n \tif (ret == UPDATE_REFS_NAME_CONFLICT)\n \t\tdf_conflict = 1;\n \tif (ret)\n \t\tgoto fail;\n \n-\tref_transaction_free(transaction);\n+\ttransaction_free(transaction);\n \treturn 0;\n fail:\n-\tref_transaction_free(transaction);\n+\ttransaction_free(transaction);\n \terror(\"%s\", err.buf);\n \tstrbuf_release(&err);\n \treturn df_conflict ? STORE_REF_ERROR_DF_CONFLICT\ndiff --git a/builtin/receive-pack.c b/builtin/receive-pack.c\nindex d2baef2..ac03640 100644\n--- a/builtin/receive-pack.c\n+++ b/builtin/receive-pack.c\n@@ -582,20 +582,20 @@ static char *update(struct command *cmd, struct shallow_info *si)\n \t\t    update_shallow_ref(cmd, si))\n \t\t  return xstrdup(\"shallow error\");\n \n-\t\ttransaction = ref_transaction_begin(&err);\n+\t\ttransaction = transaction_begin(&err);\n \t\tif (!transaction ||\n-\t\t    ref_transaction_update(transaction, namespaced_name,\n-\t\t\t\t\t   new_sha1, old_sha1, 0, 1, \"push\",\n-\t\t\t\t\t   &err) ||\n-\t\t    ref_transaction_commit(transaction, &err)) {\n+\t\t    transaction_update_sha1(transaction, namespaced_name,\n+\t\t\t\t\t    new_sha1, old_sha1, 0, 1, \"push\",\n+\t\t\t\t\t    &err) ||\n+\t\t    transaction_commit(transaction, &err)) {\n \t\t\tchar *str = strbuf_detach(&err, NULL);\n-\t\t\tref_transaction_free(transaction);\n+\t\t\ttransaction_free(transaction);\n \n \t\t\trp_error(\"%s\", str);\n \t\t\treturn str;\n \t\t}\n \n-\t\tref_transaction_free(transaction);\n+\t\ttransaction_free(transaction);\n \t\tstrbuf_release(&err);\n \t\treturn NULL; /* good */\n \t}\ndiff --git a/builtin/replace.c b/builtin/replace.c\nindex f708f5d..96da9ad 100644\n--- a/builtin/replace.c\n+++ b/builtin/replace.c\n@@ -168,14 +168,14 @@ static int replace_object_sha1(const char *object_ref,\n \n \tcheck_ref_valid(object, prev, ref, sizeof(ref), force);\n \n-\ttransaction = ref_transaction_begin(&err);\n+\ttransaction = transaction_begin(&err);\n \tif (!transaction ||\n-\t    ref_transaction_update(transaction, ref, repl, prev,\n-\t\t\t\t   0, !is_null_sha1(prev), NULL, &err) ||\n-\t    ref_transaction_commit(transaction, &err))\n+\t    transaction_update_sha1(transaction, ref, repl, prev,\n+\t\t\t\t    0, !is_null_sha1(prev), NULL, &err) ||\n+\t    transaction_commit(transaction, &err))\n \t\tdie(\"%s\", err.buf);\n \n-\tref_transaction_free(transaction);\n+\ttransaction_free(transaction);\n \treturn 0;\n }\n \ndiff --git a/builtin/tag.c b/builtin/tag.c\nindex 74af63e..4525c69 100644\n--- a/builtin/tag.c\n+++ b/builtin/tag.c\n@@ -702,13 +702,13 @@ 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-\ttransaction = ref_transaction_begin(&err);\n+\ttransaction = transaction_begin(&err);\n \tif (!transaction ||\n-\t    ref_transaction_update(transaction, ref.buf, object, prev,\n-\t\t\t\t   0, !is_null_sha1(prev), NULL, &err) ||\n-\t    ref_transaction_commit(transaction, &err))\n+\t    transaction_update_sha1(transaction, ref.buf, object, prev,\n+\t\t\t\t    0, !is_null_sha1(prev), NULL, &err) ||\n+\t    transaction_commit(transaction, &err))\n \t\tdie(\"%s\", err.buf);\n-\tref_transaction_free(transaction);\n+\ttransaction_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 28b478a..d62871d 100644\n--- a/builtin/update-ref.c\n+++ b/builtin/update-ref.c\n@@ -199,8 +199,8 @@ 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-\tif (ref_transaction_update(transaction, refname, new_sha1, old_sha1,\n-\t\t\t\t   update_flags, have_old, msg, &err))\n+\tif (transaction_update_sha1(transaction, refname, new_sha1, old_sha1,\n+\t\t\t\t    update_flags, have_old, msg, &err))\n \t\tdie(\"%s\", err.buf);\n \n \tupdate_flags = 0;\n@@ -227,8 +227,8 @@ 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-\tif (ref_transaction_create(transaction, refname, new_sha1,\n-\t\t\t\t   update_flags, msg, &err))\n+\tif (transaction_create_sha1(transaction, refname, new_sha1,\n+\t\t\t\t    update_flags, msg, &err))\n \t\tdie(\"%s\", err.buf);\n \n \tupdate_flags = 0;\n@@ -259,8 +259,8 @@ 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-\tif (ref_transaction_delete(transaction, refname, old_sha1,\n-\t\t\t\t   update_flags, have_old, msg, &err))\n+\tif (transaction_delete_sha1(transaction, refname, old_sha1,\n+\t\t\t\t    update_flags, have_old, msg, &err))\n \t\tdie(\"%s\", err.buf);\n \n \tupdate_flags = 0;\n@@ -292,8 +292,8 @@ 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-\tif (ref_transaction_update(transaction, refname, new_sha1, old_sha1,\n-\t\t\t\t   update_flags, have_old, msg, &err))\n+\tif (transaction_update_sha1(transaction, refname, new_sha1, old_sha1,\n+\t\t\t\t    update_flags, have_old, msg, &err))\n \t\tdie(\"%s\", err.buf);\n \n \tupdate_flags = 0;\n@@ -366,15 +366,15 @@ 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\ttransaction = ref_transaction_begin(&err);\n+\t\ttransaction = transaction_begin(&err);\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\tif (ref_transaction_commit(transaction, &err))\n+\t\tif (transaction_commit(transaction, &err))\n \t\t\tdie(\"%s\", err.buf);\n-\t\tref_transaction_free(transaction);\n+\t\ttransaction_free(transaction);\n \t\treturn 0;\n \t}\n \ndiff --git a/fast-import.c b/fast-import.c\nindex ac586b9..577c107 100644\n--- a/fast-import.c\n+++ b/fast-import.c\n@@ -1706,17 +1706,17 @@ static int update_branch(struct branch *b)\n \t\t\treturn -1;\n \t\t}\n \t}\n-\ttransaction = ref_transaction_begin(&err);\n+\ttransaction = transaction_begin(&err);\n \tif (!transaction ||\n-\t    ref_transaction_update(transaction, b->name, b->sha1, old_sha1,\n-\t\t\t\t   0, 1, msg, &err) ||\n-\t    ref_transaction_commit(transaction, &err)) {\n-\t\tref_transaction_free(transaction);\n+\t    transaction_update_sha1(transaction, b->name, b->sha1, old_sha1,\n+\t\t\t\t    0, 1, msg, &err) ||\n+\t    transaction_commit(transaction, &err)) {\n+\t\ttransaction_free(transaction);\n \t\terror(\"%s\", err.buf);\n \t\tstrbuf_release(&err);\n \t\treturn -1;\n \t}\n-\tref_transaction_free(transaction);\n+\ttransaction_free(transaction);\n \treturn 0;\n }\n \n@@ -1739,7 +1739,7 @@ static void dump_tags(void)\n \tstruct strbuf err = STRBUF_INIT;\n \tstruct ref_transaction *transaction;\n \n-\ttransaction = ref_transaction_begin(&err);\n+\ttransaction = transaction_begin(&err);\n \tif (!transaction) {\n \t\tfailure |= error(\"%s\", err.buf);\n \t\tgoto cleanup;\n@@ -1748,17 +1748,17 @@ static void dump_tags(void)\n \t\tstrbuf_reset(&ref_name);\n \t\tstrbuf_addf(&ref_name, \"refs/tags/%s\", t->name);\n \n-\t\tif (ref_transaction_update(transaction, ref_name.buf, t->sha1,\n-\t\t\t\t\t   NULL, 0, 0, msg, &err)) {\n+\t\tif (transaction_update_sha1(transaction, ref_name.buf, t->sha1,\n+\t\t\t\t\t    NULL, 0, 0, msg, &err)) {\n \t\t\tfailure |= error(\"%s\", err.buf);\n \t\t\tgoto cleanup;\n \t\t}\n \t}\n-\tif (ref_transaction_commit(transaction, &err))\n+\tif (transaction_commit(transaction, &err))\n \t\tfailure |= error(\"%s\", err.buf);\n \n  cleanup:\n-\tref_transaction_free(transaction);\n+\ttransaction_free(transaction);\n \tstrbuf_release(&ref_name);\n \tstrbuf_release(&err);\n }\ndiff --git a/refs.c b/refs.c\nindex 76732a4..c57148f 100644\n--- a/refs.c\n+++ b/refs.c\n@@ -2403,7 +2403,7 @@ static void try_remove_empty_parents(char *name)\n }\n \n /*\n- * Used as a flag to ref_transaction_delete when a loose ref is being\n+ * Used as a flag to transaction_delete_sha1 when a loose ref is being\n  * pruned.\n  */\n #define REF_ISPRUNING\t0x0100\n@@ -2417,17 +2417,17 @@ static void prune_ref(struct ref_to_prune *r)\n \tif (check_refname_format(r->name + 5, 0))\n \t\treturn;\n \n-\ttransaction = ref_transaction_begin(&err);\n+\ttransaction = transaction_begin(&err);\n \tif (!transaction ||\n-\t    ref_transaction_delete(transaction, r->name, r->sha1,\n-\t\t\t\t   REF_ISPRUNING, 1, NULL, &err) ||\n-\t    ref_transaction_commit(transaction, &err)) {\n-\t\tref_transaction_free(transaction);\n+\t    transaction_delete_sha1(transaction, r->name, r->sha1,\n+\t\t\t\t    REF_ISPRUNING, 1, NULL, &err) ||\n+\t    transaction_commit(transaction, &err)) {\n+\t\ttransaction_free(transaction);\n \t\terror(\"%s\", err.buf);\n \t\tstrbuf_release(&err);\n \t\treturn;\n \t}\n-\tref_transaction_free(transaction);\n+\ttransaction_free(transaction);\n \ttry_remove_empty_parents(r->name);\n }\n \n@@ -2616,17 +2616,17 @@ int delete_ref(const char *refname, const unsigned char *sha1, int delopt)\n \tstruct ref_transaction *transaction;\n \tstruct strbuf err = STRBUF_INIT;\n \n-\ttransaction = ref_transaction_begin(&err);\n+\ttransaction = transaction_begin(&err);\n \tif (!transaction ||\n-\t    ref_transaction_delete(transaction, refname, sha1, delopt,\n-\t\t\t\t   sha1 && !is_null_sha1(sha1), NULL, &err) ||\n-\t    ref_transaction_commit(transaction, &err)) {\n+\t    transaction_delete_sha1(transaction, refname, sha1, delopt,\n+\t\t\t\t    sha1 && !is_null_sha1(sha1), NULL, &err) ||\n+\t    transaction_commit(transaction, &err)) {\n \t\terror(\"%s\", err.buf);\n-\t\tref_transaction_free(transaction);\n+\t\ttransaction_free(transaction);\n \t\tstrbuf_release(&err);\n \t\treturn 1;\n \t}\n-\tref_transaction_free(transaction);\n+\ttransaction_free(transaction);\n \treturn 0;\n }\n \n@@ -3445,12 +3445,12 @@ struct ref_transaction {\n \tint status;\n };\n \n-struct ref_transaction *ref_transaction_begin(struct strbuf *err)\n+struct ref_transaction *transaction_begin(struct strbuf *err)\n {\n \treturn xcalloc(1, sizeof(struct ref_transaction));\n }\n \n-void ref_transaction_free(struct ref_transaction *transaction)\n+void transaction_free(struct ref_transaction *transaction)\n {\n \tint i;\n \n@@ -3477,12 +3477,12 @@ static struct ref_update *add_update(struct ref_transaction *transaction,\n \treturn update;\n }\n \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, const char *msg,\n-\t\t\t   struct strbuf *err)\n+int transaction_update_sha1(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, const char *msg,\n+\t\t\t    struct strbuf *err)\n {\n \tstruct ref_update *update;\n \n@@ -3503,11 +3503,11 @@ int ref_transaction_update(struct ref_transaction *transaction,\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, const char *msg,\n-\t\t\t   struct strbuf *err)\n+int transaction_create_sha1(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, const char *msg,\n+\t\t\t    struct strbuf *err)\n {\n \tif (transaction->state != REF_TRANSACTION_OPEN)\n \t\tdie(\"BUG: create called for transaction that is not open\");\n@@ -3515,15 +3515,15 @@ int ref_transaction_create(struct ref_transaction *transaction,\n \tif (!new_sha1 || is_null_sha1(new_sha1))\n \t\tdie(\"BUG: create ref with null new_sha1\");\n \n-\treturn ref_transaction_update(transaction, refname, new_sha1,\n-\t\t\t\t      null_sha1, flags, 1, msg, err);\n+\treturn transaction_update_sha1(transaction, refname, new_sha1,\n+\t\t\t\t       null_sha1, flags, 1, msg, err);\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, const char *msg,\n-\t\t\t   struct strbuf *err)\n+int transaction_delete_sha1(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, const char *msg,\n+\t\t\t    struct strbuf *err)\n {\n \tif (transaction->state != REF_TRANSACTION_OPEN)\n \t\tdie(\"BUG: delete called for transaction that is not open\");\n@@ -3534,7 +3534,7 @@ int ref_transaction_delete(struct ref_transaction *transaction,\n \tif (have_old && is_null_sha1(old_sha1))\n \t\tdie(\"BUG: have_old is true but old_sha1 is null_sha1\");\n \n-\treturn ref_transaction_update(transaction, refname, null_sha1,\n+\treturn transaction_update_sha1(transaction, refname, null_sha1,\n \t\t\t\t      old_sha1, flags, have_old, msg, err);\n }\n \n@@ -3545,14 +3545,14 @@ int update_ref(const char *action, const char *refname,\n \tstruct ref_transaction *t;\n \tstruct strbuf err = STRBUF_INIT;\n \n-\tt = ref_transaction_begin(&err);\n+\tt = transaction_begin(&err);\n \tif (!t ||\n-\t    ref_transaction_update(t, refname, sha1, oldval, flags,\n-\t\t\t\t   !!oldval, action, &err) ||\n-\t    ref_transaction_commit(t, &err)) {\n+\t    transaction_update_sha1(t, refname, sha1, oldval, flags,\n+\t\t\t\t    !!oldval, action, &err) ||\n+\t    transaction_commit(t, &err)) {\n \t\tconst char *str = \"update_ref failed for ref '%s': %s\";\n \n-\t\tref_transaction_free(t);\n+\t\ttransaction_free(t);\n \t\tswitch (onerr) {\n \t\tcase UPDATE_REFS_MSG_ON_ERR:\n \t\t\terror(str, refname, err.buf); break;\n@@ -3589,7 +3589,7 @@ static int ref_update_reject_duplicates(struct ref_update **updates, int n,\n \treturn 0;\n }\n \n-int ref_transaction_commit(struct ref_transaction *transaction,\n+int transaction_commit(struct ref_transaction *transaction,\n \t\t\t   struct strbuf *err)\n {\n \tint ret = 0, delnum = 0, i, df_conflict = 0;\ndiff --git a/refs.h b/refs.h\nindex 43aafb7..07bd621 100644\n--- a/refs.h\n+++ b/refs.h\n@@ -17,16 +17,16 @@ struct ref_lock {\n  * Calling sequence\n  * ----------------\n  * - Allocate and initialize a `struct ref_transaction` by calling\n- *   `ref_transaction_begin()`.\n+ *   `transaction_begin()`.\n  *\n  * - List intended ref updates by calling functions like\n- *   `ref_transaction_update()` and `ref_transaction_create()`.\n+ *   `transaction_update_sha1()` and `transaction_create_sha1()`.\n  *\n- * - Call `ref_transaction_commit()` to execute the transaction.\n+ * - Call `transaction_commit()` to execute the transaction.\n  *   If this succeeds, the ref updates will have taken place and\n  *   the transaction cannot be rolled back.\n  *\n- * - At any time call `ref_transaction_free()` to discard the\n+ * - At any time call `transaction_free()` to discard the\n  *   transaction and free associated resources.  In particular,\n  *   this rolls back the transaction if it has not been\n  *   successfully committed.\n@@ -44,7 +44,7 @@ struct ref_lock {\n  * error message:\n  *\n  *     strbuf_addf(&err, \"Error while doing foo-bar: \");\n- *     if (ref_transaction_update(..., &err)) {\n+ *     if (transaction_update_sha1(..., &err)) {\n  *         ret = error(\"%s\", err.buf);\n  *         goto cleanup;\n  *     }\n@@ -177,8 +177,8 @@ extern int ref_exists(const char *);\n extern int peel_ref(const char *refname, unsigned char *sha1);\n \n /*\n- * Flags controlling lock_any_ref_for_update(), ref_transaction_update(),\n- * ref_transaction_create(), etc.\n+ * Flags controlling lock_any_ref_for_update(), transaction_update_sha1(),\n+ * transaction_create_sha1(), etc.\n  * REF_NODEREF: act on the ref directly, instead of dereferencing\n  *              symbolic references.\n  *\n@@ -267,9 +267,9 @@ enum action_on_err {\n \n /*\n  * Begin a reference transaction.  The reference transaction must\n- * be freed by calling ref_transaction_free().\n+ * be freed by calling transaction_free().\n  */\n-struct ref_transaction *ref_transaction_begin(struct strbuf *err);\n+struct ref_transaction *transaction_begin(struct strbuf *err);\n \n /*\n  * The following functions add a reference check or update to a\n@@ -290,12 +290,12 @@ struct ref_transaction *ref_transaction_begin(struct strbuf *err);\n  * means that the transaction as a whole has failed and will need to be\n  * rolled back.\n  */\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, const char *msg,\n-\t\t\t   struct strbuf *err);\n+int transaction_update_sha1(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, const char *msg,\n+\t\t\t    struct strbuf *err);\n \n /*\n  * Add a reference creation to transaction.  new_sha1 is the value\n@@ -306,11 +306,11 @@ int ref_transaction_update(struct ref_transaction *transaction,\n  * means that the transaction as a whole has failed and will need to be\n  * rolled back.\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, const char *msg,\n-\t\t\t   struct strbuf *err);\n+int transaction_create_sha1(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, const char *msg,\n+\t\t\t    struct strbuf *err);\n \n /*\n  * Add a reference deletion to transaction.  If have_old is true, then\n@@ -320,11 +320,11 @@ int ref_transaction_create(struct ref_transaction *transaction,\n  * means that the transaction as a whole has failed and will need to be\n  * rolled back.\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, const char *msg,\n-\t\t\t   struct strbuf *err);\n+int transaction_delete_sha1(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, const char *msg,\n+\t\t\t    struct strbuf *err);\n \n /*\n  * Commit all of the changes that have been queued in transaction, as\n@@ -337,13 +337,13 @@ int ref_transaction_delete(struct ref_transaction *transaction,\n  * collision (ENOTDIR).\n  */\n #define UPDATE_REFS_NAME_CONFLICT -2\n-int ref_transaction_commit(struct ref_transaction *transaction,\n-\t\t\t   struct strbuf *err);\n+int transaction_commit(struct ref_transaction *transaction,\n+\t\t       struct strbuf *err);\n \n /*\n  * Free an existing transaction and all associated data.\n  */\n-void ref_transaction_free(struct ref_transaction *transaction);\n+void 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,\ndiff --git a/sequencer.c b/sequencer.c\nindex ee20726..b9dc0db 100644\n--- a/sequencer.c\n+++ b/sequencer.c\n@@ -247,12 +247,12 @@ static int fast_forward_to(const unsigned char *to, const unsigned char *from,\n \n \tstrbuf_addf(&sb, \"%s: fast-forward\", action_name(opts));\n \n-\ttransaction = ref_transaction_begin(&err);\n+\ttransaction = transaction_begin(&err);\n \tif (!transaction ||\n-\t    ref_transaction_update(transaction, \"HEAD\", to, from,\n-\t\t\t\t   0, !unborn, sb.buf, &err) ||\n-\t    ref_transaction_commit(transaction, &err)) {\n-\t\tref_transaction_free(transaction);\n+\t    transaction_update_sha1(transaction, \"HEAD\", to, from,\n+\t\t\t\t    0, !unborn, sb.buf, &err) ||\n+\t    transaction_commit(transaction, &err)) {\n+\t\ttransaction_free(transaction);\n \t\terror(\"%s\", err.buf);\n \t\tstrbuf_release(&sb);\n \t\tstrbuf_release(&err);\n@@ -260,7 +260,7 @@ static int fast_forward_to(const unsigned char *to, const unsigned char *from,\n \t}\n \n \tstrbuf_release(&sb);\n-\tref_transaction_free(transaction);\n+\ttransaction_free(transaction);\n \treturn 0;\n }\n \ndiff --git a/walker.c b/walker.c\nindex fd9ef87..d22d07a 100644\n--- a/walker.c\n+++ b/walker.c\n@@ -261,7 +261,7 @@ int walker_fetch(struct walker *walker, int targets, char **target,\n \tsave_commit_buffer = 0;\n \n \tif (write_ref) {\n-\t\ttransaction = ref_transaction_begin(&err);\n+\t\ttransaction = transaction_begin(&err);\n \t\tif (!transaction) {\n \t\t\terror(\"%s\", err.buf);\n \t\t\tgoto rollback_and_fail;\n@@ -293,27 +293,27 @@ int walker_fetch(struct walker *walker, int targets, char **target,\n \t\t\tcontinue;\n \t\tstrbuf_reset(&ref_name);\n \t\tstrbuf_addf(&ref_name, \"refs/%s\", write_ref[i]);\n-\t\tif (ref_transaction_update(transaction, ref_name.buf,\n-\t\t\t\t\t   &sha1[20 * i], NULL, 0, 0,\n-\t\t\t\t\t   msg ? msg : \"fetch (unknown)\",\n-\t\t\t\t\t   &err)) {\n+\t\tif (transaction_update_sha1(transaction, ref_name.buf,\n+\t\t\t\t\t    &sha1[20 * i], NULL, 0, 0,\n+\t\t\t\t\t    msg ? msg : \"fetch (unknown)\",\n+\t\t\t\t\t    &err)) {\n \t\t\terror(\"%s\", err.buf);\n \t\t\tgoto rollback_and_fail;\n \t\t}\n \t}\n \tif (write_ref) {\n-\t\tif (ref_transaction_commit(transaction, &err)) {\n+\t\tif (transaction_commit(transaction, &err)) {\n \t\t\terror(\"%s\", err.buf);\n \t\t\tgoto rollback_and_fail;\n \t\t}\n-\t\tref_transaction_free(transaction);\n+\t\ttransaction_free(transaction);\n \t}\n \n \tfree(msg);\n \treturn 0;\n \n rollback_and_fail:\n-\tref_transaction_free(transaction);\n+\ttransaction_free(transaction);\n \tfree(msg);\n \tstrbuf_release(&err);\n \tstrbuf_release(&ref_name);\n-- \n2.0.0.770.gd892650.dirty\n"},{"id":"244265","messageId":"1402937505-6091-5-git-send-email-sahlberg@google.com","threadId":"36928","inReplyTo":"1402937505-6091-1-git-send-email-sahlberg@google.com","subject":"[PATCH v2 04/14] refs.c: add a new update_type field to ref_update","fromName":"Ronnie Sahlberg","fromEmail":"sahlberg@google.com","sentAt":"2014-06-16T16:51:35Z","receivedAt":"2014-06-16T16:51:35Z","isPatch":true,"sender":{"key":"sahlberg@google.com","avatar":"https://avatars.githubusercontent.com/u/7320636?v=4"},"body":"Add a field that describes what type of update this refers to. For now\nthe only type is UPDATE_SHA1 but we will soon add more types.\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 c57148f..ca924d7 100644\n--- a/refs.c\n+++ b/refs.c\n@@ -3396,6 +3396,10 @@ int for_each_reflog(each_ref_fn fn, void *cb_data)\n \treturn retval;\n }\n \n+enum transaction_update_type {\n+       UPDATE_SHA1 = 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@@ -3403,6 +3407,7 @@ int for_each_reflog(each_ref_fn fn, void *cb_data)\n  * value or to zero to ensure the ref does not exist before update.\n  */\n struct ref_update {\n+\tenum transaction_update_type update_type;\n \tunsigned char new_sha1[20];\n \tunsigned char old_sha1[20];\n \tint flags; /* REF_NODEREF? */\n@@ -3466,12 +3471,14 @@ void transaction_free(struct ref_transaction *transaction)\n }\n \n static struct ref_update *add_update(struct ref_transaction *transaction,\n-\t\t\t\t     const char *refname)\n+\t\t\t\t     const char *refname,\n+\t\t\t\t     enum transaction_update_type update_type)\n {\n \tsize_t len = strlen(refname);\n \tstruct ref_update *update = xcalloc(1, sizeof(*update) + len + 1);\n \n \tstrcpy((char *)update->refname, refname);\n+\tupdate->update_type = update_type;\n \tALLOC_GROW(transaction->updates, transaction->nr + 1, transaction->alloc);\n \ttransaction->updates[transaction->nr++] = update;\n \treturn update;\n@@ -3492,7 +3499,7 @@ int transaction_update_sha1(struct ref_transaction *transaction,\n \tif (have_old && !old_sha1)\n \t\tdie(\"BUG: have_old is true but old_sha1 is NULL\");\n \n-\tupdate = add_update(transaction, refname);\n+\tupdate = add_update(transaction, refname, UPDATE_SHA1);\n \thashcpy(update->new_sha1, new_sha1);\n \tupdate->flags = flags;\n \tupdate->have_old = have_old;\n@@ -3577,7 +3584,10 @@ static int ref_update_reject_duplicates(struct ref_update **updates, int n,\n \t\t\t\t\tstruct strbuf *err)\n {\n \tint i;\n-\tfor (i = 1; i < n; i++)\n+\tfor (i = 1; i < n; i++) {\n+\t\tif (updates[i - 1]->update_type != UPDATE_SHA1 ||\n+\t\t    updates[i]->update_type != UPDATE_SHA1)\n+\t\t\tcontinue;\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@@ -3586,6 +3596,7 @@ static int ref_update_reject_duplicates(struct ref_update **updates, int n,\n \n \t\t\treturn 1;\n \t\t}\n+\t}\n \treturn 0;\n }\n \n@@ -3615,10 +3626,12 @@ int transaction_commit(struct ref_transaction *transaction,\n \t\tgoto cleanup;\n \t}\n \n-\t/* Acquire all locks while verifying old values */\n+\t/* Acquire all ref 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->update_type != UPDATE_SHA1)\n+\t\t\tcontinue;\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@@ -3641,6 +3654,8 @@ int 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->update_type != UPDATE_SHA1)\n+\t\t\tcontinue;\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     update->msg);\n@@ -3660,6 +3675,8 @@ int 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->update_type != UPDATE_SHA1)\n+\t\t\tcontinue;\n \t\tif (update->lock) {\n \t\t\tif (delete_ref_loose(update->lock, update->type, err))\n \t\t\t\tret = -1;\n-- \n2.0.0.770.gd892650.dirty\n"},{"id":"244255","messageId":"1402937505-6091-6-git-send-email-sahlberg@google.com","threadId":"36928","inReplyTo":"1402937505-6091-1-git-send-email-sahlberg@google.com","subject":"[PATCH v2 05/14] refs.c: add a function to append a reflog entry to a fd","fromName":"Ronnie Sahlberg","fromEmail":"sahlberg@google.com","sentAt":"2014-06-16T16:51:36Z","receivedAt":"2014-06-16T16:51:36Z","isPatch":true,"sender":{"key":"sahlberg@google.com","avatar":"https://avatars.githubusercontent.com/u/7320636?v=4"},"body":"Break out the code to create the string and writing it to the file\ndescriptor from log_ref_write and into a dedicated function log_ref_write_fd.\nFor now this is only used from log_ref_write but later on we will call\nthis function from reflog transactions too which means that we will end\nup with only a single place where we write a reflog entry to a file instead\nof the current two places (log_ref_write and builtin/reflog.c).\n\nSigned-off-by: Ronnie Sahlberg <sahlberg@google.com>\n---\n refs.c | 48 ++++++++++++++++++++++++++++++------------------\n 1 file changed, 30 insertions(+), 18 deletions(-)\n\ndiff --git a/refs.c b/refs.c\nindex ca924d7..e951f34 100644\n--- a/refs.c\n+++ b/refs.c\n@@ -2884,16 +2884,38 @@ int log_ref_setup(const char *refname, struct strbuf *logfile)\n \treturn 0;\n }\n \n+static int log_ref_write_fd(int fd, const unsigned char *old_sha1,\n+\t\t\t    const unsigned char *new_sha1,\n+\t\t\t    const char *committer, const char *msg)\n+{\n+\tint msglen, written;\n+\tunsigned maxlen, len;\n+\tchar *logrec;\n+\n+\tmsglen = msg ? strlen(msg) : 0;\n+\tmaxlen = strlen(committer) + msglen + 100;\n+\tlogrec = xmalloc(maxlen);\n+\tlen = sprintf(logrec, \"%s %s %s\\n\",\n+\t\t      sha1_to_hex(old_sha1),\n+\t\t      sha1_to_hex(new_sha1),\n+\t\t      committer);\n+\tif (msglen)\n+\t\tlen += copy_msg(logrec + len - 1, msg) - 1;\n+\n+\twritten = len <= maxlen ? write_in_full(fd, logrec, len) : -1;\n+\tfree(logrec);\n+\tif (written != len)\n+\t\treturn -1;\n+\n+\treturn 0;\n+}\n+\n static int log_ref_write(const char *refname, const unsigned char *old_sha1,\n \t\t\t const unsigned char *new_sha1, const char *msg)\n {\n-\tint logfd, result, written, oflags = O_APPEND | O_WRONLY;\n-\tunsigned maxlen, len;\n-\tint msglen;\n+\tint logfd, result, oflags = O_APPEND | O_WRONLY;\n \tstruct strbuf sb_log_file = STRBUF_INIT;\n \tconst char *log_file;\n-\tchar *logrec;\n-\tconst char *committer;\n \n \tif (log_all_ref_updates < 0)\n \t\tlog_all_ref_updates = !is_bare_repository();\n@@ -2906,19 +2928,9 @@ static int log_ref_write(const char *refname, const unsigned char *old_sha1,\n \tlogfd = open(log_file, oflags);\n \tif (logfd < 0)\n \t\tgoto done;\n-\tmsglen = msg ? strlen(msg) : 0;\n-\tcommitter = git_committer_info(0);\n-\tmaxlen = strlen(committer) + msglen + 100;\n-\tlogrec = xmalloc(maxlen);\n-\tlen = sprintf(logrec, \"%s %s %s\\n\",\n-\t\t      sha1_to_hex(old_sha1),\n-\t\t      sha1_to_hex(new_sha1),\n-\t\t      committer);\n-\tif (msglen)\n-\t\tlen += copy_msg(logrec + len - 1, msg) - 1;\n-\twritten = len <= maxlen ? write_in_full(logfd, logrec, len) : -1;\n-\tfree(logrec);\n-\tif (written != len) {\n+\tresult = log_ref_write_fd(logfd, old_sha1, new_sha1,\n+\t\t\t\t  git_committer_info(0), msg);\n+\tif (result) {\n \t\tint save_errno = errno;\n \t\tclose(logfd);\n \t\terror(\"Unable to append to %s\", log_file);\n-- \n2.0.0.770.gd892650.dirty\n"},{"id":"244254","messageId":"1402937505-6091-7-git-send-email-sahlberg@google.com","threadId":"36928","inReplyTo":"1402937505-6091-1-git-send-email-sahlberg@google.com","subject":"[PATCH v2 06/14] lockfile.c: make hold_lock_file_for_append preserve meaningful errno","fromName":"Ronnie Sahlberg","fromEmail":"sahlberg@google.com","sentAt":"2014-06-16T16:51:37Z","receivedAt":"2014-06-16T16:51:37Z","isPatch":true,"sender":{"key":"sahlberg@google.com","avatar":"https://avatars.githubusercontent.com/u/7320636?v=4"},"body":"Update hold_lock_file_for_append and copy_fd to return a meaningful errno\non failure.\n\nSigned-off-by: Ronnie Sahlberg <sahlberg@google.com>\n---\n copy.c     | 20 +++++++++++++-------\n lockfile.c |  7 ++++++-\n 2 files changed, 19 insertions(+), 8 deletions(-)\n\ndiff --git a/copy.c b/copy.c\nindex a7f58fd..5cb8679 100644\n--- a/copy.c\n+++ b/copy.c\n@@ -9,10 +9,12 @@ int copy_fd(int ifd, int ofd)\n \t\tif (!len)\n \t\t\tbreak;\n \t\tif (len < 0) {\n-\t\t\tint read_error = errno;\n+\t\t\tint save_errno = errno;\n \t\t\tclose(ifd);\n-\t\t\treturn error(\"copy-fd: read returned %s\",\n-\t\t\t\t     strerror(read_error));\n+\t\t\terror(\"copy-fd: read returned %s\",\n+\t\t\t      strerror(save_errno));\n+\t\t\terrno = save_errno;\n+\t\t\treturn -1;\n \t\t}\n \t\twhile (len) {\n \t\t\tint written = xwrite(ofd, buf, len);\n@@ -22,12 +24,16 @@ int copy_fd(int ifd, int ofd)\n \t\t\t}\n \t\t\telse if (!written) {\n \t\t\t\tclose(ifd);\n-\t\t\t\treturn error(\"copy-fd: write returned 0\");\n+\t\t\t\terror(\"copy-fd: write returned 0\");\n+\t\t\t\terrno = EAGAIN;\n+\t\t\t\treturn -1;\n \t\t\t} else {\n-\t\t\t\tint write_error = errno;\n+\t\t\t\tint save_errno = errno;\n \t\t\t\tclose(ifd);\n-\t\t\t\treturn error(\"copy-fd: write returned %s\",\n-\t\t\t\t\t     strerror(write_error));\n+\t\t\t\terror(\"copy-fd: write returned %s\",\n+\t\t\t\t      strerror(save_errno));\n+\t\t\t\terrno = save_errno;\n+\t\t\t\treturn -1;\n \t\t\t}\n \t\t}\n \t}\ndiff --git a/lockfile.c b/lockfile.c\nindex 4899270..d29bedf 100644\n--- a/lockfile.c\n+++ b/lockfile.c\n@@ -250,15 +250,20 @@ int hold_lock_file_for_append(struct lock_file *lk, const char *path, int flags)\n \torig_fd = open(path, O_RDONLY);\n \tif (orig_fd < 0) {\n \t\tif (errno != ENOENT) {\n+\t\t\tint save_errno = errno;\n \t\t\tif (flags & LOCK_DIE_ON_ERROR)\n \t\t\t\tdie(\"cannot open '%s' for copying\", path);\n \t\t\trollback_lock_file(lk);\n-\t\t\treturn error(\"cannot open '%s' for copying\", path);\n+\t\t\terror(\"cannot open '%s' for copying\", path);\n+\t\t\terrno = save_errno;\n+\t\t\treturn -1;\n \t\t}\n \t} else if (copy_fd(orig_fd, fd)) {\n+\t\tint save_errno = errno;\n \t\tif (flags & LOCK_DIE_ON_ERROR)\n \t\t\texit(128);\n \t\trollback_lock_file(lk);\n+\t\terrno = save_errno;\n \t\treturn -1;\n \t}\n \treturn fd;\n-- \n2.0.0.770.gd892650.dirty\n"},{"id":"244257","messageId":"1402937505-6091-8-git-send-email-sahlberg@google.com","threadId":"36928","inReplyTo":"1402937505-6091-1-git-send-email-sahlberg@google.com","subject":"[PATCH v2 07/14] refs.c: add a transaction function to append a reflog entry","fromName":"Ronnie Sahlberg","fromEmail":"sahlberg@google.com","sentAt":"2014-06-16T16:51:38Z","receivedAt":"2014-06-16T16:51:38Z","isPatch":true,"sender":{"key":"sahlberg@google.com","avatar":"https://avatars.githubusercontent.com/u/7320636?v=4"},"body":"Define a new transaction update type, UPDATE_LOG, and a new function\ntransaction_update_reflog. This function will lock the reflog and append\nan entry to it during transaction commit.\n\nSigned-off-by: Ronnie Sahlberg <sahlberg@google.com>\n---\n refs.c | 101 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-\n refs.h |  12 ++++++++\n 2 files changed, 112 insertions(+), 1 deletion(-)\n\ndiff --git a/refs.c b/refs.c\nindex e951f34..9db5896 100644\n--- a/refs.c\n+++ b/refs.c\n@@ -3410,6 +3410,7 @@ int for_each_reflog(each_ref_fn fn, void *cb_data)\n \n enum transaction_update_type {\n        UPDATE_SHA1 = 0,\n+       UPDATE_LOG = 1,\n };\n \n /**\n@@ -3427,6 +3428,12 @@ struct ref_update {\n \tstruct ref_lock *lock;\n \tint type;\n \tchar *msg;\n+\n+\t/* used by reflog updates */\n+\tint reflog_fd;\n+\tstruct lock_file reflog_lock;\n+\tchar *committer;\n+\n \tconst char refname[FLEX_ARRAY];\n };\n \n@@ -3476,6 +3483,7 @@ void transaction_free(struct ref_transaction *transaction)\n \n \tfor (i = 0; i < transaction->nr; i++) {\n \t\tfree(transaction->updates[i]->msg);\n+\t\tfree(transaction->updates[i]->committer);\n \t\tfree(transaction->updates[i]);\n \t}\n \tfree(transaction->updates);\n@@ -3496,6 +3504,42 @@ static struct ref_update *add_update(struct ref_transaction *transaction,\n \treturn update;\n }\n \n+int transaction_update_reflog(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      const unsigned char *email,\n+\t\t\t      unsigned long timestamp, int tz,\n+\t\t\t      const char *msg, int flags,\n+\t\t\t      struct strbuf *err)\n+{\n+\tstruct ref_update *update;\n+\n+\tif (transaction->state != REF_TRANSACTION_OPEN)\n+\t\tdie(\"BUG: update_reflog called for transaction that is not \"\n+\t\t    \"open\");\n+\n+\tupdate = add_update(transaction, refname, UPDATE_LOG);\n+\thashcpy(update->new_sha1, new_sha1);\n+\thashcpy(update->old_sha1, old_sha1);\n+\tupdate->reflog_fd = -1;\n+\tif (email) {\n+\t\tstruct strbuf buf = STRBUF_INIT;\n+\t\tchar sign = (tz < 0) ? '-' : '+';\n+\t\tint zone = (tz < 0) ? (-tz) : tz;\n+\n+\t\tstrbuf_addf(&buf, \"%s %lu %c%04d\", email, timestamp, sign,\n+\t\t\t    zone);\n+\t\tupdate->committer = xstrdup(buf.buf);\n+\t\tstrbuf_release(&buf);\n+\t}\n+\tif (msg)\n+\t\tupdate->msg = xstrdup(msg);\n+\tupdate->flags = flags;\n+\n+\treturn 0;\n+}\n+\n int transaction_update_sha1(struct ref_transaction *transaction,\n \t\t\t    const char *refname,\n \t\t\t    const unsigned char *new_sha1,\n@@ -3662,7 +3706,28 @@ int transaction_commit(struct ref_transaction *transaction,\n \t\t}\n \t}\n \n-\t/* Perform updates first so live commits remain referenced */\n+\t/* Lock all reflog files */\n+\tfor (i = 0; i < n; i++) {\n+\t\tstruct ref_update *update = updates[i];\n+\n+\t\tif (update->update_type != UPDATE_LOG)\n+\t\t\tcontinue;\n+\t\tupdate->reflog_fd = hold_lock_file_for_append(\n+\t\t\t\t\t&update->reflog_lock,\n+\t\t\t\t\tgit_path(\"logs/%s\", update->refname),\n+\t\t\t\t\t0);\n+\t\tif (update->reflog_fd < 0) {\n+\t\t\tconst char *str = \"Cannot lock reflog for '%s'. %s\";\n+\n+\t\t\tret = -1;\n+\t\t\tif (err)\n+\t\t\t\t  strbuf_addf(err, str, update->refname,\n+\t\t\t\t\t      strerror(errno));\n+\t\t\tgoto cleanup;\n+\t\t}\n+\t}\n+\n+\t/* Perform ref updates first so live commits remain referenced */\n \tfor (i = 0; i < n; i++) {\n \t\tstruct ref_update *update = updates[i];\n \n@@ -3698,6 +3763,40 @@ int transaction_commit(struct ref_transaction *transaction,\n \t\t}\n \t}\n \n+\t/* Update all reflog files */\n+\tfor (i = 0; i < n; i++) {\n+\t\tstruct ref_update *update = updates[i];\n+\n+\t\tif (update->update_type != UPDATE_LOG)\n+\t\t\tcontinue;\n+\t\tif (update->reflog_fd == -1)\n+\t\t\tcontinue;\n+\n+\t\tif (log_ref_write_fd(update->reflog_fd, update->old_sha1,\n+\t\t\t\t     update->new_sha1,\n+\t\t\t\t     update->committer, update->msg)) {\n+\t\t\terror(\"Could write to reflog: %s. %s\",\n+\t\t\t      update->refname, strerror(errno));\n+\t\t\trollback_lock_file(&update->reflog_lock);\n+\t\t\tupdate->reflog_fd = -1;\n+\t\t}\n+\t}\n+\n+\t/* Commit all reflog files */\n+\tfor (i = 0; i < n; i++) {\n+\t\tstruct ref_update *update = updates[i];\n+\n+\t\tif (update->update_type != UPDATE_LOG)\n+\t\t\tcontinue;\n+\t\tif (update->reflog_fd == -1)\n+\t\t\tcontinue;\n+\t\tif (commit_lock_file(&update->reflog_lock)) {\n+\t\t\terror(\"Could not commit reflog: %s. %s\",\n+\t\t\t      update->refname, strerror(errno));\n+\t\t\tupdate->reflog_fd = -1;\n+\t\t}\n+\t}\n+\n \tif (repack_without_refs(delnames, delnum, err))\n \t\tret = -1;\n \tfor (i = 0; i < delnum; i++)\ndiff --git a/refs.h b/refs.h\nindex 07bd621..5f4deef 100644\n--- a/refs.h\n+++ b/refs.h\n@@ -327,6 +327,18 @@ int transaction_delete_sha1(struct ref_transaction *transaction,\n \t\t\t    struct strbuf *err);\n \n /*\n+ * Append a reflog entry for refname.\n+ */\n+int transaction_update_reflog(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      const unsigned char *email,\n+\t\t\t      unsigned long timestamp, int tz,\n+\t\t\t      const char *msg, int flags,\n+\t\t\t      struct strbuf *err);\n+\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.\n-- \n2.0.0.770.gd892650.dirty\n"},{"id":"244256","messageId":"1402937505-6091-9-git-send-email-sahlberg@google.com","threadId":"36928","inReplyTo":"1402937505-6091-1-git-send-email-sahlberg@google.com","subject":"[PATCH v2 08/14] refs.c: add a flag to allow reflog updates to truncate the log","fromName":"Ronnie Sahlberg","fromEmail":"sahlberg@google.com","sentAt":"2014-06-16T16:51:39Z","receivedAt":"2014-06-16T16:51:39Z","isPatch":true,"sender":{"key":"sahlberg@google.com","avatar":"https://avatars.githubusercontent.com/u/7320636?v=4"},"body":"Add a flag that allows us to truncate the reflog before we write the\nupdate.\n\nSigned-off-by: Ronnie Sahlberg <sahlberg@google.com>\n---\n refs.c | 17 +++++++++++++++--\n refs.h | 10 +++++++++-\n 2 files changed, 24 insertions(+), 3 deletions(-)\n\ndiff --git a/refs.c b/refs.c\nindex 9db5896..379b50e 100644\n--- a/refs.c\n+++ b/refs.c\n@@ -3763,7 +3763,12 @@ int transaction_commit(struct ref_transaction *transaction,\n \t\t}\n \t}\n \n-\t/* Update all reflog files */\n+\t/*\n+\t * Update all reflog files\n+\t * We have already done all ref updates and deletes.\n+\t * There is not much we can do here if there are any reflog\n+\t * update errors other than complain.\n+\t */\n \tfor (i = 0; i < n; i++) {\n \t\tstruct ref_update *update = updates[i];\n \n@@ -3771,7 +3776,15 @@ int transaction_commit(struct ref_transaction *transaction,\n \t\t\tcontinue;\n \t\tif (update->reflog_fd == -1)\n \t\t\tcontinue;\n-\n+\t\tif (update->flags & REFLOG_TRUNCATE)\n+\t\t\tif (lseek(update->reflog_fd, 0, SEEK_SET) < 0 ||\n+\t\t\t\tftruncate(update->reflog_fd, 0)) {\n+\t\t\t\terror(\"Could not truncate reflog: %s. %s\",\n+\t\t\t\t      update->refname, strerror(errno));\n+\t\t\t\trollback_lock_file(&update->reflog_lock);\n+\t\t\t\tupdate->reflog_fd = -1;\n+\t\t\t\tcontinue;\n+\t\t\t}\n \t\tif (log_ref_write_fd(update->reflog_fd, update->old_sha1,\n \t\t\t\t     update->new_sha1,\n \t\t\t\t     update->committer, update->msg)) {\ndiff --git a/refs.h b/refs.h\nindex 5f4deef..7c3a9ca 100644\n--- a/refs.h\n+++ b/refs.h\n@@ -327,7 +327,15 @@ int transaction_delete_sha1(struct ref_transaction *transaction,\n \t\t\t    struct strbuf *err);\n \n /*\n- * Append a reflog entry for refname.\n+ * Flags controlling transaction_update_reflog().\n+ * REFLOG_TRUNCATE: Truncate the reflog.\n+ *\n+ * Flags >= 0x100 are reserved for internal use.\n+ */\n+#define REFLOG_TRUNCATE 0x01\n+/*\n+ * Append a reflog entry for refname. If the REFLOG_TRUNCATE flag is set\n+ * this update will first truncate the reflog before writing the entry.\n  */\n int transaction_update_reflog(struct ref_transaction *transaction,\n \t\t\t      const char *refname,\n-- \n2.0.0.770.gd892650.dirty\n"},{"id":"244258","messageId":"1402937505-6091-10-git-send-email-sahlberg@google.com","threadId":"36928","inReplyTo":"1402937505-6091-1-git-send-email-sahlberg@google.com","subject":"[PATCH v2 09/14] refs.c: only write reflog update if msg is non-NULL","fromName":"Ronnie Sahlberg","fromEmail":"sahlberg@google.com","sentAt":"2014-06-16T16:51:40Z","receivedAt":"2014-06-16T16:51:40Z","isPatch":true,"sender":{"key":"sahlberg@google.com","avatar":"https://avatars.githubusercontent.com/u/7320636?v=4"},"body":"When performing a reflog transaction update, only write to the reflog iff\nmsg is non-NULL. This can then be combined with REFLOG_TRUNCATE to perform\nan update that only truncates but does not write.\n\nSigned-off-by: Ronnie Sahlberg <sahlberg@google.com>\n---\n refs.c | 5 +++--\n refs.h | 1 +\n 2 files changed, 4 insertions(+), 2 deletions(-)\n\ndiff --git a/refs.c b/refs.c\nindex 379b50e..2163c90 100644\n--- a/refs.c\n+++ b/refs.c\n@@ -3785,8 +3785,9 @@ int transaction_commit(struct ref_transaction *transaction,\n \t\t\t\tupdate->reflog_fd = -1;\n \t\t\t\tcontinue;\n \t\t\t}\n-\t\tif (log_ref_write_fd(update->reflog_fd, update->old_sha1,\n-\t\t\t\t     update->new_sha1,\n+\t\tif (update->msg &&\n+\t\t    log_ref_write_fd(update->reflog_fd,\n+\t\t\t\t     update->old_sha1, update->new_sha1,\n \t\t\t\t     update->committer, update->msg)) {\n \t\t\terror(\"Could write to reflog: %s. %s\",\n \t\t\t      update->refname, strerror(errno));\ndiff --git a/refs.h b/refs.h\nindex 7c3a9ca..b0e339b 100644\n--- a/refs.h\n+++ b/refs.h\n@@ -336,6 +336,7 @@ int transaction_delete_sha1(struct ref_transaction *transaction,\n /*\n  * Append a reflog entry for refname. If the REFLOG_TRUNCATE flag is set\n  * this update will first truncate the reflog before writing the entry.\n+ * If msg is NULL no update will be written to the log.\n  */\n int transaction_update_reflog(struct ref_transaction *transaction,\n \t\t\t      const char *refname,\n-- \n2.0.0.770.gd892650.dirty\n"},{"id":"244253","messageId":"1402937505-6091-11-git-send-email-sahlberg@google.com","threadId":"36928","inReplyTo":"1402937505-6091-1-git-send-email-sahlberg@google.com","subject":"[PATCH v2 10/14] refs.c: allow multiple reflog updates during a single transaction","fromName":"Ronnie Sahlberg","fromEmail":"sahlberg@google.com","sentAt":"2014-06-16T16:51:41Z","receivedAt":"2014-06-16T16:51:41Z","isPatch":true,"sender":{"key":"sahlberg@google.com","avatar":"https://avatars.githubusercontent.com/u/7320636?v=4"},"body":"Allow to make multiple reflog updates to the same ref during a transaction.\nThis means we only need to lock the reflog once, during the first update\nthat touches the reflog, and that all further updates can just write the\nreflog entry since the reflog is already locked.\n\nThis allows us to write code such as:\n\nt = transaction_begin()\ntransaction_reflog_update(t, \"foo\", REFLOG_TRUNCATE, NULL);\nloop-over-somehting...\n   transaction_reflog_update(t, \"foo\", 0, <message>);\ntransaction_commit(t)\n\nwhere we first truncate the reflog and then build the new content one line at a\ntime.\n\nSigned-off-by: Ronnie Sahlberg <sahlberg@google.com>\n---\n refs.c | 45 ++++++++++++++++++++++++++++++++++++---------\n 1 file changed, 36 insertions(+), 9 deletions(-)\n\ndiff --git a/refs.c b/refs.c\nindex 2163c90..3b90457 100644\n--- a/refs.c\n+++ b/refs.c\n@@ -2407,6 +2407,11 @@ static void try_remove_empty_parents(char *name)\n  * pruned.\n  */\n #define REF_ISPRUNING\t0x0100\n+/*\n+ * Only the first reflog update needs to lock the reflog file. Further updates\n+ * just use the lock taken by the first update.\n+ */\n+#define UPDATE_REFLOG_NOLOCK 0x0200\n \n /* make sure nobody touched the ref, and unlink */\n static void prune_ref(struct ref_to_prune *r)\n@@ -3413,7 +3418,7 @@ enum transaction_update_type {\n        UPDATE_LOG = 1,\n };\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  * while locking the ref, set have_old to 1 and set old_sha1 to the\n@@ -3423,7 +3428,7 @@ struct ref_update {\n \tenum transaction_update_type update_type;\n \tunsigned char new_sha1[20];\n \tunsigned char old_sha1[20];\n-\tint flags; /* REF_NODEREF? */\n+\tint flags; /* REF_NODEREF? or private flags */\n \tint have_old; /* 1 if old_sha1 is valid, 0 otherwise */\n \tstruct ref_lock *lock;\n \tint type;\n@@ -3431,8 +3436,9 @@ struct ref_update {\n \n \t/* used by reflog updates */\n \tint reflog_fd;\n-\tstruct lock_file reflog_lock;\n+\tstruct lock_file *reflog_lock;\n \tchar *committer;\n+\tstruct ref_update *orig_update; /* For UPDATE_REFLOG_NOLOCK */\n \n \tconst char refname[FLEX_ARRAY];\n };\n@@ -3514,12 +3520,27 @@ int transaction_update_reflog(struct ref_transaction *transaction,\n \t\t\t      struct strbuf *err)\n {\n \tstruct ref_update *update;\n+\tint i;\n \n \tif (transaction->state != REF_TRANSACTION_OPEN)\n \t\tdie(\"BUG: update_reflog called for transaction that is not \"\n \t\t    \"open\");\n \n \tupdate = add_update(transaction, refname, UPDATE_LOG);\n+\tupdate->flags = flags;\n+\tfor (i = 0; i < transaction->nr - 1; i++) {\n+\t\tif (transaction->updates[i]->update_type != UPDATE_LOG)\n+\t\t\tcontinue;\n+\t\tif (!strcmp(transaction->updates[i]->refname,\n+\t\t\t    update->refname)) {\n+\t\t\tupdate->flags |= UPDATE_REFLOG_NOLOCK;\n+\t\t\tupdate->orig_update = transaction->updates[i];\n+\t\t\tbreak;\n+\t\t}\n+\t}\n+\tif (!(update->flags & UPDATE_REFLOG_NOLOCK))\n+\t\tupdate->reflog_lock = xcalloc(1, sizeof(struct lock_file));\n+\n \thashcpy(update->new_sha1, new_sha1);\n \thashcpy(update->old_sha1, old_sha1);\n \tupdate->reflog_fd = -1;\n@@ -3535,7 +3556,6 @@ int transaction_update_reflog(struct ref_transaction *transaction,\n \t}\n \tif (msg)\n \t\tupdate->msg = xstrdup(msg);\n-\tupdate->flags = flags;\n \n \treturn 0;\n }\n@@ -3712,10 +3732,15 @@ int transaction_commit(struct ref_transaction *transaction,\n \n \t\tif (update->update_type != UPDATE_LOG)\n \t\t\tcontinue;\n+\t\tif (update->flags & UPDATE_REFLOG_NOLOCK) {\n+\t\t\tupdate->reflog_fd = update->orig_update->reflog_fd;\n+\t\t\tupdate->reflog_lock = update->orig_update->reflog_lock;\n+\t\t\tcontinue;\n+\t\t}\n \t\tupdate->reflog_fd = hold_lock_file_for_append(\n-\t\t\t\t\t&update->reflog_lock,\n+\t\t\t\t\tupdate->reflog_lock,\n \t\t\t\t\tgit_path(\"logs/%s\", update->refname),\n-\t\t\t\t\t0);\n+\t\t\t\t\tLOCK_NODEREF);\n \t\tif (update->reflog_fd < 0) {\n \t\t\tconst char *str = \"Cannot lock reflog for '%s'. %s\";\n \n@@ -3781,7 +3806,7 @@ int transaction_commit(struct ref_transaction *transaction,\n \t\t\t\tftruncate(update->reflog_fd, 0)) {\n \t\t\t\terror(\"Could not truncate reflog: %s. %s\",\n \t\t\t\t      update->refname, strerror(errno));\n-\t\t\t\trollback_lock_file(&update->reflog_lock);\n+\t\t\t\trollback_lock_file(update->reflog_lock);\n \t\t\t\tupdate->reflog_fd = -1;\n \t\t\t\tcontinue;\n \t\t\t}\n@@ -3791,7 +3816,7 @@ int transaction_commit(struct ref_transaction *transaction,\n \t\t\t\t     update->committer, update->msg)) {\n \t\t\terror(\"Could write to reflog: %s. %s\",\n \t\t\t      update->refname, strerror(errno));\n-\t\t\trollback_lock_file(&update->reflog_lock);\n+\t\t\trollback_lock_file(update->reflog_lock);\n \t\t\tupdate->reflog_fd = -1;\n \t\t}\n \t}\n@@ -3802,9 +3827,11 @@ int transaction_commit(struct ref_transaction *transaction,\n \n \t\tif (update->update_type != UPDATE_LOG)\n \t\t\tcontinue;\n+\t\tif (update->flags & UPDATE_REFLOG_NOLOCK)\n+\t\t\tcontinue;\n \t\tif (update->reflog_fd == -1)\n \t\t\tcontinue;\n-\t\tif (commit_lock_file(&update->reflog_lock)) {\n+\t\tif (commit_lock_file(update->reflog_lock)) {\n \t\t\terror(\"Could not commit reflog: %s. %s\",\n \t\t\t      update->refname, strerror(errno));\n \t\t\tupdate->reflog_fd = -1;\n-- \n2.0.0.770.gd892650.dirty\n"},{"id":"244261","messageId":"1402937505-6091-12-git-send-email-sahlberg@google.com","threadId":"36928","inReplyTo":"1402937505-6091-1-git-send-email-sahlberg@google.com","subject":"[PATCH v2 11/14] reflog.c: use a reflog transaction when writing during expire","fromName":"Ronnie Sahlberg","fromEmail":"sahlberg@google.com","sentAt":"2014-06-16T16:51:42Z","receivedAt":"2014-06-16T16:51:42Z","isPatch":true,"sender":{"key":"sahlberg@google.com","avatar":"https://avatars.githubusercontent.com/u/7320636?v=4"},"body":"Use a transaction for all updates during expire_reflog.\n\nSigned-off-by: Ronnie Sahlberg <sahlberg@google.com>\n---\n builtin/reflog.c | 85 ++++++++++++++++++++++++--------------------------------\n refs.c           |  4 +--\n refs.h           |  2 +-\n 3 files changed, 39 insertions(+), 52 deletions(-)\n\ndiff --git a/builtin/reflog.c b/builtin/reflog.c\nindex d45344a..c3db3fb 100644\n--- a/builtin/reflog.c\n+++ b/builtin/reflog.c\n@@ -32,8 +32,11 @@ struct cmd_reflog_expire_cb {\n \tint recno;\n };\n \n+static struct strbuf err = STRBUF_INIT;\n+\n struct expire_reflog_cb {\n-\tFILE *newlog;\n+\tstruct ref_transaction *t;\n+\tconst char *refname;\n \tenum {\n \t\tUE_NORMAL,\n \t\tUE_ALWAYS,\n@@ -316,20 +319,18 @@ static int expire_reflog_ent(unsigned char *osha1, unsigned char *nsha1,\n \tif (cb->cmd->recno && --(cb->cmd->recno) == 0)\n \t\tgoto prune;\n \n-\tif (cb->newlog) {\n-\t\tchar sign = (tz < 0) ? '-' : '+';\n-\t\tint zone = (tz < 0) ? (-tz) : tz;\n-\t\tfprintf(cb->newlog, \"%s %s %s %lu %c%04d\\t%s\",\n-\t\t\tsha1_to_hex(osha1), sha1_to_hex(nsha1),\n-\t\t\temail, timestamp, sign, zone,\n-\t\t\tmessage);\n+\tif (cb->t) {\n+\t\tif (transaction_update_reflog(cb->t, cb->refname, nsha1, osha1,\n+\t\t\t\t\t      email, timestamp, tz, message, 0,\n+\t\t\t\t\t      &err))\n+\t\t\treturn -1;\n \t\thashcpy(cb->last_kept_sha1, nsha1);\n \t}\n \tif (cb->cmd->verbose)\n \t\tprintf(\"keep %s\", message);\n \treturn 0;\n  prune:\n-\tif (!cb->newlog)\n+\tif (!cb->t)\n \t\tprintf(\"would prune %s\", message);\n \telse if (cb->cmd->verbose)\n \t\tprintf(\"prune %s\", message);\n@@ -353,29 +354,26 @@ static int expire_reflog(const char *ref, const unsigned char *sha1, int unused,\n {\n \tstruct cmd_reflog_expire_cb *cmd = cb_data;\n \tstruct expire_reflog_cb cb;\n-\tstruct ref_lock *lock;\n-\tchar *log_file, *newlog_path = NULL;\n \tstruct commit *tip_commit;\n \tstruct commit_list *tips;\n \tint status = 0;\n \n \tmemset(&cb, 0, sizeof(cb));\n+\tcb.refname = ref;\n \n-\t/*\n-\t * we take the lock for the ref itself to prevent it from\n-\t * getting updated.\n-\t */\n-\tlock = lock_any_ref_for_update(ref, sha1, 0, NULL);\n-\tif (!lock)\n-\t\treturn error(\"cannot lock ref '%s'\", ref);\n-\tlog_file = git_pathdup(\"logs/%s\", ref);\n \tif (!reflog_exists(ref))\n \t\tgoto finish;\n-\tif (!cmd->dry_run) {\n-\t\tnewlog_path = mkpathdup(\"%s.lock\", log_file);\n-\t\tcb.newlog = fopen(newlog_path, \"w\");\n+\tcb.t = transaction_begin(&err);\n+\tif (!cb.t) {\n+\t\tstatus |= error(\"%s\", err.buf);\n+\t\tgoto cleanup;\n+\t}\n+\tif (transaction_update_reflog(cb.t, cb.refname, null_sha1, null_sha1,\n+\t\t\t\t      NULL, 0, 0, NULL, REFLOG_TRUNCATE,\n+\t\t\t\t      &err)) {\n+\t\tstatus |= error(\"%s\", err.buf);\n+\t\tgoto cleanup;\n \t}\n-\n \tcb.cmd = cmd;\n \n \tif (!cmd->expire_unreachable || !strcmp(ref, \"HEAD\")) {\n@@ -407,7 +405,10 @@ static int expire_reflog(const char *ref, const unsigned char *sha1, int unused,\n \t\tmark_reachable(&cb);\n \t}\n \n-\tfor_each_reflog_ent(ref, expire_reflog_ent, &cb);\n+\tif (for_each_reflog_ent(ref, expire_reflog_ent, &cb)) {\n+\t\tstatus |= error(\"%s\", err.buf);\n+\t\tgoto cleanup;\n+\t}\n \n \tif (cb.unreachable_expire_kind != UE_ALWAYS) {\n \t\tif (cb.unreachable_expire_kind == UE_HEAD) {\n@@ -420,32 +421,18 @@ static int expire_reflog(const char *ref, const unsigned char *sha1, int unused,\n \t\t}\n \t}\n  finish:\n-\tif (cb.newlog) {\n-\t\tif (fclose(cb.newlog)) {\n-\t\t\tstatus |= error(\"%s: %s\", strerror(errno),\n-\t\t\t\t\tnewlog_path);\n-\t\t\tunlink(newlog_path);\n-\t\t} else if (cmd->updateref &&\n-\t\t\t(write_in_full(lock->lock_fd,\n-\t\t\t\tsha1_to_hex(cb.last_kept_sha1), 40) != 40 ||\n-\t\t\t write_str_in_full(lock->lock_fd, \"\\n\") != 1 ||\n-\t\t\t close_ref(lock) < 0)) {\n-\t\t\tstatus |= error(\"Couldn't write %s\",\n-\t\t\t\t\tlock->lk->filename.buf);\n-\t\t\tunlink(newlog_path);\n-\t\t} else if (rename(newlog_path, log_file)) {\n-\t\t\tstatus |= error(\"cannot rename %s to %s\",\n-\t\t\t\t\tnewlog_path, log_file);\n-\t\t\tunlink(newlog_path);\n-\t\t} else if (cmd->updateref && commit_ref(lock)) {\n-\t\t\tstatus |= error(\"Couldn't set %s\", lock->ref_name);\n-\t\t} else {\n-\t\t\tadjust_shared_perm(log_file);\n-\t\t}\n+\tif (!cmd->dry_run) {\n+\t\tif (cmd->updateref &&\n+\t\t    transaction_update_sha1(cb.t, cb.refname,\n+\t\t\t\t\t    cb.last_kept_sha1, sha1,\n+\t\t\t\t\t    0, 1, NULL, &err)) {\n+\t\t\tstatus |= error(\"%s\", err.buf);\n+\t\t} else if (transaction_commit(cb.t, &err))\n+\t\t\tstatus |= error(\"%s\", err.buf);\n \t}\n-\tfree(newlog_path);\n-\tfree(log_file);\n-\tunlock_ref(lock);\n+ cleanup:\n+\ttransaction_free(cb.t);\n+\tstrbuf_release(&err);\n \treturn status;\n }\n \ndiff --git a/refs.c b/refs.c\nindex 3b90457..4069da1 100644\n--- a/refs.c\n+++ b/refs.c\n@@ -3514,7 +3514,7 @@ int transaction_update_reflog(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      const unsigned char *email,\n+\t\t\t      const char *email,\n \t\t\t      unsigned long timestamp, int tz,\n \t\t\t      const char *msg, int flags,\n \t\t\t      struct strbuf *err)\n@@ -3790,7 +3790,7 @@ int transaction_commit(struct ref_transaction *transaction,\n \n \t/*\n \t * Update all reflog files\n-\t * We have already done all ref updates and deletes.\n+\t * We have already committed all ref updates and deletes.\n \t * There is not much we can do here if there are any reflog\n \t * update errors other than complain.\n \t */\ndiff --git a/refs.h b/refs.h\nindex b0e339b..e321721 100644\n--- a/refs.h\n+++ b/refs.h\n@@ -342,7 +342,7 @@ int transaction_update_reflog(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      const unsigned char *email,\n+\t\t\t      const char *email,\n \t\t\t      unsigned long timestamp, int tz,\n \t\t\t      const char *msg, int flags,\n \t\t\t      struct strbuf *err);\n-- \n2.0.0.770.gd892650.dirty\n"},{"id":"244259","messageId":"1402937505-6091-13-git-send-email-sahlberg@google.com","threadId":"36928","inReplyTo":"1402937505-6091-1-git-send-email-sahlberg@google.com","subject":"[PATCH v2 12/14] refs.c: rename log_ref_setup to create_reflog","fromName":"Ronnie Sahlberg","fromEmail":"sahlberg@google.com","sentAt":"2014-06-16T16:51:43Z","receivedAt":"2014-06-16T16:51:43Z","isPatch":true,"sender":{"key":"sahlberg@google.com","avatar":"https://avatars.githubusercontent.com/u/7320636?v=4"},"body":"log_ref_setup is used to do several semi-related things :\n* sometimes it will create a new reflog including missing parent directories\n  and cleaning up any conflicting stale directories in the path.\n* fill in a filename buffer for the full path to the reflog.\n* unconditionally re-adjust the permissions for the file.\n\nThis function is only called from two places: checkout.c where it is always\nused to create a reflog and from refs.c:log_ref_write where it sometimes is\nused to create a reflog and sometimes just used to fill in the filename.\n\nRename log_ref_setup to create_reflog and change it to only take the\nrefname as argument to make its signature similar to delete_reflog and\nreflog_exists. Change create_reflog to ignore log_all_ref_updates and\n\"unconditionally\" create the reflog when called. Since checkout.c always\nwants to create a reflog we can call create_reflog directly and avoid the\ntemp-and-log_all_ref_update dance.\n\nIn log_ref_write, only call create_reflog iff we want to create a reflog\nand if the reflog does not yet exist. This means that for the common case\nwhere we want to create a reflog which already exists we now only need to\nperform a single lstat() to see that the log already exists instead of\nperforming, for every single update, open(O_CREAT)+lstat()+close().\n\nSigned-off-by: Ronnie Sahlberg <sahlberg@google.com>\n---\n builtin/checkout.c |  8 +-----\n refs.c             | 78 ++++++++++++++++++++++++++++++------------------------\n refs.h             |  8 +++---\n 3 files changed, 47 insertions(+), 47 deletions(-)\n\ndiff --git a/builtin/checkout.c b/builtin/checkout.c\nindex 9cbe7d1..c37c4dc 100644\n--- a/builtin/checkout.c\n+++ b/builtin/checkout.c\n@@ -597,17 +597,11 @@ static void update_refs_for_switch(const struct checkout_opts *opts,\n \tif (opts->new_branch) {\n \t\tif (opts->new_orphan_branch) {\n \t\t\tif (opts->new_branch_log && !log_all_ref_updates) {\n-\t\t\t\tint temp;\n-\t\t\t\tstruct strbuf log_file = STRBUF_INIT;\n \t\t\t\tint ret;\n \t\t\t\tconst char *ref_name;\n \n \t\t\t\tref_name = mkpath(\"refs/heads/%s\", opts->new_orphan_branch);\n-\t\t\t\ttemp = log_all_ref_updates;\n-\t\t\t\tlog_all_ref_updates = 1;\n-\t\t\t\tret = log_ref_setup(ref_name, &log_file);\n-\t\t\t\tlog_all_ref_updates = temp;\n-\t\t\t\tstrbuf_release(&log_file);\n+\t\t\t\tret = create_reflog(ref_name);\n \t\t\t\tif (ret) {\n \t\t\t\t\tfprintf(stderr, _(\"Can not do reflog for '%s'\\n\"),\n \t\t\t\t\t    opts->new_orphan_branch);\ndiff --git a/refs.c b/refs.c\nindex 4069da1..f579d63 100644\n--- a/refs.c\n+++ b/refs.c\n@@ -2839,54 +2839,60 @@ static int copy_msg(char *buf, const char *msg)\n }\n \n /* This function must set a meaningful errno on failure */\n-int log_ref_setup(const char *refname, struct strbuf *logfile)\n+int create_reflog(const char *refname)\n {\n \tint logfd, oflags = O_APPEND | O_WRONLY;\n-\n-\tstrbuf_git_path(logfile, \"logs/%s\", refname);\n-\tif (log_all_ref_updates &&\n-\t    (starts_with(refname, \"refs/heads/\") ||\n-\t     starts_with(refname, \"refs/remotes/\") ||\n-\t     starts_with(refname, \"refs/notes/\") ||\n-\t     !strcmp(refname, \"HEAD\"))) {\n-\t\tif (safe_create_leading_directories(logfile->buf) < 0) {\n-\t\t\tint save_errno = errno;\n+\tstruct strbuf logfile = STRBUF_INIT;\n+\tint save_errno, ret = 0;\n+\n+\tstrbuf_git_path(&logfile, \"logs/%s\", refname);\n+\tif (starts_with(refname, \"refs/heads/\") ||\n+\t    starts_with(refname, \"refs/remotes/\") ||\n+\t    starts_with(refname, \"refs/notes/\") ||\n+\t    !strcmp(refname, \"HEAD\")) {\n+\t\tif (safe_create_leading_directories(logfile.buf) < 0) {\n+\t\t\tsave_errno = errno;\n \t\t\terror(\"unable to create directory for %s\",\n-\t\t\t      logfile->buf);\n-\t\t\terrno = save_errno;\n-\t\t\treturn -1;\n+\t\t\t      logfile.buf);\n+\t\t\tret = -1;\n+\t\t\tgoto cleanup;\n \t\t}\n \t\toflags |= O_CREAT;\n \t}\n \n-\tlogfd = open(logfile->buf, oflags, 0666);\n+\tlogfd = open(logfile.buf, oflags, 0666);\n \tif (logfd < 0) {\n-\t\tif (!(oflags & O_CREAT) && errno == ENOENT)\n-\t\t\treturn 0;\n+\t\tif (!(oflags & O_CREAT) && errno == ENOENT) {\n+       \t\t\tgoto cleanup;\n+\t\t}\n \n \t\tif ((oflags & O_CREAT) && errno == EISDIR) {\n-\t\t\tif (remove_empty_directories(logfile->buf)) {\n-\t\t\t\tint save_errno = errno;\n+\t\t\tif (remove_empty_directories(logfile.buf)) {\n+\t\t\t\tsave_errno = errno;\n \t\t\t\terror(\"There are still logs under '%s'\",\n-\t\t\t\t      logfile->buf);\n-\t\t\t\terrno = save_errno;\n-\t\t\t\treturn -1;\n+\t\t\t\t      logfile.buf);\n+\t\t\t\tret = -1;\n+\t\t\t\tgoto cleanup;\n  \t\t\t}\n-\t\t\tlogfd = open(logfile->buf, oflags, 0666);\n+\t\t\tlogfd = open(logfile.buf, oflags, 0666);\n \t\t}\n \n \t\tif (logfd < 0) {\n-\t\t\tint save_errno = errno;\n+\t\t\tsave_errno = errno;\n \t\t\terror(\"Unable to append to %s: %s\",\n-\t\t\t      logfile->buf, strerror(errno));\n-\t\t\terrno = save_errno;\n-\t\t\treturn -1;\n+\t\t\t      logfile.buf, strerror(errno));\n+\t\t\tret = -1;\n+\t\t\tgoto cleanup;\n \t\t}\n \t}\n \n-\tadjust_shared_perm(logfile->buf);\n+\tadjust_shared_perm(logfile.buf);\n \tclose(logfd);\n-\treturn 0;\n+ cleanup:\n+\tstrbuf_release(&logfile);\n+\tif (ret)\n+\t\terrno = save_errno;\n+\treturn ret;\n }\n \n static int log_ref_write_fd(int fd, const unsigned char *old_sha1,\n@@ -2918,19 +2924,21 @@ static int log_ref_write_fd(int fd, const unsigned char *old_sha1,\n static int log_ref_write(const char *refname, const unsigned char *old_sha1,\n \t\t\t const unsigned char *new_sha1, const char *msg)\n {\n-\tint logfd, result, oflags = O_APPEND | O_WRONLY;\n+\tint logfd, result = 0, oflags = O_APPEND | O_WRONLY;\n \tstruct strbuf sb_log_file = STRBUF_INIT;\n-\tconst char *log_file;\n \n \tif (log_all_ref_updates < 0)\n \t\tlog_all_ref_updates = !is_bare_repository();\n \n-\tresult = log_ref_setup(refname, &sb_log_file);\n+\tif (log_all_ref_updates && !reflog_exists(refname))\n+\t\tresult = create_reflog(refname);\n+\n \tif (result)\n \t\tgoto done;\n-\tlog_file = sb_log_file.buf;\n \n-\tlogfd = open(log_file, oflags);\n+\tstrbuf_git_path(&sb_log_file, \"logs/%s\", refname);\n+\n+\tlogfd = open(sb_log_file.buf, oflags);\n \tif (logfd < 0)\n \t\tgoto done;\n \tresult = log_ref_write_fd(logfd, old_sha1, new_sha1,\n@@ -2938,12 +2946,12 @@ static int log_ref_write(const char *refname, const unsigned char *old_sha1,\n \tif (result) {\n \t\tint save_errno = errno;\n \t\tclose(logfd);\n-\t\terror(\"Unable to append to %s\", log_file);\n+\t\terror(\"Unable to append to %s\", sb_log_file.buf);\n \t\terrno = save_errno;\n \t\tresult = -1;\n \t} else if (close(logfd)) {\n \t\tint save_errno = errno;\n-\t\terror(\"Unable to append to %s\", log_file);\n+\t\terror(\"Unable to append to %s\", sb_log_file.buf);\n \t\terrno = save_errno;\n \t\tresult = -1;\n \t}\ndiff --git a/refs.h b/refs.h\nindex e321721..c19ca82 100644\n--- a/refs.h\n+++ b/refs.h\n@@ -202,11 +202,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-/*\n- * Setup reflog before using. Set errno to something meaningful on failure.\n- */\n-int log_ref_setup(const char *refname, struct strbuf *logfile);\n-\n /** Reads log for the value of ref during at_time. **/\n extern int read_ref_at(const char *refname, unsigned long at_time, int cnt,\n \t\t       unsigned char *sha1, char **msg,\n@@ -215,6 +210,9 @@ extern int read_ref_at(const char *refname, unsigned long at_time, int cnt,\n /** Check if a particular reflog exists */\n extern int reflog_exists(const char *refname);\n \n+/** Create reflog. Set errno to something meaningful on failure. */\n+extern int create_reflog(const char *refname);\n+\n /** Delete a reflog */\n extern int delete_reflog(const char *refname);\n \n-- \n2.0.0.770.gd892650.dirty\n"},{"id":"244260","messageId":"1402937505-6091-14-git-send-email-sahlberg@google.com","threadId":"36928","inReplyTo":"1402937505-6091-1-git-send-email-sahlberg@google.com","subject":"[PATCH v2 13/14] refs.c: make unlock_ref/close_ref/commit_ref static","fromName":"Ronnie Sahlberg","fromEmail":"sahlberg@google.com","sentAt":"2014-06-16T16:51:44Z","receivedAt":"2014-06-16T16:51:44Z","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 | 24 ++++++++++++------------\n refs.h |  9 ---------\n 2 files changed, 12 insertions(+), 21 deletions(-)\n\ndiff --git a/refs.c b/refs.c\nindex f579d63..3f16445 100644\n--- a/refs.c\n+++ b/refs.c\n@@ -1962,6 +1962,16 @@ int refname_match(const char *abbrev_name, const char *full_name)\n \treturn 0;\n }\n \n+static void unlock_ref(struct ref_lock *lock)\n+{\n+\t/* Do not free lock->lk -- atexit() still looks at them */\n+\tif (lock->lk)\n+\t\trollback_lock_file(lock->lk);\n+\tfree(lock->ref_name);\n+\tfree(lock->orig_ref_name);\n+\tfree(lock);\n+}\n+\n /* This function should make sure errno is meaningful on error */\n static struct ref_lock *verify_lock(struct ref_lock *lock,\n \tconst unsigned char *old_sha1, int mustexist)\n@@ -2786,7 +2796,7 @@ int rename_ref(const char *oldrefname, const char *newrefname, const char *logms\n \treturn 1;\n }\n \n-int close_ref(struct ref_lock *lock)\n+static int close_ref(struct ref_lock *lock)\n {\n \tif (close_lock_file(lock->lk))\n \t\treturn -1;\n@@ -2794,7 +2804,7 @@ int close_ref(struct ref_lock *lock)\n \treturn 0;\n }\n \n-int commit_ref(struct ref_lock *lock)\n+static int commit_ref(struct ref_lock *lock)\n {\n \tif (commit_lock_file(lock->lk))\n \t\treturn -1;\n@@ -2802,16 +2812,6 @@ int commit_ref(struct ref_lock *lock)\n \treturn 0;\n }\n \n-void unlock_ref(struct ref_lock *lock)\n-{\n-\t/* Do not free lock->lk -- atexit() still looks at them */\n-\tif (lock->lk)\n-\t\trollback_lock_file(lock->lk);\n-\tfree(lock->ref_name);\n-\tfree(lock->orig_ref_name);\n-\tfree(lock);\n-}\n-\n /*\n  * copy the reflog message msg to buf, which has been allocated sufficiently\n  * large, while cleaning up the whitespaces.  Especially, convert LF to space,\ndiff --git a/refs.h b/refs.h\nindex c19ca82..2025b96 100644\n--- a/refs.h\n+++ b/refs.h\n@@ -193,15 +193,6 @@ 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 \n-/** Close the file descriptor owned by a lock and return the status */\n-extern int close_ref(struct ref_lock *lock);\n-\n-/** Close and commit the ref locked by the lock */\n-extern int commit_ref(struct ref_lock *lock);\n-\n-/** Release any lock taken but not written. **/\n-extern void unlock_ref(struct ref_lock *lock);\n-\n /** Reads log for the value of ref during at_time. **/\n extern int read_ref_at(const char *refname, unsigned long at_time, int cnt,\n \t\t       unsigned char *sha1, char **msg,\n-- \n2.0.0.770.gd892650.dirty\n"},{"id":"244262","messageId":"1402937505-6091-15-git-send-email-sahlberg@google.com","threadId":"36928","inReplyTo":"1402937505-6091-1-git-send-email-sahlberg@google.com","subject":"[PATCH v2 14/14] refs.c: remove lock_any_ref_for_update","fromName":"Ronnie Sahlberg","fromEmail":"sahlberg@google.com","sentAt":"2014-06-16T16:51:45Z","receivedAt":"2014-06-16T16:51:45Z","isPatch":true,"sender":{"key":"sahlberg@google.com","avatar":"https://avatars.githubusercontent.com/u/7320636?v=4"},"body":"No one is using this function so we can delete it.\n\nSigned-off-by: Ronnie Sahlberg <sahlberg@google.com>\n---\n refs.c |  7 -------\n refs.h | 10 +---------\n 2 files changed, 1 insertion(+), 16 deletions(-)\n\ndiff --git a/refs.c b/refs.c\nindex 3f16445..a2c3630 100644\n--- a/refs.c\n+++ b/refs.c\n@@ -2207,13 +2207,6 @@ static struct ref_lock *lock_ref_sha1_basic(const char *refname,\n \treturn 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-{\n-\treturn lock_ref_sha1_basic(refname, old_sha1, flags, type_p, NULL, 0);\n-}\n-\n /*\n  * Write an entry to the packed-refs file for the specified refname.\n  * If peeled is non-NULL, write it as the entry's peeled value.\ndiff --git a/refs.h b/refs.h\nindex 2025b96..d076d69 100644\n--- a/refs.h\n+++ b/refs.h\n@@ -177,21 +177,13 @@ extern int ref_exists(const char *);\n extern int peel_ref(const char *refname, unsigned char *sha1);\n \n /*\n- * Flags controlling lock_any_ref_for_update(), transaction_update_sha1(),\n- * transaction_create_sha1(), etc.\n+ * Flags controlling transaction_update_sha1(), transaction_create_sha1(), etc.\n  * REF_NODEREF: act on the ref directly, instead of dereferencing\n  *              symbolic references.\n  *\n  * Flags >= 0x100 are reserved for internal use.\n  */\n #define REF_NODEREF\t0x01\n-/*\n- * Locks any ref (for 'HEAD' type refs) and sets errno to something\n- * meaningful on failure.\n- */\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 \n /** Reads log for the value of ref during at_time. **/\n extern int read_ref_at(const char *refname, unsigned long at_time, int cnt,\n-- \n2.0.0.770.gd892650.dirty\n"}]}