Skip to content

gittable.tag_version

tag_version

tag_version(
    directory: str | pathlib.Path = os.path.curdir,
    message: str = "",
) -> str

Tag your project with the package version number if no pre-existing tag with that version number exists.

Parameters:

  • directory (str | pathlib.Path, default: os.path.curdir ) –
  • message (str, default: '' ) –

Returns:

  • str

    The version number

Source code in src/gittable/tag_version.py
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
def tag_version(
    directory: str | Path = os.path.curdir, message: str = ""
) -> str:
    """
    Tag your project with the package version number *if* no pre-existing
    tag with that version number exists.

    Parameters:
        directory:
        message:

    Returns:
        The version number
    """
    if isinstance(directory, str):  # pragma: no cover
        directory = Path(directory)
    directory = str(directory.resolve())
    version: str = _get_python_project_version(directory)
    current_directory: str = str(Path.cwd().resolve())
    os.chdir(directory)
    try:
        tags: Iterable[str] = map(
            str.strip,
            check_output(("git", "tag")).strip().split("\n"),
        )
        if version not in tags:  # pragma: no cover
            check_output(
                ("git", "tag", "-a", version, "-m", message or version)
            )
    finally:
        os.chdir(current_directory)
    return version