git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
blob 3f1b8f1a79b5bf2aa2778d37077d8cd5b1016976 2054 bytes (raw)
name: t/helper/test-pathspec.c 	 # note: path name is non-authoritative(*)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
 
#include "test-tool.h"
#include "pathspec.h"

static const char usage_msg[] =
"test-tool pathspec trie [pathspecs...] -- [paths....]";

/*
 * XXX Yuck. This is a lot of complicated code specific to our test. Even if it
 * runs correctly, we have no real guarantee that the actual trie users are
 * doing it right. And reusing their code is tough, because it happens as part
 * of their own traversals (e.g., we walk the pathspec trie while walking the
 * tree objects themselves).
 *
 * This whole test program should probably go away in favor of directly testing
 * the tree-diff code.
 */
static int trie_match(const struct pathspec_trie *pst,
		      const char *path)
{
	int pathlen = strlen(path);
	int is_dir = 0;

	if (pathlen > 0 && path[pathlen-1] == '/') {
		is_dir = 1;
		pathlen--;
	}

	while (pathlen) {
		const char *slash = memchr(path, '/', pathlen);
		int component_len;
		int pos;

		if (slash)
			component_len = slash - path;
		else
			component_len = pathlen;

		pos = pathspec_trie_lookup(pst, path, component_len);
		if (pos < 0)
			return 0;

		pst = pst->entries[pos];
		path += component_len;
		pathlen -= component_len;

		while (pathlen && *path == '/') {
			path++;
			pathlen--;
		}

		if (pst->terminal) {
			if (!pst->must_be_dir)
				return 1;
			if (pathlen)
				return 1;
			return is_dir;
		}
	}
	return 0;
}

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, "--"))
		paths++;
	if (*paths)
		*paths++ = NULL;

	parse_pathspec(&pathspec, 0, 0, "", specs);
	trie = build_pathspec_trie(&pathspec);
	if (!trie)
		die("unable to make trie from pathspec");

	for (; *paths; paths++) {
		if (trie_match(trie, *paths))
			printf("yes\n");
		else
			printf("no\n");
	}
	return 0;
}

int cmd__pathspec(int argc, const char **argv)
{
	const char *cmd = argv[1];

	if (!cmd)
		usage(usage_msg);
	else if (!strcmp(cmd, "trie"))
		return cmd_trie(argv + 2);
	else
		die("unknown cmd: %s", cmd);
}

debug log:

solving 3f1b8f1a79 ...
found 3f1b8f1a79 in https://public-inbox.org/git/YN4z9cIrph/OBvw6@coredump.intra.peff.net/

applying [1/1] https://public-inbox.org/git/YN4z9cIrph/OBvw6@coredump.intra.peff.net/
diff --git a/t/helper/test-pathspec.c b/t/helper/test-pathspec.c
new file mode 100644
index 0000000000..3f1b8f1a79

Checking patch t/helper/test-pathspec.c...
Applied patch t/helper/test-pathspec.c cleanly.

index at:
100644 3f1b8f1a79b5bf2aa2778d37077d8cd5b1016976	t/helper/test-pathspec.c

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).