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 <raa.lkml@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Alex Riesen committed
Jul 3, 2010 at 14:41 UTC
8a57c6e9437eeebf849f0def03389078a510312e
4 files changed
+64
-103
builtin/fetch.c
+13
-29
@@ -544,40 +544,14 @@ static int will_fetch(struct ref **head, const unsigned char *sha1)
544
return 0;
545
}
546
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
-
547
static void find_non_local_tags(struct transport *transport,
548
struct ref **head,
549
struct ref ***tail)
550
{
551
struct string_list existing_refs = { NULL, 0, 0, 0 };
552
struct string_list remote_refs = { NULL, 0, 0, 0 };
577
- struct tag_data data;
553
const struct ref *ref;
554
struct string_list_item *item = NULL;
580
- data.head = head; data.tail = tail;
555
556
for_each_ref(add_existing, &existing_refs);
557
for (ref = transport_get_remote_refs(transport); ref; ref = ref->next) {
@@ -631,10 +605,20 @@ static void find_non_local_tags(struct transport *transport,
605
item->util = NULL;
606
607
/*
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
610
*/
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
+ }
622
623
string_list_clear(&remote_refs, 0);
624
}
builtin/ls-files.c
+22
-23
@@ -164,33 +164,32 @@ static void show_ce_entry(const char *tag, struct cache_entry *ce)
164
write_name(ce->name, ce_namelen(ce));
165
}
166
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
-
167
static void show_ru_info(void)
168
{
169
+ struct string_list_item *item;
170
+
171
if (!the_index.resolve_undo)
172
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
+ }
193
}
194
195
static void show_files(struct dir_struct *dir)
notes.c
+14
-32
@@ -877,14 +877,6 @@ void string_list_add_refs_from_colon_sep(struct string_list *list,
877
strbuf_release(&globbuf);
878
}
879
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
-
880
static int notes_display_config(const char *k, const char *v, void *cb)
881
{
882
int *load_refs = cb;
@@ -947,30 +939,18 @@ void init_notes(struct notes_tree *t, const char *notes_ref,
939
load_subtree(t, &root_tree, t->root, 0);
940
}
941
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
-
942
struct notes_tree **load_notes_trees(struct string_list *refs)
943
{
944
+ struct string_list_item *item;
945
+ int counter = 0;
946
struct notes_tree **trees;
968
- struct load_notes_cb_data cb_data;
947
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;
954
return trees;
955
}
956
@@ -995,10 +975,12 @@ void init_display_notes(struct display_notes_opt *opt)
975
976
git_config(notes_display_config, &load_config_refs);
977
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
+ }
984
985
display_notes_trees = load_notes_trees(&display_notes_refs);
986
string_list_clear(&display_notes_refs, 0);
resolve-undo.c
+15
-19
@@ -28,29 +28,25 @@ void record_resolve_undo(struct index_state *istate, struct cache_entry *ce)
28
ui->mode[stage - 1] = ce->ce_mode;
29
}
30
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)
32
{
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;
37
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)
39
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
+ }
49
}
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);
50
}
51
52
struct string_list *resolve_undo_read(const char *data, unsigned long size)