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

[PATCH 4/4] refs.c: sort the refs by new_sha1 and merge the two update/delete loops into one

From
Ronnie Sahlberg <sahlberg@google.com>
Date
Apr 10, 2014, 18:30 UTC
Message-ID
<1397154625-11884-5-git-send-email-sahlberg@google.com>
In-Reply-To
<1397154625-11884-1-git-send-email-sahlberg@google.com>

We want to make sure that we update all refs before we delete any refs so that there is no point in time where the tips may not be reachable from any ref in the system. We currently acheive this by first looping over all updates and applying them before we run a second loop and perform all the deletes.

If we sort the new sha1 for all the refs so that a deleted ref, with sha1 0{40} comes at the end of the array, then we can just run a single loop and still guarantee that all updates happen before any deletes.

Signed-off-by: Ronnie Sahlberg <sahlberg@google.com>
---
 refs.c | 18 +++++++++++-------
 1 file changed, 11 insertions(+), 7 deletions(-)
diff --git a/refs.c b/refs.c
index 1678e12..453318e 100644
--- a/refs.c
+++ b/refs.c
@@ -3309,6 +3309,15 @@ static int ref_update_compare(const void *r1, const void *r2)
 	return strcmp((*u1)->ref_name, (*u2)->ref_name);
 }
 
+static int ref_delete_compare(const void *r1, const void *r2)
+{
+	const struct ref_update * const *u1 = r1;
+	const struct ref_update * const *u2 = r2;
+
+	/* -strcmp so that 0{40} sorts to the end */
+	return -strcmp((*u1)->new_sha1, (*u2)->new_sha1);
+}
+
 static int ref_update_reject_duplicates(struct ref_update **updates, int n,
 					enum action_on_err onerr)
 {
@@ -3388,13 +3397,8 @@ int update_refs(const char *action, const struct ref_update **updates_orig,
 		unlink_or_warn(git_path("logs/%s", delnames[i]));
 	clear_loose_ref_cache(&ref_cache);
 
-	/* Perform updates first so live commits remain referenced */
-	for (i = 0; i < n; i++)
-		if (locks[i] && !locks[i]->delete_ref) {
-			ret |= commit_ref_lock(locks[i]);
-			locks[i] = NULL;
-		}
-	/* And finally perform all deletes */
+	/* Sort the array so that we perform all updates before any deletes */
+	qsort(updates, n, sizeof(*updates), ref_delete_compare);
 	for (i = 0; i < n; i++)
 		if (locks[i]) {
 			ret |= commit_ref_lock(locks[i]);
-- 
1.9.1.478.ga5a8238.dirty
Previous: Ronnie SahlbergNext: Junio C Hamano
Message 5 of 9 in “Make update_refs more atomic V2”
  1. 0/4 Make update_refs more atomic V2Ronnie Sahlberg, Apr 10, 2014
  2. 1/4 refs.c: split writing and commiting a ref into two separate functionsRonnie Sahlberg, Apr 10, 2014
  3. 2/4 refs.c: split delete_ref_loose() into a separate flag-for-deletion and commit phaseRonnie Sahlberg, Apr 10, 2014
  4. 3/4 refs.c: change update_refs to run the commit loops once all work is finishedRonnie Sahlberg, Apr 10, 2014
  5. 4/4 refs.c: sort the refs by new_sha1 and merge the two update/delete loops into oneRonnie Sahlberg, Apr 10, 2014
  6. Junio C HamanoApr 10, 2014
  7. Ronnie SahlbergApr 10, 2014
  8. Junio C HamanoApr 10, 2014
  9. Junio C HamanoApr 10, 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.