# [PATCH] Allow passing of --directory to git-am.

13 messages from 2008-12-04 to 2008-12-05. Participants: Simon 'corecode' Schubert, Jeff King, Junio C Hamano, Jakub Narebski.
Thread: https://gitlist.dev/t/16598

## Simon 'corecode' Schubert, 2008-12-04 18:48

Subject: [PATCH] Allow passing of --directory to git-am.
Message-ID: <49382612.3010207@fs.ei.tum.de>
URL: https://gitlist.dev/e/49382612.3010207%40fs.ei.tum.de

```
We need to play some shell tricks to be able to pass directory names
which contain spaces and/or quotes.

Signed-off-by: Simon 'corecode' Schubert <corecode@fs.ei.tum.de>
---

Boyd Stephen Smith Jr. wrote:
 > I'm thinking your sed line doesn't do what you think it does.  You 
probably
 > want something like:
 > bss@monster:~$ echo "don't" | sed -e "s/'/'\\\\''/g"
 > don'\''t

Thanks, I clearly did not test this well enough.

  git-am.sh |    8 +++++---
  1 files changed, 5 insertions(+), 3 deletions(-)

diff --git a/git-am.sh b/git-am.sh
index aa60261..3baff4e 100755
--- a/git-am.sh
+++ b/git-am.sh
@@ -16,6 +16,7 @@ s,signoff       add a Signed-off-by line to the commit 
message
  u,utf8          recode into utf8 (default)
  k,keep          pass -k flag to git-mailinfo
  whitespace=     pass it through git-apply
+directory=      pass it through git-apply
  C=              pass it through git-apply
  p=              pass it through git-apply
  resolvemsg=     override error message when patch failure occurs
@@ -155,8 +156,9 @@ do
  		;;
  	--resolvemsg)
  		shift; resolvemsg=$1 ;;
-	--whitespace)
-		git_apply_opt="$git_apply_opt $1=$2"; shift ;;
+	--whitespace|--directory)
+		quot=$(echo "$2" | sed -e "s/'/'\\\''/g")
+		git_apply_opt="$git_apply_opt $1='$quot'"; shift ;;
  	-C|-p)
  		git_apply_opt="$git_apply_opt $1$2"; shift ;;
  	--)
@@ -454,7 +456,7 @@ do

  	case "$resolved" in
  	'')
-		git apply $git_apply_opt --index "$dotest/patch"
+		eval git apply $git_apply_opt --index '"$dotest/patch"'
  		apply_status=$?
  		;;
  	t)
-- 
1.6.1.rc1.45.g123ed.dirty

```

## Jeff King, 2008-12-04 18:51

Subject: Re: [PATCH] Allow passing of --directory to git-am.
Message-ID: <20081204185138.GA24858@coredump.intra.peff.net>
URL: https://gitlist.dev/e/20081204185138.GA24858%40coredump.intra.peff.net
In-Reply-To: <49382612.3010207@fs.ei.tum.de>

```
On Thu, Dec 04, 2008 at 07:48:50PM +0100, Simon 'corecode' Schubert wrote:

> Boyd Stephen Smith Jr. wrote:
> > I'm thinking your sed line doesn't do what you think it does.  You  
> probably
> > want something like:
> > bss@monster:~$ echo "don't" | sed -e "s/'/'\\\\''/g"
> > don'\''t
>
> Thanks, I clearly did not test this well enough.

Maybe it would be a good idea to add a test to the test script, then?

-Peff

```

## Junio C Hamano, 2008-12-04 19:27

Subject: Re: [PATCH] Allow passing of --directory to git-am.
Message-ID: <7vhc5jeo60.fsf@gitster.siamese.dyndns.org>
URL: https://gitlist.dev/e/7vhc5jeo60.fsf%40gitster.siamese.dyndns.org
In-Reply-To: <49382612.3010207@fs.ei.tum.de>

```
Simon 'corecode' Schubert <corecode@fs.ei.tum.de> writes:

> We need to play some shell tricks to be able to pass directory names
> which contain spaces and/or quotes.

There already was an earlier attempt for this feature by Kevin Ballard,
which had issues I pointed out:

  http://thread.gmane.org/gmane.comp.version-control.git/94335/focus=94456

The patch was carried for a few weeks in 'pu' but was dropped due to lack
of follow-up updates.

Does your version address the issues Kevin's one had?

```

## Jakub Narebski, 2008-12-04 19:35

Subject: Re: [PATCH] Allow passing of --directory to git-am.
Message-ID: <m3hc5jivjo.fsf@localhost.localdomain>
URL: https://gitlist.dev/e/m3hc5jivjo.fsf%40localhost.localdomain
In-Reply-To: <49382612.3010207@fs.ei.tum.de>

```
Simon 'corecode' Schubert <corecode@fs.ei.tum.de> writes:

> We need to play some shell tricks to be able to pass directory names
> which contain spaces and/or quotes.
> 
> Signed-off-by: Simon 'corecode' Schubert <corecode@fs.ei.tum.de>
> ---
> 
> Boyd Stephen Smith Jr. wrote:
>  > I'm thinking your sed line doesn't do what you think it does.
>  > You probably want something like:
>  > bss@monster:~$ echo "don't" | sed -e "s/'/'\\\\''/g"
>  > don'\''t
> 
> Thanks, I clearly did not test this well enough.
> 
>   git-am.sh |    8 +++++---
>   1 files changed, 5 insertions(+), 3 deletions(-)
> 
> diff --git a/git-am.sh b/git-am.sh
> index aa60261..3baff4e 100755
> --- a/git-am.sh
> +++ b/git-am.sh
> @@ -16,6 +16,7 @@ s,signoff       add a Signed-off-by line to the commit message
>   u,utf8          recode into utf8 (default)
>   k,keep          pass -k flag to git-mailinfo
>   whitespace=     pass it through git-apply
> +directory=      pass it through git-apply

Shouldn't it be

+ directory=      pass it through git-apply

to have it aligned like the rest of it?

By the way, your patch looks whitespace damaged, although only context
for the above chunk was wrapped...

>   C=              pass it through git-apply
>   p=              pass it through git-apply
>   resolvemsg=     override error message when patch failure occurs
> @@ -155,8 +156,9 @@ do
>   		;;
>   	--resolvemsg)
>   		shift; resolvemsg=$1 ;;
> -	--whitespace)
> -		git_apply_opt="$git_apply_opt $1=$2"; shift ;;
> +	--whitespace|--directory)
> +		quot=$(echo "$2" | sed -e "s/'/'\\\''/g")

Why not simply use "git rev-parse --sq"?

> +		git_apply_opt="$git_apply_opt $1='$quot'"; shift ;;
>   	-C|-p)
>   		git_apply_opt="$git_apply_opt $1$2"; shift ;;
>   	--)
> @@ -454,7 +456,7 @@ do
> 
>   	case "$resolved" in
>   	'')
> -		git apply $git_apply_opt --index "$dotest/patch"
> +		eval git apply $git_apply_opt --index '"$dotest/patch"'

Why eval?

>   		apply_status=$?
>   		;;
>   	t)
> -- 
> 1.6.1.rc1.45.g123ed.dirty

Hmmm... 

-- 
Jakub Narebski
Poland
ShadeHawk on #git

```

## Simon 'corecode' Schubert, 2008-12-04 22:25

Subject: Re: [PATCH] Allow passing of --directory to git-am.
Message-ID: <493858CE.1030601@fs.ei.tum.de>
URL: https://gitlist.dev/e/493858CE.1030601%40fs.ei.tum.de
In-Reply-To: <m3hc5jivjo.fsf@localhost.localdomain>

```
Jakub Narebski wrote:
> Shouldn't it be
> 
> + directory=      pass it through git-apply
> 
> to have it aligned like the rest of it?
> 
> By the way, your patch looks whitespace damaged, although only context
> for the above chunk was wrapped...

Thunderbird must have pummeled it.  No clue why it does that though. 
Black magic software.

>> @@ -155,8 +156,9 @@ do
>>   		;;
>>   	--resolvemsg)
>>   		shift; resolvemsg=$1 ;;
>> -	--whitespace)
>> -		git_apply_opt="$git_apply_opt $1=$2"; shift ;;
>> +	--whitespace|--directory)
>> +		quot=$(echo "$2" | sed -e "s/'/'\\\''/g")
> 
> Why not simply use "git rev-parse --sq"?

What I need is to convert $2 into a form suitable for quoting, does git 
rev-parse --sq do that?

>> +		git_apply_opt="$git_apply_opt $1='$quot'"; shift ;;
>>   	-C|-p)
>>   		git_apply_opt="$git_apply_opt $1$2"; shift ;;
>>   	--)
>> @@ -454,7 +456,7 @@ do
>>
>>   	case "$resolved" in
>>   	'')
>> -		git apply $git_apply_opt --index "$dotest/patch"
>> +		eval git apply $git_apply_opt --index '"$dotest/patch"'
 >
 > Why eval?

I quoted the above variable, so I now need to unquote it, that's done by eval.

-- 
   <3 the future  +++  RENT this banner advert  +++   ASCII Ribbon   /"\
   rock the past  +++  space for low €€€ NOW!1  +++     Campaign     \ /
Party Enjoy Relax   |   http://dragonflybsd.org      Against  HTML   \
Dude 2c 2 the max   !   http://golden-apple.biz       Mail + News   / \

```

## Simon 'corecode' Schubert, 2008-12-04 22:26

Subject: Re: [PATCH] Allow passing of --directory to git-am.
Message-ID: <49385908.5020202@fs.ei.tum.de>
URL: https://gitlist.dev/e/49385908.5020202%40fs.ei.tum.de
In-Reply-To: <7vhc5jeo60.fsf@gitster.siamese.dyndns.org>

```
Junio C Hamano wrote:
> Simon 'corecode' Schubert <corecode@fs.ei.tum.de> writes:
> 
>> We need to play some shell tricks to be able to pass directory names
>> which contain spaces and/or quotes.
> 
> There already was an earlier attempt for this feature by Kevin Ballard,
> which had issues I pointed out:
> 
>   http://thread.gmane.org/gmane.comp.version-control.git/94335/focus=94456
> 
> The patch was carried for a few weeks in 'pu' but was dropped due to lack
> of follow-up updates.
> 
> Does your version address the issues Kevin's one had?

You mean not storing/restoring the flags across an invocation?  No, that's 
a different thing.  My patch only adds the --directory option, it does not 
fix the previously existing bug.

cheers
   simon

-- 
   <3 the future  +++  RENT this banner advert  +++   ASCII Ribbon   /"\
   rock the past  +++  space for low €€€ NOW!1  +++     Campaign     \ /
Party Enjoy Relax   |   http://dragonflybsd.org      Against  HTML   \
Dude 2c 2 the max   !   http://golden-apple.biz       Mail + News   / \

```

## Junio C Hamano, 2008-12-04 22:33

Subject: Re: [PATCH] Allow passing of --directory to git-am.
Message-ID: <7v7i6fd0zt.fsf@gitster.siamese.dyndns.org>
URL: https://gitlist.dev/e/7v7i6fd0zt.fsf%40gitster.siamese.dyndns.org
In-Reply-To: <49385908.5020202@fs.ei.tum.de>

```
Simon 'corecode' Schubert <corecode@fs.ei.tum.de> writes:

> You mean not storing/restoring the flags across an invocation?  No,
> that's a different thing.  My patch only adds the --directory option,
> it does not fix the previously existing bug.

The question is if it _introduces_ a bug that the directory given in the
initial invocation of "git am --directory=foo" is lost if an patch does
not apply and you need to manually resolve and continue.

If it does not introduce such a bug, you do not have the same issue as the
old patch.  Otherwise you have the same issue as the old patch.  The
question was if you have the same issue or you don't.  Yes?  No?

```

## Jakub Narebski, 2008-12-04 22:46

Subject: Re: [PATCH] Allow passing of --directory to git-am.
Message-ID: <200812042346.33496.jnareb@gmail.com>
URL: https://gitlist.dev/e/200812042346.33496.jnareb%40gmail.com
In-Reply-To: <493858CE.1030601@fs.ei.tum.de>

```
Simon 'corecode' Schubert wrote:
> Jakub Narebski wrote:
>> Simon 'corecode' Schubert wrote:
 
>>> @@ -155,8 +156,9 @@ do
>>>   		;;
>>>   	--resolvemsg)
>>>   		shift; resolvemsg=$1 ;;
>>> -	--whitespace)
>>> -		git_apply_opt="$git_apply_opt $1=$2"; shift ;;
>>> +	--whitespace|--directory)
>>> +		quot=$(echo "$2" | sed -e "s/'/'\\\''/g")
>> 
>> Why not simply use "git rev-parse --sq"?
> 
> What I need is to convert $2 into a form suitable for quoting, does git 
> rev-parse --sq do that?

  $ git rev-parse --sq -- "don't do that"
  '--' 'don'\''t do that'

Without terminating newline. The '--' is needed because otherwise
git-rev-parse expects revisions... and doesn't find any.
 
By the way you could both simplify option parsing _and_ take care of
proper quoting by using --parseopt, i.e. use git-rev-parse in PARSEOPT
mode. But that is more involved change.

>>> @@ -454,7 +456,7 @@ do
>>>
>>>   	case "$resolved" in
>>>   	'')
>>> -		git apply $git_apply_opt --index "$dotest/patch"
>>> +		eval git apply $git_apply_opt --index '"$dotest/patch"'
>>
>> Why eval?
> 
> I quoted the above variable, so I now need to unquote it, that's done by eval.

Ah.
-- 
Jakub Narebski
Poland

```

## Junio C Hamano, 2008-12-04 23:14

Subject: Re: [PATCH] Allow passing of --directory to git-am.
Message-ID: <7vy6yvbki6.fsf@gitster.siamese.dyndns.org>
URL: https://gitlist.dev/e/7vy6yvbki6.fsf%40gitster.siamese.dyndns.org
In-Reply-To: <7v7i6fd0zt.fsf@gitster.siamese.dyndns.org>

```
Junio C Hamano <gitster@pobox.com> writes:

> Simon 'corecode' Schubert <corecode@fs.ei.tum.de> writes:
>
>> You mean not storing/restoring the flags across an invocation?  No,
>> that's a different thing.  My patch only adds the --directory option,
>> it does not fix the previously existing bug.
>
> The question is if it _introduces_ a bug that the directory given in the
> initial invocation of "git am --directory=foo" is lost if an patch does
> not apply and you need to manually resolve and continue.
>
> If it does not introduce such a bug, you do not have the same issue as the
> old patch.  Otherwise you have the same issue as the old patch.  The
> question was if you have the same issue or you don't.  Yes?  No?

I think this fixes the --whitespace=* one, although I obviously haven't
tried to use it myself extensively.

The third hunk is just a style fix.  "am" is written in a quite old
fashioned way.

-- >8 --
Subject: [PATCH] git-am --whitespace: do not lose the command line option

When you start "git am --whitespace=fix" and the patch application process
is interrupted by an unapplicable patch early in the series, after
fixing the offending patch, the remainder of the patch should be processed
still with --whitespace=fix when restarted with "git am --resolved".

The commit 67dad68 (add -C[NUM] to git-am, 2007-02-08) broke this long
time ago.  This should fix it.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 git-am.sh |    8 ++++----
 1 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/git-am.sh b/git-am.sh
index aa60261..1bf70d4 100755
--- a/git-am.sh
+++ b/git-am.sh
@@ -121,7 +121,7 @@ It does not apply to blobs recorded in its index."
 
 prec=4
 dotest="$GIT_DIR/rebase-apply"
-sign= utf8=t keep= skip= interactive= resolved= rebasing= abort=
+sign= utf8=t keep= skip= interactive= resolved= rebasing= abort= ws=
 resolvemsg= resume=
 git_apply_opt=
 
@@ -156,7 +156,7 @@ do
 	--resolvemsg)
 		shift; resolvemsg=$1 ;;
 	--whitespace)
-		git_apply_opt="$git_apply_opt $1=$2"; shift ;;
+		ws="--whitespace=$2"; shift ;;
 	-C|-p)
 		git_apply_opt="$git_apply_opt $1$2"; shift ;;
 	--)
@@ -283,7 +283,7 @@ if test "$(cat "$dotest/keep")" = t
 then
 	keep=-k
 fi
-ws=`cat "$dotest/whitespace"`
+ws=$(cat "$dotest/whitespace")
 if test "$(cat "$dotest/sign")" = t
 then
 	SIGNOFF=`git var GIT_COMMITTER_IDENT | sed -e '
@@ -454,7 +454,7 @@ do
 
 	case "$resolved" in
 	'')
-		git apply $git_apply_opt --index "$dotest/patch"
+		git apply $git_apply_opt $ws --index "$dotest/patch"
 		apply_status=$?
 		;;
 	t)
-- 
1.6.1.rc1.60.g1d1d7

```

## Junio C Hamano, 2008-12-04 23:36

Subject: [PATCH - DONTUSE] git-am: propagate -C/-p as well
Message-ID: <7vmyfbbjir.fsf_-_@gitster.siamese.dyndns.org>
URL: https://gitlist.dev/e/7vmyfbbjir.fsf_-_%40gitster.siamese.dyndns.org
In-Reply-To: <7vy6yvbki6.fsf@gitster.siamese.dyndns.org>

```
Junio C Hamano <gitster@pobox.com> writes:

> I think this fixes the --whitespace=* one, although I obviously haven't
> tried to use it myself extensively.

This one comes on top of it *if* you want to propagate -C/-p as well, but
I think it might be a wrong idea to propagate these to begin with.

Just like --3way is a one-shot option to deal with a single unapplicable
patch (because it was based on an old version) in the whole series, and is
designed not to get propagated, I suspect that people use -C<n> to fix a
single broken patch and they may expect it not to apply to the whole
series.

The breakage --whitespace deals with is an attribute of the submitter (use
of a broken editor and lack of diligence).  You most often feed a single
series from the same submitter in the same mbox to "git am", preserving
the --whitespace=fix option during the same "am" run makes sense, and
somewhat more importantly, even though the option indeed modifies what you
received, the change the option causes and the risk of breaking the
semantics of the patch is minimum.  I am not sure the breakage --3way
deals with falls into the exactly the same category, but it is similar (if
the first patch in the series was based on an old version, it is very
likely that the subsequent ones are also based on the same old version).
So after all it might be better to propagate --3way as well (which this
patch does not do).

If we decide that propagating --3way is a good thing, then it would be
equally good to propagate -C, -p and --directory options.

I dunno.

 git-am.sh |   14 ++++++--------
 1 files changed, 6 insertions(+), 8 deletions(-)

diff --git c/git-am.sh w/git-am.sh
index 1bf70d4..a35e07a 100755
--- c/git-am.sh
+++ w/git-am.sh
@@ -121,7 +121,7 @@ It does not apply to blobs recorded in its index."
 
 prec=4
 dotest="$GIT_DIR/rebase-apply"
-sign= utf8=t keep= skip= interactive= resolved= rebasing= abort= ws=
+sign= utf8=t keep= skip= interactive= resolved= rebasing= abort=
 resolvemsg= resume=
 git_apply_opt=
 
@@ -155,9 +155,7 @@ do
 		;;
 	--resolvemsg)
 		shift; resolvemsg=$1 ;;
-	--whitespace)
-		ws="--whitespace=$2"; shift ;;
-	-C|-p)
+	-C|-p|--whitespace)
 		git_apply_opt="$git_apply_opt $1$2"; shift ;;
 	--)
 		shift; break ;;
@@ -247,10 +245,10 @@ else
 		exit 1
 	}
 
-	# -s, -u, -k and --whitespace flags are kept for the
-	# resuming session after a patch failure.
+	# -s, -u, -k, --whitespace, -C and -p flags are kept
+	# for the resuming session after a patch failure.
 	# -3 and -i can and must be given when resuming.
-	echo " $ws" >"$dotest/whitespace"
+	echo " $git_apply_opt" >"$dotest/apply_opt_extra"
 	echo "$sign" >"$dotest/sign"
 	echo "$utf8" >"$dotest/utf8"
 	echo "$keep" >"$dotest/keep"
@@ -283,7 +281,7 @@ if test "$(cat "$dotest/keep")" = t
 then
 	keep=-k
 fi
-ws=$(cat "$dotest/whitespace")
+apply_opt_extra=$(cat "$dotest/apply_opt_extra")
 if test "$(cat "$dotest/sign")" = t
 then
 	SIGNOFF=`git var GIT_COMMITTER_IDENT | sed -e '

```

## Simon 'corecode' Schubert, 2008-12-04 23:41

Subject: Re: [PATCH] Allow passing of --directory to git-am.
Message-ID: <49386ABE.2050404@fs.ei.tum.de>
URL: https://gitlist.dev/e/49386ABE.2050404%40fs.ei.tum.de
In-Reply-To: <7v7i6fd0zt.fsf@gitster.siamese.dyndns.org>

```
Junio C Hamano wrote:
> Simon 'corecode' Schubert <corecode@fs.ei.tum.de> writes:
> 
>> You mean not storing/restoring the flags across an invocation?  No,
>> that's a different thing.  My patch only adds the --directory option,
>> it does not fix the previously existing bug.
> 
> The question is if it _introduces_ a bug that the directory given in the
> initial invocation of "git am --directory=foo" is lost if an patch does
> not apply and you need to manually resolve and continue.
> 
> If it does not introduce such a bug, you do not have the same issue as the
> old patch.  Otherwise you have the same issue as the old patch.  The
> question was if you have the same issue or you don't.  Yes?  No?

Yes, that's the issue.  In this regard it behaves bug-compatible with the 
-p and -C options.

-- 
   <3 the future  +++  RENT this banner advert  +++   ASCII Ribbon   /"\
   rock the past  +++  space for low €€€ NOW!1  +++     Campaign     \ /
Party Enjoy Relax   |   http://dragonflybsd.org      Against  HTML   \
Dude 2c 2 the max   !   http://golden-apple.biz       Mail + News   / \

```

## Junio C Hamano, 2008-12-05 00:11

Subject: Re: [PATCH] Allow passing of --directory to git-am.
Message-ID: <7viqpzbhvr.fsf@gitster.siamese.dyndns.org>
URL: https://gitlist.dev/e/7viqpzbhvr.fsf%40gitster.siamese.dyndns.org
In-Reply-To: <49386ABE.2050404@fs.ei.tum.de>

```
Simon 'corecode' Schubert <corecode@fs.ei.tum.de> writes:

> Junio C Hamano wrote:
>> Simon 'corecode' Schubert <corecode@fs.ei.tum.de> writes:
>>
>>> You mean not storing/restoring the flags across an invocation?  No,
>>> that's a different thing.  My patch only adds the --directory option,
>>> it does not fix the previously existing bug.
>>
>> The question is if it _introduces_ a bug that the directory given in the
>> initial invocation of "git am --directory=foo" is lost if an patch does
>> not apply and you need to manually resolve and continue.
>>
>> If it does not introduce such a bug, you do not have the same issue as the
>> old patch.  Otherwise you have the same issue as the old patch.  The
>> question was if you have the same issue or you don't.  Yes?  No?
>
> Yes, that's the issue.  In this regard it behaves bug-compatible with
> the -p and -C options.

If that is the case, and assuming that propagating -C/-p would be a good
idea (which I am not sure yet), the patch I sent out earlier (which was
flawed somewhat; it should use "$git_apply_opt_extra" where it invokes the
"git apply" command) with necessary fix would serve as the basis to
implement --directory=<dir>?

```

## Simon 'corecode' Schubert, 2008-12-05 00:16

Subject: Re: [PATCH] Allow passing of --directory to git-am.
Message-ID: <493872C9.9060600@fs.ei.tum.de>
URL: https://gitlist.dev/e/493872C9.9060600%40fs.ei.tum.de
In-Reply-To: <7viqpzbhvr.fsf@gitster.siamese.dyndns.org>

```
Junio C Hamano wrote:
> If that is the case, and assuming that propagating -C/-p would be a good
> idea (which I am not sure yet), the patch I sent out earlier (which was
> flawed somewhat; it should use "$git_apply_opt_extra" where it invokes the
> "git apply" command) with necessary fix would serve as the basis to
> implement --directory=<dir>?

certainly.  I'll be travelling, so don't expect anything real soon, will 
resubmit unless I forget.

-- 
   <3 the future  +++  RENT this banner advert  +++   ASCII Ribbon   /"\
   rock the past  +++  space for low €€€ NOW!1  +++     Campaign     \ /
Party Enjoy Relax   |   http://dragonflybsd.org      Against  HTML   \
Dude 2c 2 the max   !   http://golden-apple.biz       Mail + News   / \

```
