git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: David Aguilar <davvid@gmail.com>
To: Eugene Sajine <euguess@gmail.com>
Cc: Junio C Hamano <gitster@pobox.com>,
	Ralf Thielow <ralf.thielow@gmail.com>, git <git@vger.kernel.org>,
	Andrew Ardill <andrew.ardill@gmail.com>
Subject: Re: Help creating git alias
Date: Thu, 31 Oct 2013 10:40:15 -0700	[thread overview]
Message-ID: <20131031174008.GA39079@gmail.com> (raw)
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

  reply	other threads:[~2013-10-31 17:40 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-10-30 19:34 Help creating git alias Eugene Sajine
2013-10-30 19:47 ` Andrew Ardill
2013-10-30 19:53   ` Eugene Sajine
2013-10-30 19:57 ` Ralf Thielow
2013-10-30 20:10   ` Eugene Sajine
2013-10-30 21:02     ` Junio C Hamano
2013-10-31  1:26       ` Eugene Sajine
2013-10-31  3:54         ` Junio C Hamano
2013-10-31 15:36           ` Eugene Sajine
2013-10-31 17:40             ` David Aguilar [this message]
2013-10-31 18:07               ` Junio C Hamano
2013-10-31 18:15                 ` David Aguilar
2013-10-31 19:31                   ` Eugene Sajine
2013-10-31 19:41                     ` Junio C Hamano
2013-10-31 20:06                       ` Eugene Sajine

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20131031174008.GA39079@gmail.com \
    --to=davvid@gmail.com \
    --cc=andrew.ardill@gmail.com \
    --cc=euguess@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=ralf.thielow@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).