git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 7/8] setup_tree_pathspec(): interpret '^' as negative pathspec

From
Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Date
Sep 8, 2010, 15:50 UTC
Message-ID
<1283961023-4491-8-git-send-email-pclouds@gmail.com>
In-Reply-To
<1283961023-4491-1-git-send-email-pclouds@gmail.com>

This patch does preparation work for tree exclusion in tree_entry_interesting(). '^' has similar meaning to '!' in gitexcludes. '!' is not used because bash does not like arguments with a leading '!'.

Eventually, "git diff -- foo ^foo/bar" should show differences in foo, except foo/bar. If "git diff -- ^foo" is given, then it implies everything except foo, which could surprise users that "bar" in "git diff -- bar ^foo" has no effect at all.

NOTE: pathspec in diff machinery is also used by ce_path_match() and
read_index_preload(), which currently do not understand '^' at all.
Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 tree-walk.c |   28 ++++++++++++++++++++++++++++
 tree-walk.h |    3 +++
 2 files changed, 31 insertions(+), 0 deletions(-)
diff --git a/tree-walk.c b/tree-walk.c
index e7041d7..a2f4a00 100644
--- a/tree-walk.c
+++ b/tree-walk.c
@@ -479,7 +479,35 @@ int setup_tree_pathspec(const char **paths, struct tree_pathspec_list *ps)
 	for (i=0; i < ps->nr; i++) {
 		struct tree_pathspec *exc = ps->info+i;
 		exc->path = ps->paths[i];
+		if (*exc->path == '^') {
+			exc->path++;
+			exc->to_exclude = 1;
+		}
 		exc->pathlen = strlen(exc->path);
+		if (exc->to_exclude) {
+			int j, found = 0;
+
+			for (j = i-1; j >= 0; j--) {
+				int len = strlen(ps->info[j].path);
+				if (len < exc->pathlen &&
+				    !strncmp(ps->info[j].path, exc->path, len) &&
+				    exc->path[len] == '/') {
+					ps->info[j].has_sub_pathspec = 1;
+					found = 1;
+				}
+			}
+
+			/*
+			 * If a negative pathspec has nothing to
+			 * negate from, include everything so it can
+			 * negate from that.  This way is not
+			 * perfect. You may be surprised to find out
+			 * "^Documentation t" does not take "t" into
+			 * account at all
+			 */
+			if (!found)
+				ps->include_by_default = 1;
+		}
 	}
 	return 0;
 }
diff --git a/tree-walk.h b/tree-walk.h
index bb55656..6be1e6c 100644
--- a/tree-walk.h
+++ b/tree-walk.h
@@ -16,9 +16,12 @@ struct tree_desc {
 struct tree_pathspec_list {
 	const char **paths;
 	int nr;
+	int include_by_default:1;
 	struct tree_pathspec {
 		const char *path;
 		int pathlen;
+		int to_exclude:1;
+		int has_sub_pathspec:1;
 	} *info;
 };
 
-- 
1.7.1.rc1.70.g13aff
Previous: Nguyễn Thái Ngọc DuyNext: Elijah Newren
Message 12 of 21 in “en/object-list-with-pathspec v4”
  1. 0/8 en/object-list-with-pathspec v4Nguyễn Thái Ngọc Duy, Sep 8, 2010
  2. 1/8 diff-no-index: use diff_tree_setup_paths()Nguyễn Thái Ngọc Duy, Sep 8, 2010
  3. 2/8 Introduce struct tree_pathspec_listNguyễn Thái Ngọc Duy, Sep 8, 2010
  4. 3/8 tree_entry_interesting(): remove dependency on struct diff_optionsNguyễn Thái Ngọc Duy, Sep 8, 2010
  5. Junio C HamanoSep 14, 2010
  6. Nguyen Thai Ngoc DuySep 14, 2010
  7. Junio C HamanoSep 14, 2010
  8. 4/8 tree-walk: move tree_entry_interesting() from tree-diff.cNguyễn Thái Ngọc Duy, Sep 8, 2010
  9. 5/8 Add testcases showing how pathspecs are ignored with rev-list --objectsNguyễn Thái Ngọc Duy, Sep 8, 2010
  10. Junio C HamanoSep 14, 2010
  11. 6/8 Make rev-list --objects work together with pathspecsNguyễn Thái Ngọc Duy, Sep 8, 2010
  12. 7/8 setup_tree_pathspec(): interpret '^' as negative pathspecNguyễn Thái Ngọc Duy, Sep 8, 2010
  13. Elijah NewrenSep 11, 2010
  14. Nguyen Thai Ngoc DuySep 13, 2010
  15. Junio C HamanoSep 14, 2010
  16. Nguyen Thai Ngoc DuySep 14, 2010
  17. 8/8 tree_entry_interesting(): support negative pathspecNguyễn Thái Ngọc Duy, Sep 8, 2010
  18. Elijah NewrenSep 11, 2010
  19. Junio C HamanoSep 14, 2010
  20. Nguyen Thai Ngoc DuySep 14, 2010
  21. Elijah NewrenSep 11, 2010

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.