Skip to content

Commit add3564

Browse files
10ne1gitster
authored andcommitted
hook: move unsorted_string_list_remove() to string-list.[ch]
Move the convenience wrapper from hook to string-list since it's a more suitable place. Add a doc comment to the header. Also add a free_util arg to make the function more generic and make the API similar to other functions in string-list.h. Update the existing call-sites. Suggested-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Adrian Ratiu <adrian.ratiu@collabora.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 8367733 commit add3564

3 files changed

Lines changed: 20 additions & 11 deletions

File tree

hook.c

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -110,14 +110,6 @@ static void list_hooks_add_default(struct repository *r, const char *hookname,
110110
string_list_append(hook_list, hook_path)->util = h;
111111
}
112112

113-
static void unsorted_string_list_remove(struct string_list *list,
114-
const char *str)
115-
{
116-
struct string_list_item *item = unsorted_string_list_lookup(list, str);
117-
if (item)
118-
unsorted_string_list_delete_item(list, item - list->items, 0);
119-
}
120-
121113
/*
122114
* Callback struct to collect all hook.* keys in a single config pass.
123115
* commands: friendly-name to command map.
@@ -156,7 +148,7 @@ static int hook_config_lookup_all(const char *key, const char *value,
156148
struct strmap_entry *e;
157149

158150
strmap_for_each_entry(&data->event_hooks, &iter, e)
159-
unsorted_string_list_remove(e->value, hook_name);
151+
unsorted_string_list_remove(e->value, hook_name, 0);
160152
} else {
161153
struct string_list *hooks =
162154
strmap_get(&data->event_hooks, value);
@@ -168,7 +160,7 @@ static int hook_config_lookup_all(const char *key, const char *value,
168160
}
169161

170162
/* Re-insert if necessary to preserve last-seen order. */
171-
unsorted_string_list_remove(hooks, hook_name);
163+
unsorted_string_list_remove(hooks, hook_name, 0);
172164
string_list_append(hooks, hook_name);
173165
}
174166
} else if (!strcmp(subkey, "command")) {
@@ -186,7 +178,7 @@ static int hook_config_lookup_all(const char *key, const char *value,
186178
break;
187179
case 1: /* enabled: undo a prior disabled entry */
188180
unsorted_string_list_remove(&data->disabled_hooks,
189-
hook_name);
181+
hook_name, 0);
190182
break;
191183
default:
192184
break; /* ignore unrecognised values */

string-list.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,15 @@ void unsorted_string_list_delete_item(struct string_list *list, int i, int free_
281281
list->nr--;
282282
}
283283

284+
void unsorted_string_list_remove(struct string_list *list, const char *str,
285+
int free_util)
286+
{
287+
struct string_list_item *item = unsorted_string_list_lookup(list, str);
288+
if (item)
289+
unsorted_string_list_delete_item(list, item - list->items,
290+
free_util);
291+
}
292+
284293
/*
285294
* append a substring [p..end] to list; return number of things it
286295
* appended to the list.

string-list.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,14 @@ struct string_list_item *unsorted_string_list_lookup(struct string_list *list,
265265
*/
266266
void unsorted_string_list_delete_item(struct string_list *list, int i, int free_util);
267267

268+
/**
269+
* Remove the first item matching `str` from an unsorted string_list.
270+
* No-op if `str` is not found. If `free_util` is non-zero, the `util`
271+
* pointer of the removed item is freed before deletion.
272+
*/
273+
void unsorted_string_list_remove(struct string_list *list, const char *str,
274+
int free_util);
275+
268276
/**
269277
* Split string into substrings on characters in `delim` and append the
270278
* substrings to `list`. The input string is not modified.

0 commit comments

Comments
 (0)