Skip to content
Snippets Groups Projects
Commit 625a62c2 authored by Sushil Khanchi's avatar Sushil Khanchi :koala:
Browse files

Commit: implement CountDivergingCommits

parent 2857aa14
No related branches found
No related tags found
1 merge request!16Commit: implement CountDivergingCommits
Pipeline #13320 passed with warnings
......@@ -182,6 +182,10 @@
count = max_count
return CountCommitsResponse(count=count)
# CountDivergingCommits counts the diverging commits between from and to.
# Important to note that when --max-count is applied, the counts are not
# guaranteed to be accurate.
def CountDivergingCommits(self,
request: CountDivergingCommitsRequest,
context) -> CountDivergingCommitsResponse:
......@@ -185,8 +189,33 @@
def CountDivergingCommits(self,
request: CountDivergingCommitsRequest,
context) -> CountDivergingCommitsResponse:
return not_implemented(context, CountDivergingCommitsResponse,
issue=17) # pragma no cover
repo = self.load_repo(request.repository, context)
rev_from = gitlab_revision_changeset(repo, getattr(request, 'from'))
rev_to = gitlab_revision_changeset(repo, getattr(request, 'to'))
max_count = request.max_count
if rev_from is None:
logger.warning("cannot resolve 'from' revision in %r", request)
if rev_to is None:
logger.warning("cannot resolve 'to' revision in %r", request)
if rev_from is None or rev_to is None:
return CountDivergingCommitsResponse(left_count=0, right_count=0)
left = rev_from.rev()
right = rev_to.rev()
branchpoint = repo.revs(b"ancestor(%d, %d)" % (left, right)).first()
left_count = len(repo.revs(b"%d::%d - %d" %
(branchpoint, left, branchpoint)))
right_count = len(repo.revs(b"%d::%d - %d" %
(branchpoint, right, branchpoint)))
if max_count and (left_count + right_count) > max_count:
delta = (left_count + right_count) - max_count
if left_count >= delta:
left_count -= delta
else:
delta -= left_count
left_count = 0
right_count -= delta
return CountDivergingCommitsResponse(left_count=left_count,
right_count=right_count)
def GetTreeEntries(self, request: GetTreeEntriesRequest,
context) -> GetTreeEntriesResponse:
......
......@@ -21,6 +21,7 @@
FindCommitRequest,
FindCommitsRequest,
CountCommitsRequest,
CountDivergingCommitsRequest,
LastCommitForPathRequest,
ListCommitsByOidRequest,
ListCommitsByRefNameRequest,
......@@ -252,6 +253,46 @@
assert resp[b"topic/default/feature"] == ctx3.hex()
def test_count_divergening_commits(grpc_channel, server_repos_root):
grpc_stub = CommitServiceStub(grpc_channel)
wrapper, grpc_repo = make_empty_repo(server_repos_root)
def do_rpc(gl_from, gl_to, max_count=None):
request = CountDivergingCommitsRequest(repository=grpc_repo,
to=gl_to,
max_count=max_count)
setattr(request, 'from', gl_from)
response = grpc_stub.CountDivergingCommits(request)
return [response.left_count, response.right_count]
# prepare repo as:
#
# 2 (branch/default)
# |
# 1
# | 3 (topic/default/feature)
# | /
# 0
#
ctx0 = wrapper.write_commit('foo')
wrapper.write_commit('bar')
wrapper.write_commit('baz')
wrapper.write_commit('animals', topic='feature', parent=ctx0)
assert do_rpc(b"branch/default", b"topic/default/feature") == [2, 1]
# count 0 for the same "from" and "to"
assert do_rpc(b"branch/default", b"branch/default") == [0, 0]
# when one of them is invalid ref
assert do_rpc(b"branch/default", b"does-not-exists") == [0, 0]
assert do_rpc(b"does-not-exists", b"branch/default") == [0, 0]
# count bounded with max_count
for max_count in [1, 2, 3]:
resp = do_rpc(b"branch/default", b"topic/default/feature", max_count)
assert (resp[0] + resp[1]) == max_count
resp = do_rpc(b"topic/default/feature", b"branch/default", max_count)
assert (resp[0] + resp[1]) == max_count
def test_count_commits(grpc_channel, server_repos_root):
grpc_stub = CommitServiceStub(grpc_channel)
wrapper, grpc_repo = make_empty_repo(server_repos_root)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment