Skip to content

Commit

Permalink
[webkitscmpy] Add merge-base function
Browse files Browse the repository at this point in the history
https://bugs.webkit.org/show_bug.cgi?id=246689
rdar://101292972

Reviewed by Aakash Jain.

* Tools/Scripts/libraries/webkitscmpy/setup.py: Bump version.
* Tools/Scripts/libraries/webkitscmpy/webkitscmpy/__init__.py: Ditto.
* Tools/Scripts/libraries/webkitscmpy/webkitscmpy/local/git.py:
(Git.merge_base): Determine the merge-base of the provided git refs.
* Tools/Scripts/libraries/webkitscmpy/webkitscmpy/mocks/local/git.py:
(Git.__init__): Add `git merge-base` mock.
(Git.merge_base): Mock the `git merge-base` command.
* Tools/Scripts/libraries/webkitscmpy/webkitscmpy/test/git_unittest.py:
(TestGit.test_merge_base):

Canonical link: https://commits.webkit.org/255689@main
  • Loading branch information
JonWBedard committed Oct 18, 2022
1 parent 4040b04 commit 402b411
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Tools/Scripts/libraries/webkitscmpy/setup.py
Expand Up @@ -29,7 +29,7 @@ def readme():

setup(
name='webkitscmpy',
version='5.6.25',
version='5.6.26',
description='Library designed to interact with git and svn repositories.',
long_description=readme(),
classifiers=[
Expand Down
Expand Up @@ -46,7 +46,7 @@ def _maybe_add_webkitcorepy_path():
"Please install webkitcorepy with `pip install webkitcorepy --extra-index-url <package index URL>`"
)

version = Version(5, 6, 25)
version = Version(5, 6, 26)

AutoInstall.register(Package('fasteners', Version(0, 15, 0)))
AutoInstall.register(Package('jinja2', Version(2, 11, 3)))
Expand Down
13 changes: 13 additions & 0 deletions Tools/Scripts/libraries/webkitscmpy/webkitscmpy/local/git.py
Expand Up @@ -1125,3 +1125,16 @@ def remote_for(self, argument):
if argument in self.branches_for(remote=remote):
return remote
return None

def merge_base(self, ref_a, ref_b, include_log=True, include_identifier=True):
a = self.find(ref_a, include_log=False, include_identifier=False)
b = self.find(ref_b, include_log=False, include_identifier=False)
if not a or not b:
return None
result = run(
[self.executable(), 'merge-base', a.hash, b.hash],
capture_output=True, encoding='utf-8', cwd=self.path,
)
if result.returncode:
return None
return self.commit(hash=result.stdout.rstrip(), include_log=include_log, include_identifier=include_identifier)
24 changes: 24 additions & 0 deletions Tools/Scripts/libraries/webkitscmpy/webkitscmpy/mocks/local/git.py
Expand Up @@ -607,6 +607,10 @@ def __init__(
generator=lambda *args, **kwargs: mocks.ProcessCompletion(
returncode=0,
) if args[4] in self.remotes else mocks.ProcessCompletion(returncode=128, stderr="fatal: branch '{}' does not exist".format(args[4])),
), mocks.Subprocess.Route(
self.executable, 'merge-base', re.compile(r'.+'), re.compile(r'.+'),
cwd=self.path,
generator=lambda *args, **kwargs: self.merge_base(args[2], args[3]),
), mocks.Subprocess.Route(
self.executable,
cwd=self.path,
Expand Down Expand Up @@ -1247,3 +1251,23 @@ def _configure_git_lfs(self):
returncode=0,
stdout='Updated Git hooks.\nGit LFS initialized.\n',
)

def merge_base(self, *refs):
objs = [self.find(ref) for ref in refs]
for i in [0, 1]:
if not refs[i] or not objs[i]:
return mocks.ProcessCompletion(
returncode=128,
stderr='fatal: Not a valid object name {}\n'.format(refs[i]),
)
if objs[0].branch != objs[1].branch:
for i in [0, 1]:
if objs[i].branch == self.default_branch:
continue
objs[i] = self.commits[self.default_branch][objs[i].branch_point - 1]

base = objs[0] if objs[0].identifier < objs[1].identifier else objs[1]
return mocks.ProcessCompletion(
returncode=0,
stdout='{}\n'.format(base.hash),
)
Expand Up @@ -562,6 +562,17 @@ def test_files_changed(self):
['Source/main.cpp', 'Source/main.h'],
)

def test_merge_base(self):
with mocks.local.Git(self.path), OutputCapture():
self.assertEqual(
str(local.Git(self.path).merge_base('main', 'branch-b')),
'2@main',
)
self.assertEqual(
str(local.Git(self.path).merge_base('branch-a', 'branch-b')),
'2@main',
)


class TestGitHub(testing.TestCase):
remote = 'https://github.example.com/WebKit/WebKit'
Expand Down

0 comments on commit 402b411

Please sign in to comment.