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

Re: get commit ID from a tree object ID

From
Jeff King <peff@peff.net>
Date
Mar 17, 2018, 16:18 UTC
Message-ID
<20180317161819.GB25113@sigill.intra.peff.net>
In-Reply-To
<20180317130128.mr2sfsrncykkubwo@tigra>
On Sat, Mar 17, 2018 at 04:01:28PM +0300, Konstantin Khomoutov wrote:
Show 5 quoted lines
> So actually a generic approach to what you need is a full scan of all
> the commits in the repository with recursive traversing of the hierarchy
> of trees of each of them (via `git ls-tree`) and looking for the SHA-1
> name of the reference tree object.  As you can see, this is not going to
> be fast on repos of realistic size.

If you assume that the tree is a root tree (which is by no means certain, but a good guess), it's not _too_ bad to do:

  git rev-list --all --format='%T %H' | grep ^$desired_tree

That's linear in the number of commits, but still takes only about 7 seconds on linux.git.

If you want to dig further, you can use the diff machinery to show which commit introduced a particular tree, like:

  git rev-list --all |
  git diff-tree --stdin --pretty=raw --raw -t -r |
  less +/$desired_tree

That "less" will find the mentioned tree, and then you'll have to manually read the commit. It would be possible to do it mechanically with a short perl script, but I'll leave that as an exercise for the reader.

-Peff
Previous: Konstantin KhomoutovNext: Junio C Hamano
Message 3 of 6 in “get commit ID from a tree object ID”
  1. Michal NovotnyMar 17, 2018
  2. Konstantin KhomoutovMar 17, 2018
  3. Jeff KingMar 17, 2018
  4. Junio C HamanoMar 17, 2018
  5. Stefan BellerMar 26, 2018
  6. Jonathan NiederMar 26, 2018

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.