# Getting path to a file from arbitrary project directory

5 messages from 2023-06-23 to 2023-06-23. Participants: Konstantin Kharlamov, SZEDER Gábor, Junio C Hamano.
Thread: https://gitlist.dev/t/59902

## Konstantin Kharlamov, 2023-06-23 08:52

Subject: Getting path to a file from arbitrary project directory
Message-ID: <21c8396c1441dd4f74cd2097ac970a2033b81da4.camel@yandex.ru>
URL: https://gitlist.dev/e/21c8396c1441dd4f74cd2097ac970a2033b81da4.camel%40yandex.ru

```
(please keep me CC'ed, I'm not subscribed)

Hello! I'm trying to solve a simple problem: while I am inside an arbitrary project directory, I want to get a path to a file `filename.c` located elsewhere in the same project.¹

One way to implement that is with a command chain:

	cd $(git rev-parse --show-toplevel) && git ls-files --full-name -- "*filename.c"

But it is pretty clunky, because that requires you to modify state (changing current directory). It may not matter though, but I'm just wondering if there's a better way to do that, something like `git ls-files --top -- …`, or anything like that? Haven't found nothing similar in `man git-ls-files`.

As a separate note, this doesn't work:

	ls-files --full-name -- $(git rev-parse --show-toplevel)"*filename.c"

1: the usecase is I have a Emacs helper function to pick up a an aribtrarily mangled path to a file in the project from the primary clipboard and open that file. It's often "mangled", because gdb prints it with `../`, then logs print no path whatsoever, just a filename… So it's generally useful to have.

```

## SZEDER Gábor, 2023-06-23 09:45

Subject: Re: Getting path to a file from arbitrary project directory
Message-ID: <20230623094525.GA4204@szeder.dev>
URL: https://gitlist.dev/e/20230623094525.GA4204%40szeder.dev
In-Reply-To: <21c8396c1441dd4f74cd2097ac970a2033b81da4.camel@yandex.ru>

```
On Fri, Jun 23, 2023 at 11:52:58AM +0300, Konstantin Kharlamov wrote:
> (please keep me CC'ed, I'm not subscribed)
> 
> Hello! I'm trying to solve a simple problem: while I am inside an arbitrary project directory, I want to get a path to a file `filename.c` located elsewhere in the same project.¹
> 
> One way to implement that is with a command chain:
> 
> 	cd $(git rev-parse --show-toplevel) && git ls-files --full-name -- "*filename.c"
> 
> But it is pretty clunky, because that requires you to modify state (changing current directory). It may not matter though, but I'm just wondering if there's a better way to do that, something like `git ls-files --top -- …`, or anything like that? Haven't found nothing similar in `man git-ls-files`.

When a command expects pathspecs as arguments, then in general the
leading ':/' magic means the root of the working tree (not sure where
it is documented, though), i.e. you could try this:

  git ls-files --full-name ':/*filename.c'


> As a separate note, this doesn't work:
> 
> 	ls-files --full-name -- $(git rev-parse --show-toplevel)"*filename.c"
> 
> 1: the usecase is I have a Emacs helper function to pick up a an aribtrarily mangled path to a file in the project from the primary clipboard and open that file. It's often "mangled", because gdb prints it with `../`, then logs print no path whatsoever, just a filename… So it's generally useful to have.

```

## Junio C Hamano, 2023-06-23 09:52

Subject: Re: Getting path to a file from arbitrary project directory
Message-ID: <xmqqzg4qbj4y.fsf@gitster.g>
URL: https://gitlist.dev/e/xmqqzg4qbj4y.fsf%40gitster.g
In-Reply-To: <21c8396c1441dd4f74cd2097ac970a2033b81da4.camel@yandex.ru>

```
Konstantin Kharlamov <hi-angel@yandex.ru> writes:

> 1: the usecase is I have a Emacs helper function to pick up a an
> aribtrarily mangled path to a file in the project from the primary
> clipboard and open that file. It's often "mangled", because gdb
> prints it with `../`, then logs print no path whatsoever, just a
> filename… So it's generally useful to have.

Sounds like you are looking for the top (and possibly glob) magic
pathspec, e.g. in the source tree of Git itself, I can go a few
levels down into a random directory and get exactly the same listing
of two files whose name is "rerere.c" located in two directories:

    $ cd t/helper
    $ git ls-files --full-name ':(top,glob)**/rerere.c'
    builtin/rerere.c
    rerere.c

Look for magic pathspec in "git help glossary" to learn more.



```

## Konstantin Kharlamov, 2023-06-23 10:02

Subject: Re: Getting path to a file from arbitrary project directory
Message-ID: <0610eb14b9ebafd531d9e952e135171267479c66.camel@yandex.ru>
URL: https://gitlist.dev/e/0610eb14b9ebafd531d9e952e135171267479c66.camel%40yandex.ru
In-Reply-To: <20230623094525.GA4204@szeder.dev>

```
Thank you very much, works for me!

On Fri, 2023-06-23 at 11:45 +0200, SZEDER Gábor wrote:
> On Fri, Jun 23, 2023 at 11:52:58AM +0300, Konstantin Kharlamov wrote:
> > (please keep me CC'ed, I'm not subscribed)
> > 
> > Hello! I'm trying to solve a simple problem: while I am inside an arbitrary
> > project directory, I want to get a path to a file `filename.c` located
> > elsewhere in the same project.¹
> > 
> > One way to implement that is with a command chain:
> > 
> >         cd $(git rev-parse --show-toplevel) && git ls-files --full-name --
> > "*filename.c"
> > 
> > But it is pretty clunky, because that requires you to modify state (changing
> > current directory). It may not matter though, but I'm just wondering if
> > there's a better way to do that, something like `git ls-files --top -- …`,
> > or anything like that? Haven't found nothing similar in `man git-ls-files`.
> 
> When a command expects pathspecs as arguments, then in general the
> leading ':/' magic means the root of the working tree (not sure where
> it is documented, though), i.e. you could try this:
> 
>   git ls-files --full-name ':/*filename.c'
> 
> 
> > As a separate note, this doesn't work:
> > 
> >         ls-files --full-name -- $(git rev-parse --show-
> > toplevel)"*filename.c"
> > 
> > 1: the usecase is I have a Emacs helper function to pick up a an aribtrarily
> > mangled path to a file in the project from the primary clipboard and open
> > that file. It's often "mangled", because gdb prints it with `../`, then logs
> > print no path whatsoever, just a filename… So it's generally useful to have.


```

## Konstantin Kharlamov, 2023-06-23 10:02

Subject: Re: Getting path to a file from arbitrary project directory
Message-ID: <d20ac97bbb82256732e15cdc512709db98d1da88.camel@yandex.ru>
URL: https://gitlist.dev/e/d20ac97bbb82256732e15cdc512709db98d1da88.camel%40yandex.ru
In-Reply-To: <xmqqzg4qbj4y.fsf@gitster.g>

```
Thank you, it works!

On Fri, 2023-06-23 at 02:52 -0700, Junio C Hamano wrote:
> Konstantin Kharlamov <hi-angel@yandex.ru> writes:
> 
> > 1: the usecase is I have a Emacs helper function to pick up a an
> > aribtrarily mangled path to a file in the project from the primary
> > clipboard and open that file. It's often "mangled", because gdb
> > prints it with `../`, then logs print no path whatsoever, just a
> > filename… So it's generally useful to have.
> 
> Sounds like you are looking for the top (and possibly glob) magic
> pathspec, e.g. in the source tree of Git itself, I can go a few
> levels down into a random directory and get exactly the same listing
> of two files whose name is "rerere.c" located in two directories:
> 
>     $ cd t/helper
>     $ git ls-files --full-name ':(top,glob)**/rerere.c'
>     builtin/rerere.c
>     rerere.c
> 
> Look for magic pathspec in "git help glossary" to learn more.
> 
> 


```
