Skip to content

Commit a7ddaa8

Browse files
committed
Merge branch 'mh/simplify-repack-without-refs'
"git remote update --prune" to drop many refs has been optimized. * mh/simplify-repack-without-refs: sort_string_list(): rename to string_list_sort() prune_remote(): iterate using for_each_string_list_item() prune_remote(): rename local variable repack_without_refs(): make the refnames argument a string_list prune_remote(): sort delete_refs_list references en masse prune_remote(): initialize both delete_refs lists in a single loop prune_remote(): exit early if there are no stale references
2 parents 8e606f9 + 3383e19 commit a7ddaa8

13 files changed

Lines changed: 75 additions & 70 deletions

File tree

Documentation/technical/api-string-list.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ member (you need this if you add things later) and you should set the
2929
`unsorted_string_list_has_string` and get it from the list using
3030
`string_list_lookup` for sorted lists.
3131

32-
. Can sort an unsorted list using `sort_string_list`.
32+
. Can sort an unsorted list using `string_list_sort`.
3333

3434
. Can remove duplicate items from a sorted list using
3535
`string_list_remove_duplicates`.
@@ -146,7 +146,7 @@ write `string_list_insert(...)->util = ...;`.
146146
ownership of a malloc()ed string to a `string_list` that has
147147
`strdup_string` set.
148148

149-
`sort_string_list`::
149+
`string_list_sort`::
150150

151151
Sort the list's entries by string value in `strcmp()` order.
152152

builtin/apply.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4180,7 +4180,7 @@ static int write_out_results(struct patch *list)
41804180
if (cpath.nr) {
41814181
struct string_list_item *item;
41824182

4183-
sort_string_list(&cpath);
4183+
string_list_sort(&cpath);
41844184
for_each_string_list_item(item, &cpath)
41854185
fprintf(stderr, "U %s\n", item->string);
41864186
string_list_clear(&cpath, 0);

builtin/receive-pack.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -964,7 +964,7 @@ static void check_aliased_updates(struct command *commands)
964964
string_list_append(&ref_list, cmd->ref_name);
965965
item->util = (void *)cmd;
966966
}
967-
sort_string_list(&ref_list);
967+
string_list_sort(&ref_list);
968968

969969
for (cmd = commands; cmd; cmd = cmd->next) {
970970
if (!cmd->error_string)

builtin/remote.c

Lines changed: 32 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -352,9 +352,9 @@ static int get_ref_states(const struct ref *remote_refs, struct ref_states *stat
352352
free_refs(stale_refs);
353353
free_refs(fetch_map);
354354

355-
sort_string_list(&states->new);
356-
sort_string_list(&states->tracked);
357-
sort_string_list(&states->stale);
355+
string_list_sort(&states->new);
356+
string_list_sort(&states->tracked);
357+
string_list_sort(&states->stale);
358358

359359
return 0;
360360
}
@@ -750,16 +750,11 @@ static int mv(int argc, const char **argv)
750750
static int remove_branches(struct string_list *branches)
751751
{
752752
struct strbuf err = STRBUF_INIT;
753-
const char **branch_names;
754753
int i, result = 0;
755754

756-
branch_names = xmalloc(branches->nr * sizeof(*branch_names));
757-
for (i = 0; i < branches->nr; i++)
758-
branch_names[i] = branches->items[i].string;
759-
if (repack_without_refs(branch_names, branches->nr, &err))
755+
if (repack_without_refs(branches, &err))
760756
result |= error("%s", err.buf);
761757
strbuf_release(&err);
762-
free(branch_names);
763758

764759
for (i = 0; i < branches->nr; i++) {
765760
struct string_list_item *item = branches->items + i;
@@ -914,7 +909,7 @@ static int get_remote_ref_states(const char *name,
914909
get_push_ref_states(remote_refs, states);
915910
} else {
916911
for_each_ref(append_ref_to_tracked_list, states);
917-
sort_string_list(&states->tracked);
912+
string_list_sort(&states->tracked);
918913
get_push_ref_states_noquery(states);
919914
}
920915

@@ -1133,7 +1128,7 @@ static int show_all(void)
11331128
if (!result) {
11341129
int i;
11351130

1136-
sort_string_list(&list);
1131+
string_list_sort(&list);
11371132
for (i = 0; i < list.nr; i++) {
11381133
struct string_list_item *item = list.items + i;
11391134
if (verbose)
@@ -1314,41 +1309,41 @@ static int set_head(int argc, const char **argv)
13141309

13151310
static int prune_remote(const char *remote, int dry_run)
13161311
{
1317-
int result = 0, i;
1312+
int result = 0;
13181313
struct ref_states states;
1319-
struct string_list delete_refs_list = STRING_LIST_INIT_NODUP;
1320-
const char **delete_refs;
1314+
struct string_list refs_to_prune = STRING_LIST_INIT_NODUP;
1315+
struct string_list_item *item;
13211316
const char *dangling_msg = dry_run
13221317
? _(" %s will become dangling!")
13231318
: _(" %s has become dangling!");
13241319

13251320
memset(&states, 0, sizeof(states));
13261321
get_remote_ref_states(remote, &states, GET_REF_STATES);
13271322

1328-
if (states.stale.nr) {
1329-
printf_ln(_("Pruning %s"), remote);
1330-
printf_ln(_("URL: %s"),
1331-
states.remote->url_nr
1332-
? states.remote->url[0]
1333-
: _("(no URL)"));
1334-
1335-
delete_refs = xmalloc(states.stale.nr * sizeof(*delete_refs));
1336-
for (i = 0; i < states.stale.nr; i++)
1337-
delete_refs[i] = states.stale.items[i].util;
1338-
if (!dry_run) {
1339-
struct strbuf err = STRBUF_INIT;
1340-
if (repack_without_refs(delete_refs, states.stale.nr,
1341-
&err))
1342-
result |= error("%s", err.buf);
1343-
strbuf_release(&err);
1344-
}
1345-
free(delete_refs);
1323+
if (!states.stale.nr) {
1324+
free_remote_ref_states(&states);
1325+
return 0;
13461326
}
13471327

1348-
for (i = 0; i < states.stale.nr; i++) {
1349-
const char *refname = states.stale.items[i].util;
1328+
printf_ln(_("Pruning %s"), remote);
1329+
printf_ln(_("URL: %s"),
1330+
states.remote->url_nr
1331+
? states.remote->url[0]
1332+
: _("(no URL)"));
1333+
1334+
for_each_string_list_item(item, &states.stale)
1335+
string_list_append(&refs_to_prune, item->util);
1336+
string_list_sort(&refs_to_prune);
1337+
1338+
if (!dry_run) {
1339+
struct strbuf err = STRBUF_INIT;
1340+
if (repack_without_refs(&refs_to_prune, &err))
1341+
result |= error("%s", err.buf);
1342+
strbuf_release(&err);
1343+
}
13501344

1351-
string_list_insert(&delete_refs_list, refname);
1345+
for_each_string_list_item(item, &states.stale) {
1346+
const char *refname = item->util;
13521347

13531348
if (!dry_run)
13541349
result |= delete_ref(refname, NULL, 0);
@@ -1361,9 +1356,9 @@ static int prune_remote(const char *remote, int dry_run)
13611356
abbrev_ref(refname, "refs/remotes/"));
13621357
}
13631358

1364-
warn_dangling_symrefs(stdout, dangling_msg, &delete_refs_list);
1365-
string_list_clear(&delete_refs_list, 0);
1359+
warn_dangling_symrefs(stdout, dangling_msg, &refs_to_prune);
13661360

1361+
string_list_clear(&refs_to_prune, 0);
13671362
free_remote_ref_states(&states);
13681363
return result;
13691364
}

builtin/repack.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ int cmd_repack(int argc, const char **argv, const char *prefix)
376376

377377
if (delete_redundant) {
378378
int opts = 0;
379-
sort_string_list(&names);
379+
string_list_sort(&names);
380380
for_each_string_list_item(item, &existing_packs) {
381381
char *sha1;
382382
size_t len = strlen(item->string);

connect.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ static void annotate_refs_with_symref_info(struct ref *ref)
9393
parse_one_symref_info(&symref, val, len);
9494
feature_list = val + 1;
9595
}
96-
sort_string_list(&symref);
96+
string_list_sort(&symref);
9797

9898
for (; ref; ref = ref->next) {
9999
struct string_list_item *item;

notes.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -902,7 +902,7 @@ int combine_notes_cat_sort_uniq(unsigned char *cur_sha1,
902902
if (string_list_add_note_lines(&sort_uniq_list, new_sha1))
903903
goto out;
904904
string_list_remove_empty_items(&sort_uniq_list, 0);
905-
sort_string_list(&sort_uniq_list);
905+
string_list_sort(&sort_uniq_list);
906906
string_list_remove_duplicates(&sort_uniq_list, 0);
907907

908908
/* create a new blob object from sort_uniq_list */

refs.c

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2645,22 +2645,25 @@ static int curate_packed_ref_fn(struct ref_entry *entry, void *cb_data)
26452645
return 0;
26462646
}
26472647

2648-
int repack_without_refs(const char **refnames, int n, struct strbuf *err)
2648+
int repack_without_refs(struct string_list *refnames, struct strbuf *err)
26492649
{
26502650
struct ref_dir *packed;
26512651
struct string_list refs_to_delete = STRING_LIST_INIT_DUP;
2652-
struct string_list_item *ref_to_delete;
2653-
int i, ret, removed = 0;
2652+
struct string_list_item *refname, *ref_to_delete;
2653+
int ret, needs_repacking = 0, removed = 0;
26542654

26552655
assert(err);
26562656

26572657
/* Look for a packed ref */
2658-
for (i = 0; i < n; i++)
2659-
if (get_packed_ref(refnames[i]))
2658+
for_each_string_list_item(refname, refnames) {
2659+
if (get_packed_ref(refname->string)) {
2660+
needs_repacking = 1;
26602661
break;
2662+
}
2663+
}
26612664

26622665
/* Avoid locking if we have nothing to do */
2663-
if (i == n)
2666+
if (!needs_repacking)
26642667
return 0; /* no refname exists in packed refs */
26652668

26662669
if (lock_packed_refs(0)) {
@@ -2670,8 +2673,8 @@ int repack_without_refs(const char **refnames, int n, struct strbuf *err)
26702673
packed = get_packed_refs(&ref_cache);
26712674

26722675
/* Remove refnames from the cache */
2673-
for (i = 0; i < n; i++)
2674-
if (remove_entry(packed, refnames[i]) != -1)
2676+
for_each_string_list_item(refname, refnames)
2677+
if (remove_entry(packed, refname->string) != -1)
26752678
removed = 1;
26762679
if (!removed) {
26772680
/*
@@ -3744,10 +3747,11 @@ static int ref_update_reject_duplicates(struct ref_update **updates, int n,
37443747
int ref_transaction_commit(struct ref_transaction *transaction,
37453748
struct strbuf *err)
37463749
{
3747-
int ret = 0, delnum = 0, i;
3748-
const char **delnames;
3750+
int ret = 0, i;
37493751
int n = transaction->nr;
37503752
struct ref_update **updates = transaction->updates;
3753+
struct string_list refs_to_delete = STRING_LIST_INIT_NODUP;
3754+
struct string_list_item *ref_to_delete;
37513755

37523756
assert(err);
37533757

@@ -3759,9 +3763,6 @@ int ref_transaction_commit(struct ref_transaction *transaction,
37593763
return 0;
37603764
}
37613765

3762-
/* Allocate work space */
3763-
delnames = xmalloc(sizeof(*delnames) * n);
3764-
37653766
/* Copy, sort, and reject duplicate refs */
37663767
qsort(updates, n, sizeof(*updates), ref_update_compare);
37673768
if (ref_update_reject_duplicates(updates, n, err)) {
@@ -3821,16 +3822,17 @@ int ref_transaction_commit(struct ref_transaction *transaction,
38213822
}
38223823

38233824
if (!(update->flags & REF_ISPRUNING))
3824-
delnames[delnum++] = update->lock->ref_name;
3825+
string_list_append(&refs_to_delete,
3826+
update->lock->ref_name);
38253827
}
38263828
}
38273829

3828-
if (repack_without_refs(delnames, delnum, err)) {
3830+
if (repack_without_refs(&refs_to_delete, err)) {
38293831
ret = TRANSACTION_GENERIC_ERROR;
38303832
goto cleanup;
38313833
}
3832-
for (i = 0; i < delnum; i++)
3833-
unlink_or_warn(git_path("logs/%s", delnames[i]));
3834+
for_each_string_list_item(ref_to_delete, &refs_to_delete)
3835+
unlink_or_warn(git_path("logs/%s", ref_to_delete->string));
38343836
clear_loose_ref_cache(&ref_cache);
38353837

38363838
cleanup:
@@ -3839,7 +3841,7 @@ int ref_transaction_commit(struct ref_transaction *transaction,
38393841
for (i = 0; i < n; i++)
38403842
if (updates[i]->lock)
38413843
unlock_ref(updates[i]->lock);
3842-
free(delnames);
3844+
string_list_clear(&refs_to_delete, 0);
38433845
return ret;
38443846
}
38453847

refs.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,15 @@ extern void rollback_packed_refs(void);
163163
*/
164164
int pack_refs(unsigned int flags);
165165

166-
extern int repack_without_refs(const char **refnames, int n,
166+
/*
167+
* Rewrite the packed-refs file, omitting any refs listed in
168+
* 'refnames'. On error, packed-refs will be unchanged, the return
169+
* value is nonzero, and a message about the error is written to the
170+
* 'err' strbuf.
171+
*
172+
* The refs in 'refnames' needn't be sorted. `err` must not be NULL.
173+
*/
174+
extern int repack_without_refs(struct string_list *refnames,
167175
struct strbuf *err);
168176

169177
extern int ref_exists(const char *);

remote.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1356,7 +1356,7 @@ static void add_missing_tags(struct ref *src, struct ref **dst, struct ref ***ds
13561356
}
13571357
clear_commit_marks_many(sent_tips.nr, sent_tips.tip, TMP_MARK);
13581358

1359-
sort_string_list(&dst_tag);
1359+
string_list_sort(&dst_tag);
13601360

13611361
/* Collect tags they do not have. */
13621362
for (ref = src; ref; ref = ref->next) {
@@ -1421,7 +1421,7 @@ static void prepare_ref_index(struct string_list *ref_index, struct ref *ref)
14211421
for ( ; ref; ref = ref->next)
14221422
string_list_append_nodup(ref_index, ref->name)->util = ref;
14231423

1424-
sort_string_list(ref_index);
1424+
string_list_sort(ref_index);
14251425
}
14261426

14271427
/*
@@ -2135,7 +2135,7 @@ struct ref *get_stale_heads(struct refspec *refs, int ref_count, struct ref *fet
21352135
info.ref_count = ref_count;
21362136
for (ref = fetch_map; ref; ref = ref->next)
21372137
string_list_append(&ref_names, ref->name);
2138-
sort_string_list(&ref_names);
2138+
string_list_sort(&ref_names);
21392139
for_each_ref(get_stale_heads_cb, &info);
21402140
string_list_clear(&ref_names, 0);
21412141
return stale_refs;

0 commit comments

Comments
 (0)