Skip to content

Commit 672eca3

Browse files
authored
Merge pull request #400 from saroad2/change_default_check_branch
Default check branch should be "origin/main"
2 parents 6525465 + affa1a2 commit 672eca3

3 files changed

Lines changed: 76 additions & 1 deletion

File tree

src/towncrier/check.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import sys
77

88
from subprocess import STDOUT, CalledProcessError, check_output
9+
from warnings import warn
910

1011
import click
1112

@@ -18,10 +19,28 @@ def _run(args, **kwargs):
1819
return check_output(args, **kwargs)
1920

2021

22+
def get_default_compare_branch(base_directory, encoding):
23+
branches = (
24+
_run(["git", "branch", "-r"], cwd=base_directory).decode(encoding).splitlines()
25+
)
26+
branches = [branch.strip() for branch in branches]
27+
if "origin/main" in branches:
28+
return "origin/main"
29+
if "origin/master" in branches:
30+
warn(
31+
'Using "origin/master" as default compare branch is deprecated '
32+
"and will be removed in a future version.",
33+
DeprecationWarning,
34+
stacklevel=2,
35+
)
36+
return "origin/master"
37+
return None
38+
39+
2140
@click.command(name="check")
2241
@click.option(
2342
"--compare-with",
24-
default="origin/master",
43+
default=None,
2544
metavar="BRANCH",
2645
help=(
2746
"Checks files changed running git diff --name-ony BRANCH... "
@@ -58,6 +77,14 @@ def __main(comparewith, directory, config):
5877
# when the attribute is present but set to None (explicitly piped output
5978
# and also some CI such as GitHub Actions).
6079
encoding = getattr(sys.stdout, "encoding", "utf8")
80+
if comparewith is None:
81+
comparewith = get_default_compare_branch(
82+
base_directory=base_directory, encoding=encoding
83+
)
84+
85+
if comparewith is None:
86+
click.echo("Could not detect default branch. Aborting.")
87+
sys.exit(1)
6188

6289
try:
6390
files_changed = (
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Default branch for `towncrier check` is now "origin/main" instead of "origin/master".
2+
If "origin/main" does not exist, fallback to "origin/master" with deprecation warning.

src/towncrier/test/test_check.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@
66
import sys
77

88
from subprocess import PIPE, Popen, call
9+
from unittest.mock import patch
910

1011
from click.testing import CliRunner
1112
from twisted.trial.unittest import TestCase
1213

14+
from towncrier import check
1315
from towncrier.check import _main as towncrier_check
1416

1517

@@ -271,3 +273,47 @@ def test_release_branch(self):
271273
# Assert
272274
self.assertEqual(0, result.exit_code, (result, result.output))
273275
self.assertIn("Checks SKIPPED: news file changes detected", result.output)
276+
277+
def test_get_default_compare_branch_missingf(self):
278+
"""
279+
If there's no recognized remote origin, exit with an error.
280+
"""
281+
runner = CliRunner()
282+
283+
with runner.isolated_filesystem():
284+
create_project()
285+
286+
result = runner.invoke(towncrier_check)
287+
288+
self.assertEqual(1, result.exit_code)
289+
self.assertEqual("Could not detect default branch. Aborting.\n", result.output)
290+
291+
def test_get_default_compare_branch_main(self):
292+
"""
293+
If there's a remote branch origin/main, prefer it over everything else.
294+
"""
295+
runner = CliRunner()
296+
297+
with runner.isolated_filesystem():
298+
create_project()
299+
300+
with patch("towncrier.check._run") as m:
301+
m.return_value = b" origin/master\n origin/main\n\n"
302+
branch = check.get_default_compare_branch(".", "utf-8")
303+
304+
self.assertEqual("origin/main", branch)
305+
306+
def test_get_default_compare_branch_fallback(self):
307+
"""
308+
If there's origin/master and no main, use it.
309+
"""
310+
runner = CliRunner()
311+
312+
with runner.isolated_filesystem():
313+
create_project()
314+
315+
with patch("towncrier.check._run") as m:
316+
m.return_value = b" origin/master\n origin/foo\n\n"
317+
branch = check.get_default_compare_branch(".", "utf-8")
318+
319+
self.assertEqual("origin/master", branch)

0 commit comments

Comments
 (0)