Skip to contentSkip to conversation

Exclude pathspec

PATCH In review Nguyễn Thái Ngọc Duy 2 participants 5 messages Jun 15, 2016 Documentation

lore
Share & tools
lore
#0 lore raw

cover Exclude pathspec

A WIP patch [1] was posted a while ago. This is the polished version with tests and stuff. '!' is used as the shortcut instead of '-'. You can now say "select this path except this subpath..." for nearly all commands that take pathspec.

[1] http://thread.gmane.org/gmane.comp.version-control.git/234835/focus=238062

Nguyễn Thái Ngọc Duy (3):
glossary-content.txt: rephrase magic signature part
Support pathspec magic :(exclude) and its short form :! pathspec.c: support adding prefix magic to a pathspec with mnemonic magic

 Documentation/glossary-content.txt   |  34 +++----
 builtin/add.c                        |   5 +-
 dir.c                                |  47 +++++++--
 pathspec.c                           |  37 ++++---
 pathspec.h                           |   4 +-
 t/t6132-pathspec-exclude.sh (new +x) | 184 +++++++++++++++++++++++++++++++++++
 tree-walk.c                          |  83 +++++++++++++++-
 7 files changed, 354 insertions(+), 40 deletions(-)
Signature · 1 line
1.8.5.1.25.g8667982
#1 lore raw

PATCH 1/3 glossary-content.txt: rephrase magic signature part

---
 Documentation/glossary-content.txt | 29 ++++++++++++-----------------
 1 file changed, 12 insertions(+), 17 deletions(-)
Documentation/glossary-content.txt +12 -17
index aa1c888..b7e7ab5 100644
@@ -323,24 +323,26 @@ including Documentation/chapter_1/figure_1.jpg.
 A pathspec that begins with a colon `:` has special meaning.  In the
 short form, the leading colon `:` is followed by zero or more "magic
 signature" letters (which optionally is terminated by another colon `:`),
-and the remainder is the pattern to match against the path. The optional
-colon that terminates the "magic signature" can be omitted if the pattern
-begins with a character that cannot be a "magic signature" and is not a
-colon.
+and the remainder is the pattern to match against the path.
+The "magic signature" consists of ASCII symbols that are neither
+alphanumeric, glob, regex special charaters nor colon.
+The optional colon that terminates the "magic signature" can be
+omitted if the pattern begins with a character that does not belong to
+"magic signature" symbol set and is not a colon.
 +
 In the long form, the leading colon `:` is followed by a open
 parenthesis `(`, a comma-separated list of zero or more "magic words",
 and a close parentheses `)`, and the remainder is the pattern to match
 against the path.
 +
-The "magic signature" consists of an ASCII symbol that is not
-alphanumeric.
+A pathspec with only a colon means "there is no pathspec". This form
+should not be combined with other pathspec.
 +
 --
-top `/`;;
-	The magic word `top` (mnemonic: `/`) makes the pattern match
-	from the root of the working tree, even when you are running
-	the command from inside a subdirectory.
+top;;
+	The magic word `top` (magic signature: `/`) makes the pattern
+	match from the root of the working tree, even when you are
+	running the command from inside a subdirectory.
 
 literal;;
 	Wildcards in the pattern such as `*` or `?` are treated
@@ -378,13 +380,6 @@ full pathname may have special meaning:
 +
 Glob magic is incompatible with literal magic.
 --
-+
-Currently only the slash `/` is recognized as the "magic signature",
-but it is envisioned that we will support more types of magic in later
-versions of Git.
-+
-A pathspec with only a colon means "there is no pathspec". This form
-should not be combined with other pathspec.
 
 [[def_parent]]parent::
 	A <<def_commit_object,commit object>> contains a (possibly empty) list
Signature · 1 line
1.8.5.1.25.g8667982
#2 lore raw

PATCH 2/3 Support pathspec magic :(exclude) and its short form :!

In reply to Nguyễn Thái Ngọc Duy
---
 Documentation/glossary-content.txt   |   5 +
 builtin/add.c                        |   5 +-
 dir.c                                |  47 +++++++--
 pathspec.c                           |   9 +-
 pathspec.h                           |   4 +-
 t/t6132-pathspec-exclude.sh (new +x) | 184 +++++++++++++++++++++++++++++++++++
 tree-walk.c                          |  83 +++++++++++++++-
 7 files changed, 324 insertions(+), 13 deletions(-)
 create mode 100755 t/t6132-pathspec-exclude.sh
Documentation/glossary-content.txt +5 -0
index b7e7ab5..378306f 100644
@@ -379,6 +379,11 @@ full pathname may have special meaning:
  - Other consecutive asterisks are considered invalid.
 +
 Glob magic is incompatible with literal magic.
+
+exclude;;
+	After a path matches any non-exclude pathspec, it will be run
+	through all exclude pathspec (magic signature: `!`). If it
+	matches, the path is ignored.
 --
 
 [[def_parent]]parent::
builtin/add.c +4 -1
index 226f758..0df73ae 100644
@@ -540,10 +540,13 @@ int cmd_add(int argc, const char **argv, const char *prefix)
 			       PATHSPEC_FROMTOP |
 			       PATHSPEC_LITERAL |
 			       PATHSPEC_GLOB |
-			       PATHSPEC_ICASE);
+			       PATHSPEC_ICASE |
+			       PATHSPEC_EXCLUDE);
 
 		for (i = 0; i < pathspec.nr; i++) {
 			const char *path = pathspec.items[i].match;
+			if (pathspec.items[i].magic & PATHSPEC_EXCLUDE)
+				continue;
 			if (!seen[i] &&
 			    ((pathspec.items[i].magic &
 			      (PATHSPEC_GLOB | PATHSPEC_ICASE)) ||
dir.c +41 -6

92 lines not on this page · raw message

pathspec.c +8 -1
index 87b3b82..4e6a727 100644
@@ -71,6 +71,7 @@ static struct pathspec_magic {
 	{ PATHSPEC_LITERAL,   0, "literal" },
 	{ PATHSPEC_GLOB,   '\0', "glob" },
 	{ PATHSPEC_ICASE,  '\0', "icase" },
+	{ PATHSPEC_EXCLUDE, '!', "exclude" },
 };
 
 /*
@@ -355,7 +356,7 @@ void parse_pathspec(struct pathspec *pathspec,
 {
 	struct pathspec_item *item;
 	const char *entry = argv ? *argv : NULL;
-	int i, n, prefixlen;
+	int i, n, prefixlen, nr_exclude = 0;
 
 	memset(pathspec, 0, sizeof(*pathspec));
 
@@ -412,6 +413,8 @@ void parse_pathspec(struct pathspec *pathspec,
 		if ((flags & PATHSPEC_LITERAL_PATH) &&
 		    !(magic_mask & PATHSPEC_LITERAL))
 			item[i].magic |= PATHSPEC_LITERAL;
+		if (item[i].magic & PATHSPEC_EXCLUDE)
+			nr_exclude++;
 		if (item[i].magic & magic_mask)
 			unsupported_magic(entry,
 					  item[i].magic & magic_mask,
@@ -427,6 +430,10 @@ void parse_pathspec(struct pathspec *pathspec,
 		pathspec->magic |= item[i].magic;
 	}
 
+	if (nr_exclude == n)
+		die(_("There is nothing to exclude from by :(exclude) patterns.\n"
+		      "Perhaps you forgot to add either ':/' or '.' ?"));
+
 
 	if (pathspec->magic & PATHSPEC_MAXDEPTH) {
 		if (flags & PATHSPEC_KEEP_ORDER)
pathspec.h +3 -1
index a75e924..0c11262 100644
@@ -7,12 +7,14 @@
 #define PATHSPEC_LITERAL	(1<<2)
 #define PATHSPEC_GLOB		(1<<3)
 #define PATHSPEC_ICASE		(1<<4)
+#define PATHSPEC_EXCLUDE	(1<<5)
 #define PATHSPEC_ALL_MAGIC	  \
 	(PATHSPEC_FROMTOP	| \
 	 PATHSPEC_MAXDEPTH	| \
 	 PATHSPEC_LITERAL	| \
 	 PATHSPEC_GLOB		| \
-	 PATHSPEC_ICASE)
+	 PATHSPEC_ICASE		| \
+	 PATHSPEC_EXCLUDE)
 
 #define PATHSPEC_ONESTAR 1	/* the pathspec pattern satisfies GFNM_ONESTAR */
 
t/t6132-pathspec-exclude.sh new file +184 -0

184 lines not on this page · raw message

tree-walk.c +79 -4

104 lines not on this page · raw message

Signature · 1 line
1.8.5.1.25.g8667982
#3 lore raw

PATCH 3/3 pathspec.c: support adding prefix magic to a pathspec with mnemonic magic

In reply to Nguyễn Thái Ngọc Duy

Back in 233c3e6 (parse_pathspec: preserve prefix length via PATHSPEC_PREFIX_ORIGIN - 2013-07-14), parse_pathspec() is taught to save prefix length as a dynamic magic. This is needed when the pathspec is passed to another process and and prefix lenght would be lost.

Back then we support two cases. If the pathspec is normal, e.g. "abc", we simply add the prefix to become ":(prefix:2)abc". If the pathspec contains long magic, e.g. ":(foo,bar)abc" then we turn it to ":(foo,bar,prefix:2)abc". We do not support prefixing on short form, because the only supported mnemonic '/' disappears after the the preprocessing steps.

With the introduction of exclude magic with mnemonic '!', we need to add support for the short form case so that ':!abc' becomes ':(exclude,prefix:2)abc'. Without this, it will break

    cd Documentation
    git add -p -- . ':!technical'
---
 pathspec.c | 28 ++++++++++++++++++----------
 1 file changed, 18 insertions(+), 10 deletions(-)
pathspec.c +18 -10
index 4e6a727..2b7f2e2 100644
@@ -74,6 +74,20 @@ static struct pathspec_magic {
 	{ PATHSPEC_EXCLUDE, '!', "exclude" },
 };
 
+static void prefix_short_magic(struct strbuf *sb, int prefixlen,
+			       unsigned short_magic)
+{
+	int i;
+	strbuf_addstr(sb, ":(");
+	for (i = 0; i < ARRAY_SIZE(pathspec_magic); i++)
+		if (short_magic & pathspec_magic[i].bit) {
+			if (sb->buf[sb->len - 1] != '(')
+				strbuf_addch(sb, ',');
+			strbuf_addstr(sb, pathspec_magic[i].name);
+		}
+	strbuf_addf(sb, ",prefix:%d)", prefixlen);
+}
+
 /*
  * Take an element of a pathspec and check for magic signatures.
  * Append the result to the prefix. Return the magic bitmap.
@@ -233,22 +247,16 @@ static unsigned prefix_pathspec(struct pathspec_item *item,
 	 */
 	if (flags & PATHSPEC_PREFIX_ORIGIN) {
 		struct strbuf sb = STRBUF_INIT;
-		const char *start = elt;
 		if (prefixlen && !literal_global) {
 			/* Preserve the actual prefix length of each pattern */
 			if (short_magic)
-				die("BUG: prefixing on short magic is not supported");
+				prefix_short_magic(&sb, prefixlen, short_magic);
 			else if (long_magic_end) {
-				strbuf_add(&sb, start, long_magic_end - start);
-				strbuf_addf(&sb, ",prefix:%d", prefixlen);
-				start = long_magic_end;
-			} else {
-				if (*start == ':')
-					start++;
+				strbuf_add(&sb, elt, long_magic_end - elt);
+				strbuf_addf(&sb, ",prefix:%d)", prefixlen);
+			} else
 				strbuf_addf(&sb, ":(prefix:%d)", prefixlen);
-			}
 		}
-		strbuf_add(&sb, start, copyfrom - start);
 		strbuf_addstr(&sb, match);
 		item->original = strbuf_detach(&sb, NULL);
 	} else
Signature · 1 line
1.8.5.1.25.g8667982