]> git.ipfire.org Git - thirdparty/git.git/commit - commit-reach.c
commit-reach: implement get_reachable_subset
authorDerrick Stolee <dstolee@microsoft.com>
Fri, 2 Nov 2018 13:14:45 +0000 (06:14 -0700)
committerJunio C Hamano <gitster@pobox.com>
Fri, 2 Nov 2018 15:12:06 +0000 (00:12 +0900)
commitfcb2c0769db54022b5bf3ed134623fbab48cdc20
tree2ea20d682272fe4dd99009167c344271c3e62faa
parentd582ea202b626dcc6c3b01e1e11a296d9badd730
commit-reach: implement get_reachable_subset

The existing reachability algorithms in commit-reach.c focus on
finding merge-bases or determining if all commits in a set X can
reach at least one commit in a set Y. However, for two commits sets
X and Y, we may also care about which commits in Y are reachable
from at least one commit in X.

Implement get_reachable_subset() which answers this question. Given
two arrays of commits, 'from' and 'to', return a commit_list with
every commit from the 'to' array that is reachable from at least
one commit in the 'from' array.

The algorithm is a simple walk starting at the 'from' commits, using
the PARENT2 flag to indicate "this commit has already been added to
the walk queue". By marking the 'to' commits with the PARENT1 flag,
we can determine when we see a commit from the 'to' array. We remove
the PARENT1 flag as we add that commit to the result list to avoid
duplicates.

The order of the resulting list is a reverse of the order that the
commits are discovered in the walk.

There are a couple shortcuts to avoid walking more than we need:

1. We determine the minimum generation number of commits in the
   'to' array. We do not walk commits with generation number
   below this minimum.

2. We count how many distinct commits are in the 'to' array, and
   decrement this count when we discover a 'to' commit during the
   walk. If this number reaches zero, then we can terminate the
   walk.

Tests will be added using the 'test-tool reach' helper in a
subsequent commit.

Signed-off-by: Derrick Stolee <dstolee@microsoft.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
commit-reach.c
commit-reach.h