Skip to content

Commit 4a2d5ae

Browse files
pcloudsgitster
authored andcommitted
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 <[email protected]> Acked-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 3d092bf commit 4a2d5ae

File tree

6 files changed

+30
-4
lines changed

6 files changed

+30
-4
lines changed

builtin/blame.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,9 @@ static struct origin *find_origin(struct scoreboard *sb,
409409
paths[0] = origin->path;
410410
paths[1] = NULL;
411411

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);
413415
diff_setup_done(&diff_opts);
414416

415417
if (is_null_sha1(origin->commit->object.sha1))

pathspec.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,11 @@ static unsigned prefix_pathspec(struct pathspec_item *item,
128128
die(_("global 'literal' pathspec setting is incompatible "
129129
"with all other global pathspec settings"));
130130

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)) {
132136
; /* nothing to do */
133137
} else if (elt[1] == '(') {
134138
/* longhand */
@@ -405,6 +409,9 @@ void parse_pathspec(struct pathspec *pathspec,
405409
item[i].magic = prefix_pathspec(item + i, &short_magic,
406410
argv + i, flags,
407411
prefix, prefixlen, entry);
412+
if ((flags & PATHSPEC_LITERAL_PATH) &&
413+
!(magic_mask & PATHSPEC_LITERAL))
414+
item[i].magic |= PATHSPEC_LITERAL;
408415
if (item[i].magic & magic_mask)
409416
unsupported_magic(entry,
410417
item[i].magic & magic_mask,

pathspec.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,13 @@ struct pathspec {
5858
#define PATHSPEC_STRIP_SUBMODULE_SLASH_EXPENSIVE (1<<5)
5959
#define PATHSPEC_PREFIX_ORIGIN (1<<6)
6060
#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)
6168

6269
extern void parse_pathspec(struct pathspec *pathspec,
6370
unsigned magic_mask,

revision.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1372,7 +1372,8 @@ static void prepare_show_merge(struct rev_info *revs)
13721372
i++;
13731373
}
13741374
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);
13761377
revs->limited = 1;
13771378
}
13781379

t/t6130-pathspec-noglob.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,13 @@ test_expect_success 'no-glob environment variable works' '
108108
test_cmp expect actual
109109
'
110110

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+
111118
test_expect_success 'setup xxx/bar' '
112119
mkdir xxx &&
113120
test_commit xxx xxx/bar

tree-diff.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,9 @@ static void try_to_follow_renames(struct tree_desc *t1, struct tree_desc *t2, co
254254
path[0] = p->one->path;
255255
path[1] = NULL;
256256
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);
258260

259261
/*
260262
* The caller expects us to return a set of vanilla

0 commit comments

Comments
 (0)