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>

Christian Couder committed May 24, 2016 at 10:11 UTC 36371e4c7ef87a2895b954284c29e8beff576084
1 file changed +49 -33
builtin/apply.c
+49 -33
@@ -51,6 +51,7 @@ struct apply_state {
51 const char *fake_ancestor;
52 const char *patch_input_file;
53 int line_termination;
54 + struct strbuf root;
55 int p_value;
56 int p_value_known;
57 unsigned int p_context;
@@ -83,8 +84,6 @@ static enum ws_ignore {
84 } ws_ignore_action = ignore_ws_none;
85
86
86 -static struct strbuf root = STRBUF_INIT;
87 -
87 static void parse_whitespace_option(const char *option)
88 {
89 if (!option) {
@@ -474,7 +473,10 @@ static char *squash_slash(char *name)
473 return name;
474 }
475
477 -static char *find_name_gnu(const char *line, const char *def, int p_value)
476 +static char *find_name_gnu(struct apply_state *state,
477 + const char *line,
478 + const char *def,
479 + int p_value)
480 {
481 struct strbuf name = STRBUF_INIT;
482 char *cp;
@@ -498,8 +500,8 @@ static char *find_name_gnu(const char *line, const char *def, int p_value)
500 }
501
502 strbuf_remove(&name, 0, cp - name.buf);
501 - if (root.len)
502 - strbuf_insert(&name, 0, root.buf, root.len);
503 + if (state->root.len)
504 + strbuf_insert(&name, 0, state->root.buf, state->root.len);
505 return squash_slash(strbuf_detach(&name, NULL));
506 }
507
@@ -662,8 +664,12 @@ static size_t diff_timestamp_len(const char *line, size_t len)
664 return line + len - end;
665 }
666
665 -static char *find_name_common(const char *line, const char *def,
666 - int p_value, const char *end, int terminate)
667 +static char *find_name_common(struct apply_state *state,
668 + const char *line,
669 + const char *def,
670 + int p_value,
671 + const char *end,
672 + int terminate)
673 {
674 int len;
675 const char *start = NULL;
@@ -701,32 +707,39 @@ static char *find_name_common(const char *line, const char *def,
707 return squash_slash(xstrdup(def));
708 }
709
704 - if (root.len) {
705 - char *ret = xstrfmt("%s%.*s", root.buf, len, start);
710 + if (state->root.len) {
711 + char *ret = xstrfmt("%s%.*s", state->root.buf, len, start);
712 return squash_slash(ret);
713 }
714
715 return squash_slash(xmemdupz(start, len));
716 }
717
712 -static char *find_name(const char *line, char *def, int p_value, int terminate)
718 +static char *find_name(struct apply_state *state,
719 + const char *line,
720 + char *def,
721 + int p_value,
722 + int terminate)
723 {
724 if (*line == '"') {
715 - char *name = find_name_gnu(line, def, p_value);
725 + char *name = find_name_gnu(state, line, def, p_value);
726 if (name)
727 return name;
728 }
729
720 - return find_name_common(line, def, p_value, NULL, terminate);
730 + return find_name_common(state, line, def, p_value, NULL, terminate);
731 }
732
723 -static char *find_name_traditional(const char *line, char *def, int p_value)
733 +static char *find_name_traditional(struct apply_state *state,
734 + const char *line,
735 + char *def,
736 + int p_value)
737 {
738 size_t len;
739 size_t date_len;
740
741 if (*line == '"') {
729 - char *name = find_name_gnu(line, def, p_value);
742 + char *name = find_name_gnu(state, line, def, p_value);
743 if (name)
744 return name;
745 }
@@ -734,10 +747,10 @@ static char *find_name_traditional(const char *line, char *def, int p_value)
747 len = strchrnul(line, '\n') - line;
748 date_len = diff_timestamp_len(line, len);
749 if (!date_len)
737 - return find_name_common(line, def, p_value, NULL, TERM_TAB);
750 + return find_name_common(state, line, def, p_value, NULL, TERM_TAB);
751 len -= date_len;
752
740 - return find_name_common(line, def, p_value, line + len, 0);
753 + return find_name_common(state, line, def, p_value, line + len, 0);
754 }
755
756 static int count_slashes(const char *cp)
@@ -762,7 +775,7 @@ static int guess_p_value(struct apply_state *state, const char *nameline)
775
776 if (is_dev_null(nameline))
777 return -1;
765 - name = find_name_traditional(nameline, NULL, 0);
778 + name = find_name_traditional(state, nameline, NULL, 0);
779 if (!name)
780 return -1;
781 cp = strchr(name, '/');
@@ -887,17 +900,17 @@ static void parse_traditional_patch(struct apply_state *state,
900 if (is_dev_null(first)) {
901 patch->is_new = 1;
902 patch->is_delete = 0;
890 - name = find_name_traditional(second, NULL, state->p_value);
903 + name = find_name_traditional(state, second, NULL, state->p_value);
904 patch->new_name = name;
905 } else if (is_dev_null(second)) {
906 patch->is_new = 0;
907 patch->is_delete = 1;
895 - name = find_name_traditional(first, NULL, state->p_value);
908 + name = find_name_traditional(state, first, NULL, state->p_value);
909 patch->old_name = name;
910 } else {
911 char *first_name;
899 - first_name = find_name_traditional(first, NULL, state->p_value);
900 - name = find_name_traditional(second, first_name, state->p_value);
912 + first_name = find_name_traditional(state, first, NULL, state->p_value);
913 + name = find_name_traditional(state, second, first_name, state->p_value);
914 free(first_name);
915 if (has_epoch_timestamp(first)) {
916 patch->is_new = 1;
@@ -942,7 +955,7 @@ static void gitdiff_verify_name(struct apply_state *state,
955 int side)
956 {
957 if (!*name && !isnull) {
945 - *name = find_name(line, NULL, state->p_value, TERM_TAB);
958 + *name = find_name(state, line, NULL, state->p_value, TERM_TAB);
959 return;
960 }
961
@@ -952,7 +965,7 @@ static void gitdiff_verify_name(struct apply_state *state,
965 if (isnull)
966 die(_("git apply: bad git-diff - expected /dev/null, got %s on line %d"),
967 *name, state_linenr);
955 - another = find_name(line, NULL, state->p_value, TERM_TAB);
968 + another = find_name(state, line, NULL, state->p_value, TERM_TAB);
969 if (!another || memcmp(another, *name, len + 1))
970 die((side == DIFF_NEW_NAME) ?
971 _("git apply: bad git-diff - inconsistent new filename on line %d") :
@@ -1027,7 +1040,7 @@ static int gitdiff_copysrc(struct apply_state *state,
1040 {
1041 patch->is_copy = 1;
1042 free(patch->old_name);
1030 - patch->old_name = find_name(line, NULL, state->p_value ? state->p_value - 1 : 0, 0);
1043 + patch->old_name = find_name(state, line, NULL, state->p_value ? state->p_value - 1 : 0, 0);
1044 return 0;
1045 }
1046
@@ -1037,7 +1050,7 @@ static int gitdiff_copydst(struct apply_state *state,
1050 {
1051 patch->is_copy = 1;
1052 free(patch->new_name);
1040 - patch->new_name = find_name(line, NULL, state->p_value ? state->p_value - 1 : 0, 0);
1053 + patch->new_name = find_name(state, line, NULL, state->p_value ? state->p_value - 1 : 0, 0);
1054 return 0;
1055 }
1056
@@ -1047,7 +1060,7 @@ static int gitdiff_renamesrc(struct apply_state *state,
1060 {
1061 patch->is_rename = 1;
1062 free(patch->old_name);
1050 - patch->old_name = find_name(line, NULL, state->p_value ? state->p_value - 1 : 0, 0);
1063 + patch->old_name = find_name(state, line, NULL, state->p_value ? state->p_value - 1 : 0, 0);
1064 return 0;
1065 }
1066
@@ -1057,7 +1070,7 @@ static int gitdiff_renamedst(struct apply_state *state,
1070 {
1071 patch->is_rename = 1;
1072 free(patch->new_name);
1060 - patch->new_name = find_name(line, NULL, state->p_value ? state->p_value - 1 : 0, 0);
1073 + patch->new_name = find_name(state, line, NULL, state->p_value ? state->p_value - 1 : 0, 0);
1074 return 0;
1075 }
1076
@@ -1316,8 +1329,8 @@ static int parse_git_header(struct apply_state *state,
1329 * the default name from the header.
1330 */
1331 patch->def_name = git_header_name(state, line, len);
1319 - if (patch->def_name && root.len) {
1320 - char *s = xstrfmt("%s%s", root.buf, patch->def_name);
1332 + if (patch->def_name && state->root.len) {
1333 + char *s = xstrfmt("%s%s", state->root.buf, patch->def_name);
1334 free(patch->def_name);
1335 patch->def_name = s;
1336 }
@@ -4614,9 +4627,10 @@ static int option_parse_whitespace(const struct option *opt,
4627 static int option_parse_directory(const struct option *opt,
4628 const char *arg, int unset)
4629 {
4617 - strbuf_reset(&root);
4618 - strbuf_addstr(&root, arg);
4619 - strbuf_complete(&root, '/');
4630 + struct apply_state *state = opt->value;
4631 + strbuf_reset(&state->root);
4632 + strbuf_addstr(&state->root, arg);
4633 + strbuf_complete(&state->root, '/');
4634 return 0;
4635 }
4636
@@ -4629,6 +4643,7 @@ static void init_apply_state(struct apply_state *state, const char *prefix)
4643 state->line_termination = '\n';
4644 state->p_value = 1;
4645 state->p_context = UINT_MAX;
4646 + strbuf_init(&state->root, 0);
4647
4648 git_apply_config();
4649 if (apply_default_whitespace)
@@ -4640,6 +4655,7 @@ static void init_apply_state(struct apply_state *state, const char *prefix)
4655 static void clear_apply_state(struct apply_state *state)
4656 {
4657 string_list_clear(&state->limit_by_name, 0);
4658 + strbuf_release(&state->root);
4659 }
4660
4661 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)
4733 OPT_BIT(0, "recount", &options,
4734 N_("do not trust the line counts in the hunk headers"),
4735 RECOUNT),
4720 - { OPTION_CALLBACK, 0, "directory", NULL, N_("root"),
4736 + { OPTION_CALLBACK, 0, "directory", &state, N_("root"),
4737 N_("prepend <root> to all filenames"),
4738 0, option_parse_directory },
4739 OPT_END()