Skip to content

Commit 6c31c22

Browse files
peffgitster
authored andcommitted
apply: convert root string to strbuf
We use manual computation and strcpy to allocate the "root" variable. This would be much simpler using xstrfmt. But since we store the length, too, we can just use a strbuf, which handles that for us. Note that we stop distinguishing between "no root" and "empty root" in some cases, but that's OK; the results are the same (e.g., inserting an empty string is a noop). Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 9c28390 commit 6c31c22

File tree

1 file changed

+10
-16
lines changed

1 file changed

+10
-16
lines changed

builtin/apply.c

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,7 @@ static enum ws_ignore {
7777

7878

7979
static const char *patch_input_file;
80-
static const char *root;
81-
static int root_len;
80+
static struct strbuf root = STRBUF_INIT;
8281
static int read_stdin = 1;
8382
static int options;
8483

@@ -494,8 +493,8 @@ static char *find_name_gnu(const char *line, const char *def, int p_value)
494493
}
495494

496495
strbuf_remove(&name, 0, cp - name.buf);
497-
if (root)
498-
strbuf_insert(&name, 0, root, root_len);
496+
if (root.len)
497+
strbuf_insert(&name, 0, root.buf, root.len);
499498
return squash_slash(strbuf_detach(&name, NULL));
500499
}
501500

@@ -697,8 +696,8 @@ static char *find_name_common(const char *line, const char *def,
697696
return squash_slash(xstrdup(def));
698697
}
699698

700-
if (root) {
701-
char *ret = xstrfmt("%s%.*s", root, len, start);
699+
if (root.len) {
700+
char *ret = xstrfmt("%s%.*s", root.buf, len, start);
702701
return squash_slash(ret);
703702
}
704703

@@ -1274,8 +1273,8 @@ static int parse_git_header(const char *line, int len, unsigned int size, struct
12741273
* the default name from the header.
12751274
*/
12761275
patch->def_name = git_header_name(line, len);
1277-
if (patch->def_name && root) {
1278-
char *s = xstrfmt("%s%s", root, patch->def_name);
1276+
if (patch->def_name && root.len) {
1277+
char *s = xstrfmt("%s%s", root.buf, patch->def_name);
12791278
free(patch->def_name);
12801279
patch->def_name = s;
12811280
}
@@ -4498,14 +4497,9 @@ static int option_parse_whitespace(const struct option *opt,
44984497
static int option_parse_directory(const struct option *opt,
44994498
const char *arg, int unset)
45004499
{
4501-
root_len = strlen(arg);
4502-
if (root_len && arg[root_len - 1] != '/') {
4503-
char *new_root;
4504-
root = new_root = xmalloc(root_len + 2);
4505-
strcpy(new_root, arg);
4506-
strcpy(new_root + root_len++, "/");
4507-
} else
4508-
root = arg;
4500+
strbuf_reset(&root);
4501+
strbuf_addstr(&root, arg);
4502+
strbuf_complete(&root, '/');
45094503
return 0;
45104504
}
45114505

0 commit comments

Comments
 (0)