Skip to content

vcs.py

Utilities related to VCS.

checkout_latest_tag(local_repo, use_prereleases=False)

Checkout latest git tag and check it out, sorted by PEP 440.

Parameters:

Name Type Description Default
local_repo Union[str, pathlib.Path]

A git repository in the local filesystem.

required
use_prereleases Optional[bool]

If False, skip prerelease git tags.

False
Source code in copier/vcs.py
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
def checkout_latest_tag(local_repo: StrOrPath, use_prereleases: OptBool = False) -> str:
    """Checkout latest git tag and check it out, sorted by PEP 440.

    Parameters:
        local_repo:
            A git repository in the local filesystem.
        use_prereleases:
            If `False`, skip prerelease git tags.
    """
    with local.cwd(local_repo):
        all_tags = git("tag").split()
        if not use_prereleases:
            all_tags = filter(
                lambda tag: not version.parse(tag).is_prerelease, all_tags
            )
        sorted_tags = sorted(all_tags, key=version.parse, reverse=True)
        try:
            latest_tag = str(sorted_tags[0])
        except IndexError:
            print(
                colors.warn | "No git tags found in template; using HEAD as ref",
                file=sys.stderr,
            )
            latest_tag = "HEAD"
        git("checkout", "--force", latest_tag)
        git("submodule", "update", "--checkout", "--init", "--recursive", "--force")
        return latest_tag

is_git_bundle(path)

Indicate if a path is a valid git bundle.

Source code in copier/vcs.py
34
35
36
37
38
39
def is_git_bundle(path: Path) -> bool:
    """Indicate if a path is a valid git bundle."""
    with tempfile.TemporaryDirectory(prefix=f"{__name__}.is_git_bundle.") as dirname:
        with local.cwd(dirname):
            git("init")
            return bool(git["bundle", "verify", path] & TF)

is_git_repo_root(path)

Indicate if a given path is a git repo root directory.

Source code in copier/vcs.py
25
26
27
28
29
30
31
def is_git_repo_root(path: Path) -> bool:
    """Indicate if a given path is a git repo root directory."""
    try:
        with local.cwd(path / ".git"):
            return bool(git("rev-parse", "--is-inside-git-dir").strip() == "true")
    except OSError:
        return False