Skip to content

Commit 1649612

Browse files
pcloudsgitster
authored andcommitted
pathspec.c: support adding prefix magic to a pathspec with mnemonic magic
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent ef79b1f commit 1649612

File tree

1 file changed

+18
-10
lines changed

1 file changed

+18
-10
lines changed

pathspec.c

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,20 @@ static struct pathspec_magic {
7474
{ PATHSPEC_EXCLUDE, '!', "exclude" },
7575
};
7676

77+
static void prefix_short_magic(struct strbuf *sb, int prefixlen,
78+
unsigned short_magic)
79+
{
80+
int i;
81+
strbuf_addstr(sb, ":(");
82+
for (i = 0; i < ARRAY_SIZE(pathspec_magic); i++)
83+
if (short_magic & pathspec_magic[i].bit) {
84+
if (sb->buf[sb->len - 1] != '(')
85+
strbuf_addch(sb, ',');
86+
strbuf_addstr(sb, pathspec_magic[i].name);
87+
}
88+
strbuf_addf(sb, ",prefix:%d)", prefixlen);
89+
}
90+
7791
/*
7892
* Take an element of a pathspec and check for magic signatures.
7993
* Append the result to the prefix. Return the magic bitmap.
@@ -233,22 +247,16 @@ static unsigned prefix_pathspec(struct pathspec_item *item,
233247
*/
234248
if (flags & PATHSPEC_PREFIX_ORIGIN) {
235249
struct strbuf sb = STRBUF_INIT;
236-
const char *start = elt;
237250
if (prefixlen && !literal_global) {
238251
/* Preserve the actual prefix length of each pattern */
239252
if (short_magic)
240-
die("BUG: prefixing on short magic is not supported");
253+
prefix_short_magic(&sb, prefixlen, short_magic);
241254
else if (long_magic_end) {
242-
strbuf_add(&sb, start, long_magic_end - start);
243-
strbuf_addf(&sb, ",prefix:%d", prefixlen);
244-
start = long_magic_end;
245-
} else {
246-
if (*start == ':')
247-
start++;
255+
strbuf_add(&sb, elt, long_magic_end - elt);
256+
strbuf_addf(&sb, ",prefix:%d)", prefixlen);
257+
} else
248258
strbuf_addf(&sb, ":(prefix:%d)", prefixlen);
249-
}
250259
}
251-
strbuf_add(&sb, start, copyfrom - start);
252260
strbuf_addstr(&sb, match);
253261
item->original = strbuf_detach(&sb, NULL);
254262
} else

0 commit comments

Comments
 (0)