Skip to content

Commit dd02538

Browse files
committed
Allow running on detached head
1 parent 25f9818 commit dd02538

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

hooks/utils.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,7 @@ def get_untracked_files() -> List[str]:
1111

1212

1313
def get_current_branch() -> str:
14-
output = subprocess.check_output(BRANCH_CMD)
15-
return output.decode().rstrip()
14+
result = subprocess.run(BRANCH_CMD, stdout=subprocess.PIPE)
15+
if result.returncode == 0:
16+
return result.stdout.decode().rstrip()
17+
return None

tests/check_untracked_migrations_test.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import subprocess
12
from hooks.check_untracked_migrations import main
23
from hooks.utils import get_current_branch
34

@@ -26,3 +27,10 @@ def test_running_on_incorrect_branch(temp_git_dir):
2627
with temp_git_dir.as_cwd():
2728
assert main(["--branches", "branch_one", "branch_two"]) == 1
2829

30+
def test_running_on_detached_head_no_filter(temp_git_detached_dir):
31+
with temp_git_detached_dir.as_cwd():
32+
assert main() == 0
33+
34+
def test_running_on_detached_head_with_filter(temp_git_detached_dir):
35+
with temp_git_detached_dir.as_cwd():
36+
assert main(["--branches", "branch_one", "branch_two"]) == 1

tests/conftest.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,11 @@ def temp_git_dir(tmpdir):
88
git_dir = tmpdir.join('gits')
99
subprocess.call(['git', 'init', '--', str(git_dir)])
1010
yield git_dir
11+
12+
@pytest.fixture
13+
def temp_git_detached_dir(tmpdir):
14+
git_dir = tmpdir.join('gits')
15+
subprocess.call(['git', 'init', '--', str(git_dir)])
16+
subprocess.call(['git', 'commit', '--allow-empty', '-m', 'initial commit'], cwd=git_dir)
17+
subprocess.call(['git', 'checkout', '--detach'], cwd=git_dir)
18+
yield git_dir

0 commit comments

Comments
 (0)