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 <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Jeff King committed
Sep 24, 2015 at 17:07 UTC
6c31c22cebe9f0b117fd93ee7792d88e82aaa61e
1 file changed
+10
-16
builtin/apply.c
+10
-16
@@ -77,8 +77,7 @@ static enum ws_ignore {
77
78
79
static const char *patch_input_file;
80
-static const char *root;
81
-static int root_len;
80
+static struct strbuf root = STRBUF_INIT;
81
static int read_stdin = 1;
82
static int options;
83
@@ -494,8 +493,8 @@ static char *find_name_gnu(const char *line, const char *def, int p_value)
493
}
494
495
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);
498
return squash_slash(strbuf_detach(&name, NULL));
499
}
500
@@ -697,8 +696,8 @@ static char *find_name_common(const char *line, const char *def,
696
return squash_slash(xstrdup(def));
697
}
698
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);
701
return squash_slash(ret);
702
}
703
@@ -1274,8 +1273,8 @@ static int parse_git_header(const char *line, int len, unsigned int size, struct
1273
* the default name from the header.
1274
*/
1275
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);
1278
free(patch->def_name);
1279
patch->def_name = s;
1280
}
@@ -4498,14 +4497,9 @@ static int option_parse_whitespace(const struct option *opt,
4497
static int option_parse_directory(const struct option *opt,
4498
const char *arg, int unset)
4499
{
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, '/');
4503
return 0;
4504
}
4505