|
| 1 | +# Copyright (c) towncrier contributors, 2025 |
| 2 | +# See LICENSE for details. |
| 3 | + |
| 4 | +from __future__ import annotations |
| 5 | + |
| 6 | +import os |
| 7 | + |
| 8 | +from typing import Container, Protocol |
| 9 | + |
| 10 | + |
| 11 | +class VCSMod(Protocol): |
| 12 | + def get_default_compare_branch(self, branches: Container[str]) -> str | None: ... |
| 13 | + def remove_files(self, fragment_filenames: list[str]) -> None: ... |
| 14 | + def stage_newsfile(self, directory: str, filename: str) -> None: ... |
| 15 | + def get_remote_branches(self, base_directory: str) -> list[str]: ... |
| 16 | + |
| 17 | + def list_changed_files_compared_to_branch( |
| 18 | + self, base_directory: str, compare_with: str, include_staged: bool |
| 19 | + ) -> list[str]: ... |
| 20 | + |
| 21 | + |
| 22 | +def _get_mod(base_directory: str) -> VCSMod: |
| 23 | + base_directory = os.path.abspath(base_directory) |
| 24 | + if os.path.exists(os.path.join(base_directory, ".git")): |
| 25 | + from . import _git |
| 26 | + |
| 27 | + return _git |
| 28 | + elif os.path.exists(os.path.join(base_directory, ".hg")): |
| 29 | + from . import _hg |
| 30 | + |
| 31 | + hg: VCSMod = _hg |
| 32 | + |
| 33 | + return hg |
| 34 | + else: |
| 35 | + # No VCS was found in the current directory |
| 36 | + # We will try our luck in the parent directory. |
| 37 | + parent = os.path.dirname(base_directory) |
| 38 | + if parent == base_directory: |
| 39 | + # We reached the fs root, abandoning |
| 40 | + from . import _novcs |
| 41 | + |
| 42 | + return _novcs |
| 43 | + |
| 44 | + return _get_mod(parent) |
| 45 | + |
| 46 | + |
| 47 | +def get_default_compare_branch( |
| 48 | + base_directory: str, branches: Container[str] |
| 49 | +) -> str | None: |
| 50 | + return _get_mod(base_directory).get_default_compare_branch(branches) |
| 51 | + |
| 52 | + |
| 53 | +def remove_files(base_directory: str, fragment_filenames: list[str]) -> None: |
| 54 | + return _get_mod(base_directory).remove_files(fragment_filenames) |
| 55 | + |
| 56 | + |
| 57 | +def stage_newsfile(directory: str, filename: str) -> None: |
| 58 | + return _get_mod(directory).stage_newsfile(directory, filename) |
| 59 | + |
| 60 | + |
| 61 | +def get_remote_branches(base_directory: str) -> list[str]: |
| 62 | + return _get_mod(base_directory).get_remote_branches(base_directory) |
| 63 | + |
| 64 | + |
| 65 | +def list_changed_files_compared_to_branch( |
| 66 | + base_directory: str, compare_with: str, include_staged: bool |
| 67 | +) -> list[str]: |
| 68 | + return _get_mod(base_directory).list_changed_files_compared_to_branch( |
| 69 | + base_directory, |
| 70 | + compare_with, |
| 71 | + include_staged, |
| 72 | + ) |
0 commit comments