Skip to content

Commit 858d376

Browse files
committed
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 <[email protected]>
1 parent ec9c038 commit 858d376

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

environment.c

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -163,10 +163,10 @@ int have_git_dir(void)
163163
const char *get_git_namespace(void)
164164
{
165165
static const char *namespace;
166-
167166
struct strbuf buf = STRBUF_INIT;
168-
struct strbuf **components, **c;
169167
const char *raw_namespace;
168+
struct string_list components = STRING_LIST_INIT_DUP;
169+
struct string_list_item *item;
170170

171171
if (namespace)
172172
return namespace;
@@ -178,12 +178,17 @@ const char *get_git_namespace(void)
178178
}
179179

180180
strbuf_addstr(&buf, raw_namespace);
181-
components = strbuf_split(&buf, '/');
181+
182+
string_list_split(&components, buf.buf, "/", -1);
182183
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);
187192
if (check_refname_format(buf.buf, 0))
188193
die(_("bad git namespace path \"%s\""), raw_namespace);
189194
strbuf_addch(&buf, '/');

0 commit comments

Comments
 (0)