threads / patch / 8796

patchMake '!' aliases more useful

Subject: [PATCH] Make '!' aliases more useful

## tl;dr

11 messages between Jul 1, 2007 and Jul 3, 2007. Diffs are folded; open one to read it.

replies: 10people: 5as markdown or json

Johannes Schindelin· Jul 1, 2007, 21:51 UTC · lore

When an alias starts with an exclamation mark, the rest is interpreted as a shell command. However, all arguments passed to git used to be ignored.

Now you can have an alias like
	$ git config alias.e '!echo'
and
	$ git e Hello World
does what you expect it to do.
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
---
	I have this in my $HOME/.gitconfig:
		[alias]
			debug = !GIT_PAGER= gdb --args git
	which allows me to debug, say "git log a..b" by saying "git debug 
	log a..b".
 git.c |   15 +++++++++++++++
 1 files changed, 15 insertions(+), 0 deletions(-)
Show changes to git.c +15 −0
diff --git a/git.c b/git.c
index 8ea70da..32e8aa9 100644
--- a/git.c
+++ b/git.c
@@ -205,6 +205,21 @@ static int handle_alias(int *argcp, const char ***argv)
 	git_config(git_alias_config);
 	if (alias_string) {
 		if (alias_string[0] == '!') {
+			if (*argcp > 1) {
+				int i, sz = PATH_MAX;
+				char *s = xmalloc(sz), *new_alias = s;
+
+				add_to_string(&s, &sz, alias_string, 0);
+				free(alias_string);
+				alias_string = new_alias;
+				for (i = 1; i < *argcp &&
+					!add_to_string(&s, &sz, " ", 0) &&
+					!add_to_string(&s, &sz, (*argv)[i], 1)
+					; i++)
+					; /* do nothing */
+				if (!sz)
+					die("Too many or long arguments");
+			}
 			trace_printf("trace: alias to shell cmd: %s => %s\n",
 				     alias_command, alias_string + 1);
 			ret = system(alias_string + 1);
-- 
1.5.2.2.3276.gf2524-dirty
Theodore Tso· Jul 2, 2007, 14:55 UTC · re: Johannes Schindelin · lore

Re: [PATCH] Make '!' aliases more useful

On Sun, Jul 01, 2007 at 10:51:58PM +0100, Johannes Schindelin wrote:
Show 14 quoted lines
> 
> When an alias starts with an exclamation mark, the rest is interpreted
> as a shell command. However, all arguments passed to git used to be
> ignored.
> 
> Now you can have an alias like
> 
> 	$ git config alias.e '!echo'
> 
> and
> 
> 	$ git e Hello World
> 
> does what you expect it to do.

But what if you don't want the argument passed at the end of the alias, but somewhere else? I suspect the better answer would be to support $* and $1, $2, $3, et. al interpolation, no? It was on my list of things to do when I had a spare moment, but I never got around to it.

					- Ted
Johannes Schindelin· Jul 2, 2007, 15:55 UTC · re: Theodore Tso · lore

Re: [PATCH] Make '!' aliases more useful

Hi,
On Mon, 2 Jul 2007, Theodore Tso wrote:
Show 21 quoted lines
> On Sun, Jul 01, 2007 at 10:51:58PM +0100, Johannes Schindelin wrote:
> > 
> > When an alias starts with an exclamation mark, the rest is interpreted
> > as a shell command. However, all arguments passed to git used to be
> > ignored.
> > 
> > Now you can have an alias like
> > 
> > 	$ git config alias.e '!echo'
> > 
> > and
> > 
> > 	$ git e Hello World
> > 
> > does what you expect it to do.
> 
> But what if you don't want the argument passed at the end of the
> alias, but somewhere else?  I suspect the better answer would be to
> support $* and $1, $2, $3, et. al interpolation, no?  It was on my
> list of things to do when I had a spare moment, but I never got around
> to it.

There is a point where you do not want to complicate git, but rather write a script. This is such a point IMHO.

Ciao, Dscho

Theodore Tso· Jul 2, 2007, 16:08 UTC · re: Johannes Schindelin · lore

Re: [PATCH] Make '!' aliases more useful

On Mon, Jul 02, 2007 at 04:55:24PM +0100, Johannes Schindelin wrote:
Show 8 quoted lines
> > But what if you don't want the argument passed at the end of the
> > alias, but somewhere else?  I suspect the better answer would be to
> > support $* and $1, $2, $3, et. al interpolation, no?  It was on my
> > list of things to do when I had a spare moment, but I never got around
> > to it.
> 
> There is a point where you do not want to complicate git, but rather write 
> a script. This is such a point IMHO.

Such a point exists, I agree, but I would draw after $* and $1/$2/$3 interpolation. There is a lot more value that gets added with positional arguments support, and it makes git aliases more usable on platforms such as Windows where scripting capability is much more limited.

Regards,
					- Ted
Junio C Hamano· Jul 2, 2007, 23:11 UTC · re: Theodore Tso · lore

Re: [PATCH] Make '!' aliases more useful

Theodore Tso <tytso@mit.edu> writes:
Show 15 quoted lines
> On Mon, Jul 02, 2007 at 04:55:24PM +0100, Johannes Schindelin wrote:
>> > But what if you don't want the argument passed at the end of the
>> > alias, but somewhere else?  I suspect the better answer would be to
>> > support $* and $1, $2, $3, et. al interpolation, no?  It was on my
>> > list of things to do when I had a spare moment, but I never got around
>> > to it.
>> 
>> There is a point where you do not want to complicate git, but rather write 
>> a script. This is such a point IMHO.
>
> Such a point exists, I agree, but I would draw after $* and $1/$2/$3
> interpolation.  There is a lot more value that gets added with
> positional arguments support, and it makes git aliases more usable on
> platforms such as Windows where scripting capability is much more
> limited.
That actually sounds sensible, but you could alias
	!sh -c 'command $2 $1 $3'
to reorder the parameters, couldn't you?
	
Theodore Tso· Jul 3, 2007, 01:14 UTC · re: Junio C Hamano · lore

Re: [PATCH] Make '!' aliases more useful

On Mon, Jul 02, 2007 at 04:11:21PM -0700, Junio C Hamano wrote:
Show 11 quoted lines
> > Such a point exists, I agree, but I would draw after $* and $1/$2/$3
> > interpolation.  There is a lot more value that gets added with
> > positional arguments support, and it makes git aliases more usable on
> > platforms such as Windows where scripting capability is much more
> > limited.
> 
> That actually sounds sensible, but you could alias
> 
> 	!sh -c 'command $2 $1 $3'
> 
> to reorder the parameters, couldn't you?
Um, how would that work on the Windows platform?

(I'm assuming here that we are trying to support Windows better, since there are projects such as Mozilla, and MySQL that care very much about first class Windows support. We could say this isn't important enough, and that would be fine; but I wanted to at least raise the question.)

						- Ted
Johannes Schindelin· Jul 3, 2007, 01:37 UTC · re: Theodore Tso · lore

Re: [PATCH] Make '!' aliases more useful

Hi,
On Mon, 2 Jul 2007, Theodore Tso wrote:
Show 20 quoted lines
> On Mon, Jul 02, 2007 at 04:11:21PM -0700, Junio C Hamano wrote:
> > > Such a point exists, I agree, but I would draw after $* and $1/$2/$3
> > > interpolation.  There is a lot more value that gets added with
> > > positional arguments support, and it makes git aliases more usable on
> > > platforms such as Windows where scripting capability is much more
> > > limited.
> > 
> > That actually sounds sensible, but you could alias
> > 
> > 	!sh -c 'command $2 $1 $3'
> > 
> > to reorder the parameters, couldn't you?
> 
> Um, how would that work on the Windows platform?
> 
> (I'm assuming here that we are trying to support Windows better, since
> there are projects such as Mozilla, and MySQL that care very much
> about first class Windows support.  We could say this isn't important
> enough, and that would be fine; but I wanted to at least raise the
> question.)

Windows support for those people unwilling to use cygwin amounts to anything, but a command line interface. Since aliases are _bound_ to the command line, I do not really get your point here.

In my view of things, for Windows support it is first of all important to get rid of the dependency on bash in the core parts. Then we should continue on the work Han-Wen did with his gub, and produce a proper Windows-clicky-style installer which installs git-gui with a proper Windows Start Menu item. But of course, then we should brace ourselves for a lot of clueless^Winterested users.

Ciao, Dscho

Theodore Tso· Jul 3, 2007, 12:10 UTC · re: Johannes Schindelin · lore

Re: [PATCH] Make '!' aliases more useful

On Tue, Jul 03, 2007 at 02:37:43AM +0100, Johannes Schindelin wrote:
> Windows support for those people unwilling to use cygwin amounts to 
> anything, but a command line interface. Since aliases are _bound_ to 
> the command line, I do not really get your point here.
You're right of course.  <small>Never mind...</small>
						- Ted
Johannes Sixt· Jul 3, 2007, 07:24 UTC · re: Theodore Tso · lore

Re: [PATCH] Make '!' aliases more useful

Theodore Tso wrote:
Show 15 quoted lines
> 
> On Mon, Jul 02, 2007 at 04:11:21PM -0700, Junio C Hamano wrote:
> > > Such a point exists, I agree, but I would draw after $* and $1/$2/$3
> > > interpolation.  There is a lot more value that gets added with
> > > positional arguments support, and it makes git aliases more usable on
> > > platforms such as Windows where scripting capability is much more
> > > limited.
> >
> > That actually sounds sensible, but you could alias
> >
> >       !sh -c 'command $2 $1 $3'
> >
> > to reorder the parameters, couldn't you?
> 
> Um, how would that work on the Windows platform?

Shouldn't be a problem for Cygwin users, nor for MinGW port users. The latter need a Bourne shell anyway for the various scripted git tools.

-- Hannes
Alex Riesen· Jul 3, 2007, 07:07 UTC · re: Theodore Tso · lore

Re: [PATCH] Make '!' aliases more useful

On 7/2/07, Theodore Tso <tytso@mit.edu> wrote:
Show 15 quoted lines
> On Mon, Jul 02, 2007 at 04:55:24PM +0100, Johannes Schindelin wrote:
> > > But what if you don't want the argument passed at the end of the
> > > alias, but somewhere else?  I suspect the better answer would be to
> > > support $* and $1, $2, $3, et. al interpolation, no?  It was on my
> > > list of things to do when I had a spare moment, but I never got around
> > > to it.
> >
> > There is a point where you do not want to complicate git, but rather write
> > a script. This is such a point IMHO.
>
> Such a point exists, I agree, but I would draw after $* and $1/$2/$3
> interpolation.  There is a lot more value that gets added with
> positional arguments support, and it makes git aliases more usable on
> platforms such as Windows where scripting capability is much more
> limited.

I don't think it is _possible_ to make it work on Windows properly: you have to quote the arguments with whitespaces as there is nothing like argv there (windows programs don't use command line, so it does not exist). As the quoting is implemented in the programs the rules are all different and mostly suboptimal.

Johannes Schindelin· Jul 3, 2007, 11:27 UTC · re: Alex Riesen · lore

Re: [PATCH] Make '!' aliases more useful

Hi,
On Tue, 3 Jul 2007, Alex Riesen wrote:
Show 21 quoted lines
> On 7/2/07, Theodore Tso <tytso@mit.edu> wrote:
> > On Mon, Jul 02, 2007 at 04:55:24PM +0100, Johannes Schindelin wrote:
> > > > But what if you don't want the argument passed at the end of the
> > > > alias, but somewhere else?  I suspect the better answer would be to
> > > > support $* and $1, $2, $3, et. al interpolation, no?  It was on my
> > > > list of things to do when I had a spare moment, but I never got around
> > > > to it.
> > >
> > > There is a point where you do not want to complicate git, but rather
> > write
> > > a script. This is such a point IMHO.
> > 
> > Such a point exists, I agree, but I would draw after $* and $1/$2/$3
> > interpolation.  There is a lot more value that gets added with
> > positional arguments support, and it makes git aliases more usable on
> > platforms such as Windows where scripting capability is much more
> > limited.
> 
> I don't think it is _possible_ to make it work on Windows properly: you have
> to quote the arguments with whitespaces as there is nothing like argv there
> (windows programs don't use command line, so it does not exist).

Since the "!" aliases use system() (i.e. no argv, but a plain string as parameter), my patch already does that. It quotes the arguments properly.

> As the quoting is implemented in the programs the rules are all different
> and mostly suboptimal.

You mean the programs that could be called via "!" aliases? Yes, that is right. And there is also the problem that you have to convert path names if you are working on Windows, because of this c:\&@!§$ braindamage.

However, in this case Junio's idea to use the shell to do the hard work is again an alternative:

	[alias]
		alex = !sh -c 'd:\program.exe $(cygpath -w "$1")'

FWIW I do not think that executing the shell from time to time is a big problem. It's only that I would like to get rid of the dependency on perl and bash for the plain Windows user, who will not dare to leave the gui into the command line jungle where there be bisons, yaccs and kills.

Ciao, Dscho

← back to recent threads