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>
This commit is contained in:
Jeff King 2015-09-24 17:07:38 -04:00 committed by Junio C Hamano
parent 9c28390bda
commit 6c31c22ceb

View file

@ -77,8 +77,7 @@ static enum ws_ignore {
static const char *patch_input_file;
static const char *root;
static int root_len;
static struct strbuf root = STRBUF_INIT;
static int read_stdin = 1;
static int options;
@ -494,8 +493,8 @@ static char *find_name_gnu(const char *line, const char *def, int p_value)
}
strbuf_remove(&name, 0, cp - name.buf);
if (root)
strbuf_insert(&name, 0, root, root_len);
if (root.len)
strbuf_insert(&name, 0, root.buf, root.len);
return squash_slash(strbuf_detach(&name, NULL));
}
@ -697,8 +696,8 @@ static char *find_name_common(const char *line, const char *def,
return squash_slash(xstrdup(def));
}
if (root) {
char *ret = xstrfmt("%s%.*s", root, len, start);
if (root.len) {
char *ret = xstrfmt("%s%.*s", root.buf, len, start);
return squash_slash(ret);
}
@ -1274,8 +1273,8 @@ static int parse_git_header(const char *line, int len, unsigned int size, struct
* the default name from the header.
*/
patch->def_name = git_header_name(line, len);
if (patch->def_name && root) {
char *s = xstrfmt("%s%s", root, patch->def_name);
if (patch->def_name && root.len) {
char *s = xstrfmt("%s%s", root.buf, patch->def_name);
free(patch->def_name);
patch->def_name = s;
}
@ -4498,14 +4497,9 @@ static int option_parse_whitespace(const struct option *opt,
static int option_parse_directory(const struct option *opt,
const char *arg, int unset)
{
root_len = strlen(arg);
if (root_len && arg[root_len - 1] != '/') {
char *new_root;
root = new_root = xmalloc(root_len + 2);
strcpy(new_root, arg);
strcpy(new_root + root_len++, "/");
} else
root = arg;
strbuf_reset(&root);
strbuf_addstr(&root, arg);
strbuf_complete(&root, '/');
return 0;
}