{"thread":{"id":"22050","subject":"[PATCH] Add --path-prefix option to git-fast-import","startedAt":"2009-12-29T12:51:23Z","lastAt":"2009-12-30T08:17:39Z","messageCount":4,"participants":["Gisle Aas","Sverre Rabbelier","Shawn O. Pearce"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"130453","messageId":"1262091083-25401-1-git-send-email-gisle.aas@it.uib.no","threadId":"22050","inReplyTo":null,"subject":"[PATCH] Add --path-prefix option to git-fast-import","fromName":"Gisle Aas","fromEmail":"gisle.aas@it.uib.no","sentAt":"2009-12-29T12:51:23Z","receivedAt":"2009-12-29T12:51:23Z","isPatch":true,"sender":{"key":"gisle.aas@it.uib.no","avatar":null},"body":"From: Gisle Aas <gisle@aas.no>\n\nI found this useful when import multiple external repositories to be merged\ninto a single git repo.  Not having the files be renamed during the merge\nmade it easier to follow the history of the individual files.\n\nSigned-off-by: Gisle Aas <gisle@aas.no>\n---\n Documentation/git-fast-import.txt |    6 ++++++\n fast-import.c                     |   24 ++++++++++++++++++++++++\n 2 files changed, 30 insertions(+), 0 deletions(-)\n\ndiff --git a/Documentation/git-fast-import.txt b/Documentation/git-fast-import.txt\nindex 288032c..b8f9593 100644\n--- a/Documentation/git-fast-import.txt\n+++ b/Documentation/git-fast-import.txt\n@@ -58,6 +58,12 @@ OPTIONS\n \tMaximum number of branches to maintain active at once.\n \tSee ``Memory Utilization'' below for details.  Default is 5.\n \n+--path-prefix=<str>:\n+\tPrepend the given prefix to all the paths imported.\n+\tThis can be used to import stuff into a subdirectory\n+\tof where the original files where located.  Most likely\n+\tyou want <str> to end with a slash.\n+\n --export-marks=<file>::\n \tDumps the internal marks table to <file> when complete.\n \tMarks are written one per line as `:markid SHA-1`.\ndiff --git a/fast-import.c b/fast-import.c\nindex dd3c99d..32b0d70 100644\n--- a/fast-import.c\n+++ b/fast-import.c\n@@ -351,6 +351,8 @@ static struct recent_command *rc_free;\n static unsigned int cmd_save = 100;\n static uintmax_t next_mark;\n static struct strbuf new_data = STRBUF_INIT;\n+static const char *path_prefix;\n+static size_t path_prefix_len;\n \n static void write_branch_report(FILE *rpt, struct branch *b)\n {\n@@ -1860,6 +1862,16 @@ static void load_branch(struct branch *b)\n \t}\n }\n \n+static const char *path_prefix_prepend(struct strbuf *sb, const char *p)\n+{\n+\tif (p != sb->buf) {\n+\t    strbuf_reset(sb);\n+\t    strbuf_addstr(sb, p);\n+\t}\n+\tstrbuf_insert(sb, 0, path_prefix, path_prefix_len);\n+\treturn sb->buf;\n+}\n+\n static void file_change_m(struct branch *b)\n {\n \tconst char *p = command_buf.buf + 2;\n@@ -1909,6 +1921,8 @@ static void file_change_m(struct branch *b)\n \t\t\tdie(\"Garbage after path in: %s\", command_buf.buf);\n \t\tp = uq.buf;\n \t}\n+\tif (path_prefix)\n+\t    p = path_prefix_prepend(&uq, p);\n \n \tif (S_ISGITLINK(mode)) {\n \t\tif (inline_data)\n@@ -1961,6 +1975,8 @@ static void file_change_d(struct branch *b)\n \t\t\tdie(\"Garbage after path in: %s\", command_buf.buf);\n \t\tp = uq.buf;\n \t}\n+\tif (path_prefix)\n+\t    p = path_prefix_prepend(&uq, p);\n \ttree_content_remove(&b->branch_tree, p, NULL);\n }\n \n@@ -1984,6 +2000,8 @@ static void file_change_cr(struct branch *b, int rename)\n \t\tstrbuf_add(&s_uq, s, endp - s);\n \t}\n \ts = s_uq.buf;\n+\tif (path_prefix)\n+\t    s = path_prefix_prepend(&s_uq, s);\n \n \tendp++;\n \tif (!*endp)\n@@ -1996,6 +2014,8 @@ static void file_change_cr(struct branch *b, int rename)\n \t\t\tdie(\"Garbage after dest in: %s\", command_buf.buf);\n \t\td = d_uq.buf;\n \t}\n+\tif (path_prefix)\n+\t    d = path_prefix_prepend(&d_uq, d);\n \n \tmemset(&leaf, 0, sizeof(leaf));\n \tif (rename)\n@@ -2523,6 +2543,10 @@ int main(int argc, const char **argv)\n \t\t\tif (max_depth > MAX_DEPTH)\n \t\t\t\tdie(\"--depth cannot exceed %u\", MAX_DEPTH);\n \t\t}\n+\t\telse if (!prefixcmp(a, \"--path-prefix=\")) {\n+\t\t\tpath_prefix = a + 14;\n+\t\t\tpath_prefix_len = strlen(path_prefix);\n+\t\t}\n \t\telse if (!prefixcmp(a, \"--active-branches=\"))\n \t\t\tmax_active_branches = strtoul(a + 18, NULL, 0);\n \t\telse if (!prefixcmp(a, \"--import-marks=\"))\n-- \n1.6.6.rc4.12.g269e7\n"},{"id":"130454","messageId":"fabb9a1e0912290606ib1f2e46y1ddbae9aa68e5194@mail.gmail.com","threadId":"22050","inReplyTo":"1262091083-25401-1-git-send-email-gisle.aas@it.uib.no","subject":"Re: [PATCH] Add --path-prefix option to git-fast-import","fromName":"Sverre Rabbelier","fromEmail":"srabbelier@gmail.com","sentAt":"2009-12-29T14:06:16Z","receivedAt":"2009-12-29T14:06:16Z","isPatch":true,"sender":{"key":"srabbelier@gmail.com","avatar":"https://avatars.githubusercontent.com/u/3098?v=4"},"body":"Heya,\n\nOn Tue, Dec 29, 2009 at 06:51, Gisle Aas <gisle.aas@it.uib.no> wrote:\n> +static const char *path_prefix_prepend(struct strbuf *sb, const char *p)\n> +{\n> +       if (p != sb->buf) {\n> +           strbuf_reset(sb);\n> +           strbuf_addstr(sb, p);\n> +       }\n> +       strbuf_insert(sb, 0, path_prefix, path_prefix_len);\n> +       return sb->buf;\n> +}\n> +\n>  static void file_change_m(struct branch *b)\n>  {\n>        const char *p = command_buf.buf + 2;\n> @@ -1909,6 +1921,8 @@ static void file_change_m(struct branch *b)\n>                        die(\"Garbage after path in: %s\", command_buf.buf);\n>                p = uq.buf;\n>        }\n> +       if (path_prefix)\n> +           p = path_prefix_prepend(&uq, p);\n\nYou could reduce the size of this change by having path_prefix_prepend\ncheck for path_prefix and just do nothing if it is not set.\n\n-- \nCheers,\n\nSverre Rabbelier\n"},{"id":"130462","messageId":"20091229150844.GC6152@spearce.org","threadId":"22050","inReplyTo":"1262091083-25401-1-git-send-email-gisle.aas@it.uib.no","subject":"Re: [PATCH] Add --path-prefix option to git-fast-import","fromName":"Shawn O. Pearce","fromEmail":"spearce@spearce.org","sentAt":"2009-12-29T15:08:44Z","receivedAt":"2009-12-29T15:08:44Z","isPatch":true,"sender":{"key":"spearce@spearce.org","avatar":"https://avatars.githubusercontent.com/u/34844?v=4"},"body":"Gisle Aas <gisle.aas@it.uib.no> wrote:\n> I found this useful when import multiple external repositories to be merged\n> into a single git repo.  Not having the files be renamed during the merge\n> made it easier to follow the history of the individual files.\n> \n> Signed-off-by: Gisle Aas <gisle@aas.no>\n> ---\n>  Documentation/git-fast-import.txt |    6 ++++++\n>  fast-import.c                     |   24 ++++++++++++++++++++++++\n>  2 files changed, 30 insertions(+), 0 deletions(-)\n\nInteresting.  Test cases?\n \n> +static const char *path_prefix_prepend(struct strbuf *sb, const char *p)\n> +{\n> +\tif (p != sb->buf) {\n> +\t    strbuf_reset(sb);\n> +\t    strbuf_addstr(sb, p);\n> +\t}\n\nI'd be a bit happier about the change if you could check not only\nthat p != sb->buf, but that p is not within sb->buf + sb->alloc.\n\nI can't remember if all of the cases below are safe such that\nany time you call the function with a p that p isn't pointing to\nsomething within the strbuf you are handing in.\n\n-- \nShawn.\n"},{"id":"130528","messageId":"6B75CF25-4EE4-4475-975D-BB1F9A5B8CC2@it.uib.no","threadId":"22050","inReplyTo":"fabb9a1e0912290606ib1f2e46y1ddbae9aa68e5194@mail.gmail.com","subject":"Re: [PATCH] Add --path-prefix option to git-fast-import","fromName":"Gisle Aas","fromEmail":"gisle.aas@it.uib.no","sentAt":"2009-12-30T08:17:39Z","receivedAt":"2009-12-30T08:17:39Z","isPatch":true,"sender":{"key":"gisle.aas@it.uib.no","avatar":null},"body":"On Dec 29, 2009, at 15:06, Sverre Rabbelier wrote:\n\n> Heya,\n> \n> On Tue, Dec 29, 2009 at 06:51, Gisle Aas <gisle.aas@it.uib.no> wrote:\n>> +static const char *path_prefix_prepend(struct strbuf *sb, const char *p)\n>> +{\n>> +       if (p != sb->buf) {\n>> +           strbuf_reset(sb);\n>> +           strbuf_addstr(sb, p);\n>> +       }\n>> +       strbuf_insert(sb, 0, path_prefix, path_prefix_len);\n>> +       return sb->buf;\n>> +}\n>> +\n>>  static void file_change_m(struct branch *b)\n>>  {\n>>        const char *p = command_buf.buf + 2;\n>> @@ -1909,6 +1921,8 @@ static void file_change_m(struct branch *b)\n>>                        die(\"Garbage after path in: %s\", command_buf.buf);\n>>                p = uq.buf;\n>>        }\n>> +       if (path_prefix)\n>> +           p = path_prefix_prepend(&uq, p);\n> \n> You could reduce the size of this change by having path_prefix_prepend\n> check for path_prefix and just do nothing if it is not set.\n\nI felt the explict test was better style -- more obvious that nothing happens to p\nwhen there is no path_prefix.\n\n--Gisle"}]}