repack_without_refs(): make the refnames argument a string_list
Most of the callers have string_lists available already, whereas two of them had to read data out of a string_list into an array of strings just to call this function. So change repack_without_refs() to take the list of refnames to omit as a string_list, and change the callers accordingly. Suggested-by: Ronnie Sahlberg <sahlberg@google.com> Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Michael Haggerty committed
Nov 25, 2014 at 09:02 UTC
4a45b2f347245d321bf5c07fbe3f8091d122732f
3 files changed
+31
-31
builtin/remote.c
+2
-12
@@ -750,16 +750,11 @@ static int mv(int argc, const char **argv)
750
static int remove_branches(struct string_list *branches)
751
{
752
struct strbuf err = STRBUF_INIT;
753
- const char **branch_names;
753
int i, result = 0;
754
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))
756
result |= error("%s", err.buf);
757
strbuf_release(&err);
762
- free(branch_names);
758
759
for (i = 0; i < branches->nr; i++) {
760
struct string_list_item *item = branches->items + i;
@@ -1317,7 +1312,6 @@ static int prune_remote(const char *remote, int dry_run)
1312
int result = 0, i;
1313
struct ref_states states;
1314
struct string_list delete_refs_list = STRING_LIST_INIT_NODUP;
1320
- const char **delete_refs;
1315
const char *dangling_msg = dry_run
1316
? _(" %s will become dangling!")
1317
: _(" %s has become dangling!");
@@ -1336,19 +1330,16 @@ static int prune_remote(const char *remote, int dry_run)
1330
? states.remote->url[0]
1331
: _("(no URL)"));
1332
1339
- delete_refs = xmalloc(states.stale.nr * sizeof(*delete_refs));
1333
for (i = 0; i < states.stale.nr; i++) {
1334
const char *refname = states.stale.items[i].util;
1335
1343
- delete_refs[i] = refname;
1336
string_list_append(&delete_refs_list, refname);
1337
}
1338
sort_string_list(&delete_refs_list);
1339
1340
if (!dry_run) {
1341
struct strbuf err = STRBUF_INIT;
1350
- if (repack_without_refs(delete_refs, states.stale.nr,
1351
- &err))
1342
+ if (repack_without_refs(&delete_refs_list, &err))
1343
result |= error("%s", err.buf);
1344
strbuf_release(&err);
1345
}
@@ -1369,7 +1360,6 @@ static int prune_remote(const char *remote, int dry_run)
1360
1361
warn_dangling_symrefs(stdout, dangling_msg, &delete_refs_list);
1362
1372
- free(delete_refs);
1363
string_list_clear(&delete_refs_list, 0);
1364
free_remote_ref_states(&states);
1365
return result;
refs.c
+20
-18
@@ -2639,22 +2639,25 @@ static int curate_packed_ref_fn(struct ref_entry *entry, void *cb_data)
2639
return 0;
2640
}
2641
2642
-int repack_without_refs(const char **refnames, int n, struct strbuf *err)
2642
+int repack_without_refs(struct string_list *refnames, struct strbuf *err)
2643
{
2644
struct ref_dir *packed;
2645
struct string_list refs_to_delete = STRING_LIST_INIT_DUP;
2646
- struct string_list_item *ref_to_delete;
2647
- int i, ret, removed = 0;
2646
+ struct string_list_item *refname, *ref_to_delete;
2647
+ int ret, needs_repacking = 0, removed = 0;
2648
2649
assert(err);
2650
2651
/* Look for a packed ref */
2652
- for (i = 0; i < n; i++)
2653
- if (get_packed_ref(refnames[i]))
2652
+ for_each_string_list_item(refname, refnames) {
2653
+ if (get_packed_ref(refname->string)) {
2654
+ needs_repacking = 1;
2655
break;
2656
+ }
2657
+ }
2658
2659
/* Avoid locking if we have nothing to do */
2657
- if (i == n)
2660
+ if (!needs_repacking)
2661
return 0; /* no refname exists in packed refs */
2662
2663
if (lock_packed_refs(0)) {
@@ -2664,8 +2667,8 @@ int repack_without_refs(const char **refnames, int n, struct strbuf *err)
2667
packed = get_packed_refs(&ref_cache);
2668
2669
/* Remove refnames from the cache */
2667
- for (i = 0; i < n; i++)
2668
- if (remove_entry(packed, refnames[i]) != -1)
2670
+ for_each_string_list_item(refname, refnames)
2671
+ if (remove_entry(packed, refname->string) != -1)
2672
removed = 1;
2673
if (!removed) {
2674
/*
@@ -3738,10 +3741,11 @@ static int ref_update_reject_duplicates(struct ref_update **updates, int n,
3741
int ref_transaction_commit(struct ref_transaction *transaction,
3742
struct strbuf *err)
3743
{
3741
- int ret = 0, delnum = 0, i;
3742
- const char **delnames;
3744
+ int ret = 0, i;
3745
int n = transaction->nr;
3746
struct ref_update **updates = transaction->updates;
3747
+ struct string_list refs_to_delete = STRING_LIST_INIT_NODUP;
3748
+ struct string_list_item *ref_to_delete;
3749
3750
assert(err);
3751
@@ -3753,9 +3757,6 @@ int ref_transaction_commit(struct ref_transaction *transaction,
3757
return 0;
3758
}
3759
3756
- /* Allocate work space */
3757
- delnames = xmalloc(sizeof(*delnames) * n);
3758
-
3760
/* Copy, sort, and reject duplicate refs */
3761
qsort(updates, n, sizeof(*updates), ref_update_compare);
3762
if (ref_update_reject_duplicates(updates, n, err)) {
@@ -3815,16 +3816,17 @@ int ref_transaction_commit(struct ref_transaction *transaction,
3816
}
3817
3818
if (!(update->flags & REF_ISPRUNING))
3818
- delnames[delnum++] = update->lock->ref_name;
3819
+ string_list_append(&refs_to_delete,
3820
+ update->lock->ref_name);
3821
}
3822
}
3823
3822
- if (repack_without_refs(delnames, delnum, err)) {
3824
+ if (repack_without_refs(&refs_to_delete, err)) {
3825
ret = TRANSACTION_GENERIC_ERROR;
3826
goto cleanup;
3827
}
3826
- for (i = 0; i < delnum; i++)
3827
- unlink_or_warn(git_path("logs/%s", delnames[i]));
3828
+ for_each_string_list_item(ref_to_delete, &refs_to_delete)
3829
+ unlink_or_warn(git_path("logs/%s", ref_to_delete->string));
3830
clear_loose_ref_cache(&ref_cache);
3831
3832
cleanup:
@@ -3833,7 +3835,7 @@ cleanup:
3835
for (i = 0; i < n; i++)
3836
if (updates[i]->lock)
3837
unlock_ref(updates[i]->lock);
3836
- free(delnames);
3838
+ string_list_clear(&refs_to_delete, 0);
3839
return ret;
3840
}
3841
refs.h
+9
-1
@@ -163,7 +163,15 @@ extern void rollback_packed_refs(void);
163
*/
164
int pack_refs(unsigned int flags);
165
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,
175
struct strbuf *err);
176
177
extern int ref_exists(const char *);