Skip to content

Commit 00466c1

Browse files
jacob-kellergitster
authored andcommitted
pathspec: add flag to indicate operation without repository
A following change will add support for pathspecs to the git diff --no-index command. This mode of git diff does not load any repository. Add a new PATHSPEC_NO_REPOSITORY flag indicating that we're parsing pathspecs without a repository. Both PATHSPEC_ATTR and PATHSPEC_FROMTOP require a repository to function. Thus, verify that both of these are set in magic_mask to ensure they won't be accepted when PATHSPEC_NO_REPOSITORY is set. Check PATHSPEC_NO_REPOSITORY when warning about paths outside the directory tree. When the flag is set, do not look for a git repository when generating the warning message. Finally, add a BUG in match_pathspec_item if the istate is NULL but the pathspec has PATHSPEC_ATTR set. Callers which support PATHSPEC_ATTR should always pass a valid istate, and callers which don't pass a valid istate should have set PATHSPEC_ATTR in the magic_mask field to disable support for attribute-based pathspecs. Signed-off-by: Jacob Keller <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 6e4fb00 commit 00466c1

File tree

3 files changed

+16
-4
lines changed

3 files changed

+16
-4
lines changed

dir.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -397,9 +397,12 @@ static int match_pathspec_item(struct index_state *istate,
397397
strncmp(item->match, name - prefix, item->prefix))
398398
return 0;
399399

400-
if (item->attr_match_nr &&
401-
!match_pathspec_attrs(istate, name - prefix, namelen + prefix, item))
402-
return 0;
400+
if (item->attr_match_nr) {
401+
if (!istate)
402+
BUG("magic PATHSPEC_ATTR requires an index");
403+
if (!match_pathspec_attrs(istate, name - prefix, namelen + prefix, item))
404+
return 0;
405+
}
403406

404407
/* If the match was just the prefix, we matched */
405408
if (!*match)

pathspec.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,7 @@ static void init_pathspec_item(struct pathspec_item *item, unsigned flags,
492492
if (!match) {
493493
const char *hint_path;
494494

495-
if (!have_git_dir())
495+
if ((flags & PATHSPEC_NO_REPOSITORY) || !have_git_dir())
496496
die(_("'%s' is outside the directory tree"),
497497
copyfrom);
498498
hint_path = repo_get_work_tree(the_repository);
@@ -614,6 +614,10 @@ void parse_pathspec(struct pathspec *pathspec,
614614
(flags & PATHSPEC_PREFER_FULL))
615615
BUG("PATHSPEC_PREFER_CWD and PATHSPEC_PREFER_FULL are incompatible");
616616

617+
if ((flags & PATHSPEC_NO_REPOSITORY) &&
618+
(~magic_mask & (PATHSPEC_ATTR | PATHSPEC_FROMTOP)))
619+
BUG("PATHSPEC_NO_REPOSITORY is incompatible with PATHSPEC_ATTR and PATHSPEC_FROMTOP");
620+
617621
/* No arguments with prefix -> prefix pathspec */
618622
if (!entry) {
619623
if (flags & PATHSPEC_PREFER_FULL)

pathspec.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,11 @@ struct pathspec {
7676
* allowed, then it will automatically set for every pathspec.
7777
*/
7878
#define PATHSPEC_LITERAL_PATH (1<<6)
79+
/*
80+
* For git diff --no-index, indicate that we are operating without
81+
* a repository or index.
82+
*/
83+
#define PATHSPEC_NO_REPOSITORY (1<<7)
7984

8085
/**
8186
* Given command line arguments and a prefix, convert the input to

0 commit comments

Comments
 (0)