Skip to content

Commit 9cf2009

Browse files
Merge pull request #118 from RonnyPfannschmidt/git-shallow
address #93 - experimental tools for interacting with git shallow clones
2 parents dccfea9 + 79ddd09 commit 9cf2009

File tree

4 files changed

+72
-5
lines changed

4 files changed

+72
-5
lines changed

CHANGELOG.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ v1.15.0
99
* update trove classifiers
1010
* fix issue #84: document using the installed package metadata for sphinx
1111
* fix issue #81: fail more gracious when git/hg are missing
12+
* address issue #93: provide an experimental api to customize behaviour on shallow git repos
13+
a custom parse function may pick pre parse actions to do when using git
14+
1215

1316
v1.14.1
1417
=======

setuptools_scm/git.py

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from .utils import do_ex, trace, has_command
22
from .version import meta
3-
from os.path import abspath, normcase, realpath
4-
3+
from os.path import abspath, normcase, realpath, isfile, join
4+
import warnings
55

66
FILES_COMMAND = 'git ls-files'
77
DEFAULT_DESCRIBE = 'git describe --tags --long --match *.*'
@@ -12,6 +12,7 @@ def _normalized(path):
1212

1313

1414
class GitWorkdir(object):
15+
"""experimental, may change at any time"""
1516
def __init__(self, path):
1617
self.path = path
1718

@@ -33,6 +34,12 @@ def is_dirty(self):
3334
out, _, _ = self.do_ex("git status --porcelain --untracked-files=no")
3435
return bool(out)
3536

37+
def is_shallow(self):
38+
return isfile(join(self.path, '.git/shallow'))
39+
40+
def fetch_shallow(self):
41+
self.do_ex("git fetch --unshallow")
42+
3643
def node(self):
3744
rev_node, _, ret = self.do_ex('git rev-parse --verify --quiet HEAD')
3845
if not ret:
@@ -43,11 +50,36 @@ def count_all_nodes(self):
4350
return revs.count('\n') + 1
4451

4552

46-
def parse(root, describe_command=DEFAULT_DESCRIBE):
53+
def warn_on_shallow(wd):
54+
"""experimental, may change at any time"""
55+
if wd.is_shallow():
56+
warnings.warn('"%s" is shallow and may cause errors' % (wd.path,))
57+
58+
59+
def fetch_on_shallow(wd):
60+
"""experimental, may change at any time"""
61+
if wd.is_shallow():
62+
warnings.warn('"%s" was shallow, git fetch was used to rectify')
63+
wd.fetch_shallow()
64+
65+
66+
def fail_on_shallow(wd):
67+
"""experimental, may change at any time"""
68+
if wd.is_shallow():
69+
raise ValueError(
70+
'%r is shallow, please correct with '
71+
'"git fetch --unshallow"' % wd.path)
72+
73+
74+
def parse(root, describe_command=DEFAULT_DESCRIBE, pre_parse=warn_on_shallow):
75+
"""
76+
:param pre_parse: experimental pre_parse action, may change at any time
77+
"""
4778
if not has_command('git'):
4879
return
4980
wd = GitWorkdir(root)
50-
81+
if pre_parse:
82+
pre_parse(wd)
5183
rev_node = wd.node()
5284
dirty = wd.is_dirty()
5385

testing/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,4 @@ def version(self):
6363

6464
@pytest.fixture
6565
def wd(tmpdir):
66-
return Wd(tmpdir)
66+
return Wd(tmpdir.ensure('wd', dir=True))

testing/test_git.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
from setuptools_scm import integration
2+
from setuptools_scm.utils import do
3+
from setuptools_scm import git
24
import pytest
35
from datetime import date
46

@@ -54,6 +56,36 @@ def test_git_dirty_notag(wd):
5456
assert today.strftime('.d%Y%m%d') in wd.version
5557

5658

59+
@pytest.fixture
60+
def shallow_wd(wd, tmpdir):
61+
wd.commit_testfile()
62+
wd.commit_testfile()
63+
wd.commit_testfile()
64+
target = tmpdir.join('wd_shallow')
65+
do(['git', 'clone', "file://%s" % wd.cwd, str(target,), '--depth=1'])
66+
return target
67+
68+
69+
def test_git_parse_shallow_warns(shallow_wd, recwarn):
70+
git.parse(str(shallow_wd))
71+
msg = recwarn.pop()
72+
assert 'is shallow and may cause errors' in str(msg.message)
73+
74+
75+
def test_git_parse_shallow_fail(shallow_wd):
76+
with pytest.raises(ValueError) as einfo:
77+
git.parse(str(shallow_wd), pre_parse=git.fail_on_shallow)
78+
79+
assert 'git fetch' in str(einfo.value)
80+
81+
82+
def test_git_shallow_autocorrect(shallow_wd, recwarn):
83+
git.parse(str(shallow_wd), pre_parse=git.fetch_on_shallow)
84+
msg = recwarn.pop()
85+
assert 'git fetch was used to rectify' in str(msg.message)
86+
git.parse(str(shallow_wd), pre_parse=git.fail_on_shallow)
87+
88+
5789
def test_find_files_stop_at_root_git(wd):
5890
wd.commit_testfile()
5991
wd.cwd.ensure('project/setup.cfg')

0 commit comments

Comments
 (0)