Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default check branch should be "origin/main" #400

Merged
merged 9 commits into from
Aug 31, 2022
29 changes: 28 additions & 1 deletion src/towncrier/check.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import sys

from subprocess import STDOUT, CalledProcessError, check_output
from warnings import warn

import click

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


def get_default_compare_branch(base_directory, encoding):
branches = (
_run(["git", "branch", "-r"], cwd=base_directory).decode(encoding).splitlines()
)
branches = [branch.strip() for branch in branches]
if "origin/main" in branches:
return "origin/main"
if "origin/master" in branches:
warn(
'Using "origin/master" as default compare branch is deprecated '
"and will be removed in a future version.",
DeprecationWarning,
stacklevel=2,
)
return "origin/master"
return None


@click.command(name="check")
@click.option(
"--compare-with",
default="origin/master",
default=None,
metavar="BRANCH",
help=(
"Checks files changed running git diff --name-ony BRANCH... "
Expand Down Expand Up @@ -58,6 +77,14 @@ def __main(comparewith, directory, config):
# when the attribute is present but set to None (explicitly piped output
# and also some CI such as GitHub Actions).
encoding = getattr(sys.stdout, "encoding", "utf8")
if comparewith is None:
comparewith = get_default_compare_branch(
base_directory=base_directory, encoding=encoding
)

if comparewith is None:
click.echo("Could not detect default branch. Aborting.")
sys.exit(1)

try:
files_changed = (
Expand Down
2 changes: 2 additions & 0 deletions src/towncrier/newsfragments/400.removal.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Default branch for `towncrier check` is now "origin/main" instead of "origin/master".
If "origin/main" does not exist, fallback to "origin/master" with deprecation warning.
46 changes: 46 additions & 0 deletions src/towncrier/test/test_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
import sys

from subprocess import PIPE, Popen, call
from unittest.mock import patch

from click.testing import CliRunner
from twisted.trial.unittest import TestCase

from towncrier import check
from towncrier.check import _main as towncrier_check


Expand Down Expand Up @@ -271,3 +273,47 @@ def test_release_branch(self):
# Assert
self.assertEqual(0, result.exit_code, (result, result.output))
self.assertIn("Checks SKIPPED: news file changes detected", result.output)

def test_get_default_compare_branch_missingf(self):
"""
If there's no recognized remote origin, exit with an error.
"""
runner = CliRunner()

with runner.isolated_filesystem():
create_project()

result = runner.invoke(towncrier_check)

self.assertEqual(1, result.exit_code)
self.assertEqual("Could not detect default branch. Aborting.\n", result.output)

def test_get_default_compare_branch_main(self):
"""
If there's a remote branch origin/main, prefer it over everything else.
"""
runner = CliRunner()

with runner.isolated_filesystem():
create_project()

with patch("towncrier.check._run") as m:
m.return_value = b" origin/master\n origin/main\n\n"
branch = check.get_default_compare_branch(".", "utf-8")

self.assertEqual("origin/main", branch)

def test_get_default_compare_branch_fallback(self):
"""
If there's origin/master and no main, use it.
"""
runner = CliRunner()

with runner.isolated_filesystem():
create_project()

with patch("towncrier.check._run") as m:
m.return_value = b" origin/master\n origin/foo\n\n"
branch = check.get_default_compare_branch(".", "utf-8")

self.assertEqual("origin/master", branch)