1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
|
from .utils import do_ex, trace, has_command
from .version import meta
from os.path import isfile, join
import warnings
try:
from os.path import samefile
except ImportError:
from .win_py31_compat import samefile
FILES_COMMAND = 'git ls-files'
DEFAULT_DESCRIBE = 'git describe --dirty --tags --long --match *.*'
class GitWorkdir(object):
"""experimental, may change at any time"""
def __init__(self, path):
self.path = path
def do_ex(self, cmd):
return do_ex(cmd, cwd=self.path)
@classmethod
def from_potential_worktree(cls, wd):
real_wd, _, ret = do_ex('git rev-parse --show-toplevel', wd)
if ret:
return
trace('real root', real_wd)
if not samefile(real_wd, wd):
return
return cls(real_wd)
def is_dirty(self):
out, _, _ = self.do_ex("git status --porcelain --untracked-files=no")
return bool(out)
def is_shallow(self):
return isfile(join(self.path, '.git/shallow'))
def fetch_shallow(self):
self.do_ex("git fetch --unshallow")
def node(self):
rev_node, _, ret = self.do_ex('git rev-parse --verify --quiet HEAD')
if not ret:
return rev_node[:7]
def count_all_nodes(self):
revs, _, _ = self.do_ex('git rev-list HEAD')
return revs.count('\n') + 1
def warn_on_shallow(wd):
"""experimental, may change at any time"""
if wd.is_shallow():
warnings.warn('"%s" is shallow and may cause errors' % (wd.path,))
def fetch_on_shallow(wd):
"""experimental, may change at any time"""
if wd.is_shallow():
warnings.warn('"%s" was shallow, git fetch was used to rectify')
wd.fetch_shallow()
def fail_on_shallow(wd):
"""experimental, may change at any time"""
if wd.is_shallow():
raise ValueError(
'%r is shallow, please correct with '
'"git fetch --unshallow"' % wd.path)
def parse(root, describe_command=DEFAULT_DESCRIBE, pre_parse=warn_on_shallow):
"""
:param pre_parse: experimental pre_parse action, may change at any time
"""
if not has_command('git'):
return
wd = GitWorkdir.from_potential_worktree(root)
if wd is None:
return
if pre_parse:
pre_parse(wd)
out, err, ret = do_ex(describe_command, root)
if ret:
# If 'git describe' failed, try to get the information otherwise.
rev_node = wd.node()
dirty = wd.is_dirty()
if rev_node is None:
return meta('0.0', distance=0, dirty=dirty)
return meta(
'0.0',
distance=wd.count_all_nodes(),
node='g' + rev_node,
dirty=dirty,
)
# 'out' looks e.g. like 'v1.5.0-0-g4060507' or
# 'v1.15.1rc1-37-g9bd1298-dirty'.
if out.endswith('-dirty'):
dirty = True
out = out[:-6]
else:
dirty = False
tag, number, node = out.rsplit('-', 2)
number = int(number)
if number:
return meta(tag, distance=number, node=node, dirty=dirty)
else:
return meta(tag, node=node, dirty=dirty)
|