threads / discuss / 22296

git locate

Subject: git locate

## tl;dr

17 messages between Jan 20, 2010 and Jun 3, 2010.

replies: 16people: 11as markdown or json

John Tapsell· Jan 20, 2010, 01:17 UTC · lore
Hey all,
  Could we add a:  git locate <filename>       or git find <filename>
 that is just an alias to "git ls-files | grep filename" ?
Reasons:
1) git ls-files   does not autocomplete, since it's an low-level
plumbing command
2) It's nice having shell equivalents - e.g.  git grep etc
John
Jakub Narebski· Jan 20, 2010, 02:14 UTC · re: Johannes Schindelin · lore

Re: git locate

Johannes Schindelin <Johannes.Schindelin@gmx.de> writes:
Show 5 quoted lines
> On Wed, 20 Jan 2010, John Tapsell wrote:
> 
> >   Could we add a:  git locate <filename>       or git find <filename>
> 
> How about "git ls-files \*<filename>"?
Or "git ls-files '*filename'...
... but how to make an (git) alias for this?
-- 
Jakub Narebski
Poland
ShadeHawk on #git
Johannes Schindelin· Jan 20, 2010, 09:32 UTC · re: Jakub Narebski · lore

Re: git locate

Hi,
On Tue, 19 Jan 2010, Jakub Narebski wrote:
Show 11 quoted lines
> Johannes Schindelin <Johannes.Schindelin@gmx.de> writes:
> 
> > On Wed, 20 Jan 2010, John Tapsell wrote:
> > 
> > >   Could we add a:  git locate <filename>       or git find <filename>
> > 
> > How about "git ls-files \*<filename>"?
> 
> Or "git ls-files '*filename'...
> 
> ... but how to make an (git) alias for this?
Add something like this to your $HOME/.gitconfig:
[alias]
        locate = !sh -c 'git ls-files "\\*$1"' -

Ciao, Dscho

Jakub Narebski· Jan 20, 2010, 10:06 UTC · re: Johannes Schindelin · lore

Re: git locate

On Wed, 20 Jan 2010, Johannes Schindelin wrote:
Show 16 quoted lines
> On Tue, 19 Jan 2010, Jakub Narebski wrote:
>> Johannes Schindelin <Johannes.Schindelin@gmx.de> writes:
>>> On Wed, 20 Jan 2010, John Tapsell wrote:
>>> 
>>>>   Could we add a:  git locate <filename>       or git find <filename>
>>> 
>>> How about "git ls-files \*<filename>"?
>> 
>> Or "git ls-files '*filename'...
>> 
>> ... but how to make an (git) alias for this?
> 
> Add something like this to your $HOME/.gitconfig:
> 
> [alias]
>         locate = !sh -c 'git ls-files "\\*$1"' -
Thanks a lot, Dscho!
I think it would be nice to have this trick 
  alias.<cmd> = !sh -c '<commands with $1, $@, $#>' -

described in git-config (alias) documentation. Unfortunately I don't understand this trick well enough to do it myself...

-- 
Jakub Narebski
Poland
John Tapsell· Jan 20, 2010, 11:07 UTC · re: Jakub Narebski · lore

Re: git locate

2010/1/20 Jakub Narebski <jnareb@gmail.com>:
Show 18 quoted lines
> On Wed, 20 Jan 2010, Johannes Schindelin wrote:
>> On Tue, 19 Jan 2010, Jakub Narebski wrote:
>>> Johannes Schindelin <Johannes.Schindelin@gmx.de> writes:
>>>> On Wed, 20 Jan 2010, John Tapsell wrote:
>>>>
>>>>>   Could we add a:  git locate <filename>       or git find <filename>
>>>>
>>>> How about "git ls-files \*<filename>"?
>>>
>>> Or "git ls-files '*filename'...
>>>
>>> ... but how to make an (git) alias for this?
>>
>> Add something like this to your $HOME/.gitconfig:
>>
>> [alias]
>>         locate = !sh -c 'git ls-files "\\*$1"' -
>

I think having this alias by default would be very nice though - it's pretty common to locate/find files, and it's nice to have unixy equivalents in git.

If this really isn't wanted, then perhaps the ls-files command should be bash auto-completed?

Thanks!
John
Johannes Schindelin· Jan 20, 2010, 11:43 UTC · re: Jakub Narebski · lore

Re: git locate

Hi,
On Wed, 20 Jan 2010, Jakub Narebski wrote:
Show 24 quoted lines
> On Wed, 20 Jan 2010, Johannes Schindelin wrote:
> > On Tue, 19 Jan 2010, Jakub Narebski wrote:
> >> Johannes Schindelin <Johannes.Schindelin@gmx.de> writes:
> >>> On Wed, 20 Jan 2010, John Tapsell wrote:
> >>> 
> >>>>   Could we add a:  git locate <filename>       or git find <filename>
> >>> 
> >>> How about "git ls-files \*<filename>"?
> >> 
> >> Or "git ls-files '*filename'...
> >> 
> >> ... but how to make an (git) alias for this?
> > 
> > Add something like this to your $HOME/.gitconfig:
> > 
> > [alias]
> >         locate = !sh -c 'git ls-files "\\*$1"' -
> 
> Thanks a lot, Dscho!
> 
> 
> I think it would be nice to have this trick 
> 
>   alias.<cmd> = !sh -c '<commands with $1, $@, $#>' -
Heh.  I initially read @$^&*#^&*@#$.
> described in git-config (alias) documentation.  Unfortunately I don't 
> understand this trick well enough to do it myself...
I tried to explain things properly in
http://git.wiki.kernel.org/index.php/Aliases#Advanced_aliases_with_arguments

Ciao, Dscho

SungHyun Nam· Jan 21, 2010, 00:11 UTC · re: Johannes Schindelin · lore

Re: git locate

Hello,
Johannes Schindelin wrote:
Show 19 quoted lines
>
> On Tue, 19 Jan 2010, Jakub Narebski wrote:
>
>> Johannes Schindelin<Johannes.Schindelin@gmx.de>  writes:
>>
>>> On Wed, 20 Jan 2010, John Tapsell wrote:
>>>
>>>>    Could we add a:  git locate<filename>        or git find<filename>
>>>
>>> How about "git ls-files \*<filename>"?
>>
>> Or "git ls-files '*filename'...
>>
>> ... but how to make an (git) alias for this?
>
> Add something like this to your $HOME/.gitconfig:
>
> [alias]
>          locate = !sh -c 'git ls-files "\\*$1"' -

The alias 'git locate' and the command 'git ls-files' runs differently if I run them in a subdirectory. Is it expected?

[master] ~/srcs/git[299]$ git locate urls.txt Documentation/urls.txt [master] ~/srcs/git[300]$ git ls-files '*urls.txt' Documentation/urls.txt [master] ~/srcs/git[301]$ cd t [master] ~/srcs/git/t[302]$ git locate urls.txt Documentation/urls.txt [master] ~/srcs/git/t[303]$ git ls-files '*urls.txt' [master] ~/srcs/git/t[304]$

Thanks, namsh

Michael J Gruber· Jan 21, 2010, 09:42 UTC · re: SungHyun Nam · lore

Re: git locate

SungHyun Nam venit, vidit, dixit 21.01.2010 01:11:
Show 36 quoted lines
> Hello,
> 
> Johannes Schindelin wrote:
>>
>> On Tue, 19 Jan 2010, Jakub Narebski wrote:
>>
>>> Johannes Schindelin<Johannes.Schindelin@gmx.de>  writes:
>>>
>>>> On Wed, 20 Jan 2010, John Tapsell wrote:
>>>>
>>>>>    Could we add a:  git locate<filename>        or git find<filename>
>>>>
>>>> How about "git ls-files \*<filename>"?
>>>
>>> Or "git ls-files '*filename'...
>>>
>>> ... but how to make an (git) alias for this?
>>
>> Add something like this to your $HOME/.gitconfig:
>>
>> [alias]
>>          locate = !sh -c 'git ls-files "\\*$1"' -
> 
> The alias 'git locate' and the command 'git ls-files' runs
> differently if I run them in a subdirectory.
> Is it expected?
> 
> [master] ~/srcs/git[299]$ git locate urls.txt
> Documentation/urls.txt
> [master] ~/srcs/git[300]$ git ls-files '*urls.txt'
> Documentation/urls.txt
> [master] ~/srcs/git[301]$ cd t
> [master] ~/srcs/git/t[302]$ git locate urls.txt
> Documentation/urls.txt
> [master] ~/srcs/git/t[303]$ git ls-files '*urls.txt'
> [master] ~/srcs/git/t[304]$

Yes, aliases with "!" are executed from the root of the worktree, ls-files from the current wd.

Michael
Štěpán Němec· Jan 21, 2010, 09:51 UTC · re: SungHyun Nam · lore

Re: git locate

On Thu, Jan 21, 2010 at 09:11:20AM +0900, SungHyun Nam wrote:
Show 36 quoted lines
> Hello,
> 
> Johannes Schindelin wrote:
> >
> >On Tue, 19 Jan 2010, Jakub Narebski wrote:
> >
> >>Johannes Schindelin<Johannes.Schindelin@gmx.de>  writes:
> >>
> >>>On Wed, 20 Jan 2010, John Tapsell wrote:
> >>>
> >>>>   Could we add a:  git locate<filename>        or git find<filename>
> >>>
> >>>How about "git ls-files \*<filename>"?
> >>
> >>Or "git ls-files '*filename'...
> >>
> >>... but how to make an (git) alias for this?
> >
> >Add something like this to your $HOME/.gitconfig:
> >
> >[alias]
> >         locate = !sh -c 'git ls-files "\\*$1"' -
> 
> The alias 'git locate' and the command 'git ls-files' runs
> differently if I run them in a subdirectory.
> Is it expected?
> 
> [master] ~/srcs/git[299]$ git locate urls.txt
> Documentation/urls.txt
> [master] ~/srcs/git[300]$ git ls-files '*urls.txt'
> Documentation/urls.txt
> [master] ~/srcs/git[301]$ cd t
> [master] ~/srcs/git/t[302]$ git locate urls.txt
> Documentation/urls.txt
> [master] ~/srcs/git/t[303]$ git ls-files '*urls.txt'
> [master] ~/srcs/git/t[304]$

Yeah, as stated in another part of the Wiki page Dscho referred to (namely the "Serve repo on the spot" section):

"It makes use of the fact that (currently, as of git 1.5.6.1) non-git alias are executed from the top-level dir of a repo."

("non-git" == those starting with "!", which is the case here)
    Štěpán
Sverre Rabbelier· Feb 15, 2010, 13:20 UTC · re: SungHyun Nam · lore

Re: git locate

Heya,
On Thu, Jan 21, 2010 at 01:11, SungHyun Nam <goweol@gmail.com> wrote:
> The alias 'git locate' and the command 'git ls-files' runs
> differently if I run them in a subdirectory.
> Is it expected?

I just ran into this (I wanted to alias 'git find' but to me the 'run ls-files from cwd' is desirable. Also, I prefer to have a trailing glob as well, so I added one :). An easy solution is to drop 'git-find' in your path somewhere:

sverre@laptop-sverre:~/bin$ cat git-find #!/bin/bash

git ls-files "*$1*"
-- 
Cheers,

Sverre Rabbelier
John Tapsell· Jun 2, 2010, 05:00 UTC · re: Sverre Rabbelier · lore

Re: git locate

On 15 February 2010 22:20, Sverre Rabbelier <srabbelier@gmail.com> wrote:
Show 11 quoted lines
> Heya,
>
> On Thu, Jan 21, 2010 at 01:11, SungHyun Nam <goweol@gmail.com> wrote:
>> The alias 'git locate' and the command 'git ls-files' runs
>> differently if I run them in a subdirectory.
>> Is it expected?
>
> I just ran into this (I wanted to alias 'git find' but to me the 'run
> ls-files from cwd' is desirable. Also, I prefer to have a trailing
> glob as well, so I added one :). An easy solution is to drop
> 'git-find' in your path somewhere:
I have finally settled on:
[alias]
    locate = !sh -c 'git ls-files | grep --color=auto "$1"' -

This acts as if there is a leading and trailing wildcard, and also colors the output nicely.

Can we add this as default please? There didn't seem to be any resistance in this thread to making it default, just that nobody did it :)

John
John Tapsell· Jun 2, 2010, 10:02 UTC · re: John Tapsell · lore

Re: git locate

On 2 June 2010 14:00, John Tapsell <johnflux@gmail.com> wrote:
Show 17 quoted lines
> On 15 February 2010 22:20, Sverre Rabbelier <srabbelier@gmail.com> wrote:
>> Heya,
>>
>> On Thu, Jan 21, 2010 at 01:11, SungHyun Nam <goweol@gmail.com> wrote:
>>> The alias 'git locate' and the command 'git ls-files' runs
>>> differently if I run them in a subdirectory.
>>> Is it expected?
>>
>> I just ran into this (I wanted to alias 'git find' but to me the 'run
>> ls-files from cwd' is desirable. Also, I prefer to have a trailing
>> glob as well, so I added one :). An easy solution is to drop
>> 'git-find' in your path somewhere:
>
> I have finally settled on:
>
> [alias]
>    locate = !sh -c 'git ls-files | grep --color=auto "$1"' -

Actually could someone help me with this.. the trouble is that this returns paths relative to the root.

Can I get it to find all the files, but relative to where I am now?
John
Bruce Stephens· Jun 2, 2010, 10:23 UTC · re: John Tapsell · lore

Re: git locate

John Tapsell <johnflux@gmail.com> writes:
[...]
> Actually could someone help me with this.. the trouble is that this
> returns paths relative to the root.
>
> Can I get it to find all the files, but relative to where I am now?

Something like this is probably part of the answer, which allows an alias like this to work:

	locate = !cd $GIT_CWD && sh -c 'git ls-files "*$1*"' -
That doesn't give you all the files (just those below where you are).
diff --git a/git.c b/git.c
index 99f0363..81c877b 100644
--- a/git.c
+++ b/git.c
@@ -178,6 +178,10 @@ static int handle_alias(int *argcp, const char ***argv)
                        }
                        trace_printf("trace: alias to shell cmd: %s => %s\n",
                                     alias_command, alias_string + 1);
+                       if (!subdir || !*subdir)
+                         setenv("GIT_CWD", "./", 1);
+                       else
+                         setenv("GIT_CWD", subdir, 1);
                        ret = system(alias_string + 1);
                        if (ret >= 0 && WIFEXITED(ret) &&
                            WEXITSTATUS(ret) != 127)
Matthieu Moy· Jun 2, 2010, 11:27 UTC · re: Bruce Stephens · lore

Re: git locate

Bruce Stephens <bruce.stephens@isode.com> writes:
Show 13 quoted lines
> John Tapsell <johnflux@gmail.com> writes:
>
> [...]
>
>> Actually could someone help me with this.. the trouble is that this
>> returns paths relative to the root.
>>
>> Can I get it to find all the files, but relative to where I am now?
>
> Something like this is probably part of the answer, which allows an
> alias like this to work:
>
> 	locate = !cd $GIT_CWD && sh -c 'git ls-files "*$1*"' -
There's a patch pending to do more or less the same thing:
  http://thread.gmane.org/gmane.comp.version-control.git/146360/focus=146450
Read Junio's reply there:
  http://thread.gmane.org/gmane.comp.version-control.git/146360/focus=146450
-- 
Matthieu Moy
http://www-verimag.imag.fr/~moy/
Clemens Buchacher· Jun 3, 2010, 13:39 UTC · re: Bruce Stephens · lore

Re: git locate

On Wed, Jun 02, 2010 at 11:23:32AM +0100, Bruce Stephens wrote:
Show 6 quoted lines
> Something like this is probably part of the answer, which allows an
> alias like this to work:
> 
> 	locate = !cd $GIT_CWD && sh -c 'git ls-files "*$1*"' -
> 
> That doesn't give you all the files (just those below where you are).

Once relative path support for ls-files is done (patch pending) you can do git ls-files $(git rev-parse --show-cdup).

Clemens
Nguyen Thai Ngoc Duy· Jun 2, 2010, 11:29 UTC · re: John Tapsell · lore

Re: git locate

On Wed, Jun 2, 2010 at 12:02 PM, John Tapsell <johnflux@gmail.com> wrote:
Show 24 quoted lines
> On 2 June 2010 14:00, John Tapsell <johnflux@gmail.com> wrote:
>> On 15 February 2010 22:20, Sverre Rabbelier <srabbelier@gmail.com> wrote:
>>> Heya,
>>>
>>> On Thu, Jan 21, 2010 at 01:11, SungHyun Nam <goweol@gmail.com> wrote:
>>>> The alias 'git locate' and the command 'git ls-files' runs
>>>> differently if I run them in a subdirectory.
>>>> Is it expected?
>>>
>>> I just ran into this (I wanted to alias 'git find' but to me the 'run
>>> ls-files from cwd' is desirable. Also, I prefer to have a trailing
>>> glob as well, so I added one :). An easy solution is to drop
>>> 'git-find' in your path somewhere:
>>
>> I have finally settled on:
>>
>> [alias]
>>    locate = !sh -c 'git ls-files | grep --color=auto "$1"' -
>
>
> Actually could someone help me with this.. the trouble is that this
> returns paths relative to the root.
>
> Can I get it to find all the files, but relative to where I am now?

Make it a git command, not an alias. You'll then be fine. put git-locate somewhere in your $PATH

--<-- #!/bin/sh git ls-files | grep --color=auto "$1" --<--

Of course you need to chmod u+x for it.
-- 
Duy

← back to recent threads