Skip to content

Commit 55ee631

Browse files
pks-tgitster
authored andcommitted
packfile: introduce macro to iterate through packs
We have a bunch of different sites that want to iterate through all packs of a given `struct packfile_store`. This pattern is somewhat verbose and repetitive, which makes it somewhat cumbersome. Introduce a new macro `packfile_store_for_each_pack()` that removes some of the boilerplate. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent a2d1939 commit 55ee631

20 files changed

+55
-50
lines changed

builtin/cat-file.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -855,7 +855,7 @@ static void batch_each_object(struct batch_options *opt,
855855
struct packfile_store *packs = the_repository->objects->packfiles;
856856
struct packed_git *pack;
857857

858-
for (pack = packfile_store_get_all_packs(packs); pack; pack = pack->next) {
858+
packfile_store_for_each_pack(packs, pack) {
859859
if (bitmap_index_contains_pack(bitmap, pack) ||
860860
open_pack_index(pack))
861861
continue;

builtin/count-objects.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ int cmd_count_objects(int argc,
130130
struct strbuf pack_buf = STRBUF_INIT;
131131
struct strbuf garbage_buf = STRBUF_INIT;
132132

133-
for (p = packfile_store_get_all_packs(packs); p; p = p->next) {
133+
packfile_store_for_each_pack(packs, p) {
134134
if (!p->pack_local)
135135
continue;
136136
if (open_pack_index(p))

builtin/fsck.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -869,18 +869,19 @@ static int check_pack_rev_indexes(struct repository *r, int show_progress)
869869
{
870870
struct packfile_store *packs = r->objects->packfiles;
871871
struct progress *progress = NULL;
872+
struct packed_git *p;
872873
uint32_t pack_count = 0;
873874
int res = 0;
874875

875876
if (show_progress) {
876-
for (struct packed_git *p = packfile_store_get_all_packs(packs); p; p = p->next)
877+
packfile_store_for_each_pack(packs, p)
877878
pack_count++;
878879
progress = start_delayed_progress(the_repository,
879880
"Verifying reverse pack-indexes", pack_count);
880881
pack_count = 0;
881882
}
882883

883-
for (struct packed_git *p = packfile_store_get_all_packs(packs); p; p = p->next) {
884+
packfile_store_for_each_pack(packs, p) {
884885
int load_error = load_pack_revindex_from_disk(p);
885886

886887
if (load_error < 0) {
@@ -1012,8 +1013,7 @@ int cmd_fsck(int argc,
10121013
struct progress *progress = NULL;
10131014

10141015
if (show_progress) {
1015-
for (p = packfile_store_get_all_packs(packs); p;
1016-
p = p->next) {
1016+
packfile_store_for_each_pack(packs, p) {
10171017
if (open_pack_index(p))
10181018
continue;
10191019
total += p->num_objects;
@@ -1022,8 +1022,8 @@ int cmd_fsck(int argc,
10221022
progress = start_progress(the_repository,
10231023
_("Checking objects"), total);
10241024
}
1025-
for (p = packfile_store_get_all_packs(packs); p;
1026-
p = p->next) {
1025+
1026+
packfile_store_for_each_pack(packs, p) {
10271027
/* verify gives error messages itself */
10281028
if (verify_pack(the_repository,
10291029
p, fsck_obj_buffer,

builtin/gc.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,7 @@ static struct packed_git *find_base_packs(struct string_list *packs,
490490
struct packfile_store *packfiles = the_repository->objects->packfiles;
491491
struct packed_git *p, *base = NULL;
492492

493-
for (p = packfile_store_get_all_packs(packfiles); p; p = p->next) {
493+
packfile_store_for_each_pack(packfiles, p) {
494494
if (!p->pack_local || p->is_cruft)
495495
continue;
496496
if (limit) {
@@ -511,12 +511,12 @@ static int too_many_packs(struct gc_config *cfg)
511511
{
512512
struct packfile_store *packs = the_repository->objects->packfiles;
513513
struct packed_git *p;
514-
int cnt;
514+
int cnt = 0;
515515

516516
if (cfg->gc_auto_pack_limit <= 0)
517517
return 0;
518518

519-
for (cnt = 0, p = packfile_store_get_all_packs(packs); p; p = p->next) {
519+
packfile_store_for_each_pack(packs, p) {
520520
if (!p->pack_local)
521521
continue;
522522
if (p->pack_keep)
@@ -1425,9 +1425,9 @@ static int incremental_repack_auto_condition(struct gc_config *cfg UNUSED)
14251425
if (incremental_repack_auto_limit < 0)
14261426
return 1;
14271427

1428-
for (p = packfile_store_get_all_packs(the_repository->objects->packfiles);
1429-
count < incremental_repack_auto_limit && p;
1430-
p = p->next) {
1428+
packfile_store_for_each_pack(the_repository->objects->packfiles, p) {
1429+
if (count >= incremental_repack_auto_limit)
1430+
break;
14311431
if (!p->multi_pack_index)
14321432
count++;
14331433
}
@@ -1494,7 +1494,7 @@ static off_t get_auto_pack_size(void)
14941494
struct repository *r = the_repository;
14951495

14961496
odb_reprepare(r->objects);
1497-
for (p = packfile_store_get_all_packs(r->objects->packfiles); p; p = p->next) {
1497+
packfile_store_for_each_pack(r->objects->packfiles, p) {
14981498
if (p->pack_size > max_size) {
14991499
second_largest_size = max_size;
15001500
max_size = p->pack_size;

builtin/pack-objects.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3856,7 +3856,7 @@ static void read_packs_list_from_stdin(struct rev_info *revs)
38563856
string_list_sort(&exclude_packs);
38573857
string_list_remove_duplicates(&exclude_packs, 0);
38583858

3859-
for (p = packfile_store_get_all_packs(packs); p; p = p->next) {
3859+
packfile_store_for_each_pack(packs, p) {
38603860
const char *pack_name = pack_basename(p);
38613861

38623862
if ((item = string_list_lookup(&include_packs, pack_name)))
@@ -4107,7 +4107,7 @@ static void enumerate_and_traverse_cruft_objects(struct string_list *fresh_packs
41074107
* Re-mark only the fresh packs as kept so that objects in
41084108
* unknown packs do not halt the reachability traversal early.
41094109
*/
4110-
for (p = packfile_store_get_all_packs(packs); p; p = p->next)
4110+
packfile_store_for_each_pack(packs, p)
41114111
p->pack_keep_in_core = 0;
41124112
mark_pack_kept_in_core(fresh_packs, 1);
41134113

@@ -4145,7 +4145,7 @@ static void read_cruft_objects(void)
41454145
string_list_sort(&discard_packs);
41464146
string_list_sort(&fresh_packs);
41474147

4148-
for (p = packfile_store_get_all_packs(packs); p; p = p->next) {
4148+
packfile_store_for_each_pack(packs, p) {
41494149
const char *pack_name = pack_basename(p);
41504150
struct string_list_item *item;
41514151

@@ -4446,7 +4446,7 @@ static void loosen_unused_packed_objects(void)
44464446
uint32_t loosened_objects_nr = 0;
44474447
struct object_id oid;
44484448

4449-
for (p = packfile_store_get_all_packs(packs); p; p = p->next) {
4449+
packfile_store_for_each_pack(packs, p) {
44504450
if (!p->pack_local || p->pack_keep || p->pack_keep_in_core)
44514451
continue;
44524452

@@ -4753,7 +4753,7 @@ static void add_extra_kept_packs(const struct string_list *names)
47534753
if (!names->nr)
47544754
return;
47554755

4756-
for (p = packfile_store_get_all_packs(packs); p; p = p->next) {
4756+
packfile_store_for_each_pack(packs, p) {
47574757
const char *name = basename(p->pack_name);
47584758
int i;
47594759

@@ -5194,7 +5194,7 @@ int cmd_pack_objects(int argc,
51945194
struct packfile_store *packs = the_repository->objects->packfiles;
51955195
struct packed_git *p;
51965196

5197-
for (p = packfile_store_get_all_packs(packs); p; p = p->next)
5197+
packfile_store_for_each_pack(packs, p)
51985198
if (p->pack_local && p->pack_keep)
51995199
break;
52005200
if (!p) /* no keep-able packs found */
@@ -5209,7 +5209,7 @@ int cmd_pack_objects(int argc,
52095209
struct packfile_store *packs = the_repository->objects->packfiles;
52105210
struct packed_git *p;
52115211

5212-
for (p = packfile_store_get_all_packs(packs); p; p = p->next) {
5212+
packfile_store_for_each_pack(packs, p) {
52135213
if (!p->pack_local) {
52145214
have_non_local_packs = 1;
52155215
break;

builtin/pack-redundant.c

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -567,28 +567,24 @@ static struct pack_list * add_pack(struct packed_git *p)
567567
static struct pack_list * add_pack_file(const char *filename)
568568
{
569569
struct packfile_store *packs = the_repository->objects->packfiles;
570-
struct packed_git *p = packfile_store_get_all_packs(packs);
570+
struct packed_git *p;
571571

572572
if (strlen(filename) < 40)
573573
die("Bad pack filename: %s", filename);
574574

575-
while (p) {
575+
packfile_store_for_each_pack(packs, p)
576576
if (strstr(p->pack_name, filename))
577577
return add_pack(p);
578-
p = p->next;
579-
}
580578
die("Filename %s not found in packed_git", filename);
581579
}
582580

583581
static void load_all(void)
584582
{
585583
struct packfile_store *packs = the_repository->objects->packfiles;
586-
struct packed_git *p = packfile_store_get_all_packs(packs);
584+
struct packed_git *p;
587585

588-
while (p) {
586+
packfile_store_for_each_pack(packs, p)
589587
add_pack(p);
590-
p = p->next;
591-
}
592588
}
593589

594590
int cmd_pack_redundant(int argc, const char **argv, const char *prefix UNUSED, struct repository *repo UNUSED) {

connected.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ int check_connected(oid_iterate_fn fn, void *cb_data,
7777
struct packfile_store *packs = the_repository->objects->packfiles;
7878
struct packed_git *p;
7979

80-
for (p = packfile_store_get_all_packs(packs); p; p = p->next) {
80+
packfile_store_for_each_pack(packs, p) {
8181
if (!p->pack_promisor)
8282
continue;
8383
if (find_pack_entry_one(oid, p))

http-backend.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -609,13 +609,13 @@ static void get_info_packs(struct strbuf *hdr, char *arg UNUSED)
609609
size_t cnt = 0;
610610

611611
select_getanyfile(hdr);
612-
for (p = packfile_store_get_all_packs(packs); p; p = p->next) {
612+
packfile_store_for_each_pack(packs, p) {
613613
if (p->pack_local)
614614
cnt++;
615615
}
616616

617617
strbuf_grow(&buf, cnt * 53 + 2);
618-
for (p = packfile_store_get_all_packs(packs); p; p = p->next) {
618+
packfile_store_for_each_pack(packs, p) {
619619
if (p->pack_local)
620620
strbuf_addf(&buf, "P %s\n", p->pack_name + objdirlen + 6);
621621
}

http.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2425,7 +2425,7 @@ static int fetch_and_setup_pack_index(struct packed_git **packs_head,
24252425
* If we already have the pack locally, no need to fetch its index or
24262426
* even add it to list; we already have all of its objects.
24272427
*/
2428-
for (p = packfile_store_get_all_packs(packs); p; p = p->next) {
2428+
packfile_store_for_each_pack(packs, p) {
24292429
if (hasheq(p->hash, sha1, the_repository->hash_algo))
24302430
return 0;
24312431
}

object-name.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -213,9 +213,11 @@ static void find_short_packed_object(struct disambiguate_state *ds)
213213
unique_in_midx(m, ds);
214214
}
215215

216-
for (p = packfile_store_get_all_packs(ds->repo->objects->packfiles); p && !ds->ambiguous;
217-
p = p->next)
216+
packfile_store_for_each_pack(ds->repo->objects->packfiles, p) {
217+
if (ds->ambiguous)
218+
break;
218219
unique_in_pack(p, ds);
220+
}
219221
}
220222

221223
static int finish_object_disambiguation(struct disambiguate_state *ds,
@@ -805,7 +807,7 @@ static void find_abbrev_len_packed(struct min_abbrev_data *mad)
805807
find_abbrev_len_for_midx(m, mad);
806808
}
807809

808-
for (p = packfile_store_get_all_packs(mad->repo->objects->packfiles); p; p = p->next)
810+
packfile_store_for_each_pack(mad->repo->objects->packfiles, p)
809811
find_abbrev_len_for_pack(p, mad);
810812
}
811813

0 commit comments

Comments
 (0)