{"thread":{"id":"12401","subject":"[PATCH] allow git-am to run in a subdirectory","startedAt":"2008-03-01T06:22:55Z","lastAt":"2008-03-03T17:01:57Z","messageCount":11,"participants":["Jeff King","Junio C Hamano","Jay Soffian","Johannes Schindelin"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"70481","messageId":"20080301062255.GA27538@coredump.intra.peff.net","threadId":"12401","inReplyTo":null,"subject":"[PATCH] allow git-am to run in a subdirectory","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2008-03-01T06:22:55Z","receivedAt":"2008-03-01T06:22:55Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"We just move to the top of the tree and proceed. This\nshouldn't break any existing callers, since the behavior was\npreviously disallowed.\n\nSigned-off-by: Jeff King <peff@peff.net>\n---\nI can't imagine anyone would be confused by this behavior. The only\nother behavior that would make sense is perhaps trying to apply from the\ngiven prefix rather than moving to the toplevel, but that just seems\ncrazy and useless to me.\n\nIt will fail to find your mbox file if you specified a relative path\nand it had to change directories. I don't know if that case is worth\nhandling.\n\nMy use case, btw, is that I have an \"apply this patch\" macro in my MUA.\nFor some messages, it's easy to guess \"cd /my/git/repo && git am\". But\nfor others, the repo is not obvious, so I apply in the current\ndirectory. If I am at the root of the repo, it works fine. If I'm not,\nthen it fails annoyingly.\n\n git-am.sh |    2 ++\n 1 files changed, 2 insertions(+), 0 deletions(-)\n\ndiff --git a/git-am.sh b/git-am.sh\nindex 2ecebc4..a2c6fea 100755\n--- a/git-am.sh\n+++ b/git-am.sh\n@@ -2,6 +2,7 @@\n #\n # Copyright (c) 2005, 2006 Junio C Hamano\n \n+SUBDIRECTORY_OK=Yes\n OPTIONS_KEEPDASHDASH=\n OPTIONS_SPEC=\"\\\n git-am [options] <mbox>|<Maildir>...\n@@ -25,6 +26,7 @@ skip            skip the current patch\"\n . git-sh-setup\n set_reflog_action am\n require_work_tree\n+cd_to_toplevel\n \n git var GIT_COMMITTER_IDENT >/dev/null || exit\n \n-- \n1.5.4.3.422.g55194\n"},{"id":"70488","messageId":"7vprue6ghc.fsf@gitster.siamese.dyndns.org","threadId":"12401","inReplyTo":"20080301062255.GA27538@coredump.intra.peff.net","subject":"Re: [PATCH] allow git-am to run in a subdirectory","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2008-03-01T07:38:55Z","receivedAt":"2008-03-01T07:38:55Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Jeff King <peff@peff.net> writes:\n\n> I can't imagine anyone would be confused by this behavior.\n\nAs long as you do not lose sight of the mailbox parameter by chdir'ing\naround, I am Ok with the patch.\n\n... and after I started writing that, I find...\n\n> ...\n> It will fail to find your mbox file if you specified a relative path\n> and it had to change directories. I don't know if that case is worth\n> handling.\n\nHmmm ;-).\n\n"},{"id":"70495","messageId":"20080301081235.GA31855@coredump.intra.peff.net","threadId":"12401","inReplyTo":"7vprue6ghc.fsf@gitster.siamese.dyndns.org","subject":"Re: [PATCH] allow git-am to run in a subdirectory","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2008-03-01T08:12:35Z","receivedAt":"2008-03-01T08:12:35Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"On Fri, Feb 29, 2008 at 11:38:55PM -0800, Junio C Hamano wrote:\n\n> As long as you do not lose sight of the mailbox parameter by chdir'ing\n> around, I am Ok with the patch.\n> \n> ... and after I started writing that, I find...\n\nUgh. Below is a patch that saves the original pwd and prefixes it for\nrelative paths. However:\n\n  - it probably doesn't correctly determine absolute versus relative\n    paths on Windows. I don't think we have a solution for fixing this\n    within shell scripts.\n\n  - it will eat newlines in parameters names (actually, turning them\n    into spaces)\n\nThe problem is that I need to turn the original \"$@\" into a new \"$@\"\nthat is correctly prefixed, which requires proper quoting. Please, spend\nsome of your shell guru points to show me how to do this correctly and\nportably.\n\nWe could wait on doing the 'cd_to_toplevel' until after the mailsplit,\nbut then your .dotest will end up in a prefixed directory (and of course\nwe have a similar munging problem when we point .dotest to the right\nspot).\n\nMaybe I should just wait for a git-am rewrite in C. ;)\n\n---\ndiff --git a/git-am.sh b/git-am.sh\nindex 2ecebc4..a5e0c43 100755\n--- a/git-am.sh\n+++ b/git-am.sh\n@@ -2,6 +2,7 @@\n #\n # Copyright (c) 2005, 2006 Junio C Hamano\n \n+SUBDIRECTORY_OK=Yes\n OPTIONS_KEEPDASHDASH=\n OPTIONS_SPEC=\"\\\n git-am [options] <mbox>|<Maildir>...\n@@ -25,6 +26,13 @@ skip            skip the current patch\"\n . git-sh-setup\n set_reflog_action am\n require_work_tree\n+orig_pwd=$(pwd)\n+cd_to_toplevel\n+if test \"$(pwd)\" = \"$orig_pwd\"; then\n+\torig_pwd=\n+else\n+\torig_pwd=\"$orig_pwd/\"\n+fi\n \n git var GIT_COMMITTER_IDENT >/dev/null || exit\n \n@@ -121,6 +129,21 @@ reread_subject () {\n \tgit stripspace <\"$1\" | sed -e 1q\n }\n \n+shellquote() {\n+\tprintf \\'\n+\tprintf \"$1\" | sed \"s/'/\\\\'/g\"\n+\techo \\'\n+}\n+\n+handle_file_args() {\n+\tfor i in \"$@\"; do\n+\t\tcase \"$i\" in\n+\t\t/*) shellquote \"$i\";;\n+\t\t *) shellquote \"$orig_pwd$i\";;\n+\t\tesac\n+\tdone\n+}\n+\n prec=4\n dotest=.dotest sign= utf8=t keep= skip= interactive= resolved= binary=\n resolvemsg= resume=\n@@ -148,7 +171,7 @@ do\n \t--skip)\n \t\tskip=t ;;\n \t-d|--dotest)\n-\t\tshift; dotest=$1;;\n+\t\tshift; eval dotest=`handle_file_args \"$1\"` ;;\n \t--resolvemsg)\n \t\tshift; resolvemsg=$1 ;;\n \t--whitespace)\n@@ -163,6 +186,8 @@ do\n \tshift\n done\n \n+eval set -- `handle_file_args \"$@\"`\n+\n # If the dotest directory exists, but we have finished applying all the\n # patches in them, clear it out.\n if test -d \"$dotest\" &&\n"},{"id":"70496","messageId":"7vabli6es4.fsf@gitster.siamese.dyndns.org","threadId":"12401","inReplyTo":"20080301081235.GA31855@coredump.intra.peff.net","subject":"Re: [PATCH] allow git-am to run in a subdirectory","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2008-03-01T08:15:39Z","receivedAt":"2008-03-01T08:15:39Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Sorry for making you do an extra work.  I hoped ;-) was explicit enough\nbut apparently it was not...\n"},{"id":"70499","messageId":"20080301082012.GC31855@coredump.intra.peff.net","threadId":"12401","inReplyTo":"7vabli6es4.fsf@gitster.siamese.dyndns.org","subject":"Re: [PATCH] allow git-am to run in a subdirectory","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2008-03-01T08:20:12Z","receivedAt":"2008-03-01T08:20:12Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"On Sat, Mar 01, 2008 at 12:15:39AM -0800, Junio C Hamano wrote:\n\n> Sorry for making you do an extra work.  I hoped ;-) was explicit enough\n> but apparently it was not...\n\nHeh. I was a little uncomfortable submitting a patch that didn't totally\nwork anyway. Of course, I'm not sure the result is that much better, and\nit's a lot more complex.\n\n-Peff\n"},{"id":"70734","messageId":"76718490803021926w29a6a44bha9fd22c48c94250a@mail.gmail.com","threadId":"12401","inReplyTo":"20080301081235.GA31855@coredump.intra.peff.net","subject":"Re: [PATCH] allow git-am to run in a subdirectory","fromName":"Jay Soffian","fromEmail":"jaysoffian@gmail.com","sentAt":"2008-03-03T03:26:51Z","receivedAt":"2008-03-03T03:26:51Z","isPatch":true,"sender":{"key":"jaysoffian@gmail.com","avatar":"https://avatars.githubusercontent.com/u/155970?v=4"},"body":"On Sat, Mar 1, 2008 at 3:12 AM, Jeff King <peff@peff.net> wrote:\n>  The problem is that I need to turn the original \"$@\" into a new \"$@\"\n>  that is correctly prefixed, which requires proper quoting.\n\nPerhaps rev-parse could be taught to optionally convert relative\npaths to absolute?\n\nj.\n"},{"id":"70745","messageId":"7vlk50joe0.fsf_-_@gitster.siamese.dyndns.org","threadId":"12401","inReplyTo":"20080301081235.GA31855@coredump.intra.peff.net","subject":"Re* [PATCH] allow git-am to run in a subdirectory","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2008-03-03T06:46:31Z","receivedAt":"2008-03-03T06:46:31Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Jeff King <peff@peff.net> writes:\n\n> The problem is that I need to turn the original \"$@\" into a new \"$@\"\n> that is correctly prefixed, which requires proper quoting.\n\nPerhaps like this?\n\n---\n git-am.sh            |   19 +++++++++++++\n t/t4150-am-subdir.sh |   72 ++++++++++++++++++++++++++++++++++++++++++++++++++\n 2 files changed, 91 insertions(+), 0 deletions(-)\n\ndiff --git a/git-am.sh b/git-am.sh\nindex a2c6fea..de34636 100755\n--- a/git-am.sh\n+++ b/git-am.sh\n@@ -24,6 +24,7 @@ r,resolved      to be used after a patch failure\n skip            skip the current patch\"\n \n . git-sh-setup\n+prefix=$(git rev-parse --show-prefix)\n set_reflog_action am\n require_work_tree\n cd_to_toplevel\n@@ -206,6 +207,24 @@ else\n \t# Start afresh.\n \tmkdir -p \"$dotest\" || exit\n \n+\tif test -n \"$prefix\" && test $# != 0\n+\tthen\n+\t\tfirst=t\n+\t\tfor arg\n+\t\tdo\n+\t\t\ttest -n \"$first\" && {\n+\t\t\t\tset x\n+\t\t\t\tfirst=\n+\t\t\t}\n+\t\t\tcase \"$arg\" in\n+\t\t\t/*)\n+\t\t\t\tset \"$@\" \"$arg\" ;;\n+\t\t\t*)\n+\t\t\t\tset \"$@\" \"$prefix$arg\" ;;\n+\t\t\tesac\n+\t\tdone\n+\t\tshift\n+\tfi\n \tgit mailsplit -d\"$prec\" -o\"$dotest\" -b -- \"$@\" > \"$dotest/last\" ||  {\n \t\trm -fr \"$dotest\"\n \t\texit 1\ndiff --git a/t/t4150-am-subdir.sh b/t/t4150-am-subdir.sh\nnew file mode 100755\nindex 0000000..929d2cb\n--- /dev/null\n+++ b/t/t4150-am-subdir.sh\n@@ -0,0 +1,72 @@\n+#!/bin/sh\n+\n+test_description='git am running from a subdirectory'\n+\n+. ./test-lib.sh\n+\n+test_expect_success setup '\n+\techo hello >world &&\n+\tgit add world &&\n+\ttest_tick &&\n+\tgit commit -m initial &&\n+\tgit tag initial &&\n+\techo goodbye >world &&\n+\tgit add world &&\n+\ttest_tick &&\n+\tgit commit -m second &&\n+\tgit format-patch --stdout HEAD^ >patchfile &&\n+\t: >expect\n+'\n+\n+test_expect_success 'am regularly from stdin' '\n+\tgit checkout initial &&\n+\tgit am <patchfile &&\n+\tgit diff master >actual &&\n+\tdiff -u expect actual\n+'\n+\n+test_expect_success 'am regularly from file' '\n+\tgit checkout initial &&\n+\tgit am patchfile &&\n+\tgit diff master >actual &&\n+\tdiff -u expect actual\n+'\n+\n+test_expect_success 'am regularly from stdin in subdirectory' '\n+\trm -fr subdir &&\n+\tgit checkout initial &&\n+\t(\n+\t\tmkdir -p subdir &&\n+\t\tcd subdir &&\n+\t\tgit am <../patchfile\n+\t) &&\n+\tgit diff master>actual &&\n+\tdiff -u expect actual\n+'\n+\n+test_expect_success 'am regularly from file in subdirectory' '\n+\trm -fr subdir &&\n+\tgit checkout initial &&\n+\t(\n+\t\tmkdir -p subdir &&\n+\t\tcd subdir &&\n+\t\tgit am ../patchfile\n+\t) &&\n+\tgit diff master >actual &&\n+\tdiff -u expect actual\n+'\n+\n+test_expect_success 'am regularly from file in subdirectory with full path' '\n+\trm -fr subdir &&\n+\tgit checkout initial &&\n+\tP=$(pwd) &&\n+\t(\n+\t\tmkdir -p subdir &&\n+\t\tcd subdir &&\n+\t\tgit am \"$P/patchfile\"\n+\t) &&\n+\tgit diff master >actual &&\n+\tdiff -u expect actual\n+'\n+\n+test_done\n"},{"id":"70748","messageId":"20080303065808.GA22810@coredump.intra.peff.net","threadId":"12401","inReplyTo":"7vlk50joe0.fsf_-_@gitster.siamese.dyndns.org","subject":"Re: Re* [PATCH] allow git-am to run in a subdirectory","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2008-03-03T06:58:08Z","receivedAt":"2008-03-03T06:58:08Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"On Sun, Mar 02, 2008 at 10:46:31PM -0800, Junio C Hamano wrote:\n\n> > The problem is that I need to turn the original \"$@\" into a new \"$@\"\n> > that is correctly prefixed, which requires proper quoting.\n> \n> Perhaps like this?\n\nYes. For some reason I didn't think of doing 'set \"$@\" \"$new\"' to\nrebuild the list one by one.\n\n> +\tif test -n \"$prefix\" && test $# != 0\n> +\tthen\n> +\t\tfirst=t\n> +\t\tfor arg\n> +\t\tdo\n> +\t\t\ttest -n \"$first\" && {\n> +\t\t\t\tset x\n> +\t\t\t\tfirst=\n> +\t\t\t}\n> +\t\t\tcase \"$arg\" in\n> +\t\t\t/*)\n> +\t\t\t\tset \"$@\" \"$arg\" ;;\n> +\t\t\t*)\n> +\t\t\t\tset \"$@\" \"$prefix$arg\" ;;\n> +\t\t\tesac\n> +\t\tdone\n> +\t\tshift\n> +\tfi\n\nThis handles the mailbox parameters. The only other file parameter is\n$dotest. If I do \"cd t && git am --dotest=.foo\", then should it be\n\"t/.foo\" or \".foo\"?\n\n-Peff\n"},{"id":"70752","messageId":"7vhcfojndo.fsf@gitster.siamese.dyndns.org","threadId":"12401","inReplyTo":"20080303065808.GA22810@coredump.intra.peff.net","subject":"Re: Re* [PATCH] allow git-am to run in a subdirectory","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2008-03-03T07:08:19Z","receivedAt":"2008-03-03T07:08:19Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Jeff King <peff@peff.net> writes:\n\n> The only other file parameter is\n> $dotest. If I do \"cd t && git am --dotest=.foo\", then should it be\n> \"t/.foo\" or \".foo\"?\n\nThat brings up an interesting point.\n\nHave people actually used --dotest=<foo> ever?\n\nAs far as I know, I do not think it was useful in the real world.  Too\nmany things assumed that the spool directory for \"am\" is .dotest.\n\nAnd I am very much tempted to say we should remove that option.\n\n"},{"id":"70774","messageId":"alpine.LSU.1.00.0803031152270.22527@racer.site","threadId":"12401","inReplyTo":"7vhcfojndo.fsf@gitster.siamese.dyndns.org","subject":"Re: Re* [PATCH] allow git-am to run in a subdirectory","fromName":"Johannes Schindelin","fromEmail":"johannes.schindelin@gmx.de","sentAt":"2008-03-03T11:53:38Z","receivedAt":"2008-03-03T11:53:38Z","isPatch":true,"sender":{"key":"johannes.schindelin@gmx.de","avatar":"https://avatars.githubusercontent.com/u/127790?v=4"},"body":"Hi,\n\nOn Sun, 2 Mar 2008, Junio C Hamano wrote:\n\n> Have people actually used --dotest=<foo> ever?\n> \n> As far as I know, I do not think it was useful in the real world.  Too \n> many things assumed that the spool directory for \"am\" is .dotest.\n> \n> And I am very much tempted to say we should remove that option.\n\nConcur.  While at it, we could move both .dotest/ and .git/.dotest-merge/\nto .git/dotest/, no?\n\nI know, I was opposed to this change, but let's just see how many heads \npop up all of a sudden on the mailing list, okay?\n\nCiao,\nDscho\n\n"},{"id":"70801","messageId":"7vod9vivwa.fsf@gitster.siamese.dyndns.org","threadId":"12401","inReplyTo":"alpine.LSU.1.00.0803031152270.22527@racer.site","subject":"Re: Re* [PATCH] allow git-am to run in a subdirectory","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2008-03-03T17:01:57Z","receivedAt":"2008-03-03T17:01:57Z","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> Concur.  While at it, we could move both .dotest/ and .git/.dotest-merge/\n> to .git/dotest/, no?\n\nWe could, yes, while at it, no.\n"}]}