Skip to content

Commit c4f79d1

Browse files
committed
Merge branch 'jl/remote-rm-prune' into maint
"git remote rm" and "git remote prune" can involve removing many refs at once, which is not a very efficient thing to do when very many refs exist in the packed-refs file. * jl/remote-rm-prune: remote prune: optimize "dangling symref" check/warning remote: repack packed-refs once when deleting multiple refs remote rm: delete remote configuration as the last
2 parents ada8710 + e6bea66 commit c4f79d1

File tree

3 files changed

+52
-9
lines changed

3 files changed

+52
-9
lines changed

builtin/remote.c

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -749,15 +749,23 @@ static int mv(int argc, const char **argv)
749749

750750
static int remove_branches(struct string_list *branches)
751751
{
752+
const char **branch_names;
752753
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+
753761
for (i = 0; i < branches->nr; i++) {
754762
struct string_list_item *item = branches->items + i;
755763
const char *refname = item->string;
756-
unsigned char *sha1 = item->util;
757764

758-
if (delete_ref(refname, sha1, 0))
765+
if (delete_ref(refname, NULL, 0))
759766
result |= error(_("Could not remove branch %s"), refname);
760767
}
768+
761769
return result;
762770
}
763771

@@ -789,10 +797,6 @@ static int rm(int argc, const char **argv)
789797
known_remotes.to_delete = remote;
790798
for_each_remote(add_known_remote, &known_remotes);
791799

792-
strbuf_addf(&buf, "remote.%s", remote->name);
793-
if (git_config_rename_section(buf.buf, NULL) < 1)
794-
return error(_("Could not remove config section '%s'"), buf.buf);
795-
796800
read_branches();
797801
for (i = 0; i < branch_list.nr; i++) {
798802
struct string_list_item *item = branch_list.items + i;
@@ -837,6 +841,12 @@ static int rm(int argc, const char **argv)
837841
}
838842
string_list_clear(&skipped, 0);
839843

844+
if (!result) {
845+
strbuf_addf(&buf, "remote.%s", remote->name);
846+
if (git_config_rename_section(buf.buf, NULL) < 1)
847+
return error(_("Could not remove config section '%s'"), buf.buf);
848+
}
849+
840850
return result;
841851
}
842852

@@ -1303,6 +1313,8 @@ static int prune_remote(const char *remote, int dry_run)
13031313
{
13041314
int result = 0, i;
13051315
struct ref_states states;
1316+
struct string_list delete_refs_list = STRING_LIST_INIT_NODUP;
1317+
const char **delete_refs;
13061318
const char *dangling_msg = dry_run
13071319
? _(" %s will become dangling!")
13081320
: _(" %s has become dangling!");
@@ -1316,11 +1328,20 @@ static int prune_remote(const char *remote, int dry_run)
13161328
states.remote->url_nr
13171329
? states.remote->url[0]
13181330
: _("(no URL)"));
1331+
1332+
delete_refs = xmalloc(states.stale.nr * sizeof(*delete_refs));
1333+
for (i = 0; i < states.stale.nr; i++)
1334+
delete_refs[i] = states.stale.items[i].util;
1335+
if (!dry_run)
1336+
result |= repack_without_refs(delete_refs, states.stale.nr);
1337+
free(delete_refs);
13191338
}
13201339

13211340
for (i = 0; i < states.stale.nr; i++) {
13221341
const char *refname = states.stale.items[i].util;
13231342

1343+
string_list_insert(&delete_refs_list, refname);
1344+
13241345
if (!dry_run)
13251346
result |= delete_ref(refname, NULL, 0);
13261347

@@ -1330,9 +1351,11 @@ static int prune_remote(const char *remote, int dry_run)
13301351
else
13311352
printf_ln(_(" * [pruned] %s"),
13321353
abbrev_ref(refname, "refs/remotes/"));
1333-
warn_dangling_symref(stdout, dangling_msg, refname);
13341354
}
13351355

1356+
warn_dangling_symrefs(stdout, dangling_msg, &delete_refs_list);
1357+
string_list_clear(&delete_refs_list, 0);
1358+
13361359
free_remote_ref_states(&states);
13371360
return result;
13381361
}

refs.c

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1611,6 +1611,7 @@ int peel_ref(const char *refname, unsigned char *sha1)
16111611
struct warn_if_dangling_data {
16121612
FILE *fp;
16131613
const char *refname;
1614+
const struct string_list *refnames;
16141615
const char *msg_fmt;
16151616
};
16161617

@@ -1625,8 +1626,12 @@ static int warn_if_dangling_symref(const char *refname, const unsigned char *sha
16251626
return 0;
16261627

16271628
resolves_to = resolve_ref_unsafe(refname, junk, 0, NULL);
1628-
if (!resolves_to || strcmp(resolves_to, d->refname))
1629+
if (!resolves_to
1630+
|| (d->refname
1631+
? strcmp(resolves_to, d->refname)
1632+
: !string_list_has_string(d->refnames, resolves_to))) {
16291633
return 0;
1634+
}
16301635

16311636
fprintf(d->fp, d->msg_fmt, refname);
16321637
fputc('\n', d->fp);
@@ -1639,6 +1644,18 @@ void warn_dangling_symref(FILE *fp, const char *msg_fmt, const char *refname)
16391644

16401645
data.fp = fp;
16411646
data.refname = refname;
1647+
data.refnames = NULL;
1648+
data.msg_fmt = msg_fmt;
1649+
for_each_rawref(warn_if_dangling_symref, &data);
1650+
}
1651+
1652+
void warn_dangling_symrefs(FILE *fp, const char *msg_fmt, const struct string_list *refnames)
1653+
{
1654+
struct warn_if_dangling_data data;
1655+
1656+
data.fp = fp;
1657+
data.refname = NULL;
1658+
data.refnames = refnames;
16421659
data.msg_fmt = msg_fmt;
16431660
for_each_rawref(warn_if_dangling_symref, &data);
16441661
}
@@ -2431,7 +2448,7 @@ static int curate_packed_ref_fn(struct ref_entry *entry, void *cb_data)
24312448
return 0;
24322449
}
24332450

2434-
static int repack_without_refs(const char **refnames, int n)
2451+
int repack_without_refs(const char **refnames, int n)
24352452
{
24362453
struct ref_dir *packed;
24372454
struct string_list refs_to_delete = STRING_LIST_INIT_DUP;

refs.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ static inline const char *has_glob_specials(const char *pattern)
8989
extern int for_each_rawref(each_ref_fn, void *);
9090

9191
extern void warn_dangling_symref(FILE *fp, const char *msg_fmt, const char *refname);
92+
extern void warn_dangling_symrefs(FILE *fp, const char *msg_fmt, const struct string_list* refnames);
9293

9394
/*
9495
* Lock the packed-refs file for writing. Flags is passed to
@@ -132,6 +133,8 @@ extern void rollback_packed_refs(void);
132133
*/
133134
int pack_refs(unsigned int flags);
134135

136+
extern int repack_without_refs(const char **refnames, int n);
137+
135138
extern int ref_exists(const char *);
136139

137140
/*

0 commit comments

Comments
 (0)