Skip to content

Commit 620f4de

Browse files
authored
Merge pull request #223 from hartwork/robust-towards-git-branch-column-output
Be robust towards `git branch` column output
2 parents 1df0f4a + c0dbf09 commit 620f4de

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

git_delete_merged_branches/_git.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ def _find_branches(self, extra_argv=None, strip_left: int = 2) -> list[str]:
109109
# strip_left==2 strips leading "refs/heads/" and "refs/remotes/"
110110
argv = [
111111
self._GIT,
112+
"-c",
113+
"column.branch=plain",
112114
"branch",
113115
f"--format=%(refname:lstrip={strip_left})",
114116
]

git_delete_merged_branches/tests/test_git.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Copyright (C) 2020 Sebastian Pipping <[email protected]>
22
# Licensed under GPL v3 or later
33
import os
4+
import shlex
45
import subprocess
56
from tempfile import TemporaryDirectory
67
from textwrap import dedent
@@ -27,6 +28,33 @@ def test_find_branches_drops_head(self):
2728

2829
self.assertEqual(actual_branches, expected_branches)
2930

31+
@parameterized.expand(
32+
[
33+
# NOTE: No point in testing values "never" or "auto"
34+
("column.ui", "always"),
35+
# NOTE: No point in testing value "plain"
36+
("column.branch", "column"),
37+
("column.branch", "row"),
38+
]
39+
)
40+
def test_find_branches_robust_towards_column_mode(self, git_config_key, git_config_value):
41+
expected_branches = ["main_1", "main_2"]
42+
with TemporaryDirectory() as tempdir:
43+
run_script(
44+
dedent(
45+
f"""
46+
git -c init.defaultBranch=main_1 init
47+
git commit --allow-empty -m 'First commit'
48+
git config {shlex.quote(git_config_key)} {shlex.quote(git_config_value)}
49+
git branch main_2 main_1
50+
"""
51+
),
52+
tempdir,
53+
)
54+
git = Git(Messenger(colorize=False), pretend=True, verbose=False, work_dir=tempdir)
55+
actual_branches = git._find_branches()
56+
self.assertEqual(actual_branches, expected_branches)
57+
3058

3159
class FindBranchNamesTest(TestCase):
3260
def test(self):

0 commit comments

Comments
 (0)