commit.c: use ref transactions for updates

Change commit.c to use ref transactions for all ref updates. Make sure we pass a NULL pointer to ref_transaction_update if have_old is false. Signed-off-by: Ronnie Sahlberg <sahlberg@google.com> Reviewed-by: Michael Haggerty <mhagger@alum.mit.edu> Signed-off-by: Jonathan Nieder <jrnieder@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Ronnie Sahlberg committed Apr 16, 2014 at 15:34 UTC c0fe1ed0841868989c088681e1263f248629e92f
1 file changed +12 -13
builtin/commit.c
+12 -13
@@ -1627,11 +1627,12 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
1627 const char *index_file, *reflog_msg;
1628 char *nl;
1629 unsigned char sha1[20];
1630 - struct ref_lock *ref_lock;
1630 struct commit_list *parents = NULL, **pptr = &parents;
1631 struct stat statbuf;
1632 struct commit *current_head = NULL;
1633 struct commit_extra_header *extra = NULL;
1634 + struct ref_transaction *transaction;
1635 + struct strbuf err = STRBUF_INIT;
1636
1637 if (argc == 2 && !strcmp(argv[1], "-h"))
1638 usage_with_options(builtin_commit_usage, builtin_commit_options);
@@ -1753,16 +1754,6 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
1754 strbuf_release(&author_ident);
1755 free_commit_extra_headers(extra);
1756
1756 - ref_lock = lock_any_ref_for_update("HEAD",
1757 - !current_head
1758 - ? NULL
1759 - : current_head->object.sha1,
1760 - 0, NULL);
1761 - if (!ref_lock) {
1762 - rollback_index_files();
1763 - die(_("cannot lock HEAD ref"));
1764 - }
1765 -
1757 nl = strchr(sb.buf, '\n');
1758 if (nl)
1759 strbuf_setlen(&sb, nl + 1 - sb.buf);
@@ -1771,10 +1762,17 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
1762 strbuf_insert(&sb, 0, reflog_msg, strlen(reflog_msg));
1763 strbuf_insert(&sb, strlen(reflog_msg), ": ", 2);
1764
1774 - if (write_ref_sha1(ref_lock, sha1, sb.buf) < 0) {
1765 + transaction = ref_transaction_begin(&err);
1766 + if (!transaction ||
1767 + ref_transaction_update(transaction, "HEAD", sha1,
1768 + current_head
1769 + ? current_head->object.sha1 : NULL,
1770 + 0, !!current_head, &err) ||
1771 + ref_transaction_commit(transaction, sb.buf, &err)) {
1772 rollback_index_files();
1776 - die(_("cannot update HEAD ref"));
1773 + die("%s", err.buf);
1774 }
1775 + ref_transaction_free(transaction);
1776
1777 unlink(git_path("CHERRY_PICK_HEAD"));
1778 unlink(git_path("REVERT_HEAD"));
@@ -1803,5 +1801,6 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
1801 if (!quiet)
1802 print_summary(prefix, sha1, !current_head);
1803
1804 + strbuf_release(&err);
1805 return 0;
1806 }