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 <jacob.keller@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jacob Keller committed May 21, 2025 at 16:29 UTC 00466c162082c4c5f2ea96384a9f51147e1c874b
3 files changed +16 -4
dir.c
+6 -3
@@ -397,9 +397,12 @@ static int match_pathspec_item(struct index_state *istate,
397 strncmp(item->match, name - prefix, item->prefix))
398 return 0;
399
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 + }
406
407 /* If the match was just the prefix, we matched */
408 if (!*match)
pathspec.c
+5 -1
@@ -492,7 +492,7 @@ static void init_pathspec_item(struct pathspec_item *item, unsigned flags,
492 if (!match) {
493 const char *hint_path;
494
495 - if (!have_git_dir())
495 + if ((flags & PATHSPEC_NO_REPOSITORY) || !have_git_dir())
496 die(_("'%s' is outside the directory tree"),
497 copyfrom);
498 hint_path = repo_get_work_tree(the_repository);
@@ -614,6 +614,10 @@ void parse_pathspec(struct pathspec *pathspec,
614 (flags & PATHSPEC_PREFER_FULL))
615 BUG("PATHSPEC_PREFER_CWD and PATHSPEC_PREFER_FULL are incompatible");
616
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 +
621 /* No arguments with prefix -> prefix pathspec */
622 if (!entry) {
623 if (flags & PATHSPEC_PREFER_FULL)
pathspec.h
+5
@@ -76,6 +76,11 @@ struct pathspec {
76 * allowed, then it will automatically set for every pathspec.
77 */
78 #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)
84
85 /**
86 * Given command line arguments and a prefix, convert the input to