threads / patch / 12621

patchlaunch_editor(): allow spaces in the filename

Subject: [PATCH] launch_editor(): allow spaces in the filename

## tl;dr

11 messages between Mar 10, 2008 and Mar 11, 2008. Diffs are folded; open one to read it.

replies: 10people: 3as markdown or json

Johannes Schindelin· Mar 10, 2008, 20:42 UTC · lore
For some reason, the construct
	sh -c "$0 \"$@\"" <editor> <file>

does not pick up quotes in <editor> as expected. So replace $0 with <editor>.

This fixes
	git config core.editor "c:/Program Files/What/Ever.exe"
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
---
	Incidentally, this fixes issue 78 in msysGit.
	Zeng (is this the correct way to address you?), Evan, please test.
 builtin-tag.c     |    6 +++++-
 t/t7005-editor.sh |   28 ++++++++++++++++++++++++++++
 2 files changed, 33 insertions(+), 1 deletions(-)
Show changes to 2 files +33 −1

builtin-tag.c, t/t7005-editor.sh

diff --git a/builtin-tag.c b/builtin-tag.c
index 28c36fd..8dd959f 100644
--- a/builtin-tag.c
+++ b/builtin-tag.c
@@ -50,12 +50,15 @@ void launch_editor(const char *path, struct strbuf *buffer, const char *const *e
 		size_t len = strlen(editor);
 		int i = 0;
 		const char *args[6];
+		struct strbuf arg0;
 
+		strbuf_init(&arg0, 0);
 		if (strcspn(editor, "$ \t'") != len) {
 			/* there are specials */
+			strbuf_addf(&arg0, "%s \"$@\"", editor);
 			args[i++] = "sh";
 			args[i++] = "-c";
-			args[i++] = "$0 \"$@\"";
+			args[i++] = arg0.buf;
 		}
 		args[i++] = editor;
 		args[i++] = path;
@@ -63,6 +66,7 @@ void launch_editor(const char *path, struct strbuf *buffer, const char *const *e
 
 		if (run_command_v_opt_cd_env(args, 0, NULL, env))
 			die("There was a problem with the editor %s.", editor);
+		strbuf_release(&arg0);
 	}
 
 	if (!buffer)
diff --git a/t/t7005-editor.sh b/t/t7005-editor.sh
index c1cec55..5395b74 100755
--- a/t/t7005-editor.sh
+++ b/t/t7005-editor.sh
@@ -89,6 +89,34 @@ do
 	'
 done
 
+test_expect_success 'editor with a space' '
+
+	if echo "echo space > \"\$1\"" > "e space.sh"
+	then
+		chmod a+x "e space.sh" &&
+		GIT_EDITOR="./e\ space.sh" \
+			git --exec-path=. commit --amend &&
+		test space = "$(git show -s --pretty=format:%s)"
+	else
+		say "Skipping; FS does not support spaces in filenames"
+	fi
+
+'
+
+unset GIT_EDITOR
+test_expect_success 'core.editor with a space' '
+
+	if test -f "e space.sh"
+	then
+		git config core.editor \"./e\ space.sh\" &&
+		git --exec-path=. commit --amend &&
+		test space = "$(git show -s --pretty=format:%s)"
+	else
+		say "Skipping; FS does not support spaces in filenames"
+	fi
+
+'
+
 TERM="$OLD_TERM"
 
 test_done
-- 
1.5.4.4.643.g7cb9b.dirty
Johannes Sixt· Mar 10, 2008, 21:32 UTC · re: Johannes Schindelin · lore

Re: [PATCH] launch_editor(): allow spaces in the filename

On Monday 10 March 2008 21:42, Johannes Schindelin wrote:
Show 6 quoted lines
> For some reason, the construct
>
> 	sh -c "$0 \"$@\"" <editor> <file>
>
> does not pick up quotes in <editor> as expected.  So replace $0 with
> <editor>.
No surprise. It must be
	sh -c '"$0" "$@"' <editor> <file>
Note the extra quotes around $0.
>  			args[i++] = "sh";
>  			args[i++] = "-c";
> -			args[i++] = "$0 \"$@\"";
> +			args[i++] = arg0.buf;
IOW:
+			args[i++] = "\"$0\" \"$@\"";
-- Hannes
Junio C Hamano· Mar 10, 2008, 21:37 UTC · re: Johannes Sixt · lore

Re: [PATCH] launch_editor(): allow spaces in the filename

Johannes Sixt <johannes.sixt@telecom.at> writes:
Show 13 quoted lines
> On Monday 10 March 2008 21:42, Johannes Schindelin wrote:
>> For some reason, the construct
>>
>> 	sh -c "$0 \"$@\"" <editor> <file>
>>
>> does not pick up quotes in <editor> as expected.  So replace $0 with
>> <editor>.
>
> No surprise. It must be
>
> 	sh -c '"$0" "$@"' <editor> <file>
>
> Note the extra quotes around $0.

... assuming that there is no dq, $var_reference and other nastiness in <editor> ;-)

Johannes Sixt· Mar 10, 2008, 21:46 UTC · re: Junio C Hamano · lore

Re: [PATCH] launch_editor(): allow spaces in the filename

On Monday 10 March 2008 22:37, Junio C Hamano wrote:
Show 17 quoted lines
> Johannes Sixt <johannes.sixt@telecom.at> writes:
> > On Monday 10 March 2008 21:42, Johannes Schindelin wrote:
> >> For some reason, the construct
> >>
> >> 	sh -c "$0 \"$@\"" <editor> <file>
> >>
> >> does not pick up quotes in <editor> as expected.  So replace $0 with
> >> <editor>.
> >
> > No surprise. It must be
> >
> > 	sh -c '"$0" "$@"' <editor> <file>
> >
> > Note the extra quotes around $0.
>
> ... assuming that there is no dq, $var_reference and other nastiness in
> <editor> ;-)

They are not a problem; there isn't yet another level of evaluation. I just tried with an <editor> that goes by this name:

   f "$DISPLAY"b
and I got the expected result, i.e. it was found and executed.
-- Hannes
Johannes Schindelin· Mar 10, 2008, 22:18 UTC · re: Johannes Sixt · lore

Re: [PATCH] launch_editor(): allow spaces in the filename

Hi,
On Mon, 10 Mar 2008, Johannes Sixt wrote:
Show 22 quoted lines
> On Monday 10 March 2008 21:42, Johannes Schindelin wrote:
> > For some reason, the construct
> >
> > 	sh -c "$0 \"$@\"" <editor> <file>
> >
> > does not pick up quotes in <editor> as expected.  So replace $0 with
> > <editor>.
> 
> No surprise. It must be
> 
> 	sh -c '"$0" "$@"' <editor> <file>
> 
> Note the extra quotes around $0.
> 
> >  			args[i++] = "sh";
> >  			args[i++] = "-c";
> > -			args[i++] = "$0 \"$@\"";
> > +			args[i++] = arg0.buf;
> 
> IOW:
> 
> +			args[i++] = "\"$0\" \"$@\"";
Isn't this wrong? What would this do with a core.editor like this:
[core]
	editor = this-editor --with-this --option
Hmm?

Ciao, Dscho

Junio C Hamano· Mar 10, 2008, 21:33 UTC · re: Johannes Schindelin · lore

Re: [PATCH] launch_editor(): allow spaces in the filename

Johannes Schindelin <Johannes.Schindelin@gmx.de> writes:
Show 6 quoted lines
> For some reason, the construct
>
> 	sh -c "$0 \"$@\"" <editor> <file>
>
> does not pick up quotes in <editor> as expected.  So replace $0 with
> <editor>.
That's not "for some reason" but "I am asking shell to split", and
	sh -c '"$@"' ignoreme <editor> <files>...
would work better ;-).
Junio C Hamano· Mar 10, 2008, 21:42 UTC · re: Johannes Schindelin · lore

Re: [PATCH] launch_editor(): allow spaces in the filename

Johannes Schindelin <Johannes.Schindelin@gmx.de> writes:
Show 11 quoted lines
> For some reason, the construct
>
> 	sh -c "$0 \"$@\"" <editor> <file>
>
> does not pick up quotes in <editor> as expected.  So replace $0 with
> <editor>.
>
> This fixes
>
> 	git config core.editor "c:/Program Files/What/Ever.exe"
>

Having sent a few messages about shell quoting, it makes me wonder if this was done very deliberately in the first place, so that you can do things like:

	git config core.editor "emacs -nw"
Blame followed by list archive hunting around that timeperiod would tell.
Junio C Hamano· Mar 10, 2008, 21:48 UTC · re: Junio C Hamano · lore

Re: [PATCH] launch_editor(): allow spaces in the filename

Junio C Hamano <gitster@pobox.com> writes:
Show 14 quoted lines
> Johannes Schindelin <Johannes.Schindelin@gmx.de> writes:
> ...
>> This fixes
>>
>> 	git config core.editor "c:/Program Files/What/Ever.exe"
>>
>
> Having sent a few messages about shell quoting, it makes me wonder if this
> was done very deliberately in the first place, so that you can do things
> like:
>
> 	git config core.editor "emacs -nw"
>
> Blame followed by list archive hunting around that timeperiod would tell.

I'll let others look for the bug report and user request from the list archive, but the pertinent commit is 5e2de4f9 (Fix $EDITOR regression introduced by rewrite in C.):

    When git-tag and git-commit launches the editor, they used to
    honor EDITOR="editor -options args..." but recent rewrite in C
    insisted on $EDITOR to be the path to the editor executable.
    
    This restores the older behaviour.
    
In other words, your example is a user error and your patch is a
regression.
I guess the right way to do that would be:
	git config core.editor '"c:/Program Files/What/Ever.exe"'
but I do not do windows, so this is obviously untested.
Johannes Schindelin· Mar 10, 2008, 22:24 UTC · re: Junio C Hamano · lore

Re: [PATCH] launch_editor(): allow spaces in the filename

Hi,
On Mon, 10 Mar 2008, Junio C Hamano wrote:
Show 29 quoted lines
> Junio C Hamano <gitster@pobox.com> writes:
> 
> > Johannes Schindelin <Johannes.Schindelin@gmx.de> writes:
> > ...
> >> This fixes
> >>
> >> 	git config core.editor "c:/Program Files/What/Ever.exe"
> >>
> >
> > Having sent a few messages about shell quoting, it makes me wonder if this
> > was done very deliberately in the first place, so that you can do things
> > like:
> >
> > 	git config core.editor "emacs -nw"
> >
> > Blame followed by list archive hunting around that timeperiod would tell.
> 
> I'll let others look for the bug report and user request from the list
> archive, but the pertinent commit is 5e2de4f9 (Fix $EDITOR regression
> introduced by rewrite in C.):
> 
>     When git-tag and git-commit launches the editor, they used to
>     honor EDITOR="editor -options args..." but recent rewrite in C
>     insisted on $EDITOR to be the path to the editor executable.
>     
>     This restores the older behaviour.
>     
> In other words, your example is a user error and your patch is a
> regression.
I was well aware of that commit when I wrote the patch, but...
Show 5 quoted lines
> I guess the right way to do that would be:
> 
> 	git config core.editor '"c:/Program Files/What/Ever.exe"'
> 
> but I do not do windows, so this is obviously untested.

Ooops. The commit message I gave did not escape the double quotes, but I _meant_ to, promise! So yes, it was my intention that

	git config core.editor \"C:/Program\ Files/vim/vim71/gvim.exe\"
works, but
	git config core.editor "C:/Program Files/vim/vim71/gvim.exe"
does not.  Note that the test was written correctly.

Sorry, Dscho

Junio C Hamano· Mar 11, 2008, 00:15 UTC · re: Johannes Schindelin · lore

Re: [PATCH] launch_editor(): allow spaces in the filename

Johannes Schindelin <Johannes.Schindelin@gmx.de> writes:
Show 10 quoted lines
> For some reason, the construct
>
> 	sh -c "$0 \"$@\"" <editor> <file>
>
> does not pick up quotes in <editor> as expected.  So replace $0 with
> <editor>.
>
> This fixes
>
> 	git config core.editor "c:/Program Files/What/Ever.exe"

Ah, I misread your patch. The above example needs to be fixed as discussed, but what the patch does to builtin-tag.c is fine.

Show 15 quoted lines
> diff --git a/t/t7005-editor.sh b/t/t7005-editor.sh
> index c1cec55..5395b74 100755
> --- a/t/t7005-editor.sh
> +++ b/t/t7005-editor.sh
> @@ -89,6 +89,34 @@ do
>  	'
>  done
>  
> +test_expect_success 'editor with a space' '
> +
> +	if echo "echo space > \"\$1\"" > "e space.sh"
> +	then
> +		chmod a+x "e space.sh" &&
> +		GIT_EDITOR="./e\ space.sh" \
> +			git --exec-path=. commit --amend &&

Why do you need --exec-path=. to tell the newly built git to find its subcommands from t/trash directory, when you give an explicit instruction to find GIT_EDITOR in "./"?

Johannes Schindelin· Mar 11, 2008, 09:56 UTC · re: Junio C Hamano · lore
For some reason, the construct
	sh -c "$0 \"$@\"" <editor> <file>

does not pick up quotes in <editor> as expected. So replace $0 with <editor>.

This fixes
	git config core.editor '"c:/Program Files/What/Ever.exe"'

In other words, you can specify an editor with spaces in its path using a config containing something like this:

	[core]
		editor = \"c:/Program Files/Darn/Spaces.exe\"
NOTE: we cannot just replace the $0 with \"$0\", because we still want 
this to work:
	[core]
		editor = emacs -nw
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
---
	On Mon, 10 Mar 2008, Junio C Hamano wrote:
	> Johannes Schindelin <Johannes.Schindelin@gmx.de> writes:
	> 
	> > For some reason, the construct
	> >
	> > 	sh -c "$0 \"$@\"" <editor> <file>
	> >
	> > does not pick up quotes in <editor> as expected.  So replace 
	> > $0 with <editor>.
	> >
	> > This fixes
	> >
	> > 	git config core.editor "c:/Program Files/What/Ever.exe"
	> 
	> Ah, I misread your patch.  The above example needs to be fixed 
	> as discussed, but what the patch does to builtin-tag.c is fine.
	Here comes the fixed commit message and...
	> > diff --git a/t/t7005-editor.sh b/t/t7005-editor.sh
	> > index c1cec55..5395b74 100755
	> > --- a/t/t7005-editor.sh
	> > +++ b/t/t7005-editor.sh
	> > @@ -89,6 +89,34 @@ do
	> >  	'
	> >  done
	> >  
	> > +test_expect_success 'editor with a space' '
	> > +
	> > +	if echo "echo space > \"\$1\"" > "e space.sh"
	> > +	then
	> > +		chmod a+x "e space.sh" &&
	> > +		GIT_EDITOR="./e\ space.sh" \
	> > +			git --exec-path=. commit --amend &&
	> 
	> Why do you need --exec-path=. to tell the newly built git to 
	> find its subcommands from t/trash directory, when you give an explicit 
	> instruction to find GIT_EDITOR in "./"?
	... the fixed tests.
	I copied this piece from the earlier tests.  Probably it is only 
	needed to avoid "vi" from being found in the PATH (--exec-path's argument 
	is prepended to the PATH).
 builtin-tag.c     |    6 +++++-
 t/t7005-editor.sh |   27 +++++++++++++++++++++++++++
 2 files changed, 32 insertions(+), 1 deletions(-)
Show changes to 2 files +32 −1

builtin-tag.c, t/t7005-editor.sh

diff --git a/builtin-tag.c b/builtin-tag.c
index 28c36fd..8dd959f 100644
--- a/builtin-tag.c
+++ b/builtin-tag.c
@@ -50,12 +50,15 @@ void launch_editor(const char *path, struct strbuf *buffer, const char *const *e
 		size_t len = strlen(editor);
 		int i = 0;
 		const char *args[6];
+		struct strbuf arg0;
 
+		strbuf_init(&arg0, 0);
 		if (strcspn(editor, "$ \t'") != len) {
 			/* there are specials */
+			strbuf_addf(&arg0, "%s \"$@\"", editor);
 			args[i++] = "sh";
 			args[i++] = "-c";
-			args[i++] = "$0 \"$@\"";
+			args[i++] = arg0.buf;
 		}
 		args[i++] = editor;
 		args[i++] = path;
@@ -63,6 +66,7 @@ void launch_editor(const char *path, struct strbuf *buffer, const char *const *e
 
 		if (run_command_v_opt_cd_env(args, 0, NULL, env))
 			die("There was a problem with the editor %s.", editor);
+		strbuf_release(&arg0);
 	}
 
 	if (!buffer)
diff --git a/t/t7005-editor.sh b/t/t7005-editor.sh
index c1cec55..6a74b3a 100755
--- a/t/t7005-editor.sh
+++ b/t/t7005-editor.sh
@@ -89,6 +89,33 @@ do
 	'
 done
 
+test_expect_success 'editor with a space' '
+
+	if echo "echo space > \"\$1\"" > "e space.sh"
+	then
+		chmod a+x "e space.sh" &&
+		GIT_EDITOR="./e\ space.sh" git commit --amend &&
+		test space = "$(git show -s --pretty=format:%s)"
+	else
+		say "Skipping; FS does not support spaces in filenames"
+	fi
+
+'
+
+unset GIT_EDITOR
+test_expect_success 'core.editor with a space' '
+
+	if test -f "e space.sh"
+	then
+		git config core.editor \"./e\ space.sh\" &&
+		git commit --amend &&
+		test space = "$(git show -s --pretty=format:%s)"
+	else
+		say "Skipping; FS does not support spaces in filenames"
+	fi
+
+'
+
 TERM="$OLD_TERM"
 
 test_done
-- 
1.5.4.4.643.g7cb9b.dirty

← back to recent threads