threads / discuss / 29261

git alias question

Subject: git alias question

## tl;dr

8 messages between Dec 29, 2011 and Jan 2, 2012.

replies: 7people: 6as markdown or json

Michael Horowitz· Dec 29, 2011, 01:27 UTC · lore
Hello all,

I have been unable to find a solution to this, so I figured I would post to this list...

I am trying to make an easy command to let me look at the last diff to a specified file, either plain or with a diff tool, such as vimdiff. This is the last actual change, not just HEAD^, because the file may not have changed in many commits.

I was able to make the following 2 aliases, which work perfectly except for one problem...

ldiff = "!git diff `git rev-list --reverse -n 2 HEAD -- $1` -- $1" ldifft = "!git difftool `git rev-list --reverse -n 2 HEAD -- $1` -- $1"

The problem is the limitation that shell commands are always executed at the top-level directory of the repository. Normally I am in a deeply nested directory, so if I specify the file name in that directory, it doesn't work. Having to specify the full path relative to the top level makes these aliases more cumbersome to use than their worth.

Is there a way to get around this, or even a completely different way to do this that I am missing? I want to avoid making a completely separate shell script.

Thanks,
Mike
Miles Bader· Dec 29, 2011, 03:21 UTC · re: Michael Horowitz · lore

Re: git alias question

Michael Horowitz <michael.horowitz@ieee.org> writes:
Show 8 quoted lines
> I was able to make the following 2 aliases, which work perfectly
> except for one problem...
>
> ldiff = "!git diff `git rev-list --reverse -n 2 HEAD -- $1` -- $1"
> ldifft = "!git difftool `git rev-list --reverse -n 2 HEAD -- $1` -- $1"
>
> The problem is the limitation that shell commands are always executed
> at the top-level directory of the repository.

Hmmm, it'd be cool if git exported an environment variable containing the CWD when it invoked external aliases like this...!

-Miles
-- 
Love is the difficult realization that something other than oneself is real.
[Iris Murdoch]
Junio C Hamano· Dec 29, 2011, 05:32 UTC · re: Miles Bader · lore

Re: git alias question

Miles Bader <miles@gnu.org> writes:
Show 12 quoted lines
> Michael Horowitz <michael.horowitz@ieee.org> writes:
>> I was able to make the following 2 aliases, which work perfectly
>> except for one problem...
>>
>> ldiff = "!git diff `git rev-list --reverse -n 2 HEAD -- $1` -- $1"
>> ldifft = "!git difftool `git rev-list --reverse -n 2 HEAD -- $1` -- $1"
>>
>> The problem is the limitation that shell commands are always executed
>> at the top-level directory of the repository.
>
> Hmmm, it'd be cool if git exported an environment variable containing
> the CWD when it invoked external aliases like this...!
Yeah, something like GIT_PREFIX environment variable, I guess.
Dave Borowitz· Dec 29, 2011, 17:08 UTC · re: Michael Horowitz · lore

Re: git alias question

On Wed, Dec 28, 2011 at 17:27, Michael Horowitz <michael.horowitz@ieee.org> wrote:

> ldiff = "!git diff `git rev-list --reverse -n 2 HEAD -- $1` -- $1"
FWIW, you can also do this as:
  ldiff = log -p -1 --format=format: --
> ldifft = "!git difftool `git rev-list --reverse -n 2 HEAD -- $1` -- $1"

I don't know that you can do something equivalent with difftool. I suppose you could do the above with "GIT_EXTERNAL_DIFF=<some difftool wrapper> git ldiff", but that's not very helpful.

David Aguilar· Dec 30, 2011, 01:59 UTC · re: Dave Borowitz · lore

Re: git alias question

On Thu, Dec 29, 2011 at 9:08 AM, Dave Borowitz <dborowitz@google.com> wrote:
Show 12 quoted lines
> On Wed, Dec 28, 2011 at 17:27, Michael Horowitz
> <michael.horowitz@ieee.org> wrote:
>> ldiff = "!git diff `git rev-list --reverse -n 2 HEAD -- $1` -- $1"
>
> FWIW, you can also do this as:
>  ldiff = log -p -1 --format=format: --
>
>> ldifft = "!git difftool `git rev-list --reverse -n 2 HEAD -- $1` -- $1"
>
> I don't know that you can do something equivalent with difftool. I
> suppose you could do the above with "GIT_EXTERNAL_DIFF=<some difftool
> wrapper> git ldiff", but that's not very helpful.

difftool cannot be driven by log right now. It is something we thought would be helpful in the past:

http://thread.gmane.org/gmane.comp.version-control.git/114269/focus=114367
On 2009-03-23 Junio C Hamano <gitster <at> pobox.com> wrote:
> Perhaps we would want a convenient way for "log -p" or "show -p" to drive
> difftool as a backend?

I think that's exactly it. difftool wraps diff; a log equivalent would be quite helpful.

One idea is for difftool to learn a "--log" option to make it wrap log instead. I don't know if a diff-like command having a "--log" option is ideal from a consistency-of-user-interface POV so I'm open to ideas. It is convenient, though. It does seem like difftool would be a good place to expose this feature.

I'd be interested in the "teach log / show -p about GIT_EXTERNAL_DIFF" route, if that sounds like a good idea.

-- 
            David
Michael Horowitz· Dec 31, 2011, 21:31 UTC · re: David Aguilar · lore

Re: git alias question

The log operation does seem to make the most sense as the mechanism to search for the results. Making it work with difftool would work... Not sure if "--log" to difftool or some other options as suggested in the other thread would be most consistent UI-wise as stated, but either would work for me.

On a separate note, some environment variable like GIT_PREFIX with the CWD would make the alias functionality more flexible.

Mike
On Thu, Dec 29, 2011 at 8:59 PM, David Aguilar <davvid@gmail.com> wrote:
Show 37 quoted lines
>
> On Thu, Dec 29, 2011 at 9:08 AM, Dave Borowitz <dborowitz@google.com> wrote:
> > On Wed, Dec 28, 2011 at 17:27, Michael Horowitz
> > <michael.horowitz@ieee.org> wrote:
> >> ldiff = "!git diff `git rev-list --reverse -n 2 HEAD -- $1` -- $1"
> >
> > FWIW, you can also do this as:
> >  ldiff = log -p -1 --format=format: --
> >
> >> ldifft = "!git difftool `git rev-list --reverse -n 2 HEAD -- $1` -- $1"
> >
> > I don't know that you can do something equivalent with difftool. I
> > suppose you could do the above with "GIT_EXTERNAL_DIFF=<some difftool
> > wrapper> git ldiff", but that's not very helpful.
>
> difftool cannot be driven by log right now.  It is something we
> thought would be helpful in the past:
>
> http://thread.gmane.org/gmane.comp.version-control.git/114269/focus=114367
>
> On 2009-03-23 Junio C Hamano <gitster <at> pobox.com> wrote:
> > Perhaps we would want a convenient way for "log -p" or "show -p" to drive
> > difftool as a backend?
>
> I think that's exactly it.  difftool wraps diff; a log equivalent
> would be quite helpful.
>
> One idea is for difftool to learn a "--log" option to make it wrap log
> instead.  I don't know if a diff-like command having a "--log" option
> is ideal from a consistency-of-user-interface POV so I'm open to
> ideas.  It is convenient, though.  It does seem like difftool would be
> a good place to expose this feature.
>
> I'd be interested in the "teach log / show -p about GIT_EXTERNAL_DIFF"
> route, if that sounds like a good idea.
> --
>             David
Jakub Narebski· Dec 31, 2011, 23:30 UTC · re: Michael Horowitz · lore

Re: git alias question

Michael Horowitz <michael.horowitz@ieee.org> writes:
Show 8 quoted lines
> The log operation does seem to make the most sense as the mechanism to
> search for the results.  Making it work with difftool would work...
> Not sure if "--log" to difftool or some other options as suggested in
> the other thread would be most consistent UI-wise as stated, but
> either would work for me.
> 
> On a separate note, some environment variable like GIT_PREFIX with the
> CWD would make the alias functionality more flexible.

1f5d271 (setup: Provide GIT_PREFIX to built-ins, 2011-05-25) is present in v1.7.7; 7cf16a1 (handle_alias: provide GIT_PREFIX to !alias, 2011-04-27) is in 1.7.6

-- 
Jakub Narebski
Michael Horowitz· Jan 2, 2012, 05:57 UTC · re: Jakub Narebski · lore

Re: git alias question

Nice, works perfectly! Didn't notice that was there, guess last time I looked, I was using an older version. I see it is in the git config man page and all now.

Thanks,
Mike
On Sat, Dec 31, 2011 at 6:30 PM, Jakub Narebski <jnareb@gmail.com> wrote:
Show 18 quoted lines
>
> Michael Horowitz <michael.horowitz@ieee.org> writes:
>
> > The log operation does seem to make the most sense as the mechanism to
> > search for the results.  Making it work with difftool would work...
> > Not sure if "--log" to difftool or some other options as suggested in
> > the other thread would be most consistent UI-wise as stated, but
> > either would work for me.
> >
> > On a separate note, some environment variable like GIT_PREFIX with the
> > CWD would make the alias functionality more flexible.
>
> 1f5d271 (setup: Provide GIT_PREFIX to built-ins, 2011-05-25) is
> present in v1.7.7; 7cf16a1 (handle_alias: provide GIT_PREFIX to
> !alias, 2011-04-27) is in 1.7.6
>
> --
> Jakub Narebski

← back to recent threads