threads / discuss / 12818

git-config: aliases with parameter expansion ($1 and such)

Subject: git-config: aliases with parameter expansion ($1 and such)

## tl;dr

13 messages between Mar 23, 2008 and Mar 24, 2008.

replies: 12people: 5as markdown or json

Dirk Süsserott· Mar 23, 2008, 13:28 UTC · lore

Hi, I've a question concerning git config and the alias things. I'd like to create an alias 'atag' that creates an annotated tag with the message being the same as the tag. Usually I create annotated tags like this:

$ git tag -a -m "mytag" mytag

(BTW: It's not documented, but I have the impression that the '-a' switch is implicitely added when the '-m' switch is present. Is that true?)

I'd like to have an alias like this:
[alias]
    atag = tag -a -m "$1"
So that I can simply call
$ git atag mytag

Is it possible to pass parameters to the alias definition? With the definition above I got a tag with message "$1".

Thanks, Dirk
Jay Soffian· Mar 23, 2008, 13:45 UTC · re: Dirk Süsserott · lore

Re: git-config: aliases with parameter expansion ($1 and such)

On Sun, Mar 23, 2008 at 9:28 AM, Dirk Süsserott <newsletter@dirk.my1.cc> wrote:
Show 22 quoted lines
> Hi, I've a question concerning git config and the alias things.
>  I'd like to create an alias 'atag' that creates an annotated
>  tag with the message being the same as the tag. Usually I create
>  annotated tags like this:
>
>  $ git tag -a -m "mytag" mytag
>
>  (BTW: It's not documented, but I have the impression that
>  the '-a' switch is implicitely added when the '-m' switch
>  is present. Is that true?)
>
>  I'd like to have an alias like this:
>
>  [alias]
>     atag = tag -a -m "$1"
>
>  So that I can simply call
>
>  $ git atag mytag
>
>  Is it possible to pass parameters to the alias definition?
>  With the definition above I got a tag with message "$1".
atag = !git tag -a -m "$1"
j.
Dirk Süsserott· Mar 23, 2008, 14:04 UTC · re: Jay Soffian · lore

Re: git-config: aliases with parameter expansion ($1 and such)

Jay Soffian schrieb:
Show 30 quoted lines
> On Sun, Mar 23, 2008 at 9:28 AM, Dirk Süsserott <newsletter@dirk.my1.cc> wrote:
>   
>> Hi, I've a question concerning git config and the alias things.
>>  I'd like to create an alias 'atag' that creates an annotated
>>  tag with the message being the same as the tag. Usually I create
>>  annotated tags like this:
>>
>>  $ git tag -a -m "mytag" mytag
>>
>>  (BTW: It's not documented, but I have the impression that
>>  the '-a' switch is implicitely added when the '-m' switch
>>  is present. Is that true?)
>>
>>  I'd like to have an alias like this:
>>
>>  [alias]
>>     atag = tag -a -m "$1"
>>
>>  So that I can simply call
>>
>>  $ git atag mytag
>>
>>  Is it possible to pass parameters to the alias definition?
>>  With the definition above I got a tag with message "$1".
>>     
>
> atag = !git tag -a -m "$1"
>
> j.
>   
Thanks for the quick reply. Did you try that out? I did
$ git atag foo

with your definition and got a tag with message "$1" and the tag's name was "'foo'" (surrounded by single quotes).

If you tried it and it worked (with linux), then it might be a problem with my environment: I'm using Git with windows and the bash (Git-1.5.4-preview20080202.exe). Usually that works very good, but sometimes it doesn't. Probably this is one of the few "it doesn't" things.

Dirk
Johannes Schindelin· Mar 23, 2008, 14:19 UTC · re: Dirk Süsserott · lore

Re: git-config: aliases with parameter expansion ($1 and such)

Hi,
On Sun, 23 Mar 2008, Dirk Süsserott wrote:
Show 29 quoted lines
> Jay Soffian schrieb:
> > On Sun, Mar 23, 2008 at 9:28 AM, Dirk Süsserott <newsletter@dirk.my1.cc>
> > wrote:
> >   
> > > Hi, I've a question concerning git config and the alias things.
> > >  I'd like to create an alias 'atag' that creates an annotated
> > >  tag with the message being the same as the tag. Usually I create
> > >  annotated tags like this:
> > >
> > >  $ git tag -a -m "mytag" mytag
> > >
> > >  (BTW: It's not documented, but I have the impression that
> > >  the '-a' switch is implicitely added when the '-m' switch
> > >  is present. Is that true?)
> > >
> > >  I'd like to have an alias like this:
> > >
> > >  [alias]
> > >     atag = tag -a -m "$1"
> > >
> > >  So that I can simply call
> > >
> > >  $ git atag mytag
> > >
> > >  Is it possible to pass parameters to the alias definition?
> > >  With the definition above I got a tag with message "$1".
> > >     
> >
> > atag = !git tag -a -m "$1"
Why so complicated?
	atag = tag -a -m
should work already.

Hth, Dscho

Dirk Süsserott· Mar 23, 2008, 14:31 UTC · re: Johannes Schindelin · lore

Re: git-config: aliases with parameter expansion ($1 and such)

Johannes Schindelin schrieb:
Show 46 quoted lines
> Hi,
>
> On Sun, 23 Mar 2008, Dirk Süsserott wrote:
>
>   
>> Jay Soffian schrieb:
>>     
>>> On Sun, Mar 23, 2008 at 9:28 AM, Dirk Süsserott <newsletter@dirk.my1.cc>
>>> wrote:
>>>   
>>>       
>>>> Hi, I've a question concerning git config and the alias things.
>>>>  I'd like to create an alias 'atag' that creates an annotated
>>>>  tag with the message being the same as the tag. Usually I create
>>>>  annotated tags like this:
>>>>
>>>>  $ git tag -a -m "mytag" mytag
>>>>
>>>>  (BTW: It's not documented, but I have the impression that
>>>>  the '-a' switch is implicitely added when the '-m' switch
>>>>  is present. Is that true?)
>>>>
>>>>  I'd like to have an alias like this:
>>>>
>>>>  [alias]
>>>>     atag = tag -a -m "$1"
>>>>
>>>>  So that I can simply call
>>>>
>>>>  $ git atag mytag
>>>>
>>>>  Is it possible to pass parameters to the alias definition?
>>>>  With the definition above I got a tag with message "$1".
>>>>     
>>>>         
>>> atag = !git tag -a -m "$1"
>>>       
>
> Why so complicated?
>
> 	atag = tag -a -m
>
> should work already.
>
> Hth,
> Dscho
Almost, but not what I want. With
    atag = tag -a -m
I still have to say
    $ git atag mytag mytag
But I just wanna say
    $ git atag mytag
to create an annotated tag as if I've said
    $ git tag -a -m "mytag" mytag
Dirk
Jeff King· Mar 23, 2008, 19:38 UTC · re: Johannes Schindelin · lore

Re: git-config: aliases with parameter expansion ($1 and such)

On Sun, Mar 23, 2008 at 03:19:01PM +0100, Johannes Schindelin wrote:
Show 7 quoted lines
> > > atag = !git tag -a -m "$1"
> 
> Why so complicated?
> 
> 	atag = tag -a -m
> 
> should work already.

The problem is that he needs to duplicate the final argument. I.e., "git atag foo" becomes "git atag -a -m foo foo".

-Peff
Jeff King· Mar 23, 2008, 19:38 UTC · re: Dirk Süsserott · lore

Re: git-config: aliases with parameter expansion ($1 and such)

On Sun, Mar 23, 2008 at 03:04:50PM +0100, Dirk Süsserott wrote:
Show 7 quoted lines
>> atag = !git tag -a -m "$1"
> Thanks for the quick reply. Did you try that out? I did
>
> $ git atag foo
>
> with your definition and got a tag with message "$1" and
> the tag's name was "'foo'" (surrounded by single quotes).

If you want to use positional parameters, you have to invoke the shell explicitly. Try:

  [alias]
    atag = !sh -c 'git tag -a -m "$0" "$0"'

As an aside, this should be "$1" but it looks like we don't pass the shell a proper argv[0].

-Peff
Jeff King· Mar 23, 2008, 19:45 UTC · re: Jeff King · lore

Re: git-config: aliases with parameter expansion ($1 and such)

On Sun, Mar 23, 2008 at 03:38:42PM -0400, Jeff King wrote:
>     atag = !sh -c 'git tag -a -m "$0" "$0"'
> 
> As an aside, this should be "$1" but it looks like we don't pass the
> shell a proper argv[0].

Hmm, I take it back. Git is doing the right thing, but it is a quirk of the shell that it counts the first argument as the "name of the script" even though the script actually comes from "-c".

  $ sh -c 'echo $0' foo
  foo
  $ echo 'echo $0' | sh
  sh
-Peff
Jay Soffian· Mar 24, 2008, 01:54 UTC · re: Jeff King · lore

Re: git-config: aliases with parameter expansion ($1 and such)

On Sun, Mar 23, 2008 at 3:45 PM, Jeff King <peff@peff.net> wrote:
Show 8 quoted lines
>  Hmm, I take it back. Git is doing the right thing, but it is a quirk of
>  the shell that it counts the first argument as the "name of the script"
>  even though the script actually comes from "-c".
>
>   $ sh -c 'echo $0' foo
>   foo
>   $ echo 'echo $0' | sh
>   sh
All my shell aliases therefor look like:
h          = "!sh -c 'git alias \"$1\" || git help \"$1\"' -"
(Notice the trailing '-'.)
j.
Junio C Hamano· Mar 24, 2008, 02:47 UTC · re: Jay Soffian · lore

Re: git-config: aliases with parameter expansion ($1 and such)

"Jay Soffian" <jaysoffian@gmail.com> writes:
Show 5 quoted lines
> All my shell aliases therefor look like:
>
> h          = "!sh -c 'git alias \"$1\" || git help \"$1\"' -"
>
> (Notice the trailing '-'.)
Just for my curiosity, why aren't they
> h          = "!sh -c 'git alias \"$0\" || git help \"$0\"'"
without trailing '-'?
Dirk Süsserott· Mar 24, 2008, 13:15 UTC · re: Junio C Hamano · lore

Re: git-config: aliases with parameter expansion ($1 and such)

Thanks for the help. Both
    (1) atag = !sh -c 'git tag -a -m "$0" "$0"'
and
    (2) atag = !sh -c 'git tag -a -m "$1" "$1"' -
work, but in the latter case the '-' is indeed crucial.
If not given, $1 is not set and git tag complains badly.
When called without any args, (1) sets a tag called "sh"
whereas (2) complains about the missing value for the -m option.
So I decided to use the second form. Shells are funny things. ;-)
BTW: Is it true that "-a" is implicitely added to "git tag"
when the "-m" switch is present, i.e. are
    $ git tag -m foo foo
and
    $ git tag -a -m foo foo
equivalent? Thought so, but it's not in the docs.
Junio C Hamano schrieb:
Show 18 quoted lines
> "Jay Soffian" <jaysoffian@gmail.com> writes:
>
>   
>> All my shell aliases therefor look like:
>>
>> h          = "!sh -c 'git alias \"$1\" || git help \"$1\"' -"
>>
>> (Notice the trailing '-'.)
>>     
>
> Just for my curiosity, why aren't they
>
>   
>> h          = "!sh -c 'git alias \"$0\" || git help \"$0\"'"
>>     
>
> without trailing '-'?
>   
Jeff King· Mar 24, 2008, 19:29 UTC · re: Dirk Süsserott · lore

Re: git-config: aliases with parameter expansion ($1 and such)

On Mon, Mar 24, 2008 at 02:15:21PM +0100, Dirk Süsserott wrote:
Show 9 quoted lines
> Thanks for the help. Both
>    (1) atag = !sh -c 'git tag -a -m "$0" "$0"'
> and
>    (2) atag = !sh -c 'git tag -a -m "$1" "$1"' -
> work, but in the latter case the '-' is indeed crucial.
> If not given, $1 is not set and git tag complains badly.
> When called without any args, (1) sets a tag called "sh"
> whereas (2) complains about the missing value for the -m option.
> So I decided to use the second form. Shells are funny things. ;-)

I hadn't thought of that when I gave (1) as an example. I think (2) is clearly superior for that reason.

Show 6 quoted lines
> BTW: Is it true that "-a" is implicitely added to "git tag"
> when the "-m" switch is present, i.e. are
>    $ git tag -m foo foo
> and
>    $ git tag -a -m foo foo
> equivalent? Thought so, but it's not in the docs.

Yes, see builtin-tag:430-433 (it is also implied by -F). Care to submit a documentation patch?

-Peff
Jay Soffian· Mar 24, 2008, 14:54 UTC · re: Junio C Hamano · lore

Re: git-config: aliases with parameter expansion ($1 and such)

On Sun, Mar 23, 2008 at 10:47 PM, Junio C Hamano <gitster@pobox.com> wrote:
Show 13 quoted lines
> "Jay Soffian" <jaysoffian@gmail.com> writes:
>
>  > All my shell aliases therefor look like:
>  >
>  > h          = "!sh -c 'git alias \"$1\" || git help \"$1\"' -"
>  >
>  > (Notice the trailing '-'.)
>
>  Just for my curiosity, why aren't they
>
>  > h          = "!sh -c 'git alias \"$0\" || git help \"$0\"'"
>
>  without trailing '-'?

Lost to the mists of time. But I think for consistency with other aliases where I was checking $#. Maybe.

j.

← back to recent threads