1
0
Fork 0
mirror of https://github.com/git/git.git synced 2024-11-01 06:47:52 +01:00

builtin/apply: move 'root' global into 'struct apply_state'

To libify the apply functionality the 'root' variable should
not be static and global to the file. Let's move it into
'struct apply_state'.

Reviewed-by: Stefan Beller <sbeller@google.com>
Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Christian Couder 2016-05-24 10:11:11 +02:00 committed by Junio C Hamano
parent b76184e410
commit 36371e4c7e

View file

@ -51,6 +51,7 @@ struct apply_state {
const char *fake_ancestor; const char *fake_ancestor;
const char *patch_input_file; const char *patch_input_file;
int line_termination; int line_termination;
struct strbuf root;
int p_value; int p_value;
int p_value_known; int p_value_known;
unsigned int p_context; unsigned int p_context;
@ -83,8 +84,6 @@ static enum ws_ignore {
} ws_ignore_action = ignore_ws_none; } ws_ignore_action = ignore_ws_none;
static struct strbuf root = STRBUF_INIT;
static void parse_whitespace_option(const char *option) static void parse_whitespace_option(const char *option)
{ {
if (!option) { if (!option) {
@ -474,7 +473,10 @@ static char *squash_slash(char *name)
return name; return name;
} }
static char *find_name_gnu(const char *line, const char *def, int p_value) static char *find_name_gnu(struct apply_state *state,
const char *line,
const char *def,
int p_value)
{ {
struct strbuf name = STRBUF_INIT; struct strbuf name = STRBUF_INIT;
char *cp; char *cp;
@ -498,8 +500,8 @@ static char *find_name_gnu(const char *line, const char *def, int p_value)
} }
strbuf_remove(&name, 0, cp - name.buf); strbuf_remove(&name, 0, cp - name.buf);
if (root.len) if (state->root.len)
strbuf_insert(&name, 0, root.buf, root.len); strbuf_insert(&name, 0, state->root.buf, state->root.len);
return squash_slash(strbuf_detach(&name, NULL)); return squash_slash(strbuf_detach(&name, NULL));
} }
@ -662,8 +664,12 @@ static size_t diff_timestamp_len(const char *line, size_t len)
return line + len - end; return line + len - end;
} }
static char *find_name_common(const char *line, const char *def, static char *find_name_common(struct apply_state *state,
int p_value, const char *end, int terminate) const char *line,
const char *def,
int p_value,
const char *end,
int terminate)
{ {
int len; int len;
const char *start = NULL; const char *start = NULL;
@ -701,32 +707,39 @@ static char *find_name_common(const char *line, const char *def,
return squash_slash(xstrdup(def)); return squash_slash(xstrdup(def));
} }
if (root.len) { if (state->root.len) {
char *ret = xstrfmt("%s%.*s", root.buf, len, start); char *ret = xstrfmt("%s%.*s", state->root.buf, len, start);
return squash_slash(ret); return squash_slash(ret);
} }
return squash_slash(xmemdupz(start, len)); return squash_slash(xmemdupz(start, len));
} }
static char *find_name(const char *line, char *def, int p_value, int terminate) static char *find_name(struct apply_state *state,
const char *line,
char *def,
int p_value,
int terminate)
{ {
if (*line == '"') { if (*line == '"') {
char *name = find_name_gnu(line, def, p_value); char *name = find_name_gnu(state, line, def, p_value);
if (name) if (name)
return name; return name;
} }
return find_name_common(line, def, p_value, NULL, terminate); return find_name_common(state, line, def, p_value, NULL, terminate);
} }
static char *find_name_traditional(const char *line, char *def, int p_value) static char *find_name_traditional(struct apply_state *state,
const char *line,
char *def,
int p_value)
{ {
size_t len; size_t len;
size_t date_len; size_t date_len;
if (*line == '"') { if (*line == '"') {
char *name = find_name_gnu(line, def, p_value); char *name = find_name_gnu(state, line, def, p_value);
if (name) if (name)
return name; return name;
} }
@ -734,10 +747,10 @@ static char *find_name_traditional(const char *line, char *def, int p_value)
len = strchrnul(line, '\n') - line; len = strchrnul(line, '\n') - line;
date_len = diff_timestamp_len(line, len); date_len = diff_timestamp_len(line, len);
if (!date_len) if (!date_len)
return find_name_common(line, def, p_value, NULL, TERM_TAB); return find_name_common(state, line, def, p_value, NULL, TERM_TAB);
len -= date_len; len -= date_len;
return find_name_common(line, def, p_value, line + len, 0); return find_name_common(state, line, def, p_value, line + len, 0);
} }
static int count_slashes(const char *cp) static int count_slashes(const char *cp)
@ -762,7 +775,7 @@ static int guess_p_value(struct apply_state *state, const char *nameline)
if (is_dev_null(nameline)) if (is_dev_null(nameline))
return -1; return -1;
name = find_name_traditional(nameline, NULL, 0); name = find_name_traditional(state, nameline, NULL, 0);
if (!name) if (!name)
return -1; return -1;
cp = strchr(name, '/'); cp = strchr(name, '/');
@ -887,17 +900,17 @@ static void parse_traditional_patch(struct apply_state *state,
if (is_dev_null(first)) { if (is_dev_null(first)) {
patch->is_new = 1; patch->is_new = 1;
patch->is_delete = 0; patch->is_delete = 0;
name = find_name_traditional(second, NULL, state->p_value); name = find_name_traditional(state, second, NULL, state->p_value);
patch->new_name = name; patch->new_name = name;
} else if (is_dev_null(second)) { } else if (is_dev_null(second)) {
patch->is_new = 0; patch->is_new = 0;
patch->is_delete = 1; patch->is_delete = 1;
name = find_name_traditional(first, NULL, state->p_value); name = find_name_traditional(state, first, NULL, state->p_value);
patch->old_name = name; patch->old_name = name;
} else { } else {
char *first_name; char *first_name;
first_name = find_name_traditional(first, NULL, state->p_value); first_name = find_name_traditional(state, first, NULL, state->p_value);
name = find_name_traditional(second, first_name, state->p_value); name = find_name_traditional(state, second, first_name, state->p_value);
free(first_name); free(first_name);
if (has_epoch_timestamp(first)) { if (has_epoch_timestamp(first)) {
patch->is_new = 1; patch->is_new = 1;
@ -942,7 +955,7 @@ static void gitdiff_verify_name(struct apply_state *state,
int side) int side)
{ {
if (!*name && !isnull) { if (!*name && !isnull) {
*name = find_name(line, NULL, state->p_value, TERM_TAB); *name = find_name(state, line, NULL, state->p_value, TERM_TAB);
return; return;
} }
@ -952,7 +965,7 @@ static void gitdiff_verify_name(struct apply_state *state,
if (isnull) if (isnull)
die(_("git apply: bad git-diff - expected /dev/null, got %s on line %d"), die(_("git apply: bad git-diff - expected /dev/null, got %s on line %d"),
*name, state_linenr); *name, state_linenr);
another = find_name(line, NULL, state->p_value, TERM_TAB); another = find_name(state, line, NULL, state->p_value, TERM_TAB);
if (!another || memcmp(another, *name, len + 1)) if (!another || memcmp(another, *name, len + 1))
die((side == DIFF_NEW_NAME) ? die((side == DIFF_NEW_NAME) ?
_("git apply: bad git-diff - inconsistent new filename on line %d") : _("git apply: bad git-diff - inconsistent new filename on line %d") :
@ -1027,7 +1040,7 @@ static int gitdiff_copysrc(struct apply_state *state,
{ {
patch->is_copy = 1; patch->is_copy = 1;
free(patch->old_name); free(patch->old_name);
patch->old_name = find_name(line, NULL, state->p_value ? state->p_value - 1 : 0, 0); patch->old_name = find_name(state, line, NULL, state->p_value ? state->p_value - 1 : 0, 0);
return 0; return 0;
} }
@ -1037,7 +1050,7 @@ static int gitdiff_copydst(struct apply_state *state,
{ {
patch->is_copy = 1; patch->is_copy = 1;
free(patch->new_name); free(patch->new_name);
patch->new_name = find_name(line, NULL, state->p_value ? state->p_value - 1 : 0, 0); patch->new_name = find_name(state, line, NULL, state->p_value ? state->p_value - 1 : 0, 0);
return 0; return 0;
} }
@ -1047,7 +1060,7 @@ static int gitdiff_renamesrc(struct apply_state *state,
{ {
patch->is_rename = 1; patch->is_rename = 1;
free(patch->old_name); free(patch->old_name);
patch->old_name = find_name(line, NULL, state->p_value ? state->p_value - 1 : 0, 0); patch->old_name = find_name(state, line, NULL, state->p_value ? state->p_value - 1 : 0, 0);
return 0; return 0;
} }
@ -1057,7 +1070,7 @@ static int gitdiff_renamedst(struct apply_state *state,
{ {
patch->is_rename = 1; patch->is_rename = 1;
free(patch->new_name); free(patch->new_name);
patch->new_name = find_name(line, NULL, state->p_value ? state->p_value - 1 : 0, 0); patch->new_name = find_name(state, line, NULL, state->p_value ? state->p_value - 1 : 0, 0);
return 0; return 0;
} }
@ -1316,8 +1329,8 @@ static int parse_git_header(struct apply_state *state,
* the default name from the header. * the default name from the header.
*/ */
patch->def_name = git_header_name(state, line, len); patch->def_name = git_header_name(state, line, len);
if (patch->def_name && root.len) { if (patch->def_name && state->root.len) {
char *s = xstrfmt("%s%s", root.buf, patch->def_name); char *s = xstrfmt("%s%s", state->root.buf, patch->def_name);
free(patch->def_name); free(patch->def_name);
patch->def_name = s; patch->def_name = s;
} }
@ -4614,9 +4627,10 @@ static int option_parse_whitespace(const struct option *opt,
static int option_parse_directory(const struct option *opt, static int option_parse_directory(const struct option *opt,
const char *arg, int unset) const char *arg, int unset)
{ {
strbuf_reset(&root); struct apply_state *state = opt->value;
strbuf_addstr(&root, arg); strbuf_reset(&state->root);
strbuf_complete(&root, '/'); strbuf_addstr(&state->root, arg);
strbuf_complete(&state->root, '/');
return 0; return 0;
} }
@ -4629,6 +4643,7 @@ static void init_apply_state(struct apply_state *state, const char *prefix)
state->line_termination = '\n'; state->line_termination = '\n';
state->p_value = 1; state->p_value = 1;
state->p_context = UINT_MAX; state->p_context = UINT_MAX;
strbuf_init(&state->root, 0);
git_apply_config(); git_apply_config();
if (apply_default_whitespace) if (apply_default_whitespace)
@ -4640,6 +4655,7 @@ static void init_apply_state(struct apply_state *state, const char *prefix)
static void clear_apply_state(struct apply_state *state) static void clear_apply_state(struct apply_state *state)
{ {
string_list_clear(&state->limit_by_name, 0); string_list_clear(&state->limit_by_name, 0);
strbuf_release(&state->root);
} }
int cmd_apply(int argc, const char **argv, const char *prefix) int cmd_apply(int argc, const char **argv, const char *prefix)
@ -4717,7 +4733,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix)
OPT_BIT(0, "recount", &options, OPT_BIT(0, "recount", &options,
N_("do not trust the line counts in the hunk headers"), N_("do not trust the line counts in the hunk headers"),
RECOUNT), RECOUNT),
{ OPTION_CALLBACK, 0, "directory", NULL, N_("root"), { OPTION_CALLBACK, 0, "directory", &state, N_("root"),
N_("prepend <root> to all filenames"), N_("prepend <root> to all filenames"),
0, option_parse_directory }, 0, option_parse_directory },
OPT_END() OPT_END()