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

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

From
Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Date
Dec 6, 2013, 07:30 UTC
Message-ID
<1386315049-19097-4-git-send-email-pclouds@gmail.com>
In-Reply-To
<1386315049-19097-1-git-send-email-pclouds@gmail.com>

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'
Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 pathspec.c | 28 ++++++++++++++++++----------
 1 file changed, 18 insertions(+), 10 deletions(-)
diff --git a/pathspec.c b/pathspec.c
index 4e6a727..2b7f2e2 100644
--- a/pathspec.c
+++ b/pathspec.c
@@ -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
-- 
1.8.5.1.25.g8667982
Previous: Nguyễn Thái Ngọc DuyNext: Junio C Hamano
Message 4 of 5 in “Exclude pathspec”
  1. 0/3 Exclude pathspecNguyễn Thái Ngọc Duy, Dec 6, 2013
  2. 1/3 glossary-content.txt: rephrase magic signature partNguyễn Thái Ngọc Duy, Dec 6, 2013
  3. 2/3 Support pathspec magic :(exclude) and its short form :!Nguyễn Thái Ngọc Duy, Dec 6, 2013
  4. 3/3 pathspec.c: support adding prefix magic to a pathspec with mnemonic magicNguyễn Thái Ngọc Duy, Dec 6, 2013
  5. Junio C HamanoDec 6, 2013

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.