Skip to contentSkip to conversation

git log exclude pathspec from file - supported? plans?

PATCH New Martin Langhoff 3 participants 8 messages Jun 30, 2021 – Jul 1, 2021 1 message not in the archive Kbuild

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): CACPiFCLtj5QF6_Goc5UYh9KHWgkrKtjApL-cCH04S5gdTFyk7Q@mail.gmail.com

#1 lore raw

Hi Git Gang!

long time no see! I'm doing some complex git repo spelunking and pushing the boundaries of the pathspec magic for excludes.

Is there a reasonable way to provide a (potentially large) set of excludes? something like

git log --exclude-pathspec-file paths-to-exclude.txt .

Has there been discussion / patches / plans related to this? I may have some cycles (hopefully!)

cheers,

m

Signature · 4 lines
 [email protected]
 - ask interesting questions  ~  http://linkedin.com/in/martinlanghoff
 - don't be distracted        ~  http://github.com/martin-langhoff
   by shiny stuff
#2 lore raw
Quoted 10 lines · Martin Langhoff

long time no see! I'm doing some complex git repo spelunking and pushing the boundaries of the pathspec magic for excludes.

Is there a reasonable way to provide a (potentially large) set of excludes? something like

git log --exclude-pathspec-file paths-to-exclude.txt .

Has there been discussion / patches / plans related to this? I may have some cycles (hopefully!)

You can feed pathspecs via --stdin. So:

  {
	echo "--"
	sed s/^/:^/ paths-to-exclude.txt

} | git log --stdin

works. Obviously it's not as turn-key if you really do have a list of paths in a file already, but it's much more flexible.

I'll caution you that the pathspec code is not well-optimized to handle a large number of pathspecs. E.g.:

[no pathspecs]

  $ time git rev-list HEAD /dev/null
  real	0m0.033s
  user	0m0.017s
  sys	0m0.017s

[trivial pathspec; now we have to actually open up trees]

  $ { echo --; echo .; } >input
  $ time git rev-list HEAD --stdin <input >/dev/null
  real	0m1.338s
  user	0m1.294s
  sys	0m0.045s

[lots of pathspecs; now we spend loads of time actually matching
strings; the ^C is when I got bored and killed it]

  $ { echo --; git ls-files; } >input
  $ time git rev-list HEAD --stdin <input >/dev/null
  ^C
  real	1m24.406s
  user	1m24.369s
  sys	0m0.036s

The problem is that we try to linearly match every pathspec against every path we consider, so it's quadratic-ish in the number of files in the repo. I played a long time ago with storing non-wildcard pathspecs in a trie that we could traverse as we talked the individual trees we were matching. It performed well, but IIRC the interface was hacky (I had to bolt it specifically onto the way the tree-walker uses pathspecs, and the other pathspec matchers didn't benefit at all).

I can probably dig it up if anybody's interested in looking at it.

-Peff

#3 lore raw
Quoted 56 lines · Jeff King
Quoted 10 lines · Martin Langhoff

long time no see! I'm doing some complex git repo spelunking and pushing the boundaries of the pathspec magic for excludes.

Is there a reasonable way to provide a (potentially large) set of excludes? something like

git log --exclude-pathspec-file paths-to-exclude.txt .

Has there been discussion / patches / plans related to this? I may have some cycles (hopefully!)

You can feed pathspecs via --stdin. So:

  {
	echo "--"
	sed s/^/:^/ paths-to-exclude.txt

} | git log --stdin

works. Obviously it's not as turn-key if you really do have a list of paths in a file already, but it's much more flexible.

I'll caution you that the pathspec code is not well-optimized to handle a large number of pathspecs. E.g.:

[no pathspecs]

  $ time git rev-list HEAD /dev/null
  real	0m0.033s
  user	0m0.017s
  sys	0m0.017s

[trivial pathspec; now we have to actually open up trees]

  $ { echo --; echo .; } >input
  $ time git rev-list HEAD --stdin <input >/dev/null
  real	0m1.338s
  user	0m1.294s
  sys	0m0.045s

[lots of pathspecs; now we spend loads of time actually matching
strings; the ^C is when I got bored and killed it]

  $ { echo --; git ls-files; } >input
  $ time git rev-list HEAD --stdin <input >/dev/null
  ^C
  real	1m24.406s
  user	1m24.369s
  sys	0m0.036s

The problem is that we try to linearly match every pathspec against every path we consider, so it's quadratic-ish in the number of files in the repo. I played a long time ago with storing non-wildcard pathspecs in a trie that we could traverse as we talked the individual trees we were matching. It performed well, but IIRC the interface was hacky (I had to bolt it specifically onto the way the tree-walker uses pathspecs, and the other pathspec matchers didn't benefit at all).

Quoted 1 line

I can probably dig it up if anybody's interested in looking at it.

If it's not too much trouble I'd find it interesting, but I likely won't do anything with it any time soon.

One of the PCREv2 experiments I had very early WIP work towards was to create a search index for commit messages, contents etc. and stick it in something similar to the --changed-paths part of the commit-graph.

The PCREv2 codebase actually has (supposedly) a bug-for-bug compatible implementation of our wildmatch function as a translator to a PCREv2 regex, I have a brnch somewhere where we run all our wildmatch tests against it successfully.

So couple that with regex introspection, and a search index that e.g. creates a trie bloom filter, then as long as your --grep=<RX>, -G<RX> or pathspec has at least 3 fixed strings among its wildcards we can ask the bloom filter "is this commit a candidate for this regex searching this path/commit message/diff/whatever".

So you can have indexed matches for things like '*/test-lib.sh", not just prefixes or fixed-strings.

#4 lore raw
Quoted 8 lines · Ævar Arnfjörð Bjarmason
Quoted 8 lines

The problem is that we try to linearly match every pathspec against every path we consider, so it's quadratic-ish in the number of files in the repo. I played a long time ago with storing non-wildcard pathspecs in a trie that we could traverse as we talked the individual trees we were matching. It performed well, but IIRC the interface was hacky (I had to bolt it specifically onto the way the tree-walker uses pathspecs, and the other pathspec matchers didn't benefit at all).

Quoted 4 lines
Quoted 1 line

I can probably dig it up if anybody's interested in looking at it.

If it's not too much trouble I'd find it interesting, but I likely won't do anything with it any time soon.

The patches are from 2014 (yikes). I forward ported them to the current tip of master, but beware:

- it fails a test in t4010 matching a gitlink with a trailing "/" in the pathspec (which I think just didn't exist back then). I suspect this is a simple bug (the trie has to record a bit for "this must be a dir", and it probably just doesn't understand gitlinks properly).

- the original renamed diff_tree_sha1() to diff_tree_sha1_recurse(), which we then call internally while recursing (to pass the trie cursor). And then top-level calls use a diff_tree_sha1() wrapper that kicks off the traversal (to avoid disrupting other callers).

When forward-porting, those functions got renamed and reorganized to handle multi-parent diffs. I was able to just use the internal ll_diff_tree_paths() as my "recurse" version, but I'm not 100% sure that's right (e.g., should internal calls to ll_diff_tree_oid also grow a cursor parameter?)

- a few bits had to be ported from the 2-parent case to handle n-parents. It _looked_ pretty trivial as I did it, but it's entirely possible there are gotchas. Tests seem to pass, though (there are no specific tests for finding changes in a merge here, but all tree diffs should be using the new code).

- there are a few basic tests included, but this was never run in any kind of production setting. Caveat emptor. :)

Quoted 3 lines

One of the PCREv2 experiments I had very early WIP work towards was to create a search index for commit messages, contents etc. and stick it in something similar to the --changed-paths part of the commit-graph.

Yeah, to some degree --change-paths may mitigate this, since for a series of simple pathspecs we'd generate the bloom filter once and then get O(1) matching per commit.

My ulterior motive with all of this was to plug it into our custom "blame-tree" (i.e., which commit last-touched each path), with the idea being that we'd start with a big pathspec, and then shrink it as we find answers. Removing an item from a bloom filter is awkward, but possible.

Anyway, here are the patches.

[1/3]: pathspec: add optional trie index
[2/3]: pathspec: turn on tries when appropriate
[3/3]: tree-diff: use pathspec tries

 Makefile                      |   1 +
 pathspec.c                    | 127 ++++++++++++++++++++++++++++++++++
 pathspec.h                    |  13 ++++
 t/helper/test-pathspec.c      |  96 +++++++++++++++++++++++++
 t/helper/test-tool.c          |   1 +
 t/helper/test-tool.h          |   1 +
 t/perf/p4002-diff-pathspec.sh |  26 +++++++
 t/t6137-pathspec-trie.sh      |  57 +++++++++++++++
 tree-diff.c                   | 102 +++++++++++++++++++++++----
 9 files changed, 411 insertions(+), 13 deletions(-)
 create mode 100644 t/helper/test-pathspec.c
 create mode 100755 t/perf/p4002-diff-pathspec.sh
 create mode 100755 t/t6137-pathspec-trie.sh

-Peff

#5 lore raw

PATCH 1/3 pathspec: add optional trie index

When checking if a path matches our pathspec list, in the worst case we walk the whole list linearly to check each pathspec. As a result, an operation like a tree diff does O(paths * pathspecs) comparisons. If we assume that the number of pathspecs may approach the number of paths, this is O(n^2) in the number of paths. We have to do it this way in the general case because pathspecs may be arbitrary fnmatch expressions, and we cannot create any meaningful index.

However, it is common that all of the pathspecs are vanilla prefixes (e.g., "git.c" or "Documentation/") that do not use any wildcards or magic features. In such a case, we can index the pathspec list to give us faster lookups.

The simplest solution would be to keep the pathspec list sorted, and use a binary search over the entries for each path. This turns out to be rather tricky, though, as we are not looking for an exact match in the list. We must also find prefix matches, and take care to handle trailing slashes for directories.

Instead, this patch introduces a pathspec_trie struct, which represents the pathspecs as a graph of entries. For example, the pathspec list ["foo/bar", "foo/baz/"] would be represented as:

        (bar, terminal)
       /

(foo)
\
(baz, terminal, must_be_dir)

To see if a path "foo/eek" is covered by the pathspec, we walk the trie, matching "foo" but ultimately seeing that "eek" is not mentioned.

This provides us with two optimizations:

1. The children of each trie node are simple strings representing directory components. This means we can sort and binary-search them, giving us logarithmic lookups (but note that rather than one log lookup on the whole pathname, it is a sequence of smaller log lookups on components of the pathname).

2. Many users of the pathspec code do not feed full pathnames, but instead are walking a tree hierarchy themselves, and limiting the walk as they go. They can walk the trie at the same time, meanining they avoid looking repeatedly at parts of the pathspec we already know are matched. The current code, by contrast, will repeatedly match "foo/" against each pathspec when looking at "foo/bar", "foo/baz", etc.

Note that this patch just introduces the data structure; the code is not used anywhere yet.

---
 Makefile                 |   1 +
 pathspec.c               | 125 +++++++++++++++++++++++++++++++++++++++
 pathspec.h               |  12 ++++
 t/helper/test-pathspec.c |  98 ++++++++++++++++++++++++++++++
 t/helper/test-tool.c     |   1 +
 t/helper/test-tool.h     |   1 +
 t/t6137-pathspec-trie.sh |  57 ++++++++++++++++++
 7 files changed, 295 insertions(+)
 create mode 100644 t/helper/test-pathspec.c
 create mode 100755 t/t6137-pathspec-trie.sh
Makefile +1 -0
index c3565fc0f8..675f51a330 100644
@@ -726,6 +726,7 @@ TEST_BUILTINS_OBJS += test-online-cpus.o
 TEST_BUILTINS_OBJS += test-parse-options.o
 TEST_BUILTINS_OBJS += test-parse-pathspec-file.o
 TEST_BUILTINS_OBJS += test-path-utils.o
+TEST_BUILTINS_OBJS += test-pathspec.o
 TEST_BUILTINS_OBJS += test-pcre2-config.o
 TEST_BUILTINS_OBJS += test-pkt-line.o
 TEST_BUILTINS_OBJS += test-prio-queue.o
pathspec.c +125 -0

128 lines not on this page · raw message

pathspec.h +12 -0
index fceebb876f..15c9244d08 100644
@@ -22,6 +22,14 @@ struct index_state;
 
 #define PATHSPEC_ONESTAR 1	/* the pathspec pattern satisfies GFNM_ONESTAR */
 
+struct pathspec_trie {
+	struct pathspec_trie **entries;
+	int nr, alloc;
+	unsigned terminal:1,
+		 must_be_dir:1;
+	char path[FLEX_ARRAY];
+};
+
 /**
  * See glossary-context.txt for the syntax of pathspec.
  * In memory, a pathspec set is represented by "struct pathspec" and is
@@ -172,4 +180,8 @@ int match_pathspec_attrs(struct index_state *istate,
 			 const char *name, int namelen,
 			 const struct pathspec_item *item);
 
+struct pathspec_trie *build_pathspec_trie(const struct pathspec *);
+int pathspec_trie_lookup(const struct pathspec_trie *pst,
+			 const char *path, size_t len);
+
 #endif /* PATHSPEC_H */
t/helper/test-pathspec.c new file +98 -0

98 lines not on this page · raw message

t/helper/test-tool.c +1 -0
index c5bd0c6d4c..c5dc6153e0 100644
@@ -47,6 +47,7 @@ static struct test_cmd cmds[] = {
 	{ "parse-options", cmd__parse_options },
 	{ "parse-pathspec-file", cmd__parse_pathspec_file },
 	{ "path-utils", cmd__path_utils },
+	{ "pathspec", cmd__pathspec },
 	{ "pcre2-config", cmd__pcre2_config },
 	{ "pkt-line", cmd__pkt_line },
 	{ "prio-queue", cmd__prio_queue },
t/helper/test-tool.h +1 -0
index e8069a3b22..878aba1bc8 100644
@@ -35,6 +35,7 @@ int cmd__oidmap(int argc, const char **argv);
 int cmd__online_cpus(int argc, const char **argv);
 int cmd__parse_options(int argc, const char **argv);
 int cmd__parse_pathspec_file(int argc, const char** argv);
+int cmd__pathspec(int argc, const char **argv);
 int cmd__path_utils(int argc, const char **argv);
 int cmd__pcre2_config(int argc, const char **argv);
 int cmd__pkt_line(int argc, const char **argv);
t/t6137-pathspec-trie.sh new file +57 -0

57 lines not on this page · raw message

Signature · 1 line
2.32.0.359.g3de86e008e.dirty
#6 lore raw

PATCH 2/3 pathspec: turn on tries when appropriate

In reply to Jeff King

An earlier commit introduced pathspec_tries, but we did not actually generate them by default. This patch causes us to do so when it is possible (i.e., when no wildcards or other pathspec magic are in use). This doesn't actually do anything yet, though, as none of the pathspec users have learned to make use of the tries.

We embed the pathspec_trie directly inside the "struct pathspec". This is not strictly necessary, as once created, the trie does not depend on the original pathspec. However, since the intended use is to optimize existing pathspec callers, passing the trie around as part of the pathspec will minimize disruption to the call chain.

---
 pathspec.c               | 2 ++
 pathspec.h               | 1 +
 t/helper/test-pathspec.c | 6 ++----
 3 files changed, 5 insertions(+), 4 deletions(-)
pathspec.c +2 -0
index 24a24f627e..435dfd3117 100644
@@ -639,6 +639,8 @@ void parse_pathspec(struct pathspec *pathspec,
 			BUG("PATHSPEC_MAXDEPTH_VALID and PATHSPEC_KEEP_ORDER are incompatible");
 		QSORT(pathspec->items, pathspec->nr, pathspec_item_cmp);
 	}
+
+	pathspec->trie = build_pathspec_trie(pathspec);
 }
 
 void parse_pathspec_file(struct pathspec *pathspec, unsigned magic_mask,
pathspec.h +1 -0
index 15c9244d08..5329c0a6f6 100644
@@ -61,6 +61,7 @@ struct pathspec {
 		} *attr_match;
 		struct attr_check *attr_check;
 	} *items;
+	struct pathspec_trie *trie;
 };
 
 #define GUARD_PATHSPEC(ps, mask) \
t/helper/test-pathspec.c +2 -4
index 3f1b8f1a79..0fb059409b 100644
@@ -63,7 +63,6 @@ static int cmd_trie(const char **argv)
 {
 	const char **specs, **paths;
 	struct pathspec pathspec;
-	struct pathspec_trie *trie;
 
 	paths = specs = argv;
 	while (*paths && strcmp(*paths, "--"))
@@ -72,12 +71,11 @@ static int cmd_trie(const char **argv)
 		*paths++ = NULL;
 
 	parse_pathspec(&pathspec, 0, 0, "", specs);
-	trie = build_pathspec_trie(&pathspec);
-	if (!trie)
+	if (!pathspec.trie)
 		die("unable to make trie from pathspec");
 
 	for (; *paths; paths++) {
-		if (trie_match(trie, *paths))
+		if (trie_match(pathspec.trie, *paths))
 			printf("yes\n");
 		else
 			printf("no\n");
Signature · 1 line
2.32.0.359.g3de86e008e.dirty
#7 lore raw

PATCH 3/3 tree-diff: use pathspec tries

In reply to Jeff King

The tree-diff currently matches each pathspec against every entry of the tree. For the common case of a handful of pathspecs, this is not a big deal. However, if you have a large number of pathspecs, it gets noticeably slow.

Now that we have the pathspec_trie optimization, we can do much better (at least for simple cases without wildcards). Here are numbers for running "git rev-list" with limiting pathspecs of varying sizes, both before and after this patch:

Test origin HEAD
---------------------------------------------------------------

4001.2: size=1       0.12(0.11+0.00)     0.12(0.12+0.00) +0.0%
4001.3: size=2       0.17(0.16+0.00)     0.16(0.15+0.01) -5.9%
4001.4: size=4       0.17(0.17+0.00)     0.17(0.17+0.00) +0.0%
4001.5: size=8       0.21(0.20+0.00)     0.20(0.20+0.00) -4.8%
4001.6: size=16      0.25(0.24+0.00)     0.21(0.20+0.00) -16.0%
4001.7: size=32      0.31(0.31+0.00)     0.21(0.20+0.00) -32.3%
4001.8: size=64      0.43(0.41+0.01)     0.21(0.21+0.00) -51.2%
4001.9: size=128     0.73(0.72+0.00)     0.22(0.21+0.00) -69.9%
4001.10: size=256    2.02(2.02+0.00)     0.37(0.36+0.00) -81.7%
4001.11: size=512    6.78(6.78+0.00)     0.64(0.64+0.00) -90.6%
4001.12: size=1024   23.67(23.67+0.02)   1.22(1.20+0.01) -94.8%

For small pathspecs, we produce no real difference (which is good; we know we are asymptotically better, but we have not regressed our constant factor). Between 16 and 32 pathspecs we start to see some small improvement, and the benefit keeps growing with the number of pathspecs.

Obviously these large-pathspec cases are unusual. But you might use them, for example, if the pathspecs were generated programatically (e.g., if you want the history of all files that are in Documentation/ _now_, not what was historically ever there, you would expand the pathspec at the current tree, and feed the result to rev-list).

---
This commit message is the original from 2014. The perf script got bumped to p4002, but the results today are similar (actually, the "before" code is slower, but perhaps that's because we have a lot more commits in git.git these days).

 t/perf/p4002-diff-pathspec.sh |  26 +++++++++
 tree-diff.c                   | 102 +++++++++++++++++++++++++++++-----
 2 files changed, 115 insertions(+), 13 deletions(-)
 create mode 100755 t/perf/p4002-diff-pathspec.sh
t/perf/p4002-diff-pathspec.sh new file +26 -0
new file mode 100755
index 0000000000..3d35b3579d
@@ -0,0 +1,26 @@
+#!/bin/sh
+
+test_description='diff performance with many pathspecs'
+. ./perf-lib.sh
+
+test_perf_default_repo
+
+sizes='1 2 4 8 16 32 64 128 256 512 1024'
+
+test_expect_success 'create pathspec lists' '
+	git ls-tree --name-only -r HEAD >all &&
+	for i in $sizes; do
+		{
+			printf "%s\n" -- &&
+			head -$i all
+		} >ps-$i
+	done
+'
+
+for i in $sizes; do
+	test_perf "size=$i" "
+		git rev-list HEAD --stdin <ps-$i >/dev/null
+	"
+done
+
+test_done
tree-diff.c +89 -13

177 lines not on this page · raw message

Signature · 1 line
2.32.0.359.g3de86e008e.dirty
#8 lore raw
In reply to Jeff King
Quoted 7 lines · Jeff King
Quoted 3 lines

One of the PCREv2 experiments I had very early WIP work towards was to create a search index for commit messages, contents etc. and stick it in something similar to the --changed-paths part of the commit-graph.

Yeah, to some degree --change-paths may mitigate this, since for a series of simple pathspecs we'd generate the bloom filter once and then get O(1) matching per commit.

The timings for "git rev-list" I shared earlier were with commit-graphs, but not with changed-path filters. They don't seem to help much, though.

I suspect the reason is that they can never give a definite answer. They can only say "probably, yes, this commit is worth looking at". And then we have to do the same slow, linear pathspec match on that commit. And if your pathspec contains virtually every path in the first place, then the answer from the bloom filters will always be "probably, yes".

So I guess they are not really a silver bullet here.

-Peff