commit_in_branch.py: variables cleanup

Signed-off-by: Eric Engestrom <eric@engestrom.ch>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/19988>
This commit is contained in:
Eric Engestrom
2022-11-24 15:59:51 +00:00
committed by Marge Bot
parent 707015891f
commit cf841cdd0b
2 changed files with 30 additions and 32 deletions

View File

@@ -45,24 +45,26 @@ def is_commit_valid(commit: str) -> bool:
return ret == 0 return ret == 0
def branch_has_commit(upstream: str, branch: str, commit: str) -> bool: def branch_has_commit(upstream_branch: str, commit: str) -> bool:
""" """
Returns True if the commit is actually present in the branch Returns True if the commit is actually present in the branch
""" """
ret = subprocess.call(['git', 'merge-base', '--is-ancestor', ret = subprocess.call(['git', 'merge-base', '--is-ancestor',
commit, upstream + '/' + branch], commit, upstream_branch],
stdout=subprocess.DEVNULL, stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL) stderr=subprocess.DEVNULL)
return ret == 0 return ret == 0
def branch_has_backport_of_commit(upstream: str, branch: str, commit: str) -> str: def branch_has_backport_of_commit(upstream_branch: str, commit: str) -> str:
""" """
Returns the commit hash if the commit has been backported to the branch, Returns the commit hash if the commit has been backported to the branch,
or an empty string if is hasn't or an empty string if is hasn't
""" """
upstream, _ = upstream_branch.split('/', 1)
out = subprocess.check_output(['git', 'log', '--format=%H', out = subprocess.check_output(['git', 'log', '--format=%H',
upstream + '..' + upstream + '/' + branch, upstream + '..' + upstream_branch,
'--grep', 'cherry picked from commit ' + commit], '--grep', 'cherry picked from commit ' + commit],
stderr=subprocess.DEVNULL) stderr=subprocess.DEVNULL)
return out.decode().strip() return out.decode().strip()
@@ -125,17 +127,15 @@ if __name__ == "__main__":
help='colorize output (default: true if stdout is a terminal)') help='colorize output (default: true if stdout is a terminal)')
args = parser.parse_args() args = parser.parse_args()
upstream, branch = args.branch.split('/', 1) if branch_has_commit(args.branch, args.commit):
print_(args, True, 'Commit ' + args.commit + ' is in branch ' + args.branch)
if branch_has_commit(upstream, branch, args.commit):
print_(args, True, 'Commit ' + args.commit + ' is in branch ' + branch)
exit(0) exit(0)
backport = branch_has_backport_of_commit(upstream, branch, args.commit) backport = branch_has_backport_of_commit(args.branch, args.commit)
if backport: if backport:
print_(args, True, print_(args, True,
'Commit ' + args.commit + ' was backported to branch ' + branch + ' as commit ' + backport) 'Commit ' + args.commit + ' was backported to branch ' + args.branch + ' as commit ' + backport)
exit(0) exit(0)
print_(args, False, 'Commit ' + args.commit + ' is NOT in branch ' + branch) print_(args, False, 'Commit ' + args.commit + ' is NOT in branch ' + args.branch)
exit(1) exit(1)

View File

@@ -88,33 +88,31 @@ def test_is_commit_valid(commit: str, expected: bool) -> None:
@pytest.mark.parametrize( @pytest.mark.parametrize(
'branch, commit, expected', 'branch, commit, expected',
[ [
('20.1', '20.1-branchpoint', True), (get_upstream() + '/20.1', '20.1-branchpoint', True),
('20.1', '20.0', False), (get_upstream() + '/20.1', '20.0', False),
('20.1', 'main', False), (get_upstream() + '/20.1', 'main', False),
('20.1', 'e58a10af640ba58b6001f5c5ad750b782547da76', True), (get_upstream() + '/20.1', 'e58a10af640ba58b6001f5c5ad750b782547da76', True),
('20.1', 'd043d24654c851f0be57dbbf48274b5373dea42b', True), (get_upstream() + '/20.1', 'd043d24654c851f0be57dbbf48274b5373dea42b', True),
('staging/20.1', 'd043d24654c851f0be57dbbf48274b5373dea42b', True), (get_upstream() + '/staging/20.1', 'd043d24654c851f0be57dbbf48274b5373dea42b', True),
('20.1', 'dd2bd68fa69124c86cd008b256d06f44fab8e6cd', False), (get_upstream() + '/20.1', 'dd2bd68fa69124c86cd008b256d06f44fab8e6cd', False),
('main', 'dd2bd68fa69124c86cd008b256d06f44fab8e6cd', True), (get_upstream() + '/main', 'dd2bd68fa69124c86cd008b256d06f44fab8e6cd', True),
('20.0', 'd043d24654c851f0be57dbbf48274b5373dea42b', False), (get_upstream() + '/20.0', 'd043d24654c851f0be57dbbf48274b5373dea42b', False),
]) ])
def test_branch_has_commit(branch: str, commit: str, expected: bool) -> None: def test_branch_has_commit(branch: str, commit: str, expected: bool) -> None:
upstream = get_upstream() assert branch_has_commit(branch, commit) == expected
assert branch_has_commit(upstream, branch, commit) == expected
@pytest.mark.parametrize( @pytest.mark.parametrize(
'branch, commit, expected', 'branch, commit, expected',
[ [
('20.1', 'dd2bd68fa69124c86cd008b256d06f44fab8e6cd', 'd043d24654c851f0be57dbbf48274b5373dea42b'), (get_upstream() + '/20.1', 'dd2bd68fa69124c86cd008b256d06f44fab8e6cd', 'd043d24654c851f0be57dbbf48274b5373dea42b'),
('staging/20.1', 'dd2bd68fa69124c86cd008b256d06f44fab8e6cd', 'd043d24654c851f0be57dbbf48274b5373dea42b'), (get_upstream() + '/staging/20.1', 'dd2bd68fa69124c86cd008b256d06f44fab8e6cd', 'd043d24654c851f0be57dbbf48274b5373dea42b'),
('20.1', '20.1-branchpoint', ''), (get_upstream() + '/20.1', '20.1-branchpoint', ''),
('20.1', '20.0', ''), (get_upstream() + '/20.1', '20.0', ''),
('20.1', '20.2', 'abac4859618e02aea00f705b841a7c5c5007ad1a'), (get_upstream() + '/20.1', '20.2', 'abac4859618e02aea00f705b841a7c5c5007ad1a'),
('20.1', 'main', ''), (get_upstream() + '/20.1', 'main', ''),
('20.1', 'd043d24654c851f0be57dbbf48274b5373dea42b', ''), (get_upstream() + '/20.1', 'd043d24654c851f0be57dbbf48274b5373dea42b', ''),
('20.0', 'dd2bd68fa69124c86cd008b256d06f44fab8e6cd', '8cd4f57381cefe69019a3282d457d5bda3644030'), (get_upstream() + '/20.0', 'dd2bd68fa69124c86cd008b256d06f44fab8e6cd', '8cd4f57381cefe69019a3282d457d5bda3644030'),
]) ])
def test_branch_has_backport_of_commit(branch: str, commit: str, expected: bool) -> None: def test_branch_has_backport_of_commit(branch: str, commit: str, expected: bool) -> None:
upstream = get_upstream() assert branch_has_backport_of_commit(branch, commit) == expected
assert branch_has_backport_of_commit(upstream, branch, commit) == expected