Skip to content
Snippets Groups Projects

More test helpers

Merged Georges Racinet requested to merge topic/default/testhelpers into branch/default
@@ -14,6 +14,7 @@
typically solved by installing Git system-wide.
"""
import os
import re
import shutil
import subprocess
from mercurial import pycompat
@@ -35,6 +36,15 @@
return {ref: info['title'] for ref, info in branches.items()}
def git_version():
out = subprocess.check_output(('git', 'version')).decode().strip()
m = re.match(r'git version (\d+)\.(\d+)\.(\d+)', out)
return tuple(int(m.group(i)) for i in range(1, 4))
GIT_VERSION = git_version()
class GitRepo(object):
"""Represent a Git repository, relying on the Git executable.
@@ -111,7 +121,7 @@
return {split[1]: split[0]
for split in (line.split(b' ', 1) for line in lines)}
def raw_refs(self):
def raw_refs(self, with_root_refs=False):
"""Use `git for-each-ref` to query all refs, including "root refs"
The output is the full `stdout` of the Git process, as bytes.
@@ -127,10 +137,12 @@
Git sorts the output on `refname` (e.g., `refs/heads/main` or `HEAD`)
by default, which is fine to compare two repositories.
"""
return self.git('for-each-ref',
'--include-root-refs',
'--format', '%(objectname)s %(refname) %(symref)',
)
git_args = ['for-each-ref',
'--format', '%(objectname)s %(refname) %(symref)']
if with_root_refs:
git_args.append('--include-root-refs') # pragma no cover
return self.git(*git_args)
def __eq__(self, other):
"""True iff the two repos have the same content.
@@ -141,4 +153,10 @@
if not isinstance(other, GitRepo):
return False
return self.raw_refs() == other.raw_refs()
opts = dict(with_root_refs=True)
if GIT_VERSION < (2, 45, 0): # pragma no cover
opts['with_root_refs'] = False
if self.get_symref('HEAD') != other.get_symref('HEAD'):
return False
return self.raw_refs(**opts) == other.raw_refs(**opts)
Loading