1

From documentation, in order to use git ls-tree you need to pass the hash of a tree object. What if I want to obtain the same output of git ls-tree starting from a commit object. I can obviously do it with something like this:

git ls-tree $(git cat-file -p my_commit | grep -oP "(?<=tree ).*")

But I feel like I am reinventing the wheel. Is there a git command that already does this?

Marco Luzzara
  • 5,540
  • 3
  • 16
  • 42
  • 1
    "*…in order to use git ls-tree you need to pass the hash of a tree object…*" Wrong, any commit or a pointer to a commit is ok. `@`, `HEAD`, `master^2~`, any branch or tag… – phd Feb 10 '21 at 16:11
  • Thank you @phd, I even tried with a commit hash on git, but I clearly made a mistake somehow. I completely misunderstood the "tree-ish" the doc refers too. Thank you very much. – Marco Luzzara Feb 10 '21 at 16:22

1 Answers1

4

No, git ls-tree takes a tree-ish object.

The "-ish" suffix here is important. Per the Cambridge Dictionary:

-ish suffix (QUITE)

used to form adjectives to give the meaning to some degree; fairly:

  • He had a sort of reddish beard.
  • She was oldish - about 60, I'd say.
  • We'll start at sevenish (= about seven o'clock).

In this case, "tree-ish" means like a tree. A tree, of course, is like a tree. But a commit is also like a tree since it has exactly one tree component; that means that you can unambiguously refer to that tree by simply using the commit itself.

So, just do git ls-tree <commit-ish>.

Edward Thomson
  • 74,857
  • 14
  • 158
  • 187
  • Thank you @EdwardThomson for the dictionary reference and the explanation (yes, I am not english), but I think that saying "a commit is also like a tree since it has exactly one tree component and that tree can be **identified** by simply using the commit itself." is a mistake: a tree can belong to many commits. – Marco Luzzara Feb 10 '21 at 17:28
  • [this](https://stackoverflow.com/questions/4044368/what-does-tree-ish-mean-in-git#answer-18605496) definition would be more correct: *"Tree-ish" is a term that refers to any identifier [...] that ultimately leads to a (sub)directory tree*. – Marco Luzzara Feb 10 '21 at 17:29
  • That a commit has a single tree does not imply that a tree cannot be part of multiple commits. – Edward Thomson Feb 10 '21 at 20:56
  • (Or even part of other trees.) – Edward Thomson Feb 10 '21 at 20:56
  • 1
    Yes exactly, we are saying the same thing I guess. I simply did not like that "identified", but now I get what you meant. Basically since a commit has a single root tree, that -ish refers to the fact that there is no ambiguity in using a commit hash. – Marco Luzzara Feb 10 '21 at 21:03
  • Yes, that's what I was trying to say. I like the way that you've phrased that a lot. I'll update my answer. – Edward Thomson Feb 10 '21 at 21:15