-
Notifications
You must be signed in to change notification settings - Fork 236
/
vcs_helpers.py
131 lines (93 loc) · 3.32 KB
/
vcs_helpers.py
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
119
120
121
122
123
124
125
126
127
128
129
130
131
import re
from git import GitCommandError, NoSuchPathError, Repo, TagObject
from .errors import GitError
from .settings import config
try:
repo = Repo('.git', search_parent_directories=True)
except NoSuchPathError:
repo = None
def get_commit_log(from_rev=None):
"""
Yields all commit messages from last to first.
"""
rev = None
if from_rev:
rev = '...{from_rev}'.format(from_rev=from_rev)
for commit in repo.iter_commits(rev):
yield (commit.hexsha, commit.message)
def get_last_version(skip_tags=None):
"""
return last version from repo tags
:return: a string contains version number
"""
skip_tags = skip_tags or []
def version_finder(x):
if isinstance(x.commit, TagObject):
return x.tag.tagged_date
return x.commit.committed_date
for i in sorted(repo.tags, reverse=True, key=version_finder):
if re.match(r'v\d+\.\d+\.\d+', i.name):
if i.name in skip_tags:
continue
return i.name[1:]
def get_version_from_tag(tag_name):
for i in repo.tags:
if i.name == tag_name:
return i.commit.hexsha
def get_repository_owner_and_name():
"""
Checks the origin remote to get the owner and name of the remote repository.
:return: a tuple of the owner and name
"""
url = repo.remote('origin').url
parts = re.search(r'([^/:]+)/([^/]+).git$', url)
return parts.group(1), parts.group(2)
def get_current_head_hash():
"""
Gets the commit hash of the current HEAD.
:return: a string with the commit hash.
"""
return repo.head.commit.name_rev.split(' ')[0]
def commit_new_version(version):
"""
Commits the file containing the version number variable with the version number as the commit
message.
:param version: The version number to be used in the commit message
"""
commit_message = config.get('semantic_release', 'commit_message')
message = '{0}\n\n{1}'.format(version, commit_message)
repo.git.add(config.get('semantic_release', 'version_variable').split(':')[0])
return repo.git.commit(m=message, author="semantic-release <semantic-release>")
def tag_new_version(version):
"""
Creates a new tag with the version number prefixed with v.
:param version: The version number used in the tag as a string.
"""
return repo.git.tag('-a', 'v{0}'.format(version), m='v{0}'.format(version))
def push_new_version(gh_token=None, owner=None, name=None):
"""
Runs git push and git push --tags
:param gh_token: Github token used to push.
:param owner: Organisation or user that owns the repository.
:param name: Name of repository.
"""
server = 'origin'
if gh_token:
server = 'https://{token}@{repo}'.format(
token=gh_token,
repo='github.com/{owner}/{name}.git'.format(owner=owner, name=name)
)
try:
repo.git.push(server, 'master')
repo.git.push('--tags', server, 'master')
except GitCommandError as error:
message = str(error)
if gh_token:
message = message.replace(gh_token, '[GH_TOKEN]')
raise GitError(message)
def checkout(branch):
"""
Checkout the given branch in the local repository.
:param branch: The branch to checkout.
"""
return repo.git.checkout(branch)