Skip to contentSkip to conversation

builtin-tag.c: allow arguments in $EDITOR

PATCH New Luciano Rocha 4 participants 7 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 builtin-tag.c: allow arguments in $EDITOR

The previous sh version of git-commit evaluated the value of the defined editor, thus allowing arguments.

Make the builtin version work the same, by adding an explicit check for arguments in the editor command, and extract them to an additional argument.

---
 builtin-tag.c |   13 ++++++++++++-
 1 files changed, 12 insertions(+), 1 deletions(-)

I personally use EDITOR="gvim -f", thus this patch.

Created on top of ce85b053d827e2f7c2ee2683cc09393e4768cc22, git-describe is now: v1.5.4-rc0-75-g5f791e5

builtin-tag.c +12 -1
index 274901a..57dcfe0 100644
@@ -46,7 +46,18 @@ void launch_editor(const char *path, struct strbuf *buffer, const char *const *e
 	if (!editor)
 		editor = "vi";
 
-	if (strcmp(editor, ":")) {
+	if (strstr(editor, " -")) {
+		char *editor_cmd = xstrdup(editor);
+		char *editor_sep = strstr(editor_cmd, " -");
+		const char *args[] = { editor_cmd, editor_sep + 1,
+			path, NULL };
+
+		*editor_sep = '\0';
+
+		if (run_command_v_opt_cd_env(args, 0, NULL, env))
+			die("There was a problem with the editor %s.",
+					editor_cmd);
+	} else if (strcmp(editor, ":")) {
 		const char *args[] = { editor, path, NULL };
 
 		if (run_command_v_opt_cd_env(args, 0, NULL, env))
Signature · 2 lines
Luciano Rocha <[email protected]>
Eurotux Informática, S.A. <http://www.eurotux.com/>
#1 lore raw

PATCH v2 1/1 builtin-tag.c: allow arguments in $EDITOR

The previous sh version of git-commit evaluated the value of the defined editor, thus allowing arguments.

Make the builtin version work the same, by adding an explicit check for arguments in the editor command, and extract them to an additional argument.

---
 builtin-tag.c |   14 +++++++++++++-
 1 files changed, 13 insertions(+), 1 deletions(-)

I personally use EDITOR="gvim -f", thus this patch. Now with free() of temporary buffer.

builtin-tag.c +13 -1
index 274901a..0e8575e 100644
@@ -46,7 +46,19 @@ void launch_editor(const char *path, struct strbuf *buffer, const char *const *e
 	if (!editor)
 		editor = "vi";
 
-	if (strcmp(editor, ":")) {
+	if (strstr(editor, " -")) {
+		char *editor_cmd = xstrdup(editor);
+		char *editor_sep = strstr(editor_cmd, " -");
+		const char *args[] = { editor_cmd, editor_sep + 1,
+			path, NULL };
+
+		*editor_sep = '\0';
+
+		if (run_command_v_opt_cd_env(args, 0, NULL, env))
+			die("There was a problem with the editor %s.",
+					editor_cmd);
+		free(editor_cmd);
+	} else if (strcmp(editor, ":")) {
 		const char *args[] = { editor, path, NULL };
 
 		if (run_command_v_opt_cd_env(args, 0, NULL, env))
Signature · 2 lines
Luciano Rocha <[email protected]>
Eurotux Informática, S.A. <http://www.eurotux.com/>
#2 lore raw

Hi,

Quoted 6 lines · Luciano Rocha

The previous sh version of git-commit evaluated the value of the defined editor, thus allowing arguments.

Make the builtin version work the same, by adding an explicit check for arguments in the editor command, and extract them to an additional argument.

Anything wrong with that patch?

http://article.gmane.org/gmane.comp.version-control.git/68444

Ciao,
Dscho

#3 lore raw
Quoted 7 lines · Johannes Schindelin

Hi,

Quoted 3 lines · Luciano Rocha

The previous sh version of git-commit evaluated the value of the defined editor, thus allowing arguments.

Quoted 7 lines
Quoted 3 lines

Make the builtin version work the same, by adding an explicit check for arguments in the editor command, and extract them to an additional argument.

Anything wrong with that patch?

http://article.gmane.org/gmane.comp.version-control.git/68444

No, I just missed it in the mailing list. That patch also supports any number of whitespace/arguments.

Signature · 2 lines
Luciano Rocha <[email protected]>
Eurotux Informática, S.A. <http://www.eurotux.com/>
#4 lore raw
In reply to Johannes Schindelin
Quoted 3 lines · Johannes Schindelin

Anything wrong with that patch?

http://article.gmane.org/gmane.comp.version-control.git/68444

I think Steven stopped after you poked holes in that patch.

The way scripted commands spawned editor is:

	eval "${GIT_EDITOR:=vi}" '"$@"'

which meant that $IFS characters in $GIT_EDITOR separated words and $environment_variables were substituted.

IOW, this is possible:

GIT_EDITOR='emacs -l $HOME/my-customization.el'

I think something like this patch is probably more appropriate. It avoids potential bugs in splitting arguments by hand and lets the shell deal with the issue.

---
 builtin-tag.c |   14 +++++++++++++-
 1 files changed, 13 insertions(+), 1 deletions(-)
builtin-tag.c +13 -1
index 274901a..fae2487 100644
@@ -47,7 +47,19 @@ void launch_editor(const char *path, struct strbuf *buffer, const char *const *e
 		editor = "vi";
 
 	if (strcmp(editor, ":")) {
-		const char *args[] = { editor, path, NULL };
+		size_t len = strlen(editor);
+		int i = 0;
+		const char *args[6];
+
+		if (strcspn(editor, "$ \t'") != len) {
+			/* there are specials */
+			args[i++] = "sh";
+			args[i++] = "-c";
+			args[i++] = "$0 \"$@\"";
+		}
+		args[i++] = editor;
+		args[i++] = path;
+		args[i] = NULL;
 
 		if (run_command_v_opt_cd_env(args, 0, NULL, env))
 			die("There was a problem with the editor %s.", editor);
#5 lore raw
Quoted 1 line · Junio C Hamano

I think Steven stopped after you poked holes in that patch.

Nah, just entered a particularly busy period in my day job and haven't had time to do much more git stuff than occasionally skim the mailing list. I do plan to revisit that at some point unless the patch in your mail ends up being what we go with. (It seems like a sensible approach to me.)

-Steve

#6 lore raw
In reply to Junio C Hamano

Hi,

Quoted 1 line · Junio C Hamano

I think something like this patch is probably more appropriate.

Looks obviously fine, especially thinking about this:

Quoted 1 line

GIT_EDITOR='emacs -l $HOME/my-customization.el'

Ciao,
Dscho