fast-import.c: change update_branch to use ref transactions

Change update_branch() to use ref transactions for updates. 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 16:21 UTC de7e86f522ea806ef40b3c9557e040d4109e55fb
1 file changed +15 -10
fast-import.c
+15 -10
@@ -1679,8 +1679,9 @@ found_entry:
1679 static int update_branch(struct branch *b)
1680 {
1681 static const char *msg = "fast-import";
1682 - struct ref_lock *lock;
1682 + struct ref_transaction *transaction;
1683 unsigned char old_sha1[20];
1684 + struct strbuf err = STRBUF_INIT;
1685
1686 if (read_ref(b->name, old_sha1))
1687 hashclr(old_sha1);
@@ -1689,29 +1690,33 @@ static int update_branch(struct branch *b)
1690 delete_ref(b->name, old_sha1, 0);
1691 return 0;
1692 }
1692 - lock = lock_any_ref_for_update(b->name, old_sha1, 0, NULL);
1693 - if (!lock)
1694 - return error("Unable to lock %s", b->name);
1693 if (!force_update && !is_null_sha1(old_sha1)) {
1694 struct commit *old_cmit, *new_cmit;
1695
1696 old_cmit = lookup_commit_reference_gently(old_sha1, 0);
1697 new_cmit = lookup_commit_reference_gently(b->sha1, 0);
1700 - if (!old_cmit || !new_cmit) {
1701 - unlock_ref(lock);
1698 + if (!old_cmit || !new_cmit)
1699 return error("Branch %s is missing commits.", b->name);
1703 - }
1700
1701 if (!in_merge_bases(old_cmit, new_cmit)) {
1706 - unlock_ref(lock);
1702 warning("Not updating %s"
1703 " (new tip %s does not contain %s)",
1704 b->name, sha1_to_hex(b->sha1), sha1_to_hex(old_sha1));
1705 return -1;
1706 }
1707 }
1713 - if (write_ref_sha1(lock, b->sha1, msg) < 0)
1714 - return error("Unable to update %s", b->name);
1708 + transaction = ref_transaction_begin(&err);
1709 + if (!transaction ||
1710 + ref_transaction_update(transaction, b->name, b->sha1, old_sha1,
1711 + 0, 1, &err) ||
1712 + ref_transaction_commit(transaction, msg, &err)) {
1713 + ref_transaction_free(transaction);
1714 + error("%s", err.buf);
1715 + strbuf_release(&err);
1716 + return -1;
1717 + }
1718 + ref_transaction_free(transaction);
1719 + strbuf_release(&err);
1720 return 0;
1721 }
1722