environment: do not use strbuf_split*()

environment.c:get_git_namespace() learns the raw namespace from an environment variable, splits it at "/", and appends them after "refs/namespaces/"; the reason why it splits first is so that an empty string resulting from double slashes can be omitted. The split pieces do not need to be edited in any way, so an array of strbufs is a wrong data structure to use. Instead split into a string list and use the pieces from there. Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Jul 31, 2025 at 15:54 UTC b894d4481f4068a84323dfc7048f007b3df5234d
1 file changed +12 -7
environment.c
+12 -7
@@ -163,10 +163,10 @@ int have_git_dir(void)
163 const char *get_git_namespace(void)
164 {
165 static const char *namespace;
166 -
166 struct strbuf buf = STRBUF_INIT;
168 - struct strbuf **components, **c;
167 const char *raw_namespace;
168 + struct string_list components = STRING_LIST_INIT_DUP;
169 + struct string_list_item *item;
170
171 if (namespace)
172 return namespace;
@@ -178,12 +178,17 @@ const char *get_git_namespace(void)
178 }
179
180 strbuf_addstr(&buf, raw_namespace);
181 - components = strbuf_split(&buf, '/');
181 +
182 + string_list_split(&components, buf.buf, "/", -1);
183 strbuf_reset(&buf);
183 - for (c = components; *c; c++)
184 - if (strcmp((*c)->buf, "/") != 0)
185 - strbuf_addf(&buf, "refs/namespaces/%s", (*c)->buf);
186 - strbuf_list_free(components);
184 +
185 + for_each_string_list_item(item, &components) {
186 + if (item->string[0])
187 + strbuf_addf(&buf, "refs/namespaces/%s/", item->string);
188 + }
189 + string_list_clear(&components, 0);
190 +
191 + strbuf_trim_trailing_dir_sep(&buf);
192 if (check_refname_format(buf.buf, 0))
193 die(_("bad git namespace path \"%s\""), raw_namespace);
194 strbuf_addch(&buf, '/');