Skip to content

Commit

Permalink
remote: repack packed-refs once when deleting multiple refs
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Jens Lindström authored and Junio C Hamano committed May 27, 2014
1 parent b07bdd3 commit c9e768b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
20 changes: 18 additions & 2 deletions builtin/remote.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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!");
Expand All @@ -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++) {
Expand Down
2 changes: 1 addition & 1 deletion refs.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions refs.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 *);

/*
Expand Down

0 comments on commit c9e768b

Please sign in to comment.