Skip to content

git

CLASS DESCRIPTION
GitProject
GitProject(dir: Path)
METHOD DESCRIPTION
clone_if_necessary

Construct an instance, cloning given repo first if necessary.

Source code in nava/platform/util/git.py
14
15
def __init__(self, dir: Path):
    self.dir = Path(dir)
clone_if_necessary(repo_uri: str) -> Generator[Self, None, None]

Construct an instance, cloning given repo first if necessary.

If repo_uri is remote, it will be cloned to a temporary directory that this removed on exit of the context.

If repo_uri is a local path, it is not deleted on exit.

Source code in nava/platform/util/git.py
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
@classmethod
@contextmanager
def clone_if_necessary(cls, repo_uri: str) -> Generator[Self, None, None]:
    """Construct an instance, cloning given repo first if necessary.

    If ``repo_uri`` is remote, it will be cloned to a temporary directory
    that this removed on exit of the context.

    If ``repo_uri`` is a local path, it is not deleted on exit.
    """
    if Path(repo_uri).exists():
        yield cls(Path(repo_uri))
    else:
        with TemporaryDirectory() as dir:
            dir_path = Path(dir)
            resolved_repo_uri = get_repo(repo_uri)
            if not resolved_repo_uri:
                raise ValueError(f"Don't understand {repo_uri}")

            clone_result = clone_to(resolved_repo_uri, dir_path)
            clone_result.check_returncode()

            yield cls(dir_path)