# Help creating git alias

15 messages from 2013-10-30 to 2013-10-31. Participants: Eugene Sajine, Andrew Ardill, Ralf Thielow, Junio C Hamano, David Aguilar.
Thread: https://gitlist.dev/t/35249

## Eugene Sajine, 2013-10-30 19:34

Subject: Help creating git alias
Message-ID: <CAPZPVFbiSx8n0W1kcczCdC6ioVuWpwuUQ_pc9T=7i4X_FuZNhg@mail.gmail.com>
URL: https://gitlist.dev/e/CAPZPVFbiSx8n0W1kcczCdC6ioVuWpwuUQ_pc9T%3D7i4X_FuZNhg%40mail.gmail.com

```
Hi,

I need some advice about creating the git command alias:

I have this as the command:

git log --pretty=format:"%h %ad %ae %s" --date=short | sed 's/@\S*//g'


The purpose is to cut off the email domain and keep only username.

I'm trying to create this as the alias:


lg = !sh -c 'git log --pretty=format:"%h %ad %ae %s" --date=short |
sed 's/@\S*//g'' -

but it complains about the \S and i'm failing to come up with the
escape sequence to make it work right.

I know i can work around that by creating shell alias, but it is not
what i would like to have.

Any ideas?

Thanks!

```

## Andrew Ardill, 2013-10-30 19:47

Subject: Re: Help creating git alias
Message-ID: <CAH5451kcTCYaXK4tqsCkHFNLxE0z8tv=n6h6QS_VkMpnWAsdGA@mail.gmail.com>
URL: https://gitlist.dev/e/CAH5451kcTCYaXK4tqsCkHFNLxE0z8tv%3Dn6h6QS_VkMpnWAsdGA%40mail.gmail.com
In-Reply-To: <CAPZPVFbiSx8n0W1kcczCdC6ioVuWpwuUQ_pc9T=7i4X_FuZNhg@mail.gmail.com>

```
Have you tried backslash escaping the backslash? double escaping?

I don't know how many are required, but I would try first \S, then
\\S, then \\\\S, etc
Regards,

Andrew Ardill


On 30 October 2013 12:34, Eugene Sajine <euguess@gmail.com> wrote:
> Hi,
>
> I need some advice about creating the git command alias:
>
> I have this as the command:
>
> git log --pretty=format:"%h %ad %ae %s" --date=short | sed 's/@\S*//g'
>
>
> The purpose is to cut off the email domain and keep only username.
>
> I'm trying to create this as the alias:
>
>
> lg = !sh -c 'git log --pretty=format:"%h %ad %ae %s" --date=short |
> sed 's/@\S*//g'' -
>
> but it complains about the \S and i'm failing to come up with the
> escape sequence to make it work right.
>
> I know i can work around that by creating shell alias, but it is not
> what i would like to have.
>
> Any ideas?
>
> Thanks!
> --
> To unsubscribe from this list: send the line "unsubscribe git" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html

```

## Eugene Sajine, 2013-10-30 19:53

Subject: Re: Help creating git alias
Message-ID: <CAPZPVFaDyjxw_vQG_4wo0opw5KEX9sKMJnVNXGUJnCQGnBrB2g@mail.gmail.com>
URL: https://gitlist.dev/e/CAPZPVFaDyjxw_vQG_4wo0opw5KEX9sKMJnVNXGUJnCQGnBrB2g%40mail.gmail.com
In-Reply-To: <CAH5451kcTCYaXK4tqsCkHFNLxE0z8tv=n6h6QS_VkMpnWAsdGA@mail.gmail.com>

```
On Wed, Oct 30, 2013 at 3:47 PM, Andrew Ardill <andrew.ardill@gmail.com> wrote:
> Have you tried backslash escaping the backslash? double escaping?
>
> I don't know how many are required, but I would try first \S, then
> \\S, then \\\\S, etc
> Regards,
>
> Andrew Ardill

When i do that it stops understanding \S* as regexp so it removes only
"@", while i need to remove from @ to the next whitespace

Thanks,
Eugene



>
> On 30 October 2013 12:34, Eugene Sajine <euguess@gmail.com> wrote:
>> Hi,
>>
>> I need some advice about creating the git command alias:
>>
>> I have this as the command:
>>
>> git log --pretty=format:"%h %ad %ae %s" --date=short | sed 's/@\S*//g'
>>
>>
>> The purpose is to cut off the email domain and keep only username.
>>
>> I'm trying to create this as the alias:
>>
>>
>> lg = !sh -c 'git log --pretty=format:"%h %ad %ae %s" --date=short |
>> sed 's/@\S*//g'' -
>>
>> but it complains about the \S and i'm failing to come up with the
>> escape sequence to make it work right.
>>
>> I know i can work around that by creating shell alias, but it is not
>> what i would like to have.
>>
>> Any ideas?
>>
>> Thanks!
>> --
>> To unsubscribe from this list: send the line "unsubscribe git" in
>> the body of a message to majordomo@vger.kernel.org
>> More majordomo info at  http://vger.kernel.org/majordomo-info.html

```

## Ralf Thielow, 2013-10-30 19:57

Subject: Re: Help creating git alias
Message-ID: <CAN0XMOKMF235S-23QcMj5cBup+Lh4vQs7QcOqXQ-MgafsAMKNg@mail.gmail.com>
URL: https://gitlist.dev/e/CAN0XMOKMF235S-23QcMj5cBup%2BLh4vQs7QcOqXQ-MgafsAMKNg%40mail.gmail.com
In-Reply-To: <CAPZPVFbiSx8n0W1kcczCdC6ioVuWpwuUQ_pc9T=7i4X_FuZNhg@mail.gmail.com>

```
lg=!git log --pretty=format:'%h %ad %ae %s' --date=short | sed 's/@\\S*//g'

should work.

On Wed, Oct 30, 2013 at 8:34 PM, Eugene Sajine <euguess@gmail.com> wrote:
> Hi,
>
> I need some advice about creating the git command alias:
>
> I have this as the command:
>
> git log --pretty=format:"%h %ad %ae %s" --date=short | sed 's/@\S*//g'
>
>
> The purpose is to cut off the email domain and keep only username.
>
> I'm trying to create this as the alias:
>
>
> lg = !sh -c 'git log --pretty=format:"%h %ad %ae %s" --date=short |
> sed 's/@\S*//g'' -
>
> but it complains about the \S and i'm failing to come up with the
> escape sequence to make it work right.
>
> I know i can work around that by creating shell alias, but it is not
> what i would like to have.
>
> Any ideas?
>
> Thanks!
> --
> To unsubscribe from this list: send the line "unsubscribe git" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html

```

## Eugene Sajine, 2013-10-30 20:10

Subject: Re: Help creating git alias
Message-ID: <CAPZPVFZ9WujUCQ1O9VfV83XUu_6g7Vp_MmYRCCO+GptOoSyvcg@mail.gmail.com>
URL: https://gitlist.dev/e/CAPZPVFZ9WujUCQ1O9VfV83XUu_6g7Vp_MmYRCCO%2BGptOoSyvcg%40mail.gmail.com
In-Reply-To: <CAN0XMOKMF235S-23QcMj5cBup+Lh4vQs7QcOqXQ-MgafsAMKNg@mail.gmail.com>

```
On Wed, Oct 30, 2013 at 3:57 PM, Ralf Thielow <ralf.thielow@gmail.com> wrote:
> lg=!git log --pretty=format:'%h %ad %ae %s' --date=short | sed 's/@\\S*//g'
>
> should work.


It did! thanks! I didn't know that "!sh -c" is not needed

>
> On Wed, Oct 30, 2013 at 8:34 PM, Eugene Sajine <euguess@gmail.com> wrote:
>> Hi,
>>
>> I need some advice about creating the git command alias:
>>
>> I have this as the command:
>>
>> git log --pretty=format:"%h %ad %ae %s" --date=short | sed 's/@\S*//g'
>>
>>
>> The purpose is to cut off the email domain and keep only username.
>>
>> I'm trying to create this as the alias:
>>
>>
>> lg = !sh -c 'git log --pretty=format:"%h %ad %ae %s" --date=short |
>> sed 's/@\S*//g'' -
>>
>> but it complains about the \S and i'm failing to come up with the
>> escape sequence to make it work right.
>>
>> I know i can work around that by creating shell alias, but it is not
>> what i would like to have.
>>
>> Any ideas?
>>
>> Thanks!
>> --
>> To unsubscribe from this list: send the line "unsubscribe git" in
>> the body of a message to majordomo@vger.kernel.org
>> More majordomo info at  http://vger.kernel.org/majordomo-info.html

```

## Junio C Hamano, 2013-10-30 21:02

Subject: Re: Help creating git alias
Message-ID: <xmqq61sebhh3.fsf@gitster.dls.corp.google.com>
URL: https://gitlist.dev/e/xmqq61sebhh3.fsf%40gitster.dls.corp.google.com
In-Reply-To: <CAPZPVFZ9WujUCQ1O9VfV83XUu_6g7Vp_MmYRCCO+GptOoSyvcg@mail.gmail.com>

```
Eugene Sajine <euguess@gmail.com> writes:

> On Wed, Oct 30, 2013 at 3:57 PM, Ralf Thielow <ralf.thielow@gmail.com> wrote:
>> lg=!git log --pretty=format:'%h %ad %ae %s' --date=short | sed 's/@\\S*//g'
>>
>> should work.
>
>
> It did! thanks! I didn't know that "!sh -c" is not needed

"sh -c" is often used when you pass arguments to your scriptlets,
e.g. to allow

	git lg master..next

you would want

	sh -c 'git log ... "$@" | sed ...' -

so that

	git lg master..next

turns into

	sh -c 'git log ... "$@" | sed ...' - master..next

which makes $1="master..next" and fed to "git log".

```

## Eugene Sajine, 2013-10-31 01:26

Subject: Re: Help creating git alias
Message-ID: <CAPZPVFarK_jKpM2f62mErAmL+mck6EN1QPfHDHqqfJbJ2AfzXg@mail.gmail.com>
URL: https://gitlist.dev/e/CAPZPVFarK_jKpM2f62mErAmL%2Bmck6EN1QPfHDHqqfJbJ2AfzXg%40mail.gmail.com
In-Reply-To: <xmqq61sebhh3.fsf@gitster.dls.corp.google.com>

```
On Wed, Oct 30, 2013 at 5:02 PM, Junio C Hamano <gitster@pobox.com> wrote:
> Eugene Sajine <euguess@gmail.com> writes:
>
>> On Wed, Oct 30, 2013 at 3:57 PM, Ralf Thielow <ralf.thielow@gmail.com> wrote:
>>> lg=!git log --pretty=format:'%h %ad %ae %s' --date=short | sed 's/@\\S*//g'
>>>
>>> should work.
>>
>>
>> It did! thanks! I didn't know that "!sh -c" is not needed
>
> "sh -c" is often used when you pass arguments to your scriptlets,
> e.g. to allow
>
>         git lg master..next
>
> you would want
>
>         sh -c 'git log ... "$@" | sed ...' -
>
> so that
>
>         git lg master..next
>
> turns into
>
>         sh -c 'git log ... "$@" | sed ...' - master..next
>
> which makes $1="master..next" and fed to "git log".

Junio,

That was my initial intention, because I would like to be able to pass
parameters like to git log or git blame correctly without the explicit
use of $1. Could you please advise about how to make it work with the
!sh -c ?

Because the same exact (sed 's/@\\S*//') syntax didn't work with "sh -c".

Thanks,
Eugene

```

## Junio C Hamano, 2013-10-31 03:54

Subject: Re: Help creating git alias
Message-ID: <xmqqk3gu9jst.fsf@gitster.dls.corp.google.com>
URL: https://gitlist.dev/e/xmqqk3gu9jst.fsf%40gitster.dls.corp.google.com
In-Reply-To: <CAPZPVFarK_jKpM2f62mErAmL+mck6EN1QPfHDHqqfJbJ2AfzXg@mail.gmail.com>

```
Eugene Sajine <euguess@gmail.com> writes:

> That was my initial intention, because I would like to be able to pass
> parameters like to git log or git blame correctly without the explicit
> use of $1. Could you please advise about how to make it work with the
> !sh -c ?
>
> Because the same exact (sed 's/@\\S*//') syntax didn't work with "sh -c".

You can make it work if you think step-by-step.  First, this is what
you want to run:

	sh -c 'git log --format="..." "$@" | sed "s/@\S*//"' -

so that "git euguess master..next" would turn into

	sh -c 'git log --format="..." "$@" | sed "s/@\S*//"' - master..next

Now, you want to wrap it into an alias, i.e.

	[alias]
        	euguess = "!sh -c ..."

That ... part is read by our configuration reader, so you need to
quote the double quotes and backslashes with backslash, which would
give you something like:

	[alias]
		euguess = "!sh -c 'git log --format=\"%h %ae %s\" --date=short \"$@\" | sed \"s/@\\S*//\"' -"

```

## Eugene Sajine, 2013-10-31 15:36

Subject: Re: Help creating git alias
Message-ID: <CAPZPVFYFSBRHThO08LmuN_0fc55gYX-A+Y3=yA_MESko1t6fXQ@mail.gmail.com>
URL: https://gitlist.dev/e/CAPZPVFYFSBRHThO08LmuN_0fc55gYX-A%2BY3%3DyA_MESko1t6fXQ%40mail.gmail.com
In-Reply-To: <xmqqk3gu9jst.fsf@gitster.dls.corp.google.com>

```
On Wed, Oct 30, 2013 at 11:54 PM, Junio C Hamano <gitster@pobox.com> wrote:
> Eugene Sajine <euguess@gmail.com> writes:
>
>> That was my initial intention, because I would like to be able to pass
>> parameters like to git log or git blame correctly without the explicit
>> use of $1. Could you please advise about how to make it work with the
>> !sh -c ?
>>
>> Because the same exact (sed 's/@\\S*//') syntax didn't work with "sh -c".
>
> You can make it work if you think step-by-step.  First, this is what
> you want to run:
>
>         sh -c 'git log --format="..." "$@" | sed "s/@\S*//"' -
>
> so that "git euguess master..next" would turn into
>
>         sh -c 'git log --format="..." "$@" | sed "s/@\S*//"' - master..next
>
> Now, you want to wrap it into an alias, i.e.
>
>         [alias]
>                 euguess = "!sh -c ..."
>
> That ... part is read by our configuration reader, so you need to
> quote the double quotes and backslashes with backslash, which would
> give you something like:
>
>         [alias]
>                 euguess = "!sh -c 'git log --format=\"%h %ae %s\" --date=short \"$@\" | sed \"s/@\\S*//\"' -"
>
>

Junio,

Thanks for taking the time - I appreciate that a lot.
It does work properly now except there is some difference between the
required pathnames:

when i'm in a subfolder in git repo i can say

git log filename

But it seems that if the alias is used i need to specify full path
from the root of the repo no matter where i am.

git log a/b/c/filename

the difference is obviously in the working directory

when i add an alias:

pd = "!sh -c 'pwd'"

i get this:

$ git pd
/home/users/euguess/repo

$ pwd
/home/users/euguess/repo/a/b/c

Is there any way to help that situation?

Thanks,
Eugene

Thanks,
Eugene

```

## David Aguilar, 2013-10-31 17:40

Subject: Re: Help creating git alias
Message-ID: <20131031174008.GA39079@gmail.com>
URL: https://gitlist.dev/e/20131031174008.GA39079%40gmail.com
In-Reply-To: <CAPZPVFYFSBRHThO08LmuN_0fc55gYX-A+Y3=yA_MESko1t6fXQ@mail.gmail.com>

```
On Thu, Oct 31, 2013 at 11:36:59AM -0400, Eugene Sajine wrote:
> On Wed, Oct 30, 2013 at 11:54 PM, Junio C Hamano <gitster@pobox.com> wrote:
> > Eugene Sajine <euguess@gmail.com> writes:
> >
> >> That was my initial intention, because I would like to be able to pass
> >> parameters like to git log or git blame correctly without the explicit
> >> use of $1. Could you please advise about how to make it work with the
> >> !sh -c ?
> >>
> >> Because the same exact (sed 's/@\\S*//') syntax didn't work with "sh -c".
> >
> > You can make it work if you think step-by-step.  First, this is what
> > you want to run:
> >
> >         sh -c 'git log --format="..." "$@" | sed "s/@\S*//"' -
> >
> > so that "git euguess master..next" would turn into
> >
> >         sh -c 'git log --format="..." "$@" | sed "s/@\S*//"' - master..next
> >
> > Now, you want to wrap it into an alias, i.e.
> >
> >         [alias]
> >                 euguess = "!sh -c ..."
> >
> > That ... part is read by our configuration reader, so you need to
> > quote the double quotes and backslashes with backslash, which would
> > give you something like:
> >
> >         [alias]
> >                 euguess = "!sh -c 'git log --format=\"%h %ae %s\" --date=short \"$@\" | sed \"s/@\\S*//\"' -"
> >
> >
> 
> Junio,
> 
> Thanks for taking the time - I appreciate that a lot.
> It does work properly now except there is some difference between the
> required pathnames:
> 
> when i'm in a subfolder in git repo i can say
> 
> git log filename
> 
> But it seems that if the alias is used i need to specify full path
> from the root of the repo no matter where i am.
> 
> git log a/b/c/filename
> 
> the difference is obviously in the working directory
> 
> when i add an alias:
> 
> pd = "!sh -c 'pwd'"
> 
> i get this:
> 
> $ git pd
> /home/users/euguess/repo
> 
> $ pwd
> /home/users/euguess/repo/a/b/c
> 
> Is there any way to help that situation?

Here's the relevant details from Documentation/config.txt:

"""
If the alias expansion is prefixed with an exclamation point,
it will be treated as a shell command.  For example, defining
"alias.new = !gitk --all --not ORIG_HEAD", the invocation
"git new" is equivalent to running the shell command
"gitk --all --not ORIG_HEAD".  Note that shell commands will be
executed from the top-level directory of a repository, which may
not necessarily be the current directory.

'GIT_PREFIX' is set as returned by running 'git rev-parse --show-prefix'
from the original current directory. See linkgit:git-rev-parse[1].
"""

The $GIT_PREFIX variable should be available to the alias; it is
a path relative to the root which corresponds to the current
directory.

That doesn't quite play well with these aliases because they use
"$@", though.

One way to do it is to add another layer of indirection.  Maybe
someone else on this list has a better suggestion, but this
should do the trick...

Create a shell script to contain your alias, and then point
your alias at it.  e.g.

[alias]
	example = "!/path/to/alias-script \"$@\""

and then the script can look like:

#!/bin/sh

unset CDPATH
if test -n "$GIT_PREFIX"
then
	cd "$GIT_PREFIX"
fi
git log --format='%h %ae %s' --date=short "$@" | sed 's/@\\S*//'


...or something like that.  I hope that helps.
I'm also curious if there's a way to avoid needing the extra script...

...

A-ha.. I think adding the chdir to alias is possible using a function.

[alias]
	example = "!f() { cd \"${GIT_PREFIX:-.}\" && git log \"$@\"; }; f"

Does that work for you?
I hope that helps.

cheers,
-- 
David

```

## Junio C Hamano, 2013-10-31 18:07

Subject: Re: Help creating git alias
Message-ID: <xmqqd2ml8gbs.fsf@gitster.dls.corp.google.com>
URL: https://gitlist.dev/e/xmqqd2ml8gbs.fsf%40gitster.dls.corp.google.com
In-Reply-To: <20131031174008.GA39079@gmail.com>

```
David Aguilar <davvid@gmail.com> writes:

> A-ha.. I think adding the chdir to alias is possible using a function.

You do not have to use a function to do so, no?

```

## David Aguilar, 2013-10-31 18:15

Subject: Re: Help creating git alias
Message-ID: <20131031181509.GB39079@gmail.com>
URL: https://gitlist.dev/e/20131031181509.GB39079%40gmail.com
In-Reply-To: <xmqqd2ml8gbs.fsf@gitster.dls.corp.google.com>

```
On Thu, Oct 31, 2013 at 11:07:19AM -0700, Junio C Hamano wrote:
> David Aguilar <davvid@gmail.com> writes:
> 
> > A-ha.. I think adding the chdir to alias is possible using a function.
> 
> You do not have to use a function to do so, no?

Right, of course.

So something like:

[alias]
	example = "!cd ${GIT_PREFIX:-.} && git log \"$@\""

should do the trick.
-- 
David

```

## Eugene Sajine, 2013-10-31 19:31

Subject: Re: Help creating git alias
Message-ID: <CAPZPVFYWf3_xtxKkdw4_gzqKagVsbNvj73cb=RvX1Zm0C1h8pQ@mail.gmail.com>
URL: https://gitlist.dev/e/CAPZPVFYWf3_xtxKkdw4_gzqKagVsbNvj73cb%3DRvX1Zm0C1h8pQ%40mail.gmail.com
In-Reply-To: <20131031181509.GB39079@gmail.com>

```
On Thu, Oct 31, 2013 at 2:15 PM, David Aguilar <davvid@gmail.com> wrote:
> On Thu, Oct 31, 2013 at 11:07:19AM -0700, Junio C Hamano wrote:
>> David Aguilar <davvid@gmail.com> writes:
>>
>> > A-ha.. I think adding the chdir to alias is possible using a function.
>>
>> You do not have to use a function to do so, no?
>
> Right, of course.
>
> So something like:
>
> [alias]
>         example = "!cd ${GIT_PREFIX:-.} && git log \"$@\""
>
> should do the trick.
> --
> David


Awesome! It does work!
One note: i tried the ${GIT_PREFIX:-.}  and ${GIT_PREFIX} and it seems
to give the same results. What is the expected difference here?

Thank you!

```

## Junio C Hamano, 2013-10-31 19:41

Subject: Re: Help creating git alias
Message-ID: <xmqq7gct5iu1.fsf@gitster.dls.corp.google.com>
URL: https://gitlist.dev/e/xmqq7gct5iu1.fsf%40gitster.dls.corp.google.com
In-Reply-To: <CAPZPVFYWf3_xtxKkdw4_gzqKagVsbNvj73cb=RvX1Zm0C1h8pQ@mail.gmail.com>

```
Eugene Sajine <euguess@gmail.com> writes:

> One note: i tried the ${GIT_PREFIX:-.}  and ${GIT_PREFIX} and it seems
> to give the same results. What is the expected difference here?

GIT_PREFIX may be an empty string when you run from the top-level,
in which case you would end up with "cd && ..." and end up working
in your $HOME.

```

## Eugene Sajine, 2013-10-31 20:06

Subject: Re: Help creating git alias
Message-ID: <CAPZPVFaFQrEdr7EN3a=ds2T_C87sct6ksH2SbjEg=43-ZNt6Jg@mail.gmail.com>
URL: https://gitlist.dev/e/CAPZPVFaFQrEdr7EN3a%3Dds2T_C87sct6ksH2SbjEg%3D43-ZNt6Jg%40mail.gmail.com
In-Reply-To: <xmqq7gct5iu1.fsf@gitster.dls.corp.google.com>

```
On Thu, Oct 31, 2013 at 3:41 PM, Junio C Hamano <gitster@pobox.com> wrote:
> Eugene Sajine <euguess@gmail.com> writes:
>
>> One note: i tried the ${GIT_PREFIX:-.}  and ${GIT_PREFIX} and it seems
>> to give the same results. What is the expected difference here?
>
> GIT_PREFIX may be an empty string when you run from the top-level,
> in which case you would end up with "cd && ..." and end up working
> in your $HOME.
>


got it! thank you!

Eugene

```
