{"thread":{"id":"59902","subject":"Getting path to a file from arbitrary project directory","startedAt":"2023-06-23T08:59:13Z","lastAt":"2023-06-23T10:03:09Z","messageCount":5,"participants":["Konstantin Kharlamov","SZEDER Gábor","Junio C Hamano"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"478724","messageId":"21c8396c1441dd4f74cd2097ac970a2033b81da4.camel@yandex.ru","threadId":"59902","inReplyTo":null,"subject":"Getting path to a file from arbitrary project directory","fromName":"Konstantin Kharlamov","fromEmail":"hi-angel@yandex.ru","sentAt":"2023-06-23T08:52:58Z","receivedAt":"2023-06-23T08:59:13Z","isPatch":false,"sender":{"key":"hi-angel@yandex.ru","avatar":null},"body":"(please keep me CC'ed, I'm not subscribed)\n\nHello! 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.¹\n\nOne way to implement that is with a command chain:\n\n\tcd $(git rev-parse --show-toplevel) && git ls-files --full-name -- \"*filename.c\"\n\nBut 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`.\n\nAs a separate note, this doesn't work:\n\n\tls-files --full-name -- $(git rev-parse --show-toplevel)\"*filename.c\"\n\n1: 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.\n"},{"id":"478725","messageId":"20230623094525.GA4204@szeder.dev","threadId":"59902","inReplyTo":"21c8396c1441dd4f74cd2097ac970a2033b81da4.camel@yandex.ru","subject":"Re: Getting path to a file from arbitrary project directory","fromName":"SZEDER Gábor","fromEmail":"szeder.dev@gmail.com","sentAt":"2023-06-23T09:45:25Z","receivedAt":"2023-06-23T09:45:59Z","isPatch":false,"sender":{"key":"szeder.dev@gmail.com","avatar":"https://avatars.githubusercontent.com/u/116324?v=4"},"body":"On Fri, Jun 23, 2023 at 11:52:58AM +0300, Konstantin Kharlamov wrote:\n> (please keep me CC'ed, I'm not subscribed)\n> \n> 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.¹\n> \n> One way to implement that is with a command chain:\n> \n> \tcd $(git rev-parse --show-toplevel) && git ls-files --full-name -- \"*filename.c\"\n> \n> 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`.\n\nWhen a command expects pathspecs as arguments, then in general the\nleading ':/' magic means the root of the working tree (not sure where\nit is documented, though), i.e. you could try this:\n\n  git ls-files --full-name ':/*filename.c'\n\n\n> As a separate note, this doesn't work:\n> \n> \tls-files --full-name -- $(git rev-parse --show-toplevel)\"*filename.c\"\n> \n> 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.\n"},{"id":"478726","messageId":"xmqqzg4qbj4y.fsf@gitster.g","threadId":"59902","inReplyTo":"21c8396c1441dd4f74cd2097ac970a2033b81da4.camel@yandex.ru","subject":"Re: Getting path to a file from arbitrary project directory","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2023-06-23T09:52:29Z","receivedAt":"2023-06-23T09:52:50Z","isPatch":false,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Konstantin Kharlamov <hi-angel@yandex.ru> writes:\n\n> 1: the usecase is I have a Emacs helper function to pick up a an\n> aribtrarily mangled path to a file in the project from the primary\n> clipboard and open that file. It's often \"mangled\", because gdb\n> prints it with `../`, then logs print no path whatsoever, just a\n> filename… So it's generally useful to have.\n\nSounds like you are looking for the top (and possibly glob) magic\npathspec, e.g. in the source tree of Git itself, I can go a few\nlevels down into a random directory and get exactly the same listing\nof two files whose name is \"rerere.c\" located in two directories:\n\n    $ cd t/helper\n    $ git ls-files --full-name ':(top,glob)**/rerere.c'\n    builtin/rerere.c\n    rerere.c\n\nLook for magic pathspec in \"git help glossary\" to learn more.\n\n\n"},{"id":"478727","messageId":"0610eb14b9ebafd531d9e952e135171267479c66.camel@yandex.ru","threadId":"59902","inReplyTo":"20230623094525.GA4204@szeder.dev","subject":"Re: Getting path to a file from arbitrary project directory","fromName":"Konstantin Kharlamov","fromEmail":"hi-angel@yandex.ru","sentAt":"2023-06-23T10:02:04Z","receivedAt":"2023-06-23T10:02:49Z","isPatch":false,"sender":{"key":"hi-angel@yandex.ru","avatar":null},"body":"Thank you very much, works for me!\n\nOn Fri, 2023-06-23 at 11:45 +0200, SZEDER Gábor wrote:\n> On Fri, Jun 23, 2023 at 11:52:58AM +0300, Konstantin Kharlamov wrote:\n> > (please keep me CC'ed, I'm not subscribed)\n> > \n> > Hello! I'm trying to solve a simple problem: while I am inside an arbitrary\n> > project directory, I want to get a path to a file `filename.c` located\n> > elsewhere in the same project.¹\n> > \n> > One way to implement that is with a command chain:\n> > \n> >         cd $(git rev-parse --show-toplevel) && git ls-files --full-name --\n> > \"*filename.c\"\n> > \n> > But it is pretty clunky, because that requires you to modify state (changing\n> > current directory). It may not matter though, but I'm just wondering if\n> > there's a better way to do that, something like `git ls-files --top -- …`,\n> > or anything like that? Haven't found nothing similar in `man git-ls-files`.\n> \n> When a command expects pathspecs as arguments, then in general the\n> leading ':/' magic means the root of the working tree (not sure where\n> it is documented, though), i.e. you could try this:\n> \n>   git ls-files --full-name ':/*filename.c'\n> \n> \n> > As a separate note, this doesn't work:\n> > \n> >         ls-files --full-name -- $(git rev-parse --show-\n> > toplevel)\"*filename.c\"\n> > \n> > 1: the usecase is I have a Emacs helper function to pick up a an aribtrarily\n> > mangled path to a file in the project from the primary clipboard and open\n> > that file. It's often \"mangled\", because gdb prints it with `../`, then logs\n> > print no path whatsoever, just a filename… So it's generally useful to have.\n\n"},{"id":"478728","messageId":"d20ac97bbb82256732e15cdc512709db98d1da88.camel@yandex.ru","threadId":"59902","inReplyTo":"xmqqzg4qbj4y.fsf@gitster.g","subject":"Re: Getting path to a file from arbitrary project directory","fromName":"Konstantin Kharlamov","fromEmail":"hi-angel@yandex.ru","sentAt":"2023-06-23T10:02:27Z","receivedAt":"2023-06-23T10:03:09Z","isPatch":false,"sender":{"key":"hi-angel@yandex.ru","avatar":null},"body":"Thank you, it works!\n\nOn Fri, 2023-06-23 at 02:52 -0700, Junio C Hamano wrote:\n> Konstantin Kharlamov <hi-angel@yandex.ru> writes:\n> \n> > 1: the usecase is I have a Emacs helper function to pick up a an\n> > aribtrarily mangled path to a file in the project from the primary\n> > clipboard and open that file. It's often \"mangled\", because gdb\n> > prints it with `../`, then logs print no path whatsoever, just a\n> > filename… So it's generally useful to have.\n> \n> Sounds like you are looking for the top (and possibly glob) magic\n> pathspec, e.g. in the source tree of Git itself, I can go a few\n> levels down into a random directory and get exactly the same listing\n> of two files whose name is \"rerere.c\" located in two directories:\n> \n>     $ cd t/helper\n>     $ git ls-files --full-name ':(top,glob)**/rerere.c'\n>     builtin/rerere.c\n>     rerere.c\n> \n> Look for magic pathspec in \"git help glossary\" to learn more.\n> \n> \n\n"}]}