git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: Relative ls-files

From
Junio C Hamano <gitster@pobox.com>
Date
Mar 23, 2011, 17:35 UTC
Message-ID
<7vzkoleo45.fsf@alter.siamese.dyndns.org>
In-Reply-To
<7v7hbqgc7g.fsf@alter.siamese.dyndns.org>
Junio C Hamano <gitster@pobox.com> writes:
Show 9 quoted lines
> Martin Langhoff <martin.langhoff@gmail.com> writes:
>
>> and the answer is git ls-files --with-tree=<treeish> | grep <regex> .
>> But the --with-tree param is so counterintuitive to me that I read the
>> manpage, everytime.
>
> Because --with-tree nor ls-files is the tool that was designed for.
>
> If you want to find out about a branch, why aren't you using "ls-tree -r"?

It obviously needs a side note to answer "what is 'ls-files --with-tree' for, then?", so here is such a note.

The --with-tree option was invented for the sole purpose of helping "git commit -- $paths" back when "commit" was still a scripted Porcelain. We wanted to make sure that the $paths given from the command line made sense (we wanted to diagnose e.g. "git commit Mkaefile" as a typo).

The obvious plumbing to check if the given pathspec matches what are known to git (i.e. in the index) is:

	git ls-files --error-unmatch -- $paths

but there is a catch. If you are about to commit a removal of a path, i.e.

	$ rm -f generated-file.c
	$ git update-index --remove generated-file.c
        $ git commit -m "Do not keep a generated file in SCM" generated-file.c

the above ls-files would report that the pathspec "generated-file.c" does not match, as that path is no longer in the index.

To make the "ls-files --error-unmatch" useful for this check, we use:
	git ls-files --error-unmatch --with-tree=HEAD -- $paths

to overlay the contents of the tree on top of what is already in the index and then determine what paths are "known" to git at that point.

So your use of "git ls-files --with-tree=<unrelated branch>" has been giving results that you didn't want to see, as the set of paths in your index that is based on your current branch most likely are different from what is in that unrelated branch.

I don't think there is any in-tree users of --with-tree option anymore, other than the tests, so it probably is not even worth thinking about removing it and replacing it with a --with-head-tree (as using any commit other than HEAD makes little sense) option.

Previous: Junio C HamanoNext: Junio C Hamano
Message 25 of 28 in “Relative ls-files”
  1. John TapsellMar 23, 2011
  2. Michael J GruberMar 23, 2011
  3. John TapsellMar 23, 2011
  4. demerphqMar 23, 2011
  5. Michael J GruberMar 23, 2011
  6. Martin LanghoffMar 23, 2011
  7. Junio C HamanoMar 23, 2011
  8. Nguyen Thai Ngoc DuyMar 23, 2011
  9. Junio C HamanoMar 23, 2011
  10. Nguyen Thai Ngoc DuyMar 23, 2011
  11. Junio C HamanoMar 23, 2011
  12. Nguyen Thai Ngoc DuyMar 24, 2011
  13. 1/2 Reimplement read_tree_recursive() using tree_entry_interesting()Nguyễn Thái Ngọc Duy, Mar 24, 2011
  14. 2/2 Convert read_tree{,_recursive} to support struct pathspecNguyễn Thái Ngọc Duy, Mar 24, 2011
  15. Nguyen Thai Ngoc DuyMar 24, 2011
  16. Junio C HamanoMar 24, 2011
  17. Junio C HamanoMar 24, 2011
  18. Junio C HamanoMar 24, 2011
  19. 1/3 Reimplement read_tree_recursive() using tree_entry_interesting()Nguyễn Thái Ngọc Duy, Mar 25, 2011
  20. 2/3 Convert read_tree{,_recursive} to support struct pathspecNguyễn Thái Ngọc Duy, Mar 25, 2011
  21. 3/3 Improve tree_entry_interesting() handling codeNguyễn Thái Ngọc Duy, Mar 25, 2011
  22. Junio C HamanoMar 24, 2011
  23. Martin LanghoffMar 23, 2011
  24. Junio C HamanoMar 23, 2011
  25. Junio C HamanoMar 23, 2011
  26. Junio C HamanoMar 23, 2011
  27. Michael J GruberMar 23, 2011
  28. Nguyen Thai Ngoc DuyMar 23, 2011

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.