remote: repack packed-refs once when deleting multiple refs

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> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jens Lindström committed May 23, 2014 at 12:29 UTC c9e768bb77762765f739fffb5c09d0888f3de20e
3 files changed +21 -3
builtin/remote.c
+18 -2
@@ -749,15 +749,23 @@ static int mv(int argc, const char **argv)
749
750 static int remove_branches(struct string_list *branches)
751 {
752 + const char **branch_names;
753 int i, result = 0;
754 +
755 + branch_names = xmalloc(branches->nr * sizeof(*branch_names));
756 + for (i = 0; i < branches->nr; i++)
757 + branch_names[i] = branches->items[i].string;
758 + result |= repack_without_refs(branch_names, branches->nr);
759 + free(branch_names);
760 +
761 for (i = 0; i < branches->nr; i++) {
762 struct string_list_item *item = branches->items + i;
763 const char *refname = item->string;
756 - unsigned char *sha1 = item->util;
764
758 - if (delete_ref(refname, sha1, 0))
765 + if (delete_ref(refname, NULL, 0))
766 result |= error(_("Could not remove branch %s"), refname);
767 }
768 +
769 return result;
770 }
771
@@ -1305,6 +1313,7 @@ static int prune_remote(const char *remote, int dry_run)
1313 {
1314 int result = 0, i;
1315 struct ref_states states;
1316 + const char **delete_refs;
1317 const char *dangling_msg = dry_run
1318 ? _(" %s will become dangling!")
1319 : _(" %s has become dangling!");
@@ -1318,6 +1327,13 @@ static int prune_remote(const char *remote, int dry_run)
1327 states.remote->url_nr
1328 ? states.remote->url[0]
1329 : _("(no URL)"));
1330 +
1331 + delete_refs = xmalloc(states.stale.nr * sizeof(*delete_refs));
1332 + for (i = 0; i < states.stale.nr; i++)
1333 + delete_refs[i] = states.stale.items[i].util;
1334 + if (!dry_run)
1335 + result |= repack_without_refs(delete_refs, states.stale.nr);
1336 + free(delete_refs);
1337 }
1338
1339 for (i = 0; i < states.stale.nr; i++) {
refs.c
+1 -1
@@ -2431,7 +2431,7 @@ static int curate_packed_ref_fn(struct ref_entry *entry, void *cb_data)
2431 return 0;
2432 }
2433
2434 -static int repack_without_refs(const char **refnames, int n)
2434 +int repack_without_refs(const char **refnames, int n)
2435 {
2436 struct ref_dir *packed;
2437 struct string_list refs_to_delete = STRING_LIST_INIT_DUP;
refs.h
+2
@@ -132,6 +132,8 @@ extern void rollback_packed_refs(void);
132 */
133 int pack_refs(unsigned int flags);
134
135 +extern int repack_without_refs(const char **refnames, int n);
136 +
137 extern int ref_exists(const char *);
138
139 /*