Skip to content

Commit 239346b

Browse files
committed
Fix #174 detect git in project subdir
1 parent 84db5a8 commit 239346b

File tree

2 files changed

+40
-16
lines changed

2 files changed

+40
-16
lines changed

skipper/git.py

Lines changed: 9 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 not is_git_repository():
88
logging.warning('*** Not working in a git repository ***')
99
return 'none'
1010

@@ -22,3 +22,10 @@ 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+
command = ['git', 'rev-parse', '--is-inside-work-tree']
31+
return subprocess.call(command, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) == 0

tests/test_git.py

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import subprocess
12
import unittest
23
import mock
34
from skipper import git
@@ -9,33 +10,49 @@
910

1011
class TestGit(unittest.TestCase):
1112
@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):
13+
@mock.patch('skipper.git.is_git_repository', return_value=True)
14+
def test_get_hash_with_default_argument(self, is_git_repository_mock, check_output_mock):
1415
git_hash = git.get_hash()
15-
exists_mock.assert_called_once_with('.git')
16+
is_git_repository_mock.assert_called_once()
1617
check_output_mock.assert_called_once_with(['git', 'rev-parse', 'HEAD'])
1718
self.assertEqual(git_hash, GIT_HASH_FULL.decode('utf-8'))
1819

1920
@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):
21+
@mock.patch('skipper.git.is_git_repository', return_value=True)
22+
def test_get_full_hash(self, is_git_repository_mock, check_output_mock):
2223
git_hash = git.get_hash(short=False)
23-
exists_mock.assert_called_once_with('.git')
24+
is_git_repository_mock.assert_called_once()
2425
check_output_mock.assert_called_once_with(['git', 'rev-parse', 'HEAD'])
2526
self.assertEqual(git_hash, GIT_HASH_FULL.decode('utf-8'))
2627

2728
@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):
29+
@mock.patch('skipper.git.is_git_repository', return_value=True)
30+
def test_get_short_hash(self, is_git_repository_mock, check_output_mock):
3031
git_hash = git.get_hash(short=True)
31-
exists_mock.assert_called_once_with('.git')
32+
is_git_repository_mock.assert_called_once()
3233
check_output_mock.assert_called_once_with(['git', 'rev-parse', '--short', 'HEAD'])
3334
self.assertEqual(git_hash, GIT_HASH_SHORT.decode('utf-8'))
3435

35-
@mock.patch('subprocess.check_output')
36+
@mock.patch('skipper.git.is_git_repository', return_value=False)
37+
def test_not_in_git_project(self, is_git_repository_mock):
38+
self.assertEqual(git.get_hash(), 'none')
39+
is_git_repository_mock.assert_called_once()
40+
41+
@mock.patch('os.path.exists', return_value=True)
42+
def test_should_be_in_git_project_os_path(self, exists_mock):
43+
self.assertTrue(git.is_git_repository())
44+
exists_mock.assert_called_once_with('.git')
45+
46+
@mock.patch('subprocess.call', return_value=0)
3647
@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()
48+
def test_should_be_in_git_project_git(self, exists_mock, call_mock):
49+
self.assertTrue(git.is_git_repository())
50+
exists_mock.assert_called_once_with('.git')
51+
call_mock.assert_called_once_with(['git', 'rev-parse', '--is-inside-work-tree'], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
52+
53+
@mock.patch('subprocess.call', return_value=1)
54+
@mock.patch('os.path.exists', return_value=False)
55+
def test_should_not_be_in_git_project(self, exists_mock, call_mock):
56+
self.assertFalse(git.is_git_repository())
3957
exists_mock.assert_called_once_with('.git')
40-
check_output_mock.assert_not_called()
41-
self.assertEqual(git_hash, 'none')
58+
call_mock.assert_called_once_with(['git', 'rev-parse', '--is-inside-work-tree'], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)

0 commit comments

Comments
 (0)