Skip to content

Commit 8a57c6e

Browse files
raalkmlgitster
authored andcommitted
Convert the users of for_each_string_list to for_each_string_list_item macro
The rule for selecting the candidates for conversion is: if the callback function returns only 0 (the condition for for_each_string_list to exit early), than it can be safely converted to the macro. A notable exception are the callers in builtin/remote.c. If converted, the readability in the file will suffer greately. Besides, the code is not very performance critical (at the moment, at least): it does output formatting of the list of remotes. Signed-off-by: Alex Riesen <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 8d31635 commit 8a57c6e

File tree

4 files changed

+64
-103
lines changed

4 files changed

+64
-103
lines changed

builtin/fetch.c

Lines changed: 13 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -544,40 +544,14 @@ static int will_fetch(struct ref **head, const unsigned char *sha1)
544544
return 0;
545545
}
546546

547-
struct tag_data {
548-
struct ref **head;
549-
struct ref ***tail;
550-
};
551-
552-
static int add_to_tail(struct string_list_item *item, void *cb_data)
553-
{
554-
struct tag_data *data = (struct tag_data *)cb_data;
555-
struct ref *rm = NULL;
556-
557-
/* We have already decided to ignore this item */
558-
if (!item->util)
559-
return 0;
560-
561-
rm = alloc_ref(item->string);
562-
rm->peer_ref = alloc_ref(item->string);
563-
hashcpy(rm->old_sha1, item->util);
564-
565-
**data->tail = rm;
566-
*data->tail = &rm->next;
567-
568-
return 0;
569-
}
570-
571547
static void find_non_local_tags(struct transport *transport,
572548
struct ref **head,
573549
struct ref ***tail)
574550
{
575551
struct string_list existing_refs = { NULL, 0, 0, 0 };
576552
struct string_list remote_refs = { NULL, 0, 0, 0 };
577-
struct tag_data data;
578553
const struct ref *ref;
579554
struct string_list_item *item = NULL;
580-
data.head = head; data.tail = tail;
581555

582556
for_each_ref(add_existing, &existing_refs);
583557
for (ref = transport_get_remote_refs(transport); ref; ref = ref->next) {
@@ -631,10 +605,20 @@ static void find_non_local_tags(struct transport *transport,
631605
item->util = NULL;
632606

633607
/*
634-
* For all the tags in the remote_refs string list, call
635-
* add_to_tail to add them to the list of refs to be fetched
608+
* For all the tags in the remote_refs string list,
609+
* add them to the list of refs to be fetched
636610
*/
637-
for_each_string_list(&remote_refs, add_to_tail, &data);
611+
for_each_string_list_item(item, &remote_refs) {
612+
/* Unless we have already decided to ignore this item... */
613+
if (item->util)
614+
{
615+
struct ref *rm = alloc_ref(item->string);
616+
rm->peer_ref = alloc_ref(item->string);
617+
hashcpy(rm->old_sha1, item->util);
618+
**tail = rm;
619+
*tail = &rm->next;
620+
}
621+
}
638622

639623
string_list_clear(&remote_refs, 0);
640624
}

builtin/ls-files.c

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -164,33 +164,32 @@ static void show_ce_entry(const char *tag, struct cache_entry *ce)
164164
write_name(ce->name, ce_namelen(ce));
165165
}
166166

167-
static int show_one_ru(struct string_list_item *item, void *cbdata)
168-
{
169-
const char *path = item->string;
170-
struct resolve_undo_info *ui = item->util;
171-
int i, len;
172-
173-
len = strlen(path);
174-
if (len < max_prefix_len)
175-
return 0; /* outside of the prefix */
176-
if (!match_pathspec(pathspec, path, len, max_prefix_len, ps_matched))
177-
return 0; /* uninterested */
178-
for (i = 0; i < 3; i++) {
179-
if (!ui->mode[i])
180-
continue;
181-
printf("%s%06o %s %d\t", tag_resolve_undo, ui->mode[i],
182-
find_unique_abbrev(ui->sha1[i], abbrev),
183-
i + 1);
184-
write_name(path, len);
185-
}
186-
return 0;
187-
}
188-
189167
static void show_ru_info(void)
190168
{
169+
struct string_list_item *item;
170+
191171
if (!the_index.resolve_undo)
192172
return;
193-
for_each_string_list(the_index.resolve_undo, show_one_ru, NULL);
173+
174+
for_each_string_list_item(item, the_index.resolve_undo) {
175+
const char *path = item->string;
176+
struct resolve_undo_info *ui = item->util;
177+
int i, len;
178+
179+
len = strlen(path);
180+
if (len < max_prefix_len)
181+
continue; /* outside of the prefix */
182+
if (!match_pathspec(pathspec, path, len, max_prefix_len, ps_matched))
183+
continue; /* uninterested */
184+
for (i = 0; i < 3; i++) {
185+
if (!ui->mode[i])
186+
continue;
187+
printf("%s%06o %s %d\t", tag_resolve_undo, ui->mode[i],
188+
find_unique_abbrev(ui->sha1[i], abbrev),
189+
i + 1);
190+
write_name(path, len);
191+
}
192+
}
194193
}
195194

196195
static void show_files(struct dir_struct *dir)

notes.c

Lines changed: 14 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -877,14 +877,6 @@ void string_list_add_refs_from_colon_sep(struct string_list *list,
877877
strbuf_release(&globbuf);
878878
}
879879

880-
static int string_list_add_refs_from_list(struct string_list_item *item,
881-
void *cb)
882-
{
883-
struct string_list *list = cb;
884-
string_list_add_refs_by_glob(list, item->string);
885-
return 0;
886-
}
887-
888880
static int notes_display_config(const char *k, const char *v, void *cb)
889881
{
890882
int *load_refs = cb;
@@ -947,30 +939,18 @@ void init_notes(struct notes_tree *t, const char *notes_ref,
947939
load_subtree(t, &root_tree, t->root, 0);
948940
}
949941

950-
struct load_notes_cb_data {
951-
int counter;
952-
struct notes_tree **trees;
953-
};
954-
955-
static int load_one_display_note_ref(struct string_list_item *item,
956-
void *cb_data)
957-
{
958-
struct load_notes_cb_data *c = cb_data;
959-
struct notes_tree *t = xcalloc(1, sizeof(struct notes_tree));
960-
init_notes(t, item->string, combine_notes_ignore, 0);
961-
c->trees[c->counter++] = t;
962-
return 0;
963-
}
964-
965942
struct notes_tree **load_notes_trees(struct string_list *refs)
966943
{
944+
struct string_list_item *item;
945+
int counter = 0;
967946
struct notes_tree **trees;
968-
struct load_notes_cb_data cb_data;
969947
trees = xmalloc((refs->nr+1) * sizeof(struct notes_tree *));
970-
cb_data.counter = 0;
971-
cb_data.trees = trees;
972-
for_each_string_list(refs, load_one_display_note_ref, &cb_data);
973-
trees[cb_data.counter] = NULL;
948+
for_each_string_list_item(item, refs) {
949+
struct notes_tree *t = xcalloc(1, sizeof(struct notes_tree));
950+
init_notes(t, item->string, combine_notes_ignore, 0);
951+
trees[counter++] = t;
952+
}
953+
trees[counter] = NULL;
974954
return trees;
975955
}
976956

@@ -995,10 +975,12 @@ void init_display_notes(struct display_notes_opt *opt)
995975

996976
git_config(notes_display_config, &load_config_refs);
997977

998-
if (opt && opt->extra_notes_refs)
999-
for_each_string_list(opt->extra_notes_refs,
1000-
string_list_add_refs_from_list,
1001-
&display_notes_refs);
978+
if (opt && opt->extra_notes_refs) {
979+
struct string_list_item *item;
980+
for_each_string_list_item(item, opt->extra_notes_refs)
981+
string_list_add_refs_by_glob(&display_notes_refs,
982+
item->string);
983+
}
1002984

1003985
display_notes_trees = load_notes_trees(&display_notes_refs);
1004986
string_list_clear(&display_notes_refs, 0);

resolve-undo.c

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -28,29 +28,25 @@ void record_resolve_undo(struct index_state *istate, struct cache_entry *ce)
2828
ui->mode[stage - 1] = ce->ce_mode;
2929
}
3030

31-
static int write_one(struct string_list_item *item, void *cbdata)
31+
void resolve_undo_write(struct strbuf *sb, struct string_list *resolve_undo)
3232
{
33-
struct strbuf *sb = cbdata;
34-
struct resolve_undo_info *ui = item->util;
35-
int i;
33+
struct string_list_item *item;
34+
for_each_string_list_item(item, resolve_undo) {
35+
struct resolve_undo_info *ui = item->util;
36+
int i;
3637

37-
if (!ui)
38-
return 0;
39-
strbuf_addstr(sb, item->string);
40-
strbuf_addch(sb, 0);
41-
for (i = 0; i < 3; i++)
42-
strbuf_addf(sb, "%o%c", ui->mode[i], 0);
43-
for (i = 0; i < 3; i++) {
44-
if (!ui->mode[i])
38+
if (!ui)
4539
continue;
46-
strbuf_add(sb, ui->sha1[i], 20);
40+
strbuf_addstr(sb, item->string);
41+
strbuf_addch(sb, 0);
42+
for (i = 0; i < 3; i++)
43+
strbuf_addf(sb, "%o%c", ui->mode[i], 0);
44+
for (i = 0; i < 3; i++) {
45+
if (!ui->mode[i])
46+
continue;
47+
strbuf_add(sb, ui->sha1[i], 20);
48+
}
4749
}
48-
return 0;
49-
}
50-
51-
void resolve_undo_write(struct strbuf *sb, struct string_list *resolve_undo)
52-
{
53-
for_each_string_list(resolve_undo, write_one, sb);
5450
}
5551

5652
struct string_list *resolve_undo_read(const char *data, unsigned long size)

0 commit comments

Comments
 (0)