fetch.c: change s_update_ref to use a ref transaction

Change s_update_ref to use a ref transaction for the ref update. Signed-off-by: Ronnie Sahlberg <sahlberg@google.com> Signed-off-by: Jonathan Nieder <jrnieder@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Ronnie Sahlberg committed Apr 28, 2014 at 13:49 UTC cd94f765720e32ea0bf72996c8dc7dd0b1cc30d0
1 file changed +24 -10
builtin/fetch.c
+24 -10
@@ -404,23 +404,37 @@ static int s_update_ref(const char *action,
404 {
405 char msg[1024];
406 char *rla = getenv("GIT_REFLOG_ACTION");
407 - static struct ref_lock *lock;
407 + struct ref_transaction *transaction;
408 + struct strbuf err = STRBUF_INIT;
409 + int ret, df_conflict = 0;
410
411 if (dry_run)
412 return 0;
413 if (!rla)
414 rla = default_rla.buf;
415 snprintf(msg, sizeof(msg), "%s: %s", rla, action);
414 - lock = lock_any_ref_for_update(ref->name,
415 - check_old ? ref->old_sha1 : NULL,
416 - 0, NULL);
417 - if (!lock)
418 - return errno == ENOTDIR ? STORE_REF_ERROR_DF_CONFLICT :
419 - STORE_REF_ERROR_OTHER;
420 - if (write_ref_sha1(lock, ref->new_sha1, msg) < 0)
421 - return errno == ENOTDIR ? STORE_REF_ERROR_DF_CONFLICT :
422 - STORE_REF_ERROR_OTHER;
416 +
417 + transaction = ref_transaction_begin(&err);
418 + if (!transaction ||
419 + ref_transaction_update(transaction, ref->name, ref->new_sha1,
420 + ref->old_sha1, 0, check_old, msg, &err))
421 + goto fail;
422 +
423 + ret = ref_transaction_commit(transaction, &err);
424 + if (ret) {
425 + df_conflict = (ret == TRANSACTION_NAME_CONFLICT);
426 + goto fail;
427 + }
428 +
429 + ref_transaction_free(transaction);
430 + strbuf_release(&err);
431 return 0;
432 +fail:
433 + ref_transaction_free(transaction);
434 + error("%s", err.buf);
435 + strbuf_release(&err);
436 + return df_conflict ? STORE_REF_ERROR_DF_CONFLICT
437 + : STORE_REF_ERROR_OTHER;
438 }
439
440 #define REFCOL_WIDTH 10