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

[PATCH v2 2/3] remote: repack packed-refs once when deleting multiple refs

From
Jens Lindström <jl@opera.com>
Date
May 23, 2014, 10:29 UTC
Message-ID
<537F2319.7040105@opera.com>
In-Reply-To
<537F2252.3010101@opera.com>

When 'git remote rm' or 'git remote prune' were used in a repository with many refs, and needed to delete many remote-tracking refs, a lot of time was spent deleting those refs since for each deleted ref, repack_without_refs() was called to rewrite packed-refs without just that deleted ref.

To avoid this, call repack_without_refs() first to repack without all the refs that will be deleted, before calling delete_ref() to delete each one completely. The call to repack_without_ref() in delete_ref() then becomes a no-op, since packed-refs already won't contain any of the deleted refs.

Signed-off-by: Jens Lindström <jl@opera.com>
---
Note: remove_branches() no longer checks that the remote-tracking
branches it deletes point at the right object before deleting them
by passing the expected SHA-1 to delete_ref().  This was a required
change since all packed refs have been deleted already by the time
we call delete_ref(), which causes delete_ref() to fail if given an
expected SHA-1 to check.  'remote prune' already behaved this way.
 builtin/remote.c | 20 ++++++++++++++++++--
 refs.c           |  2 +-
 refs.h           |  2 ++
 3 files changed, 21 insertions(+), 3 deletions(-)
diff --git a/builtin/remote.c b/builtin/remote.c
index 84802cd..d33abe6 100644
--- a/builtin/remote.c
+++ b/builtin/remote.c
@@ -749,15 +749,23 @@ static int mv(int argc, const char **argv)
 
 static int remove_branches(struct string_list *branches)
 {
+	const char **branch_names;
 	int i, result = 0;
+
+	branch_names = xmalloc(branches->nr * sizeof(*branch_names));
+	for (i = 0; i < branches->nr; i++)
+		branch_names[i] = branches->items[i].string;
+	result |= repack_without_refs(branch_names, branches->nr);
+	free(branch_names);
+
 	for (i = 0; i < branches->nr; i++) {
 		struct string_list_item *item = branches->items + i;
 		const char *refname = item->string;
-		unsigned char *sha1 = item->util;
 
-		if (delete_ref(refname, sha1, 0))
+		if (delete_ref(refname, NULL, 0))
 			result |= error(_("Could not remove branch %s"), refname);
 	}
+
 	return result;
 }
 
@@ -1305,6 +1313,7 @@ static int prune_remote(const char *remote, int dry_run)
 {
 	int result = 0, i;
 	struct ref_states states;
+	const char **delete_refs;
 	const char *dangling_msg = dry_run
 		? _(" %s will become dangling!")
 		: _(" %s has become dangling!");
@@ -1318,6 +1327,13 @@ static int prune_remote(const char *remote, int dry_run)
 		       states.remote->url_nr
 		       ? states.remote->url[0]
 		       : _("(no URL)"));
+
+		delete_refs = xmalloc(states.stale.nr * sizeof(*delete_refs));
+		for (i = 0; i < states.stale.nr; i++)
+			delete_refs[i] = states.stale.items[i].util;
+		if (!dry_run)
+			result |= repack_without_refs(delete_refs, states.stale.nr);
+		free(delete_refs);
 	}
 
 	for (i = 0; i < states.stale.nr; i++) {
diff --git a/refs.c b/refs.c
index 28d5eca..262c1c2 100644
--- a/refs.c
+++ b/refs.c
@@ -2431,7 +2431,7 @@ static int curate_packed_ref_fn(struct ref_entry *entry, void *cb_data)
 	return 0;
 }
 
-static int repack_without_refs(const char **refnames, int n)
+int repack_without_refs(const char **refnames, int n)
 {
 	struct ref_dir *packed;
 	struct string_list refs_to_delete = STRING_LIST_INIT_DUP;
diff --git a/refs.h b/refs.h
index 87a1a79..f287c7a 100644
--- a/refs.h
+++ b/refs.h
@@ -132,6 +132,8 @@ extern void rollback_packed_refs(void);
  */
 int pack_refs(unsigned int flags);
 
+extern int repack_without_refs(const char **refnames, int n);
+
 extern int ref_exists(const char *);
 
 /*
-- 
1.9.1
Previous: Junio C HamanoNext: Junio C Hamano
Message 13 of 16 in “remote: optimize rm/prune ref deletion”
  1. 0/2 remote: optimize rm/prune ref deletionJens Lindström, May 20, 2014
  2. 1/2 remote: defer repacking packed-refs when deleting refsJens Lindström, May 20, 2014
  3. Junio C HamanoMay 20, 2014
  4. Junio C HamanoMay 20, 2014
  5. Jens LindströmMay 23, 2014
  6. Junio C HamanoMay 23, 2014
  7. Jens LindströmMay 24, 2014
  8. Junio C HamanoMay 27, 2014
  9. 2/2 remote prune: optimize "dangling symref" check/warningJens Lindström, May 20, 2014
  10. 0/3 remote: optimize rm/prune ref deletionJens Lindström, May 23, 2014
  11. 1/3 remote rm: delete remote configuration as the lastJens Lindström, May 23, 2014
  12. Junio C HamanoMay 23, 2014
  13. 2/3 remote: repack packed-refs once when deleting multiple refsJens Lindström, May 23, 2014
  14. Junio C HamanoMay 23, 2014
  15. Junio C HamanoMay 23, 2014
  16. 3/3 remote prune: optimize "dangling symref" check/warningJens Lindström, May 23, 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.