[PATCH 23/31] reflog.c: use the existing transaction to also lock and update the ref

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



reflog.c has a transaction for updating the reflog. Use this transaction
also for locking the ref itself and updating the ref instead of
locking the file directly and writing to it. This now completely eliminates
the refs and reflog implementation from reflog.c.

Signed-off-by: Ronnie Sahlberg <sahlberg@xxxxxxxxxx>
---
 builtin/reflog.c | 44 ++++++++++++++++++++------------------------
 1 file changed, 20 insertions(+), 24 deletions(-)

diff --git a/builtin/reflog.c b/builtin/reflog.c
index 2239249..78fd442 100644
--- a/builtin/reflog.c
+++ b/builtin/reflog.c
@@ -351,7 +351,6 @@ static int expire_reflog(const char *ref, const unsigned char *sha1, int unused,
 {
 	struct cmd_reflog_expire_cb *cmd = cb_data;
 	struct expire_reflog_cb cb;
-	struct ref_lock *lock;
 	struct commit *tip_commit;
 	struct commit_list *tips;
 	int status = 0;
@@ -359,23 +358,24 @@ static int expire_reflog(const char *ref, const unsigned char *sha1, int unused,
 	memset(&cb, 0, sizeof(cb));
 	cb.refname = ref;
 
+	if (!reflog_exists(ref))
+		goto finish;
+
+	cb.t = transaction_begin();
 	/*
-	 * we take the lock for the ref itself to prevent it from
-	 * getting updated.
+	 * This locks the ref itself, preventing it from getting
+	 * updated.
 	 */
-	lock = lock_any_ref_for_update(ref, sha1, 0, NULL);
-	if (!lock)
+	if (transaction_update_sha1(cb.t, cb.refname,
+				    sha1, sha1, 0, 1, NULL, NULL)) {
+		transaction_rollback(cb.t);
 		return error("cannot lock ref '%s'", ref);
-	if (!reflog_exists(ref))
-		goto finish;
-	if (!cmd->dry_run) {
-		cb.t = transaction_begin();
-		transaction_update_reflog(cb.t, cb.refname,
-					  null_sha1, null_sha1, NULL,
-					  0, 0, NULL,
-					  REFLOG_TRUNCATE,
-					  NULL);
 	}
+	transaction_update_reflog(cb.t, cb.refname,
+				  null_sha1, null_sha1, NULL,
+				  0, 0, NULL,
+				  REFLOG_TRUNCATE,
+				  NULL);
 
 	cb.cmd = cmd;
 
@@ -421,23 +421,19 @@ static int expire_reflog(const char *ref, const unsigned char *sha1, int unused,
 		}
 	}
  finish:
-	if (cb.t) {
+	if (!cmd->dry_run) {
 		if (cmd->updateref &&
-		    (write_in_full(lock->lock_fd,
-				   sha1_to_hex(cb.last_kept_sha1), 40) != 40 ||
-		     write_str_in_full(lock->lock_fd, "\n") != 1 ||
-		     close_ref(lock) < 0)) {
+		    transaction_update_sha1(cb.t, cb.refname,
+					    cb.last_kept_sha1, sha1,
+					    0, 1, NULL, NULL)) {
 			status |= error("Couldn't write %s",
-				lock->lk->filename);
+				cb.refname);
 			transaction_rollback(cb.t);
 		} else if (transaction_commit(cb.t, NULL)) {
 			status |= error("cannot commit reflog for %s", ref);
-		} else if (cmd->updateref && commit_ref(lock)) {
-			status |= error("Couldn't set %s", lock->ref_name);
 		}
-		transaction_free(cb.t);
 	}
-	unlock_ref(lock);
+	transaction_free(cb.t);
 	return status;
 }
 
-- 
2.0.0.rc3.506.g3739a35

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html




[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]