git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v7 41/42] refs.c: make rename_ref use a transaction

From
Ronnie Sahlberg <sahlberg@google.com>
Date
May 14, 2014, 21:17 UTC
Message-ID
<1400102236-30082-42-git-send-email-sahlberg@google.com>
In-Reply-To
<1400102236-30082-1-git-send-email-sahlberg@google.com>
Change rename_ref to use a single transaction to perform the ref rename.
Signed-off-by: Ronnie Sahlberg <sahlberg@google.com>
---
 refs.c | 73 ++++++++++++++++++------------------------------------------------
 1 file changed, 20 insertions(+), 53 deletions(-)
diff --git a/refs.c b/refs.c
index 7569f4e..8e12386 100644
--- a/refs.c
+++ b/refs.c
@@ -2591,9 +2591,10 @@ static int rename_tmp_log(const char *newrefname)
 
 int rename_ref(const char *oldrefname, const char *newrefname, const char *logmsg)
 {
-	unsigned char sha1[20], orig_sha1[20];
-	int flag = 0, logmoved = 0;
-	struct ref_lock *lock;
+	unsigned char sha1[20];
+	int flag = 0;
+	struct ref_transaction *transaction;
+	struct strbuf err = STRBUF_INIT;
 	struct stat loginfo;
 	int log = !lstat(git_path("logs/%s", oldrefname), &loginfo);
 	const char *symref = NULL;
@@ -2604,7 +2605,7 @@ int rename_ref(const char *oldrefname, const char *newrefname, const char *logms
 	if (log && S_ISLNK(loginfo.st_mode))
 		return error("reflog for %s is a symlink", oldrefname);
 
-	symref = resolve_ref_unsafe(oldrefname, orig_sha1, 1, &flag);
+	symref = resolve_ref_unsafe(oldrefname, sha1, 1, &flag);
 	if (flag & REF_ISSYMREF)
 		return error("refname %s is a symbolic ref, renaming it is not supported",
 			oldrefname);
@@ -2626,62 +2627,28 @@ int rename_ref(const char *oldrefname, const char *newrefname, const char *logms
 	if (pack_refs(PACK_REFS_ALL | PACK_REFS_PRUNE))
 		return error("unable to pack refs");
 
-	if (delete_ref(oldrefname, orig_sha1, REF_NODEREF)) {
-		error("unable to delete old %s", oldrefname);
-		goto rollback;
-	}
-
-	if (!read_ref_full(newrefname, sha1, 1, NULL) &&
-	    delete_ref(newrefname, sha1, REF_NODEREF)) {
-		if (errno==EISDIR) {
-			if (remove_empty_directories(git_path("%s", newrefname))) {
-				error("Directory not empty: %s", newrefname);
-				goto rollback;
-			}
-		} else {
-			error("unable to delete existing %s", newrefname);
-			goto rollback;
-		}
+	transaction = ref_transaction_begin();
+	if (!transaction ||
+	    ref_transaction_delete(transaction, oldrefname, sha1,
+				   REF_NODEREF | REF_ISPACKONLY,
+				   1, NULL) ||
+	    ref_transaction_update(transaction, newrefname, sha1,
+				   NULL, 0, 0, logmsg) ||
+	    ref_transaction_commit(transaction, &err)) {
+		ref_transaction_rollback(transaction);
+		error("rename_ref failed: %s", err.buf);
+		strbuf_release(&err);
+		goto rollbacklog;
 	}
+	ref_transaction_free(transaction);
 
 	if (log && rename_tmp_log(newrefname))
-		goto rollback;
-
-	logmoved = log;
-
-	lock = lock_ref_sha1_basic(newrefname, NULL, 0, NULL, NULL, 0);
-	if (!lock) {
-		error("unable to lock %s for update", newrefname);
-		goto rollback;
-	}
-	lock->force_write = 1;
-	hashcpy(lock->old_sha1, orig_sha1);
-	if (write_ref_sha1(lock, orig_sha1, logmsg)) {
-		error("unable to write current sha1 into %s", newrefname);
-		goto rollback;
-	}
-
-	return 0;
-
- rollback:
-	lock = lock_ref_sha1_basic(oldrefname, NULL, 0, NULL, NULL, 0);
-	if (!lock) {
-		error("unable to lock %s for rollback", oldrefname);
 		goto rollbacklog;
-	}
 
-	lock->force_write = 1;
-	flag = log_all_ref_updates;
-	log_all_ref_updates = 0;
-	if (write_ref_sha1(lock, orig_sha1, NULL))
-		error("unable to write current sha1 into %s", oldrefname);
-	log_all_ref_updates = flag;
+	return 0;
 
  rollbacklog:
-	if (logmoved && rename(git_path("logs/%s", newrefname), git_path("logs/%s", oldrefname)))
-		error("unable to restore logfile %s from %s: %s",
-			oldrefname, newrefname, strerror(errno));
-	if (!logmoved && log &&
+	if (log &&
 	    rename(git_path(TMP_RENAMED_LOG), git_path("logs/%s", oldrefname)))
 		error("unable to restore logfile %s from "TMP_RENAMED_LOG": %s",
 			oldrefname, strerror(errno));
-- 
2.0.0.rc3.471.g2055d11.dirty
Previous: Ronnie SahlbergNext: Ronnie Sahlberg
Message 42 of 43 in “Use ref transactions for all ref updates”
  1. 00/42 Use ref transactions for all ref updatesRonnie Sahlberg, May 14, 2014
  2. 01/42 refs.c: constify the sha arguments for ref_transaction_create|delete|updateRonnie Sahlberg, May 14, 2014
  3. 02/42 refs.c: allow passing NULL to ref_transaction_freeRonnie Sahlberg, May 14, 2014
  4. 03/42 refs.c: add a strbuf argument to ref_transaction_commit for error loggingRonnie Sahlberg, May 14, 2014
  5. 04/42 refs.c: make ref_update_reject_duplicates take a strbuf argument for errorsRonnie Sahlberg, May 14, 2014
  6. 05/42 update-ref.c: log transaction error from the update_refRonnie Sahlberg, May 14, 2014
  7. 06/42 refs.c: make update_ref_write update a strbuf on failureRonnie Sahlberg, May 14, 2014
  8. 07/42 refs.c: remove the onerr argument to ref_transaction_commitRonnie Sahlberg, May 14, 2014
  9. 08/42 refs.c: change ref_transaction_update() to do error checking and return statusRonnie Sahlberg, May 14, 2014
  10. 09/42 refs.c: change ref_transaction_create to do error checking and return statusRonnie Sahlberg, May 14, 2014
  11. 10/42 refs.c: ref_transaction_delete to check for error and return statusRonnie Sahlberg, May 14, 2014
  12. 11/42 tag.c: use ref transactions when doing updatesRonnie Sahlberg, May 14, 2014
  13. 12/42 replace.c: use the ref transaction functions for updatesRonnie Sahlberg, May 14, 2014
  14. 13/42 commit.c: use ref transactions for updatesRonnie Sahlberg, May 14, 2014
  15. 14/42 sequencer.c: use ref transactions for all ref updatesRonnie Sahlberg, May 14, 2014
  16. 15/42 fast-import.c: change update_branch to use ref transactionsRonnie Sahlberg, May 14, 2014
  17. 16/42 branch.c: use ref transaction for all ref updatesRonnie Sahlberg, May 14, 2014
  18. 17/42 refs.c: change update_ref to use a transactionRonnie Sahlberg, May 14, 2014
  19. 18/42 refs.c: free the transaction before returning when number of updates is 0Ronnie Sahlberg, May 14, 2014
  20. 19/42 refs.c: ref_transaction_commit should not free the transactionRonnie Sahlberg, May 14, 2014
  21. 20/42 fetch.c: clear errno before calling functions that might set itRonnie Sahlberg, May 14, 2014
  22. 21/42 fetch.c: change s_update_ref to use a ref transactionRonnie Sahlberg, May 14, 2014
  23. 22/42 fetch.c: use a single ref transaction for all ref updatesRonnie Sahlberg, May 14, 2014
  24. 23/42 receive-pack.c: use a reference transaction for updating the refsRonnie Sahlberg, May 14, 2014
  25. 24/42 fast-import.c: use a ref transaction when dumping tagsRonnie Sahlberg, May 14, 2014
  26. 25/42 walker.c: use ref transaction for ref updatesRonnie Sahlberg, May 14, 2014
  27. 26/42 refs.c: make write_ref_sha1 staticRonnie Sahlberg, May 14, 2014
  28. 27/42 refs.c: make lock_ref_sha1 staticRonnie Sahlberg, May 14, 2014
  29. 28/42 refs.c: add transaction.status and track OPEN/CLOSED/ERRORRonnie Sahlberg, May 14, 2014
  30. 29/42 refs.c: remove the update_ref_lock functionRonnie Sahlberg, May 14, 2014
  31. 30/42 refs.c: remove the update_ref_write functionRonnie Sahlberg, May 14, 2014
  32. 31/42 refs.c: remove lock_ref_sha1Ronnie Sahlberg, May 14, 2014
  33. 32/42 refs.c: make prune_ref use a transaction to delete the refRonnie Sahlberg, May 14, 2014
  34. 33/42 refs.c: make delete_ref use a transactionRonnie Sahlberg, May 14, 2014
  35. 34/42 refs.c: pass the ref log message to _create/delete/update instead of _commitRonnie Sahlberg, May 14, 2014
  36. 35/42 refs.c: pass NULL as *flags to read_ref_fullRonnie Sahlberg, May 14, 2014
  37. 36/42 refs.c: pack all refs before we start to rename a refRonnie Sahlberg, May 14, 2014
  38. 37/42 refs.c: move the check for valid refname to lock_ref_sha1_basicRonnie Sahlberg, May 14, 2014
  39. 38/42 refs.c: call lock_ref_sha1_basic directly from commitRonnie Sahlberg, May 14, 2014
  40. 39/42 refs.c: add a new flag for transaction delete for refs we know are packed onlyRonnie Sahlberg, May 14, 2014
  41. 40/42 refs.c: pass a skip list to name_conflict_fnRonnie Sahlberg, May 14, 2014
  42. 41/42 refs.c: make rename_ref use a transactionRonnie Sahlberg, May 14, 2014
  43. 42/42 refs.c: remove forward declaration of write_ref_sha1Ronnie Sahlberg, May 14, 2014

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.