Re: Best method of detecting if list of commit refs is a parent
- From
Junio C Hamano <gitster@pobox.com>
- Date
- Aug 18, 2008, 04:41 UTC
- Message-ID
- <7vfxp3ndec.fsf@gitster.siamese.dyndns.org>
- In-Reply-To
- <e47324780808171924j237688faj9e13740f89e75fdf@mail.gmail.com>
"Thomas Harning Jr." <harningt@gmail.com> writes:
Show 7 quoted lines
> I'm working on my 'Stick' git bug-tracking tool and am working on the > functionality to get a list of relevant bug-items at a specific point > in history. > > Before I get into figuring out the 'best' way to do this, I thought > I'd at least get the simple single-item case of detecting if a > specific commit can be walked to from another commit...
It is unclear from your description what you are trying to do here, but if you want to know if a commit A is reachable from commit B, the standard way to do so is:
git merge-base A B
If the output from the command is the object name of commit A, that means A is reachable from B. Otherwise A is not reachable from B.
This is because "merge-base" computes the set of ancestors common to A and B that are not reachable from other ancestors common to A and B.
Examples.
(1) This one is obvious.
A---o---o---o---B
(2) Merge-base between A and B is M. The one before M is also an ancestor
common to A and B, but because it is reachable from M which is another
ancestor common between A and B, it won't be part of the merge-base
output. o---A
/
o---M---o---BIf you have bunch of commits A B C D E F and if you would want to know which one of them is reachable from X, you could of course run merge-base once for each of A..F. Another way to do this would be to run:
git rev-list A B C D E F ^X
and look at the output. The ones among A..F that appear in the output are not reachable from X. The ones that are reachable from X do not appear in the output.
This is because "rev-list" outputs everything reachable from the given commits without ^ prefix, excluding the ones that are reacahble from the ones prefixed with ^.