diff --git a/lisa/_git.py b/lisa/_git.py index c5afdddd2570807c4851e0ac3c3c85497fdf9a15..c54130c9b0a6e9385e8b32c14c0042a8b6dc3abc 100644 --- a/lisa/_git.py +++ b/lisa/_git.py @@ -72,7 +72,7 @@ def get_sha1(repo, ref='HEAD'): """ return git(repo, 'rev-list', '-1', ref).strip() -def get_uncommited_patch(repo, include_binary=True, path=None): +def get_uncommitted_patch(repo, include_binary=True, path=None): """ Return the patch of non commited changes, both staged and not staged yet. @@ -82,8 +82,14 @@ def get_uncommited_patch(repo, include_binary=True, path=None): :param path: Path to consider, rather than the entire repo. :type path: str or pathlib.Path """ + if path is None: + path = [] + else: + path = Path(repo.resolve()) / Path(path) + path = path.resolve() + path = ['--', str(path)] + text = ['--text'] if include_binary else [] - path = ['--', str(Path(path).resolve())] if path else [] return git(repo, 'diff', *text, 'HEAD', *path) def get_commit_message(repo, ref='HEAD', notes_ref='refs/notes/commits', format='%s'): @@ -166,6 +172,20 @@ def find_root(repo): root = git(repo, 'rev-parse', '--show-toplevel') return Path(root.strip()).resolve() + +def is_inside_git_repo(path): + """ + Returns ``True`` if the path is inside a git repository, ``False`` + otherwise. + """ + try: + find_root(path) + except subprocess.CalledProcessError: + return False + else: + return True + + def check_ref(repo, ref): """ Check if a given reference exists. diff --git a/lisa/_kmod.py b/lisa/_kmod.py index 4915f8ce1a6df4f312be4408860183024fbf122c..60ecd58e31bbe51cd9aa9206cb8b8fc5af441cbd 100644 --- a/lisa/_kmod.py +++ b/lisa/_kmod.py @@ -2458,7 +2458,7 @@ class _KernelBuildEnv(Loggable, SerializeViaConstructor): try: repo_root = git.find_root(tree_path) sha1 = git.get_sha1(tree_path) - patch = git.get_uncommited_patch(tree_path) + patch = git.get_uncommitted_patch(tree_path) except (FileNotFoundError, subprocess.CalledProcessError): key = None else: diff --git a/lisa/version.py b/lisa/version.py index 1bcd857668a9e843de832a5c03e5517ebfeec450..478c345b094cb6a1f91d9d82bae591613ba67bc2 100644 --- a/lisa/version.py +++ b/lisa/version.py @@ -18,14 +18,18 @@ :mod:`lisa` version identification. """ -import os -import hashlib -from subprocess import CalledProcessError - +# Re-export from lisa._version import version_tuple, __version__, format_version, parse_version -from lisa._git import get_sha1, get_uncommited_patch def _compute_version_token(): + import os + import hashlib + from subprocess import CalledProcessError + from pathlib import Path + import subprocess + + from lisa._git import get_sha1, get_uncommitted_patch, find_root + plain_version_token = f'v{format_version(version_tuple)}' forced = os.getenv('_LISA_FORCE_VERSION_TOKEN') @@ -37,16 +41,28 @@ def _compute_version_token(): elif int(os.getenv('LISA_DEVMODE', '0')): # pylint: disable=import-outside-toplevel import lisa - repo = list(lisa.__path__)[0] + path = Path(list(lisa.__path__)[0]) + path = path / '..' try: - sha1 = get_sha1(repo) - # Get uncommitted content of the LISA sources only, not the entire - # repo as it would include things like target_conf.yml - patch = get_uncommited_patch(repo, path='lisa/') - # Git is not installed, just use the regular version - except (FileNotFoundError, CalledProcessError): + repo = find_root(path) + except subprocess.CalledProcessError: return plain_version_token + else: + # If the parent folder of the lisa sources is a git repo, it must + # be the one we are looking for. This avoids accidentally catching + # cases where there is a top-level git repo in e.g. $HOME. + if path.samefile(repo): + try: + sha1 = get_sha1(repo) + # Get uncommitted content of the LISA sources only, not the entire + # repo as it would include things like target_conf.yml + patch = get_uncommitted_patch(repo, path='lisa/') + # Git is not installed, just use the regular version + except (FileNotFoundError, CalledProcessError): + return plain_version_token + else: + return plain_version_token # Dirty tree if patch: