parse_pathspec: support prefixing original patterns

This makes 'original' suitable for passing to an external command because all pathspec magic is left in place, provided that the external command understands pathspec. The prefixing is needed because we usually launch a subcommand at worktree's top directory and the subcommand can no longer calculate the prefix itself. This slightly affects the original purpose of 'original' (i.e. reporting). We should report without prefixing. So only turn this flag on when you know you are about to pass the result straight away to an external command. Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Nguyễn Thái Ngọc Duy committed Jul 14, 2013 at 15:35 UTC dad2586a6b30da902d5f1979221eccc474425a7a
2 files changed +12 -1
pathspec.c
+11 -1
@@ -203,7 +203,17 @@ static unsigned prefix_pathspec(struct pathspec_item *item,
203 else
204 match = prefix_path(prefix, prefixlen, copyfrom);
205 *raw = item->match = match;
206 - item->original = elt;
206 + /*
207 + * Prefix the pathspec (keep all magic) and assign to
208 + * original. Useful for passing to another command.
209 + */
210 + if (flags & PATHSPEC_PREFIX_ORIGIN) {
211 + struct strbuf sb = STRBUF_INIT;
212 + strbuf_add(&sb, elt, copyfrom - elt);
213 + strbuf_addstr(&sb, match);
214 + item->original = strbuf_detach(&sb, NULL);
215 + } else
216 + item->original = elt;
217 item->len = strlen(item->match);
218
219 if ((flags & PATHSPEC_STRIP_SUBMODULE_SLASH_CHEAP) &&
pathspec.h
+1
@@ -43,6 +43,7 @@ struct pathspec {
43 * safer than _SLASH_CHEAP and also more expensive.
44 */
45 #define PATHSPEC_STRIP_SUBMODULE_SLASH_EXPENSIVE (1<<5)
46 +#define PATHSPEC_PREFIX_ORIGIN (1<<6)
47
48 extern int init_pathspec(struct pathspec *, const char **);
49 extern void parse_pathspec(struct pathspec *pathspec,