threads / discuss / 39445

Redirect "git" subcommand to itself?

Subject: Redirect "git" subcommand to itself?

## tl;dr

11 messages between May 28, 2015 and May 29, 2015.

replies: 10people: 7as markdown or json

Stefan Beller· May 28, 2015, 00:28 UTC · lore
Hi,
so I just run into this problem again (which happens to me maybe twice a week):
I want to do a git operations, so I type "git " into my shell, and
then I look around what
exactly I want to do and usually I find it in the help text of a
previous command such as
    You are currently reverting commit 383c14b.
      (fix conflicts and run "git revert --continue")
      (use "git revert --abort" to cancel the revert operation)
then I copy the whole operation "git revert --abort" in this case and
paste it to the shell
and let go.
The result looks like
    $ git git revert --abort
    git: 'git' is not a git command. See 'git --help'.
    Did you mean this?
    init

I wonder if we want to make a "git" subcommand, which behaves exactly the same as git itself? Then "git git git status" would just return the same as "git status".

Thanks, Stefan

Junio C Hamano· May 28, 2015, 01:53 UTC · re: Stefan Beller · lore

Re: Redirect "git" subcommand to itself?

Stefan Beller <sbeller@google.com> writes:
Show 22 quoted lines
> so I just run into this problem again (which happens to me maybe twice a week):
> I want to do a git operations, so I type "git " into my shell, and
> then I look around what
> exactly I want to do and usually I find it in the help text of a
> previous command such as
>     You are currently reverting commit 383c14b.
>       (fix conflicts and run "git revert --continue")
>       (use "git revert --abort" to cancel the revert operation)
>
> then I copy the whole operation "git revert --abort" in this case and
> paste it to the shell
> and let go.
> The result looks like
>     $ git git revert --abort
>     git: 'git' is not a git command. See 'git --help'.
>
>     Did you mean this?
>     init
>
> I wonder if we want to make a "git" subcommand, which behaves exactly
> the same as git itself?
> Then "git git git status" would just return the same as "git status".
A few unrelated thoughts.
 * Perhaps we should omit 'git' from these advice-texts?  E.g.
     use "revert --abort" to cancel
   I dunno.
 * While we bend over backwards to a certain degree to be helpful, I
   somehow feel making "git git" a synonym to "git" is going too
   far, akin to asking POSIX maintainers to define "act", "cta",
   "atc", "tca", and "tac" all as synonyms to "cat" because you
   often fat-finger when typing "cat" (yes, "tac" does something
   else that is more useful, I know).
 * You can help yourself with something like this, I suppose:
   [alias]
	git = "!sh -c 'exec git \"$@\"' -"
   but I personally feel that it is too ugly to live as part of our
   official suggestion, so please do not send a patch to add it as
   a built-in alias ;-).
Jeff King· May 28, 2015, 05:22 UTC · re: Junio C Hamano · lore

Re: Redirect "git" subcommand to itself?

On Wed, May 27, 2015 at 06:53:26PM -0700, Junio C Hamano wrote:
Show 11 quoted lines
> > I wonder if we want to make a "git" subcommand, which behaves exactly
> > the same as git itself?
> > Then "git git git status" would just return the same as "git status".
> 
> A few unrelated thoughts.
> 
>  * Perhaps we should omit 'git' from these advice-texts?  E.g.
> 
>      use "revert --abort" to cancel
> 
>    I dunno.

Please don't. You help the set of people who type "git" and then paste the rest of the command at the expense of people who just paste the whole command. We don't know the relative numbers of those people, but we know there is at least 1 in each group. ;)

-Peff
Christian Neukirchen· May 29, 2015, 08:38 UTC · re: Junio C Hamano · lore

Re: Redirect "git" subcommand to itself?

Junio C Hamano <gitster@pobox.com> writes:
Show 8 quoted lines
>  * You can help yourself with something like this, I suppose:
>
>    [alias]
> 	git = "!sh -c 'exec git \"$@\"' -"
>
>    but I personally feel that it is too ugly to live as part of our
>    official suggestion, so please do not send a patch to add it as
>    a built-in alias ;-).
So I thought I was clever, but this didn't work:

% ln -s /usr/bin/git ~/bin/git-git % git git fatal: cannot handle git as a builtin

-- 
Christian Neukirchen  <chneukirchen@gmail.com>  http://chneukirchen.org
Aaron Schrab· May 29, 2015, 13:24 UTC · re: Christian Neukirchen · lore

Re: Redirect "git" subcommand to itself?

At 10:38 +0200 29 May 2015, Christian Neukirchen <chneukirchen@gmail.com> wrote:
Show 12 quoted lines
>Junio C Hamano <gitster@pobox.com> writes:
>
>>  * You can help yourself with something like this, I suppose:
>>
>>    [alias]
>> 	git = "!sh -c 'exec git \"$@\"' -"
>>
>>    but I personally feel that it is too ugly to live as part of our
>>    official suggestion, so please do not send a patch to add it as
>>    a built-in alias ;-).
>
>So I thought I was clever, but this didn't work:

I've done the same as the original poster a few times myself, so awhile ago I added the following to my .gitconfig file:

[alias]
    git         = "!f() { git $@; }; f"
And this has worked for me.
Junio C Hamano· May 29, 2015, 15:38 UTC · re: Christian Neukirchen · lore

Re: Redirect "git" subcommand to itself?

Christian Neukirchen <chneukirchen@gmail.com> writes:
Show 16 quoted lines
> Junio C Hamano <gitster@pobox.com> writes:
>
>>  * You can help yourself with something like this, I suppose:
>>
>>    [alias]
>> 	git = "!sh -c 'exec git \"$@\"' -"
>>
>>    but I personally feel that it is too ugly to live as part of our
>>    official suggestion, so please do not send a patch to add it as
>>    a built-in alias ;-).
>
> So I thought I was clever, but this didn't work:
>
> % ln -s /usr/bin/git ~/bin/git-git  
> % git git
> fatal: cannot handle git as a builtin
Why did you have to do that when I already gave an alias that works?
Or didn't the alias work?
Christian Neukirchen· May 29, 2015, 16:41 UTC · re: Junio C Hamano · lore

Re: Redirect "git" subcommand to itself?

Junio C Hamano <gitster@pobox.com> writes:
Show 20 quoted lines
> Christian Neukirchen <chneukirchen@gmail.com> writes:
>
>> Junio C Hamano <gitster@pobox.com> writes:
>>
>>>  * You can help yourself with something like this, I suppose:
>>>
>>>    [alias]
>>> 	git = "!sh -c 'exec git \"$@\"' -"
>>>
>>>    but I personally feel that it is too ugly to live as part of our
>>>    official suggestion, so please do not send a patch to add it as
>>>    a built-in alias ;-).
>>
>> So I thought I was clever, but this didn't work:
>>
>> % ln -s /usr/bin/git ~/bin/git-git  
>> % git git
>> fatal: cannot handle git as a builtin
>
> Why did you have to do that when I already gave an alias that works?
I was just toying around, and it would have been cute.
> Or didn't the alias work?
It does.  This seems to work just as well, and is easier:
	git = !git
Thanks,
-- 
Christian Neukirchen  <chneukirchen@gmail.com>  http://chneukirchen.org
Junio C Hamano· May 29, 2015, 16:49 UTC · re: Christian Neukirchen · lore

Re: Redirect "git" subcommand to itself?

Christian Neukirchen <chneukirchen@gmail.com> writes:
Show 5 quoted lines
> I was just toying around, and it would have been cute.
>
>> Or didn't the alias work?
>
> It does.  This seems to work just as well, and is easier:

Thanks; I was wondering if I gave something that was not portable or something.

> 	git = !git
I'd call that the ultimate cuteness.
We have a winner ;-) ;-) ;-).
Konstantin Khomoutov· May 28, 2015, 10:36 UTC · re: Stefan Beller · lore

Re: Redirect "git" subcommand to itself?

On Wed, 27 May 2015 17:28:34 -0700 Stefan Beller <sbeller@google.com> wrote:

> so I just run into this problem again (which happens to me maybe
> twice a week): I want to do a git operations, so I type "git " into
> my shell,
[...]
Show 5 quoted lines
> then I copy the whole operation "git revert --abort" in this case and
> paste it to the shell and let go.
> The result looks like
>     $ git git revert --abort
>     git: 'git' is not a git command. See 'git --help'.
[...]
> I wonder if we want to make a "git" subcommand, which behaves exactly
> the same as git itself?
> Then "git git git status" would just return the same as "git status".
In your ~/.whateverrc, put this:
git() {
  while [ $# -gt 0 ]; do
    test "$1" != "git" && break;
    shift;
  done;
  command git $@;
}

This assumes a POSIX-compatible shell but I think you've got the idea. ("command" is a builtin which forces interpreting the following word as the name of an external program.)

Matthieu Moy· May 28, 2015, 13:11 UTC · re: Stefan Beller · lore

Re: Redirect "git" subcommand to itself?

Stefan Beller <sbeller@google.com> writes:
Show 5 quoted lines
> so I just run into this problem again (which happens to me maybe twice
> a week):
> I want to do a git operations, so I type "git " into my shell, and
> then [...] I copy the whole operation "git revert --abort" in this case and
> paste it to the shell

On my side, that wouldn't be twice a week, but I often do the same mistake. I find your idea for a solution conceptually ugly but practically very convenient ;-), so I'd be in favor of doing it.

-- 
Matthieu Moy
http://www-verimag.imag.fr/~moy/

← back to recent threads