Skip to content

Commit 35f6132

Browse files
committed
Fix #174 detect git in project subdir
1 parent deef68e commit 35f6132

File tree

2 files changed

+34
-16
lines changed

2 files changed

+34
-16
lines changed

skipper/git.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import os.path
21
import logging
2+
import os
33
import subprocess
44

55

66
def get_hash(short=False):
7-
if not os.path.exists('.git'):
7+
if is_git_repository():
88
logging.warning('*** Not working in a git repository ***')
99
return 'none'
1010

@@ -22,3 +22,11 @@ def get_hash(short=False):
2222
def uncommitted_changes():
2323
"""Return True is there are uncommitted changes."""
2424
return subprocess.call(['git', 'diff', '--quiet', 'HEAD']) != 0
25+
26+
27+
def is_git_repository():
28+
if os.path.exists('.git'):
29+
return True
30+
else:
31+
command = ['git', 'rev-parse', '--is-inside-work-tree']
32+
return subprocess.call(command, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) == 0

tests/test_git.py

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,33 +9,43 @@
99

1010
class TestGit(unittest.TestCase):
1111
@mock.patch('subprocess.check_output', return_value=GIT_HASH_FULL)
12-
@mock.patch('os.path.exists', return_value=True)
13-
def test_get_hash_with_default_argument(self, exists_mock, check_output_mock):
12+
@mock.patch('git.is_git_repository', return_value=True)
13+
def test_get_hash_with_default_argument(self, is_git_repository_mock, check_output_mock):
1414
git_hash = git.get_hash()
15-
exists_mock.assert_called_once_with('.git')
15+
is_git_repository_mock.assert_called_once()
1616
check_output_mock.assert_called_once_with(['git', 'rev-parse', 'HEAD'])
1717
self.assertEqual(git_hash, GIT_HASH_FULL.decode('utf-8'))
1818

1919
@mock.patch('subprocess.check_output', return_value=GIT_HASH_FULL)
20-
@mock.patch('os.path.exists', return_value=True)
21-
def test_get_full_hash(self, exists_mock, check_output_mock):
20+
@mock.patch('git.is_git_repository', return_value=True)
21+
def test_get_full_hash(self, is_git_repository_mock, check_output_mock):
2222
git_hash = git.get_hash(short=False)
23-
exists_mock.assert_called_once_with('.git')
23+
is_git_repository_mock.assert_called_once()
2424
check_output_mock.assert_called_once_with(['git', 'rev-parse', 'HEAD'])
2525
self.assertEqual(git_hash, GIT_HASH_FULL.decode('utf-8'))
2626

2727
@mock.patch('subprocess.check_output', return_value=GIT_HASH_SHORT)
28-
@mock.patch('os.path.exists', return_value=True)
29-
def test_get_short_hash(self, exists_mock, check_output_mock):
28+
@mock.patch('git.is_git_repository', return_value=True)
29+
def test_get_short_hash(self, is_git_repository_mock, check_output_mock):
3030
git_hash = git.get_hash(short=True)
31-
exists_mock.assert_called_once_with('.git')
31+
is_git_repository_mock.assert_called_once()
3232
check_output_mock.assert_called_once_with(['git', 'rev-parse', '--short', 'HEAD'])
3333
self.assertEqual(git_hash, GIT_HASH_SHORT.decode('utf-8'))
3434

35-
@mock.patch('subprocess.check_output')
35+
@mock.patch('git.is_git_repository', return_value=False)
36+
def test_not_in_git_project(self, is_git_repository_mock):
37+
self.assertEqual(git.get_hash(), 'none')
38+
is_git_repository_mock.assert_called_once()
39+
40+
@mock.patch('subprocess.call', return_value=0)
3641
@mock.patch('os.path.exists', return_value=False)
37-
def test_not_in_git_project(self, exists_mock, check_output_mock):
38-
git_hash = git.get_hash()
42+
def test_should_be_in_git_project(self, call_mock):
43+
self.assertTrue(git.is_git_repository())
44+
call_mock.assert_called_once_with(['git', 'rev-parse', '--is-inside-work-tree'])
45+
46+
@mock.patch('subprocess.call', return_value=1)
47+
@mock.patch('os.path.exists', return_value=False)
48+
def test_should_not_be_in_git_project(self, exists_mock, call_mock):
49+
self.assertFalse(git.is_git_repository())
3950
exists_mock.assert_called_once_with('.git')
40-
check_output_mock.assert_not_called()
41-
self.assertEqual(git_hash, 'none')
51+
call_mock.assert_called_once_with(['git', 'rev-parse', '--is-inside-work-tree'])

0 commit comments

Comments
 (0)