]> git.ipfire.org Git - thirdparty/git.git/commitdiff
hook: move unsorted_string_list_remove() to string-list.[ch]
authorAdrian Ratiu <adrian.ratiu@collabora.com>
Wed, 25 Mar 2026 19:54:52 +0000 (21:54 +0200)
committerJunio C Hamano <gitster@pobox.com>
Wed, 25 Mar 2026 21:00:45 +0000 (14:00 -0700)
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>
hook.c
string-list.c
string-list.h

diff --git a/hook.c b/hook.c
index 2c8252b2c4fceb01b3a5e71b9be74900470a2082..67cc9a66df14305e16a72fe854379045647aaba5 100644 (file)
--- a/hook.c
+++ b/hook.c
@@ -110,14 +110,6 @@ static void list_hooks_add_default(struct repository *r, const char *hookname,
        string_list_append(hook_list, hook_path)->util = h;
 }
 
-static void unsorted_string_list_remove(struct string_list *list,
-                                       const char *str)
-{
-       struct string_list_item *item = unsorted_string_list_lookup(list, str);
-       if (item)
-               unsorted_string_list_delete_item(list, item - list->items, 0);
-}
-
 /*
  * Callback struct to collect all hook.* keys in a single config pass.
  * commands: friendly-name to command map.
@@ -156,7 +148,7 @@ static int hook_config_lookup_all(const char *key, const char *value,
                        struct strmap_entry *e;
 
                        strmap_for_each_entry(&data->event_hooks, &iter, e)
-                               unsorted_string_list_remove(e->value, hook_name);
+                               unsorted_string_list_remove(e->value, hook_name, 0);
                } else {
                        struct string_list *hooks =
                                strmap_get(&data->event_hooks, value);
@@ -168,7 +160,7 @@ static int hook_config_lookup_all(const char *key, const char *value,
                        }
 
                        /* Re-insert if necessary to preserve last-seen order. */
-                       unsorted_string_list_remove(hooks, hook_name);
+                       unsorted_string_list_remove(hooks, hook_name, 0);
                        string_list_append(hooks, hook_name);
                }
        } else if (!strcmp(subkey, "command")) {
@@ -186,7 +178,7 @@ static int hook_config_lookup_all(const char *key, const char *value,
                        break;
                case 1: /* enabled: undo a prior disabled entry */
                        unsorted_string_list_remove(&data->disabled_hooks,
-                                                   hook_name);
+                                                   hook_name, 0);
                        break;
                default:
                        break; /* ignore unrecognised values */
index fffa2ad4b60b32e5b2d14c2859dc61ff7566a159..d260b873c80ff91dd84e37c6186de844117b8e19 100644 (file)
@@ -281,6 +281,15 @@ void unsorted_string_list_delete_item(struct string_list *list, int i, int free_
        list->nr--;
 }
 
+void unsorted_string_list_remove(struct string_list *list, const char *str,
+                                int free_util)
+{
+       struct string_list_item *item = unsorted_string_list_lookup(list, str);
+       if (item)
+               unsorted_string_list_delete_item(list, item - list->items,
+                                                free_util);
+}
+
 /*
  * append a substring [p..end] to list; return number of things it
  * appended to the list.
index 3ad862a18718a4a65b67ed7e20686b4c593ac0b3..b86ee7c099fa50a3907ecbfc9e481f2add25a427 100644 (file)
@@ -265,6 +265,14 @@ struct string_list_item *unsorted_string_list_lookup(struct string_list *list,
  */
 void unsorted_string_list_delete_item(struct string_list *list, int i, int free_util);
 
+/**
+ * Remove the first item matching `str` from an unsorted string_list.
+ * No-op if `str` is not found. If `free_util` is non-zero, the `util`
+ * pointer of the removed item is freed before deletion.
+ */
+void unsorted_string_list_remove(struct string_list *list, const char *str,
+                                int free_util);
+
 /**
  * Split string into substrings on characters in `delim` and append the
  * substrings to `list`.  The input string is not modified.