Merge branch 'ar/string-list-foreach'

* ar/string-list-foreach:
  Convert the users of for_each_string_list to for_each_string_list_item macro
  Add a for_each_string_list_item macro
This commit is contained in:
Junio C Hamano 2010-08-18 12:14:38 -07:00
commit c7e375de42
5 changed files with 69 additions and 106 deletions

View File

@ -544,40 +544,14 @@ static int will_fetch(struct ref **head, const unsigned char *sha1)
return 0;
}
struct tag_data {
struct ref **head;
struct ref ***tail;
};
static int add_to_tail(struct string_list_item *item, void *cb_data)
{
struct tag_data *data = (struct tag_data *)cb_data;
struct ref *rm = NULL;
/* We have already decided to ignore this item */
if (!item->util)
return 0;
rm = alloc_ref(item->string);
rm->peer_ref = alloc_ref(item->string);
hashcpy(rm->old_sha1, item->util);
**data->tail = rm;
*data->tail = &rm->next;
return 0;
}
static void find_non_local_tags(struct transport *transport,
struct ref **head,
struct ref ***tail)
{
struct string_list existing_refs = { NULL, 0, 0, 0 };
struct string_list remote_refs = { NULL, 0, 0, 0 };
struct tag_data data;
const struct ref *ref;
struct string_list_item *item = NULL;
data.head = head; data.tail = tail;
for_each_ref(add_existing, &existing_refs);
for (ref = transport_get_remote_refs(transport); ref; ref = ref->next) {
@ -631,10 +605,20 @@ static void find_non_local_tags(struct transport *transport,
item->util = NULL;
/*
* For all the tags in the remote_refs string list, call
* add_to_tail to add them to the list of refs to be fetched
* For all the tags in the remote_refs string list,
* add them to the list of refs to be fetched
*/
for_each_string_list(&remote_refs, add_to_tail, &data);
for_each_string_list_item(item, &remote_refs) {
/* Unless we have already decided to ignore this item... */
if (item->util)
{
struct ref *rm = alloc_ref(item->string);
rm->peer_ref = alloc_ref(item->string);
hashcpy(rm->old_sha1, item->util);
**tail = rm;
*tail = &rm->next;
}
}
string_list_clear(&remote_refs, 0);
}

View File

@ -172,33 +172,32 @@ static void show_ce_entry(const char *tag, struct cache_entry *ce)
}
}
static int show_one_ru(struct string_list_item *item, void *cbdata)
{
const char *path = item->string;
struct resolve_undo_info *ui = item->util;
int i, len;
len = strlen(path);
if (len < max_prefix_len)
return 0; /* outside of the prefix */
if (!match_pathspec(pathspec, path, len, max_prefix_len, ps_matched))
return 0; /* uninterested */
for (i = 0; i < 3; i++) {
if (!ui->mode[i])
continue;
printf("%s%06o %s %d\t", tag_resolve_undo, ui->mode[i],
find_unique_abbrev(ui->sha1[i], abbrev),
i + 1);
write_name(path, len);
}
return 0;
}
static void show_ru_info(void)
{
struct string_list_item *item;
if (!the_index.resolve_undo)
return;
for_each_string_list(the_index.resolve_undo, show_one_ru, NULL);
for_each_string_list_item(item, the_index.resolve_undo) {
const char *path = item->string;
struct resolve_undo_info *ui = item->util;
int i, len;
len = strlen(path);
if (len < max_prefix_len)
continue; /* outside of the prefix */
if (!match_pathspec(pathspec, path, len, max_prefix_len, ps_matched))
continue; /* uninterested */
for (i = 0; i < 3; i++) {
if (!ui->mode[i])
continue;
printf("%s%06o %s %d\t", tag_resolve_undo, ui->mode[i],
find_unique_abbrev(ui->sha1[i], abbrev),
i + 1);
write_name(path, len);
}
}
}
static void show_files(struct dir_struct *dir)

46
notes.c
View File

@ -877,14 +877,6 @@ void string_list_add_refs_from_colon_sep(struct string_list *list,
strbuf_release(&globbuf);
}
static int string_list_add_refs_from_list(struct string_list_item *item,
void *cb)
{
struct string_list *list = cb;
string_list_add_refs_by_glob(list, item->string);
return 0;
}
static int notes_display_config(const char *k, const char *v, void *cb)
{
int *load_refs = cb;
@ -947,30 +939,18 @@ void init_notes(struct notes_tree *t, const char *notes_ref,
load_subtree(t, &root_tree, t->root, 0);
}
struct load_notes_cb_data {
int counter;
struct notes_tree **trees;
};
static int load_one_display_note_ref(struct string_list_item *item,
void *cb_data)
{
struct load_notes_cb_data *c = cb_data;
struct notes_tree *t = xcalloc(1, sizeof(struct notes_tree));
init_notes(t, item->string, combine_notes_ignore, 0);
c->trees[c->counter++] = t;
return 0;
}
struct notes_tree **load_notes_trees(struct string_list *refs)
{
struct string_list_item *item;
int counter = 0;
struct notes_tree **trees;
struct load_notes_cb_data cb_data;
trees = xmalloc((refs->nr+1) * sizeof(struct notes_tree *));
cb_data.counter = 0;
cb_data.trees = trees;
for_each_string_list(refs, load_one_display_note_ref, &cb_data);
trees[cb_data.counter] = NULL;
for_each_string_list_item(item, refs) {
struct notes_tree *t = xcalloc(1, sizeof(struct notes_tree));
init_notes(t, item->string, combine_notes_ignore, 0);
trees[counter++] = t;
}
trees[counter] = NULL;
return trees;
}
@ -995,10 +975,12 @@ void init_display_notes(struct display_notes_opt *opt)
git_config(notes_display_config, &load_config_refs);
if (opt && opt->extra_notes_refs)
for_each_string_list(opt->extra_notes_refs,
string_list_add_refs_from_list,
&display_notes_refs);
if (opt && opt->extra_notes_refs) {
struct string_list_item *item;
for_each_string_list_item(item, opt->extra_notes_refs)
string_list_add_refs_by_glob(&display_notes_refs,
item->string);
}
display_notes_trees = load_notes_trees(&display_notes_refs);
string_list_clear(&display_notes_refs, 0);

View File

@ -28,29 +28,25 @@ void record_resolve_undo(struct index_state *istate, struct cache_entry *ce)
ui->mode[stage - 1] = ce->ce_mode;
}
static int write_one(struct string_list_item *item, void *cbdata)
{
struct strbuf *sb = cbdata;
struct resolve_undo_info *ui = item->util;
int i;
if (!ui)
return 0;
strbuf_addstr(sb, item->string);
strbuf_addch(sb, 0);
for (i = 0; i < 3; i++)
strbuf_addf(sb, "%o%c", ui->mode[i], 0);
for (i = 0; i < 3; i++) {
if (!ui->mode[i])
continue;
strbuf_add(sb, ui->sha1[i], 20);
}
return 0;
}
void resolve_undo_write(struct strbuf *sb, struct string_list *resolve_undo)
{
for_each_string_list(resolve_undo, write_one, sb);
struct string_list_item *item;
for_each_string_list_item(item, resolve_undo) {
struct resolve_undo_info *ui = item->util;
int i;
if (!ui)
continue;
strbuf_addstr(sb, item->string);
strbuf_addch(sb, 0);
for (i = 0; i < 3; i++)
strbuf_addf(sb, "%o%c", ui->mode[i], 0);
for (i = 0; i < 3; i++) {
if (!ui->mode[i])
continue;
strbuf_add(sb, ui->sha1[i], 20);
}
}
}
struct string_list *resolve_undo_read(const char *data, unsigned long size)

View File

@ -20,10 +20,12 @@ void string_list_clear(struct string_list *list, int free_util);
typedef void (*string_list_clear_func_t)(void *p, const char *str);
void string_list_clear_func(struct string_list *list, string_list_clear_func_t clearfunc);
/* Use this function to iterate over each item */
/* Use this function or the macro below to iterate over each item */
typedef int (*string_list_each_func_t)(struct string_list_item *, void *);
int for_each_string_list(struct string_list *list,
string_list_each_func_t, void *cb_data);
#define for_each_string_list_item(item,list) \
for (item = (list)->items; item < (list)->items + (list)->nr; ++item)
/* Use these functions only on sorted lists: */
int string_list_has_string(const struct string_list *list, const char *string);