{"thread":{"id":"14235","subject":"[PATCH] Teach \"git apply\" to prepend a prefix with \"--root=<root>\"","startedAt":"2008-06-30T23:44:47Z","lastAt":"2008-07-01T20:27:43Z","messageCount":8,"participants":["Johannes Schindelin","Junio C Hamano","H. Peter Anvin","Raimund Bauer"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"81809","messageId":"alpine.DEB.1.00.0807010043440.9925@racer","threadId":"14235","inReplyTo":null,"subject":"[PATCH] Teach \"git apply\" to prepend a prefix with \"--root=<root>\"","fromName":"Johannes Schindelin","fromEmail":"johannes.schindelin@gmx.de","sentAt":"2008-06-30T23:44:47Z","receivedAt":"2008-06-30T23:44:47Z","isPatch":true,"sender":{"key":"johannes.schindelin@gmx.de","avatar":"https://avatars.githubusercontent.com/u/127790?v=4"},"body":"\nWith \"git apply --root=<root>\", all file names in the patch are prepended\nwith <root>.  If a \"-p\" value was given, the paths are stripped _before_\nprepending <root>.\n\nWished for by HPA.\n\nSigned-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>\n---\n\n\tOnly lightly tested.\n\n Documentation/git-apply.txt |    6 +++++-\n builtin-apply.c             |   24 ++++++++++++++++++++++++\n t/t4128-apply-root.sh       |   32 ++++++++++++++++++++++++++++++++\n 3 files changed, 61 insertions(+), 1 deletions(-)\n create mode 100755 t/t4128-apply-root.sh\n\ndiff --git a/Documentation/git-apply.txt b/Documentation/git-apply.txt\nindex c5ee636..7c51830 100644\n--- a/Documentation/git-apply.txt\n+++ b/Documentation/git-apply.txt\n@@ -14,7 +14,7 @@ SYNOPSIS\n \t  [--allow-binary-replacement | --binary] [--reject] [-z]\n \t  [-pNUM] [-CNUM] [--inaccurate-eof] [--recount] [--cached]\n \t  [--whitespace=<nowarn|warn|fix|error|error-all>]\n-\t  [--exclude=PATH] [--verbose] [<patch>...]\n+\t  [--exclude=PATH] [--root=<root>] [--verbose] [<patch>...]\n \n DESCRIPTION\n -----------\n@@ -182,6 +182,10 @@ behavior:\n \tby inspecting the patch (e.g. after editing the patch without\n \tadjusting the hunk headers appropriately).\n \n+--root=<root>::\n+\tPrepend <root> to all filenames.  If a \"-p\" argument was passed, too,\n+\tit is applied before prepending the new root.\n+\n Configuration\n -------------\n \ndiff --git a/builtin-apply.c b/builtin-apply.c\nindex 9fcfe39..d56d913 100644\n--- a/builtin-apply.c\n+++ b/builtin-apply.c\n@@ -58,6 +58,8 @@ static int whitespace_error;\n static int squelch_whitespace_errors = 5;\n static int applied_after_fixing_ws;\n static const char *patch_input_file;\n+static const char *root;\n+static int root_len;\n \n static void parse_whitespace_option(const char *option)\n {\n@@ -340,6 +342,8 @@ static char *find_name(const char *line, char *def, int p_value, int terminate)\n \t\t\t\t */\n \t\t\t\tstrbuf_remove(&name, 0, cp - name.buf);\n \t\t\t\tfree(def);\n+\t\t\t\tif (root)\n+\t\t\t\t\tstrbuf_insert(&name, 0, root, root_len);\n \t\t\t\treturn strbuf_detach(&name, NULL);\n \t\t\t}\n \t\t}\n@@ -378,6 +382,14 @@ static char *find_name(const char *line, char *def, int p_value, int terminate)\n \t\tfree(def);\n \t}\n \n+\tif (root) {\n+\t\tchar *ret = xmalloc(root_len + len + 1);\n+\t\tstrcpy(ret, root);\n+\t\tmemcpy(ret + root_len, start, len);\n+\t\tret[root_len + len] = '\\0';\n+\t\treturn ret;\n+\t}\n+\n \treturn xmemdupz(start, len);\n }\n \n@@ -3240,6 +3252,18 @@ int cmd_apply(int argc, const char **argv, const char *unused_prefix)\n \t\t\toptions |= RECOUNT;\n \t\t\tcontinue;\n \t\t}\n+\t\tif (!strncmp(arg, \"--root=\", strlen(\"--root=\"))) {\n+\t\t\targ += strlen(\"--root=\");\n+\t\t\troot_len = strlen(arg);\n+\t\t\tif (root_len && arg[root_len + 1] != '/') {\n+\t\t\t\tchar *new_root;\n+\t\t\t\troot = new_root = xmalloc(root_len + 2);\n+\t\t\t\tstrcpy(new_root, arg);\n+\t\t\t\tstrcpy(new_root + root_len++, \"/\");\n+\t\t\t} else\n+\t\t\t\troot = arg;\n+\t\t\tcontinue;\n+\t\t}\n \t\tif (0 < prefix_length)\n \t\t\targ = prefix_filename(prefix, prefix_length, arg);\n \ndiff --git a/t/t4128-apply-root.sh b/t/t4128-apply-root.sh\nnew file mode 100755\nindex 0000000..80b5af2\n--- /dev/null\n+++ b/t/t4128-apply-root.sh\n@@ -0,0 +1,32 @@\n+#!/bin/sh\n+\n+test_description='apply same filename'\n+\n+. ./test-lib.sh\n+\n+test_expect_success 'setup' '\n+\n+\tmkdir -p some/sub/dir &&\n+\techo Hello > some/sub/dir/file &&\n+\tgit add some/sub/dir/file\n+\n+'\n+\n+cat > patch << EOF\n+diff a/bla/blub/dir/file b/bla/blub/dir/file\n+--- a/bla/blub/dir/file\n++++ b/bla/blub/dir/file\n+@@ -1,1 +1,1 @@\n+-Hello\n++Bello\n+EOF\n+\n+test_expect_success 'apply --root -p --index' '\n+\n+\tgit apply --root=some/sub -p3 --index patch &&\n+\ttest Bello = $(git show :some/sub/dir/file) &&\n+\ttest Bello = $(cat some/sub/dir/file)\n+\n+'\n+\n+test_done\n-- \n1.5.6.1.297.g148d9\n"},{"id":"81851","messageId":"7vvdzqnemk.fsf@gitster.siamese.dyndns.org","threadId":"14235","inReplyTo":"alpine.DEB.1.00.0807010043440.9925@racer","subject":"Re: [PATCH] Teach \"git apply\" to prepend a prefix with \"--root=<root>\"","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2008-07-01T09:21:55Z","receivedAt":"2008-07-01T09:21:55Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Johannes Schindelin <Johannes.Schindelin@gmx.de> writes:\n\n> With \"git apply --root=<root>\", all file names in the patch are prepended\n> with <root>.  If a \"-p\" value was given, the paths are stripped _before_\n> prepending <root>.\n\n> @@ -182,6 +182,10 @@ behavior:\n>  \tby inspecting the patch (e.g. after editing the patch without\n>  \tadjusting the hunk headers appropriately).\n>  \n> +--root=<root>::\n> +\tPrepend <root> to all filenames.  If a \"-p\" argument was passed, too,\n> +\tit is applied before prepending the new root.\n> +\n\nI looked for precedence of this concept outside git but did not find\nanything that exactly matched.\n\nThe -p<num> came from patch where it is also called --strip=num.  This new\noption --root is about the reverse operation and it is about inserting at\nthe beginning --- it is rerooting, in other words, but then --root is good\nenough and shorter.  mkisofs uses the word \"graft\" when it allows tree\nshifting (enabled with --graft-points), but the word \"graft\" means a\ntotally different thing to us, so we would not want to use that word.\n\nI am not complaining (--root is fine by me), but just thinking aloud,\nhoping somebody's brainwave is provoked while reading this babbling and\ncomes up with a better wording ;-).\n\n> diff --git a/builtin-apply.c b/builtin-apply.c\n> index 9fcfe39..d56d913 100644\n> --- a/builtin-apply.c\n> +++ b/builtin-apply.c\n> @@ -58,6 +58,8 @@ static int whitespace_error;\n> @@ -3240,6 +3252,18 @@ int cmd_apply(int argc, const char **argv, const char *unused_prefix)\n>  \t\t\toptions |= RECOUNT;\n>  \t\t\tcontinue;\n>  \t\t}\n> +\t\tif (!strncmp(arg, \"--root=\", strlen(\"--root=\"))) {\n\nprefixcmp()?\n\n> +\t\t\targ += strlen(\"--root=\");\n> +\t\t\troot_len = strlen(arg);\n> +\t\t\tif (root_len && arg[root_len + 1] != '/') {\n\nAh, being nice not to force trailing slash on users --- how unlike you ;-)\nbut I think you meant \"arg[root_len - 1]\" there...\n\nOther than that, the change looks fine.  Thanks.\n"},{"id":"81884","messageId":"486A55B0.9050404@zytor.com","threadId":"14235","inReplyTo":"7vvdzqnemk.fsf@gitster.siamese.dyndns.org","subject":"Re: [PATCH] Teach \"git apply\" to prepend a prefix with \"--root=<root>\"","fromName":"H. Peter Anvin","fromEmail":"hpa@zytor.com","sentAt":"2008-07-01T16:05:04Z","receivedAt":"2008-07-01T16:05:04Z","isPatch":true,"sender":{"key":"hpa@zytor.com","avatar":null},"body":"Junio C Hamano wrote:\n> \n> The -p<num> came from patch where it is also called --strip=num.  This new\n> option --root is about the reverse operation and it is about inserting at\n> the beginning --- it is rerooting, in other words, but then --root is good\n> enough and shorter.  mkisofs uses the word \"graft\" when it allows tree\n> shifting (enabled with --graft-points), but the word \"graft\" means a\n> totally different thing to us, so we would not want to use that word.\n> \n> I am not complaining (--root is fine by me), but just thinking aloud,\n> hoping somebody's brainwave is provoked while reading this babbling and\n> comes up with a better wording ;-).\n> \n\nThere is an analogous concept in patch(1), it's just implemented by \ncd'ing to a subdirectory first.  ;)\n\nI think --root makes sense as the root of the patch.  The other \nalternative would be --add (by analogy with --strip); the biggest \nadvantage there is that -a, as a short option, isn't used for anything \neither by git-am, git-apply, or patch.\n\nIncidentally, has anyone talked to the patch(1) maintainers about adding \nsupport for the git extensions, like binary patches?  The main reason is \nthat patch(1) is still useful when you have to suffer though fuzzy errors.\n\n\t-hpa\n"},{"id":"81887","messageId":"alpine.LSU.1.00.0807011831130.32725@wbgn129.biozentrum.uni-wuerzburg.de","threadId":"14235","inReplyTo":"7vvdzqnemk.fsf@gitster.siamese.dyndns.org","subject":"Re: [PATCH] Teach \"git apply\" to prepend a prefix with \"--root=<root>\"","fromName":"Johannes Schindelin","fromEmail":"johannes.schindelin@gmx.de","sentAt":"2008-07-01T16:34:20Z","receivedAt":"2008-07-01T16:34:20Z","isPatch":true,"sender":{"key":"johannes.schindelin@gmx.de","avatar":"https://avatars.githubusercontent.com/u/127790?v=4"},"body":"Hi,\n\nOn Tue, 1 Jul 2008, Junio C Hamano wrote:\n\n> Johannes Schindelin <Johannes.Schindelin@gmx.de> writes:\n> \n> > With \"git apply --root=<root>\", all file names in the patch are prepended\n> > with <root>.  If a \"-p\" value was given, the paths are stripped _before_\n> > prepending <root>.\n> \n> > @@ -182,6 +182,10 @@ behavior:\n> >  \tby inspecting the patch (e.g. after editing the patch without\n> >  \tadjusting the hunk headers appropriately).\n> >  \n> > +--root=<root>::\n> > +\tPrepend <root> to all filenames.  If a \"-p\" argument was passed, too,\n> > +\tit is applied before prepending the new root.\n> > +\n> \n> I looked for precedence of this concept outside git but did not find\n> anything that exactly matched.\n> \n> The -p<num> came from patch where it is also called --strip=num.  This \n> new option --root is about the reverse operation and it is about \n> inserting at the beginning --- it is rerooting, in other words, but then \n> --root is good enough and shorter.  mkisofs uses the word \"graft\" when \n> it allows tree shifting (enabled with --graft-points), but the word \n> \"graft\" means a totally different thing to us, so we would not want to \n> use that word.\n> \n> I am not complaining (--root is fine by me), but just thinking aloud, \n> hoping somebody's brainwave is provoked while reading this babbling and \n> comes up with a better wording ;-).\n\nYeah, I am not too happy, but I saw you and hpa on IRC agreeing on that \noption name.  I would have said \"--prefix\", but that is too ambiguous, \nwhat with \"prefix\" already meaning \"path relative to the working directory \nroot\".\n\n> > diff --git a/builtin-apply.c b/builtin-apply.c\n> > index 9fcfe39..d56d913 100644\n> > --- a/builtin-apply.c\n> > +++ b/builtin-apply.c\n> > @@ -58,6 +58,8 @@ static int whitespace_error;\n> > @@ -3240,6 +3252,18 @@ int cmd_apply(int argc, const char **argv, const char *unused_prefix)\n> >  \t\t\toptions |= RECOUNT;\n> >  \t\t\tcontinue;\n> >  \t\t}\n> > +\t\tif (!strncmp(arg, \"--root=\", strlen(\"--root=\"))) {\n> \n> prefixcmp()?\n\nD'oh.\n\nI mean, thanks!\n\n> > +\t\t\targ += strlen(\"--root=\");\n> > +\t\t\troot_len = strlen(arg);\n> > +\t\t\tif (root_len && arg[root_len + 1] != '/') {\n> \n> Ah, being nice not to force trailing slash on users --- how unlike you \n> ;-)\n\nSeems to be the new favorite sports, Dscho bashing.\n\n> but I think you meant \"arg[root_len - 1]\" there...\n\nI did.  An earlier version (as I found out by using the reflogs, as I tend \nto do a lot these last couple of weeks) even had a \"-\".  Musta been late.\n\nCiao,\nDscho\n"},{"id":"81888","messageId":"alpine.LSU.1.00.0807011835090.32725@wbgn129.biozentrum.uni-wuerzburg.de","threadId":"14235","inReplyTo":"486A55B0.9050404@zytor.com","subject":"Re: [PATCH] Teach \"git apply\" to prepend a prefix with \"--root=<root>\"","fromName":"Johannes Schindelin","fromEmail":"johannes.schindelin@gmx.de","sentAt":"2008-07-01T16:36:06Z","receivedAt":"2008-07-01T16:36:06Z","isPatch":true,"sender":{"key":"johannes.schindelin@gmx.de","avatar":"https://avatars.githubusercontent.com/u/127790?v=4"},"body":"Hi,\n\nOn Tue, 1 Jul 2008, H. Peter Anvin wrote:\n\n> Junio C Hamano wrote:\n> > \n> > The -p<num> came from patch where it is also called --strip=num.  \n> > This new option --root is about the reverse operation and it is about \n> > inserting at the beginning --- it is rerooting, in other words, but \n> > then --root is good enough and shorter.  mkisofs uses the word \"graft\" \n> > when it allows tree shifting (enabled with --graft-points), but the \n> > word \"graft\" means a totally different thing to us, so we would not \n> > want to use that word.\n> > \n> > I am not complaining (--root is fine by me), but just thinking aloud, \n> > hoping somebody's brainwave is provoked while reading this babbling \n> > and comes up with a better wording ;-).\n> > \n> \n> There is an analogous concept in patch(1), it's just implemented by \n> cd'ing to a subdirectory first.  ;)\n\nHey, \"--cd=\" is free!  And it would make explaining easier why -p is \napplied first.\n\nCiao,\nDscho\n"},{"id":"81893","messageId":"486A61E5.1070208@zytor.com","threadId":"14235","inReplyTo":"alpine.LSU.1.00.0807011835090.32725@wbgn129.biozentrum.uni-wuerzburg.de","subject":"Re: [PATCH] Teach \"git apply\" to prepend a prefix with \"--root=<root>\"","fromName":"H. Peter Anvin","fromEmail":"hpa@zytor.com","sentAt":"2008-07-01T16:57:09Z","receivedAt":"2008-07-01T16:57:09Z","isPatch":true,"sender":{"key":"hpa@zytor.com","avatar":null},"body":"Johannes Schindelin wrote:\n> Hi,\n> \n> On Tue, 1 Jul 2008, H. Peter Anvin wrote:\n> \n>> Junio C Hamano wrote:\n>>> The -p<num> came from patch where it is also called --strip=num.  \n>>> This new option --root is about the reverse operation and it is about \n>>> inserting at the beginning --- it is rerooting, in other words, but \n>>> then --root is good enough and shorter.  mkisofs uses the word \"graft\" \n>>> when it allows tree shifting (enabled with --graft-points), but the \n>>> word \"graft\" means a totally different thing to us, so we would not \n>>> want to use that word.\n>>>\n>>> I am not complaining (--root is fine by me), but just thinking aloud, \n>>> hoping somebody's brainwave is provoked while reading this babbling \n>>> and comes up with a better wording ;-).\n>>>\n>> There is an analogous concept in patch(1), it's just implemented by \n>> cd'ing to a subdirectory first.  ;)\n> \n> Hey, \"--cd=\" is free!  And it would make explaining easier why -p is \n> applied first.\n> \n\nAlthough that could imply cd'ing into another git repository to some people.\n\n\t-hpa\n"},{"id":"81898","messageId":"1214935848.6619.1.camel@doriath","threadId":"14235","inReplyTo":"alpine.LSU.1.00.0807011835090.32725@wbgn129.biozentrum.uni-wuerzburg.de","subject":"Re: [PATCH] Teach \"git apply\" to prepend a prefix with \"--root=<root>\"","fromName":"Raimund Bauer","fromEmail":"ray007@gmx.net","sentAt":"2008-07-01T18:10:48Z","receivedAt":"2008-07-01T18:10:48Z","isPatch":true,"sender":{"key":"ray007@gmx.net","avatar":null},"body":"On Tue, 2008-07-01 at 18:36 +0200, Johannes Schindelin wrote:\n> > There is an analogous concept in patch(1), it's just implemented by \n> > cd'ing to a subdirectory first.  ;)\n> \n> Hey, \"--cd=\" is free!  And it would make explaining easier why -p is \n> applied first.\n\npatch uses -d or --directory\n\nregards,\nRay\n"},{"id":"81903","messageId":"486A933F.9090001@zytor.com","threadId":"14235","inReplyTo":"1214935848.6619.1.camel@doriath","subject":"Re: [PATCH] Teach \"git apply\" to prepend a prefix with \"--root=<root>\"","fromName":"H. Peter Anvin","fromEmail":"hpa@zytor.com","sentAt":"2008-07-01T20:27:43Z","receivedAt":"2008-07-01T20:27:43Z","isPatch":true,"sender":{"key":"hpa@zytor.com","avatar":null},"body":"Raimund Bauer wrote:\n> On Tue, 2008-07-01 at 18:36 +0200, Johannes Schindelin wrote:\n>>> There is an analogous concept in patch(1), it's just implemented by \n>>> cd'ing to a subdirectory first.  ;)\n>> Hey, \"--cd=\" is free!  And it would make explaining easier why -p is \n>> applied first.\n> \n> patch uses -d or --directory\n\nSo it does.\n\n\t-hpa\n"}]}