1
|
|
|
"""Utilities to call Git commands.""" |
2
|
|
|
|
3
|
1 |
|
import os |
4
|
1 |
|
import logging |
5
|
1 |
|
from contextlib import suppress |
6
|
|
|
|
7
|
1 |
|
from . import common |
8
|
1 |
|
from .shell import call |
9
|
1 |
|
from .exceptions import ShellError |
10
|
|
|
|
11
|
|
|
|
12
|
1 |
|
log = logging.getLogger(__name__) |
|
|
|
|
13
|
|
|
|
14
|
|
|
|
15
|
1 |
|
def git(*args, **kwargs): |
|
|
|
|
16
|
1 |
|
return call('git', *args, **kwargs) |
17
|
|
|
|
18
|
|
|
|
19
|
1 |
|
def clone(repo, path, *, cache=None): |
20
|
|
|
"""Clone a new Git repository.""" |
21
|
1 |
|
cache = cache or os.path.expanduser("~/.gitcache") |
22
|
1 |
|
cache = os.path.normpath(cache) |
23
|
|
|
|
24
|
1 |
|
name = repo.split('/')[-1] |
25
|
1 |
|
if name.endswith(".git"): |
26
|
1 |
|
name = name[:-4] |
27
|
|
|
|
28
|
1 |
|
reference = os.path.join(cache, name + ".reference") |
29
|
1 |
|
if not os.path.isdir(reference): |
30
|
1 |
|
git('clone', '--mirror', repo, reference) |
31
|
|
|
|
32
|
1 |
|
git('clone', '--reference', reference, repo, os.path.normpath(path)) |
33
|
|
|
|
34
|
|
|
|
35
|
1 |
|
def fetch(repo, rev=None): |
36
|
|
|
"""Fetch the latest changes from the remote repository.""" |
37
|
1 |
|
git('remote', 'set-url', 'origin', repo) |
38
|
1 |
|
args = ['fetch', '--tags', '--force', '--prune', 'origin'] |
39
|
1 |
|
if rev: |
40
|
1 |
|
if len(rev) == 40: |
41
|
1 |
|
pass # fetch only works with a SHA if already present locally |
42
|
1 |
|
elif '@' in rev: |
43
|
1 |
|
pass # fetch doesn't work with rev-parse |
44
|
|
|
else: |
45
|
1 |
|
args.append(rev) |
46
|
1 |
|
git(*args) |
|
|
|
|
47
|
|
|
|
48
|
|
|
|
49
|
1 |
|
def changes(include_untracked=False, display_status=True, _show=False): |
50
|
|
|
"""Determine if there are changes in the working tree.""" |
51
|
1 |
|
status = False |
52
|
|
|
|
53
|
1 |
|
try: |
54
|
|
|
# Refresh changes |
55
|
1 |
|
git('update-index', '-q', '--refresh', _show=False) |
56
|
|
|
|
57
|
|
|
# Check for uncommitted changes |
58
|
1 |
|
git('diff-index', '--quiet', 'HEAD', _show=_show) |
59
|
|
|
|
60
|
|
|
# Check for untracked files |
61
|
1 |
|
output = git('ls-files', '--others', '--exclude-standard', _show=_show) |
62
|
|
|
|
63
|
1 |
|
except ShellError: |
64
|
1 |
|
status = True |
65
|
|
|
|
66
|
|
|
else: |
67
|
1 |
|
status = bool(output.splitlines()) and include_untracked |
68
|
|
|
|
69
|
1 |
|
if status and display_status: |
70
|
1 |
|
with suppress(ShellError): |
71
|
1 |
|
for line in git('status', _show=True).splitlines(): |
72
|
1 |
|
common.show(line, color='changes') |
73
|
|
|
|
74
|
1 |
|
return status |
75
|
|
|
|
76
|
|
|
|
77
|
1 |
|
def update(rev, *, clean=True, fetch=False): # pylint: disable=redefined-outer-name |
|
|
|
|
78
|
|
|
"""Update the working tree to the specified revision.""" |
79
|
1 |
|
hide = {'_show': False, '_ignore': True} |
80
|
|
|
|
81
|
1 |
|
git('stash', **hide) |
|
|
|
|
82
|
1 |
|
if clean: |
83
|
1 |
|
git('clean', '--force', '-d', '-x', _show=False) |
84
|
|
|
|
85
|
1 |
|
rev = _get_sha_from_rev(rev) |
86
|
1 |
|
git('checkout', '--force', rev) |
87
|
1 |
|
git('branch', '--set-upstream-to', 'origin/' + rev, **hide) |
|
|
|
|
88
|
|
|
|
89
|
1 |
|
if fetch: |
90
|
|
|
# if `rev` was a branch it might be tracking something older |
91
|
1 |
|
git('pull', '--ff-only', '--no-rebase', **hide) |
|
|
|
|
92
|
|
|
|
93
|
|
|
|
94
|
1 |
|
def get_url(): |
95
|
|
|
"""Get the current repository's URL.""" |
96
|
1 |
|
return git('config', '--get', 'remote.origin.url', _show=False) |
97
|
|
|
|
98
|
|
|
|
99
|
1 |
|
def get_hash(_show=False): |
100
|
|
|
"""Get the current working tree's hash.""" |
101
|
1 |
|
return git('rev-parse', 'HEAD', _show=_show) |
102
|
|
|
|
103
|
|
|
|
104
|
1 |
|
def get_tag(): |
105
|
|
|
"""Get the current working tree's tag (if on a tag).""" |
106
|
1 |
|
return git('describe', '--tags', '--exact-match', _show=False, _ignore=True) |
107
|
|
|
|
108
|
|
|
|
109
|
1 |
|
def get_branch(): |
110
|
|
|
"""Get the current working tree's branch.""" |
111
|
1 |
|
return git('rev-parse', '--abbrev-ref', 'HEAD', _show=False) |
112
|
|
|
|
113
|
|
|
|
114
|
1 |
|
def _get_sha_from_rev(rev): |
115
|
|
|
"""Get a rev-parse string's hash.""" |
116
|
1 |
|
if '@{' in rev: # TODO: use regex for this |
|
|
|
|
117
|
1 |
|
parts = rev.split('@') |
118
|
1 |
|
branch = parts[0] |
119
|
1 |
|
date = parts[1].strip("{}") |
120
|
1 |
|
git('checkout', '--force', branch, _show=False) |
121
|
1 |
|
rev = git('rev-list', '-n', '1', '--before={!r}'.format(date), |
122
|
|
|
branch, _show=False) |
123
|
|
|
return rev |
124
|
|
|
|
This check looks for invalid names for a range of different identifiers.
You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.
If your project includes a Pylint configuration file, the settings contained in that file take precedence.
To find out more about Pylint, please refer to their site.