git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
* get commit ID from a tree object ID
@ 2018-03-17 12:17 Michal Novotny
  2018-03-17 13:01 ` Konstantin Khomoutov
  0 siblings, 1 reply; 6+ messages in thread
From: Michal Novotny @ 2018-03-17 12:17 UTC (permalink / raw)
  To: git

Hello,

let's say I have made an annotated tag on a certain treeish:

$ git tag -a -m msg tagname HEAD:

Now, I can try to see the content of the tag:

$ git tag -v tagname
object 42a1c36553a50ceae2f75ffc4b1446c6c393eae7
type tree
tag tagname
tagger clime <clime@redhat.com> 1521288727 +0100

msg
error: no signature found


Can I use that object ID 42a1c36553a50ceae2f75ffc4b1446c6c393eae7 to
get back to a particular commit from which the tag was created? The
reason is that I would eventually like to checkout that commit.

Thank you
clime

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: get commit ID from a tree object ID
  2018-03-17 12:17 get commit ID from a tree object ID Michal Novotny
@ 2018-03-17 13:01 ` Konstantin Khomoutov
  2018-03-17 16:18   ` Jeff King
  0 siblings, 1 reply; 6+ messages in thread
From: Konstantin Khomoutov @ 2018-03-17 13:01 UTC (permalink / raw)
  To: Michal Novotny; +Cc: git

On Sat, Mar 17, 2018 at 01:17:12PM +0100, Michal Novotny wrote:

> let's say I have made an annotated tag on a certain treeish:
> 
> $ git tag -a -m msg tagname HEAD:
> 
> Now, I can try to see the content of the tag:
> 
> $ git tag -v tagname
> object 42a1c36553a50ceae2f75ffc4b1446c6c393eae7
> type tree
> tag tagname
> tagger clime <clime@redhat.com> 1521288727 +0100
> 
> msg
> error: no signature found
> 
> 
> Can I use that object ID 42a1c36553a50ceae2f75ffc4b1446c6c393eae7 to
> get back to a particular commit from which the tag was created? The
> reason is that I would eventually like to checkout that commit.

In general, you can't, and that's because there can be any number of
commits referencing that tree. A typical case is a tree object
representing a subdirectory of your project which changes rarily, if
ever - in this case, each commit which includes the same state of this
subdirectory will refer the same tree object.

Another point to consider is that the commit itself only refers to a
single tree object -- that one which records the state of the top-level
project directory, and it usually refers to other three objects which
may, in turn, refer to others and so on - all the way down.

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.


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: get commit ID from a tree object ID
  2018-03-17 13:01 ` Konstantin Khomoutov
@ 2018-03-17 16:18   ` Jeff King
  2018-03-17 17:57     ` Junio C Hamano
  0 siblings, 1 reply; 6+ messages in thread
From: Jeff King @ 2018-03-17 16:18 UTC (permalink / raw)
  To: Konstantin Khomoutov; +Cc: Michal Novotny, git

On Sat, Mar 17, 2018 at 04:01:28PM +0300, Konstantin Khomoutov wrote:

> 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

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: get commit ID from a tree object ID
  2018-03-17 16:18   ` Jeff King
@ 2018-03-17 17:57     ` Junio C Hamano
  2018-03-26 22:14       ` Stefan Beller
  0 siblings, 1 reply; 6+ messages in thread
From: Junio C Hamano @ 2018-03-17 17:57 UTC (permalink / raw)
  To: Jeff King; +Cc: Konstantin Khomoutov, Michal Novotny, git

Jeff King <peff@peff.net> writes:

> 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.

Before Stefan jumps in ;-) I wonder if a recently materialized
"find-object" option to the diff family can be used here as a
sugar-coated way.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: get commit ID from a tree object ID
  2018-03-17 17:57     ` Junio C Hamano
@ 2018-03-26 22:14       ` Stefan Beller
  2018-03-26 22:21         ` Jonathan Nieder
  0 siblings, 1 reply; 6+ messages in thread
From: Stefan Beller @ 2018-03-26 22:14 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: Jeff King, kostix, clime, git

On Sat, Mar 17, 2018 at 10:57 AM Junio C Hamano <gitster@pobox.com> wrote:

> Jeff King <peff@peff.net> writes:

> > 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.

> Before Stefan jumps in ;-) I wonder if a recently materialized
> "find-object" option to the diff family can be used here as a
> sugar-coated way.

I am late to jump in, but testing the 'git log --find-object'
seems to have issues with trees named by sha1 here,
but the named tree via <commit>:<path> still seems to work.

It seems reasonable to be able to find trees using the find-object
flag, though.

Stefan

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: get commit ID from a tree object ID
  2018-03-26 22:14       ` Stefan Beller
@ 2018-03-26 22:21         ` Jonathan Nieder
  0 siblings, 0 replies; 6+ messages in thread
From: Jonathan Nieder @ 2018-03-26 22:21 UTC (permalink / raw)
  To: Stefan Beller; +Cc: Junio C Hamano, Jeff King, kostix, clime, git

Hi,

Stefan Beller wrote:
> On Sat, Mar 17, 2018 at 10:57 AM Junio C Hamano <gitster@pobox.com> wrote:
>> Jeff King <peff@peff.net> writes:

>>> 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.
>
>> Before Stefan jumps in ;-) I wonder if a recently materialized
>> "find-object" option to the diff family can be used here as a
>> sugar-coated way.
>
> I am late to jump in, but testing the 'git log --find-object'
> seems to have issues with trees named by sha1 here,
> but the named tree via <commit>:<path> still seems to work.

Experimenting a little more, I wondered if "-t" wasn't being passed
by default:

  $ git --oneline log -t --find-object=$(git rev-parse HEAD~30:Documentation/technical)
  $

No, that's not it.  Could it have to do with merges?

  $ git log --oneline -m --first-parent --find-object=$(git rev-parse HEAD~30:Documentation/technical)
  df6cfe8c30 Merge branch 'debian-experimental' into google3
  f86d5fd2e4 Merge branch 'debian-experimental' into google3

Yes.

That doesn't explain why <commit>:<path> worked for you, though. :)

Thanks,
Jonathan

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2018-03-26 22:21 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-03-17 12:17 get commit ID from a tree object ID Michal Novotny
2018-03-17 13:01 ` Konstantin Khomoutov
2018-03-17 16:18   ` Jeff King
2018-03-17 17:57     ` Junio C Hamano
2018-03-26 22:14       ` Stefan Beller
2018-03-26 22:21         ` Jonathan Nieder

Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).