Skip to content

Commit 986d17c

Browse files
committed
multiple vcs: detect vcs at each call
1 parent 84ef4bf commit 986d17c

File tree

5 files changed

+71
-27
lines changed

5 files changed

+71
-27
lines changed

src/towncrier/_hg.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ def _topic_enabled(directory: str):
3737

3838

3939
def has_topics(directory: str) -> bool:
40-
global _has_topics_cache
4140
if directory not in _has_topics_cache:
4241
_has_topics_cache[directory] = _topic_enabled(directory)
4342
return _has_topics_cache[directory]
@@ -96,4 +95,4 @@ def list_changed_files_compared_to_branch(
9695
stderr=STDOUT,
9796
).splitlines()
9897

99-
return [l.split("|")[0].strip() for l in output if "|" in l]
98+
return [line.split("|")[0].strip() for line in output if "|" in line]

src/towncrier/_novcs.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import os
2+
3+
from typing import Container
4+
5+
6+
def get_default_compare_branch(branches: Container[str]) -> str | None:
7+
return None
8+
9+
10+
def remove_files(fragment_filenames: list[str]) -> None:
11+
if not fragment_filenames:
12+
return
13+
14+
for fragment in fragment_filenames:
15+
os.remove(fragment)
16+
17+
18+
def stage_newsfile(directory: str, filename: str) -> None:
19+
return
20+
21+
22+
def get_remote_branches(base_directory: str) -> list[str]:
23+
return []
24+
25+
26+
def list_changed_files_compared_to_branch(
27+
base_directory: str, compare_with: str, include_staged: bool
28+
) -> list[str]:
29+
return []

src/towncrier/_vcs.py

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,46 @@
11
import os
22

3-
if os.path.exists(".git"):
4-
flavor = "git"
5-
from ._git import *
3+
from typing import Container
64

7-
elif os.path.exists(".hg"):
8-
flavor = "hg"
9-
from ._hg import *
105

11-
else:
12-
flavor = "none"
6+
def _get_mod(base_directory: str):
7+
if os.path.exists(os.path.join(base_directory, ".git")):
8+
from . import _git
139

14-
def remove_files(fragment_filenames: list[str]) -> None:
15-
if not fragment_filenames:
16-
return
10+
return _git
11+
elif os.path.exists(os.path.join(base_directory, ".hg")):
12+
from . import _hg
1713

18-
for fragment in fragment_filenames:
19-
os.remove(fragment)
14+
return _hg
15+
else:
16+
from . import _novcs
2017

21-
def stage_newsfile(directory: str, filename: str) -> None:
22-
return
18+
return _novcs
2319

24-
def get_remote_branches(base_directory: str) -> list[str]:
25-
return []
2620

27-
def list_changed_files_compared_to_branch(
28-
base_directory: str, compare_with: str, include_staged: bool
29-
) -> list[str]:
30-
return []
21+
def get_default_compare_branch(
22+
base_directory: str, branches: Container[str]
23+
) -> str | None:
24+
return _get_mod(base_directory).get_default_compare_branch(branches)
25+
26+
27+
def remove_files(base_directory: str, fragment_filenames: list[str]) -> None:
28+
return _get_mod(base_directory).remove_files(fragment_filenames)
29+
30+
31+
def stage_newsfile(directory: str, filename: str) -> None:
32+
return _get_mod(directory).stage_newsfile(directory, filename)
33+
34+
35+
def get_remote_branches(base_directory: str) -> list[str]:
36+
return _get_mod(base_directory).get_remote_branches(base_directory)
37+
38+
39+
def list_changed_files_compared_to_branch(
40+
base_directory: str, compare_with: str, include_staged: bool
41+
) -> list[str]:
42+
return _get_mod(base_directory).list_changed_files_compared_to_branch(
43+
base_directory,
44+
compare_with,
45+
include_staged,
46+
)

src/towncrier/build.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ def __main(
278278
answer_keep,
279279
):
280280
click.echo("Removing news fragments...", err=to_err)
281-
_vcs.remove_files(fragment_filenames)
281+
_vcs.remove_files(base_directory, fragment_filenames)
282282

283283
click.echo("Done!", err=to_err)
284284

src/towncrier/check.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@
1212
import click
1313

1414
from ._builder import find_fragments
15+
from ._settings import config_option_help, load_config_from_options
1516
from ._vcs import (
17+
get_default_compare_branch,
1618
get_remote_branches,
1719
list_changed_files_compared_to_branch,
18-
get_default_compare_branch,
1920
)
20-
from ._settings import config_option_help, load_config_from_options
2121

2222

2323
@click.command(name="check")
@@ -71,7 +71,7 @@ def __main(
7171

7272
if comparewith is None:
7373
comparewith = get_default_compare_branch(
74-
get_remote_branches(base_directory=base_directory)
74+
base_directory, get_remote_branches(base_directory=base_directory)
7575
)
7676

7777
if comparewith is None:

0 commit comments

Comments
 (0)