parse_pathspec: support prefixing original patterns
authorNguyễn Thái Ngọc Duy <pclouds@gmail.com>
Sun, 14 Jul 2013 08:35:35 +0000 (15:35 +0700)
committerJunio C Hamano <gitster@pobox.com>
Mon, 15 Jul 2013 17:56:07 +0000 (10:56 -0700)
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>
pathspec.c
pathspec.h
index e2a4f910f9b46f27518a14566c4d8eaaea03c58b..ba6408a74cff0e0fe0028e98e508bd8db4e2dc33 100644 (file)
@@ -203,7 +203,17 @@ static unsigned prefix_pathspec(struct pathspec_item *item,
        else
                match = prefix_path(prefix, prefixlen, copyfrom);
        *raw = item->match = match;
        else
                match = prefix_path(prefix, prefixlen, copyfrom);
        *raw = item->match = match;
-       item->original = elt;
+       /*
+        * Prefix the pathspec (keep all magic) and assign to
+        * original. Useful for passing to another command.
+        */
+       if (flags & PATHSPEC_PREFIX_ORIGIN) {
+               struct strbuf sb = STRBUF_INIT;
+               strbuf_add(&sb, elt, copyfrom - elt);
+               strbuf_addstr(&sb, match);
+               item->original = strbuf_detach(&sb, NULL);
+       } else
+               item->original = elt;
        item->len = strlen(item->match);
 
        if ((flags & PATHSPEC_STRIP_SUBMODULE_SLASH_CHEAP) &&
        item->len = strlen(item->match);
 
        if ((flags & PATHSPEC_STRIP_SUBMODULE_SLASH_CHEAP) &&
index 450fc030349bf90e59a156b19648ef4edc806eb8..2e427d7f4c36bce6285d677cb29d911b110c2d14 100644 (file)
@@ -43,6 +43,7 @@ struct pathspec {
  * safer than _SLASH_CHEAP and also more expensive.
  */
 #define PATHSPEC_STRIP_SUBMODULE_SLASH_EXPENSIVE (1<<5)
  * safer than _SLASH_CHEAP and also more expensive.
  */
 #define PATHSPEC_STRIP_SUBMODULE_SLASH_EXPENSIVE (1<<5)
+#define PATHSPEC_PREFIX_ORIGIN (1<<6)
 
 extern int init_pathspec(struct pathspec *, const char **);
 extern void parse_pathspec(struct pathspec *pathspec,
 
 extern int init_pathspec(struct pathspec *, const char **);
 extern void parse_pathspec(struct pathspec *pathspec,