pathspec: stop --*-pathspecs impact on internal parse_pathspec() uses

Normally parse_pathspec() is used on command line arguments where it can do fancy thing like parsing magic on each argument or adding magic for all pathspecs based on --*-pathspecs options. There's another use of parse_pathspec(), where pathspec is needed, but the input is known to be pure paths. In this case we usually don't want --*-pathspecs to interfere. And we definitely do not want to parse magic in these paths, regardless of --literal-pathspecs. Add new flag PATHSPEC_LITERAL_PATH for this purpose. When it's set, --*-pathspecs are ignored, no magic is parsed. And if the caller allows PATHSPEC_LITERAL (i.e. the next calls can take literal magic), then PATHSPEC_LITERAL will be set. This fixes cases where git chokes when GIT_*_PATHSPECS are set because parse_pathspec() indicates it won't take any magic. But GIT_*_PATHSPECS add them anyway. These are export GIT_LITERAL_PATHSPECS=1 git blame -- something git log --follow something git log --merge "git ls-files --with-tree=path" (aka parse_pathspec() in overlay_tree_on_cache()) is safe because the input is empty, and producing one pathspec due to PATHSPEC_PREFER_CWD does not take any magic into account. Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Acked-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Nguyễn Thái Ngọc Duy committed Oct 26, 2013 at 09:09 UTC 4a2d5ae262a6d372d0951da9cee3c7ad2a8dbca6
6 files changed +30 -4
builtin/blame.c
+3 -1
@@ -409,7 +409,9 @@ static struct origin *find_origin(struct scoreboard *sb,
409 paths[0] = origin->path;
410 paths[1] = NULL;
411
412 - parse_pathspec(&diff_opts.pathspec, PATHSPEC_ALL_MAGIC, 0, "", paths);
412 + parse_pathspec(&diff_opts.pathspec,
413 + PATHSPEC_ALL_MAGIC & ~PATHSPEC_LITERAL,
414 + PATHSPEC_LITERAL_PATH, "", paths);
415 diff_setup_done(&diff_opts);
416
417 if (is_null_sha1(origin->commit->object.sha1))
pathspec.c
+8 -1
@@ -128,7 +128,11 @@ static unsigned prefix_pathspec(struct pathspec_item *item,
128 die(_("global 'literal' pathspec setting is incompatible "
129 "with all other global pathspec settings"));
130
131 - if (elt[0] != ':' || literal_global) {
131 + if (flags & PATHSPEC_LITERAL_PATH)
132 + global_magic = 0;
133 +
134 + if (elt[0] != ':' || literal_global ||
135 + (flags & PATHSPEC_LITERAL_PATH)) {
136 ; /* nothing to do */
137 } else if (elt[1] == '(') {
138 /* longhand */
@@ -405,6 +409,9 @@ void parse_pathspec(struct pathspec *pathspec,
409 item[i].magic = prefix_pathspec(item + i, &short_magic,
410 argv + i, flags,
411 prefix, prefixlen, entry);
412 + if ((flags & PATHSPEC_LITERAL_PATH) &&
413 + !(magic_mask & PATHSPEC_LITERAL))
414 + item[i].magic |= PATHSPEC_LITERAL;
415 if (item[i].magic & magic_mask)
416 unsupported_magic(entry,
417 item[i].magic & magic_mask,
pathspec.h
+7
@@ -58,6 +58,13 @@ struct pathspec {
58 #define PATHSPEC_STRIP_SUBMODULE_SLASH_EXPENSIVE (1<<5)
59 #define PATHSPEC_PREFIX_ORIGIN (1<<6)
60 #define PATHSPEC_KEEP_ORDER (1<<7)
61 +/*
62 + * For the callers that just need pure paths from somewhere else, not
63 + * from command line. Global --*-pathspecs options are ignored. No
64 + * magic is parsed in each pathspec either. If PATHSPEC_LITERAL is
65 + * allowed, then it will automatically set for every pathspec.
66 + */
67 +#define PATHSPEC_LITERAL_PATH (1<<8)
68
69 extern void parse_pathspec(struct pathspec *pathspec,
70 unsigned magic_mask,
revision.c
+2 -1
@@ -1372,7 +1372,8 @@ static void prepare_show_merge(struct rev_info *revs)
1372 i++;
1373 }
1374 free_pathspec(&revs->prune_data);
1375 - parse_pathspec(&revs->prune_data, PATHSPEC_ALL_MAGIC, 0, "", prune);
1375 + parse_pathspec(&revs->prune_data, PATHSPEC_ALL_MAGIC & ~PATHSPEC_LITERAL,
1376 + PATHSPEC_LITERAL_PATH, "", prune);
1377 revs->limited = 1;
1378 }
1379
t/t6130-pathspec-noglob.sh
+7
@@ -108,6 +108,13 @@ test_expect_success 'no-glob environment variable works' '
108 test_cmp expect actual
109 '
110
111 +test_expect_success 'blame takes global pathspec flags' '
112 + git --literal-pathspecs blame -- foo &&
113 + git --icase-pathspecs blame -- foo &&
114 + git --glob-pathspecs blame -- foo &&
115 + git --noglob-pathspecs blame -- foo
116 +'
117 +
118 test_expect_success 'setup xxx/bar' '
119 mkdir xxx &&
120 test_commit xxx xxx/bar
tree-diff.c
+3 -1
@@ -254,7 +254,9 @@ static void try_to_follow_renames(struct tree_desc *t1, struct tree_desc *t2, co
254 path[0] = p->one->path;
255 path[1] = NULL;
256 free_pathspec(&opt->pathspec);
257 - parse_pathspec(&opt->pathspec, PATHSPEC_ALL_MAGIC, 0, "", path);
257 + parse_pathspec(&opt->pathspec,
258 + PATHSPEC_ALL_MAGIC & ~PATHSPEC_LITERAL,
259 + PATHSPEC_LITERAL_PATH, "", path);
260
261 /*
262 * The caller expects us to return a set of vanilla