]> git.ipfire.org Git - thirdparty/git.git/commitdiff
pathspec: add flag to indicate operation without repository
authorJacob Keller <jacob.keller@gmail.com>
Wed, 21 May 2025 23:29:16 +0000 (16:29 -0700)
committerJunio C Hamano <gitster@pobox.com>
Thu, 22 May 2025 21:20:11 +0000 (14:20 -0700)
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>
dir.c
pathspec.c
pathspec.h

diff --git a/dir.c b/dir.c
index 86eb77b82a79597c12199fbf9c585875cbec64c2..c8c869faed2b8d8f54c34efb545e338c27a3423c 100644 (file)
--- a/dir.c
+++ b/dir.c
@@ -397,9 +397,12 @@ static int match_pathspec_item(struct index_state *istate,
            strncmp(item->match, name - prefix, item->prefix))
                return 0;
 
-       if (item->attr_match_nr &&
-           !match_pathspec_attrs(istate, name - prefix, namelen + prefix, item))
-               return 0;
+       if (item->attr_match_nr) {
+               if (!istate)
+                       BUG("magic PATHSPEC_ATTR requires an index");
+               if (!match_pathspec_attrs(istate, name - prefix, namelen + prefix, item))
+                       return 0;
+       }
 
        /* If the match was just the prefix, we matched */
        if (!*match)
index 2b4e434bc0aa49075802646bcf5b5cd6ef34d506..a3ddd701c740c94309a55e20e443ffa207646925 100644 (file)
@@ -492,7 +492,7 @@ static void init_pathspec_item(struct pathspec_item *item, unsigned flags,
                if (!match) {
                        const char *hint_path;
 
-                       if (!have_git_dir())
+                       if ((flags & PATHSPEC_NO_REPOSITORY) || !have_git_dir())
                                die(_("'%s' is outside the directory tree"),
                                    copyfrom);
                        hint_path = repo_get_work_tree(the_repository);
@@ -614,6 +614,10 @@ void parse_pathspec(struct pathspec *pathspec,
            (flags & PATHSPEC_PREFER_FULL))
                BUG("PATHSPEC_PREFER_CWD and PATHSPEC_PREFER_FULL are incompatible");
 
+       if ((flags & PATHSPEC_NO_REPOSITORY) &&
+           (~magic_mask & (PATHSPEC_ATTR | PATHSPEC_FROMTOP)))
+               BUG("PATHSPEC_NO_REPOSITORY is incompatible with PATHSPEC_ATTR and PATHSPEC_FROMTOP");
+
        /* No arguments with prefix -> prefix pathspec */
        if (!entry) {
                if (flags & PATHSPEC_PREFER_FULL)
index cda3eb5b91f70f2f8c5138a8f2212fbe7d4b6323..5e3a6f1fe7b7c5a6b2464372afe08776c4eb7654 100644 (file)
@@ -76,6 +76,11 @@ struct pathspec {
  * allowed, then it will automatically set for every pathspec.
  */
 #define PATHSPEC_LITERAL_PATH (1<<6)
+/*
+ * For git diff --no-index, indicate that we are operating without
+ * a repository or index.
+ */
+#define PATHSPEC_NO_REPOSITORY (1<<7)
 
 /**
  * Given command line arguments and a prefix, convert the input to