Skip to contentSkip to conversation

diff --no-index: support limiting by pathspec

RFC PATCH New Jacob Keller 1 participant 1 message May 15, 2025

lore
Share & tools
lore
#0 lore raw

RFC PATCH v2 1/1 diff --no-index: support limiting by pathspec

From: Jacob Keller <[email protected]>

The --no-index option of git-diff enables using the diff machinery from git while operating outside of a repository. This mode of git diff is able to compare directories and produce a diff of their contents.

When operating git diff in a repository, git has the notion of "pathspecs" which can specify which files to compare. In particular, when using git to diff two trees, you might invoke:

  $ git diff-tree -r <treeish1> <treeish2>.

where the treeish could point to a subdirectory of the repository.

When invoked this way, users can limit the selected paths of the tree by using a pathspec. Either by providing some list of paths to accept, or by removing paths via a negative refspec.

The git diff --no-index mode does not support pathspecs, and cannot limit the diff output in this way. Other diff programs such as GNU difftools have options for excluding paths based on a pattern match. However, using git diff as a diff replacement has several advantages over many popular diff tools, including coloring moved lines, rename detections, and similar.

Teach git diff --no-index how to handle pathspecs to limit the comparisons. This will only be supported if both provided paths are directories.

This is because the --no-index mode already employs some DWIM shortcuts when dealing with comparing a directory and a file.

Executing git diff --no-index D F is interpreted as if:

  $ git diff --no-index D/$(basename F) F

Similarly, if you do git diff --no-index F D.

Modify the fixup_paths function to return 1 if both paths are directories. If this is the case, interpret any extra arguments to git diff as pathspecs via parse_pathspec. Add a new PATHSPEC_NO_REPOSITORY flag to indicate to the parser that we do not have repository. Use this to aid in error message reporting in the event that the user happens to invoke git diff --no-index from a repository.

Extend the prefix_path_gently function to correctly handle a prefix which does not end in '/', by inserting one as appropriate.

Use parse_pathspec to load the remaining arguments (if any) to git diff --no-index as pathspec items. Disable PATHSPEC_ATTR support since we do not have a repository to do attribute lookup. Disable PATHSPEC_FROMTOP since we do not have a repository root. All pathspecs are treated as rooted at the provided comparison paths.

Load the pathspecs twice, once prefixed with paths[0] and once prefixed with paths[1]. I considered trying to avoid this, but don't yet have a workable solution that reuses the same pathspec objects. We need the directory paths as prefixes otherwise the match_pathspec won't compare properly.

Pass the pathspec object for both paths into queue_diff, who in-turn passes these along to read_directory_contents.

Modify read_directory_contents to check against the pathspecs when scanning the directory. In order to properly recurse, we must handle leading directory checks. Thus, a new "match_leading_pathspecs" variant is created, which sets the DO_MATCH_LEADING_PATHSPEC flag. This is required to correctly handle nested directories. Consider this:

  $ git diff --no-index a b c/d

This should include all paths in a and b which match the c/d pathspec. In particular, if there was 'a/c/d' we need to match it. But to check 'a/c/d', we need to first get to that part, which requires comparing 'a/c' first. Without DO_MATCH_LEADING_PATHSPEC, 'a/c' won't match the 'a/c/d' pathspec string.

This implementation appears to work ok, but I still need to create some unit tests. I also think this might be a little hacky, and I am not sure how folks feel about parsing separate pathspecs for both entries.

Some other gotchas and open questions:

1) pathspecs must all come after the first two path arguments, you can't re-arrange them to come first. I'm treating them sort of like the treeish arguments to git diff-tree.

2) The pathspecs are interpreted relative to the provided paths, and thus will always need to be specified as relative paths, and will be interpreted as relative to the root of the search for each path separately.

3) negative pathspecs have to be fully qualified from the root, i.e. ':(exclude)file' will only exclude 'a/file' and not 'a/b/file' unless you also use '(glob)' or similar. I think this matches the other pathspec support, but I an not 100% sure.

I feel like some of these changes are a bit hacky and could use some further refinement or suggestion, but the core idea appears to work reasonably well!

---
This version uses pathspecs and hopefully works in a reasonable enough way to not be too confusing. Still needs tests, likely needs to be broken up into smaller pieces, or some of the hacks need to be smoothed over. I have executed this, but didn't run the full test suite yet.

 pathspec.h      | 10 ++++++
 diff-no-index.c | 85 ++++++++++++++++++++++++++++++++++++++++---------
 dir.c           | 24 ++++++++++++--
 pathspec.c      |  7 +++-
 setup.c         |  4 ++-
 5 files changed, 110 insertions(+), 20 deletions(-)
pathspec.h +10 -0
index de537cff3cb6..363fb6309663 100644
@@ -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
@@ -184,6 +189,11 @@ int match_pathspec(struct index_state *istate,
 		   const char *name, int namelen,
 		   int prefix, char *seen, int is_dir);
 
+int match_leading_pathspec(struct index_state *istate,
+			   const struct pathspec *ps,
+			   const char *name, int namelen,
+			   int prefix, char *seen, int is_dir);
+
 /*
  * Determine whether a pathspec will match only entire index entries (non-sparse
  * files and/or entire sparse directories). If the pathspec has the potential to
diff-no-index.c +70 -15

172 lines not on this page · raw message

dir.c +21 -3
index a374972b6243..7f6079475397 100644
@@ -397,9 +397,13 @@ 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) {
+		/* TODO: is there a better way to handle this? */
+		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)
@@ -577,6 +581,20 @@ int match_pathspec(struct index_state *istate,
 					 prefix, seen, flags);
 }
 
+int match_leading_pathspec(struct index_state *istate,
+			   const struct pathspec *ps,
+			   const char *name, int namelen,
+			   int prefix, char *seen, int is_dir)
+{
+	unsigned flags = DO_MATCH_LEADING_PATHSPEC;
+
+	if (is_dir)
+		flags |= DO_MATCH_DIRECTORY;
+
+	return match_pathspec_with_flags(istate, ps, name, namelen,
+					 prefix, seen, flags);
+}
+
 /**
  * Check if a submodule is a superset of the pathspec
  */
pathspec.c +6 -1
index 2b4e434bc0aa..7a46da50dc77 100644
@@ -492,7 +492,12 @@ static void init_pathspec_item(struct pathspec_item *item, unsigned flags,
 		if (!match) {
 			const char *hint_path;
 
-			if (!have_git_dir())
+			/* TODO: should we have a different error message?
+			 * Really, we can't have absolute paths for pathspec
+			 * in git diff --no-index since it must be relative to
+			 * both directories in order to work at all.
+			 */
+			if ((flags & PATHSPEC_NO_REPOSITORY) || !have_git_dir())
 				die(_("'%s' is outside the directory tree"),
 				    copyfrom);
 			hint_path = repo_get_work_tree(the_repository);
setup.c +3 -1
index f93bd6a24a5d..56ebf1218442 100644
@@ -139,7 +139,9 @@ char *prefix_path_gently(const char *prefix, int len,
 			return NULL;
 		}
 	} else {
-		sanitized = xstrfmt("%.*s%s", len, len ? prefix : "", path);
+		sanitized = xstrfmt("%.*s%s%s", len, len ? prefix : "",
+				    len && prefix[len - 1] == '/' ? "" : "/",
+				    path);
 		if (remaining_prefix)
 			*remaining_prefix = len;
 		if (normalize_path_copy_len(sanitized, sanitized, remaining_prefix)) {
Signature · 1 line
2.48.1.397.gec9d649cc640