Repository navigation
Expand file tree
/
Copy pathbranches.py
More file actions
247 lines (198 loc) · 8.11 KB
/
Copy pathbranches.py
File metadata and controls
247 lines (198 loc) · 8.11 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
from git import GitCommandError
class NoSuchBranchError(Exception):
pass
class PrefixNotUniqueError(Exception):
pass
class BranchManager(object):
"""
Initializes an instance of :class:`BranchManager`. A branch
manager is responsible for listing, creating, merging, deleting,
finishing (i.e. merging+deleting) branches of a given type.
:param gitflow:
The :class:`gitflow.core.GitFlow` instance that this branch
manager belongs to.
:param prefix:
The prefix to use for the type of branches that this branch
manager manages.
"""
def __init__(self, gitflow, prefix=None):
from gitflow.core import GitFlow
assert isinstance(gitflow, GitFlow), 'Argument \'gitflow\' must be a GitFlow instance.'
self.gitflow = gitflow
if not prefix is None:
assert isinstance(prefix, basestring), 'Argument \'prefix\' must be a string.'
self.prefix = prefix
def default_base(self):
"""
:returns:
The name of branch to use as the default base for branching off from
in case no explicit base is specified.
This method can be overriden in a subclass of :class:`BranchManager`.
If not overriden, the default is to use the "develop" branch.
"""
return self.gitflow.develop_name()
def list(self):
"""
:returns:
A list of all branches of the type that this manager manages. See
also :meth:`iter`.
"""
return list(self.iter())
def by_name_prefix(self, nameprefix):
"""
If exactly one branch of the type that this manager manages starts with
the given name prefix, returns that branch. Raises
:exc:`NoSuchBranchError` in case no branches exist with the given
prefix, or :exc:`PrefixNotUniqueError` in case multiple matches are
found.
:param nameprefix:
The name prefix (or full name) of the short branch name to match.
:returns:
The :class:`git.refs.Head` instance of the branch that can be
uniquely identified by the given name prefix.
"""
matches = filter(lambda b: self.shorten(b.name).startswith(nameprefix),
self.iter())
num_matches = len(matches)
if num_matches == 1:
return matches[0]
elif num_matches < 1:
raise NoSuchBranchError('There is no %s branch matching the '
'prefix "%s"' % (self.identifier, nameprefix))
else:
raise PrefixNotUniqueError('There are multiple %s branches '
'matching the prefix "%s": %s' % (self.identifier,
nameprefix, matches))
def iter(self):
"""
:returns:
An iterator, iterating over all branches of the type that this
manager manages.
"""
for branch in self.gitflow.repo.branches:
if branch.name.startswith(self.prefix):
yield branch
def create(self, name, base=None):
"""
Creates a branch of the type that this manager manages and checks it
out.
:param name:
The (short) name of the branch to create.
:param base:
The base commit or ref to base the branch off from. If a base is
not provided explicitly, the default base for this type of branch is
used. See also :meth:`default_base`.
:returns:
The newly created :class:`git.refs.Head` reference.
"""
repo = self.gitflow.repo
full_name = self.prefix + name
if base is None:
base = self.default_base()
branch = repo.create_head(full_name, base)
branch.checkout()
return branch
def _is_single_commit_branch(self, from_, to):
git = self.gitflow.repo.git
commits = git.rev_list('%s...%s' % (from_, to), n=2).split()
return len(commits) == 1
def merge(self, name, into, message=None):
"""
This merges the branch named :attr:`name` into the branch named
:attr:`into`, using commit message :attr:`message`.
:param name:
The name of the branch that needs merging.
:param into:
The name of the branch to merge into.
:param message:
The commit message to use for the merge commit. If it is not given,
a default merge message is used. You can use the following string
placeholders, which :meth:`merge` will expand::
%(name)s = The full name of the branch (e.g. "feature/foo")
%(short_name)s = The friendly name of the branch (e.g. "foo")
%(identifier)s = The type (e.g. "feature", "hotfix", etc.)
You typically don't need to override this method in a subclass.
"""
repo = self.gitflow.repo
repo.branches[into].checkout()
full_name = self.prefix + name
kwargs = dict()
if not self._is_single_commit_branch(into, full_name):
kwargs['no_ff'] = True
if not message is None:
message = message % \
dict(name=full_name, identifier=self.identifier,
short_name=name)
kwargs['message'] = message
repo.git.merge(full_name, **kwargs)
def delete(self, name, force=False):
"""
This deletes a branch of the type that this manager manages named
:attr:`name`.
:param name:
The (short) name of the branch to delete.
:param force:
Delete the branch, even if this would lead to data loss.
"""
repo = self.gitflow.repo
full_name = self.prefix + name
repo.delete_head(full_name, force=force)
def finish(self, name):
"""
Finishes the branch of the type that this manager manages named
:attr:`name`. Finishing means that:
* The branch is merged into all branches that require it to be merged
in to. The model prescribes the branching/merging rules for each
branch type, so this depends on the implementing subclass.
* The branch is deleted if all merges are successful.
:param name:
The (short) name of the branch to finish.
"""
self.merge(name, self.gitflow.develop_name(),
'Finished %(identifier)s %(short_name)s.')
self.delete(name)
def shorten(self, full_name):
"""
Returns the friendly (short) name of this branch, without the prefix,
given the fully qualified branch name.
:param full_name:
The full name of the branch as it is known to Git, including the
prefix.
:returns:
The friendly name of the branch.
"""
if full_name.startswith(self.prefix):
return full_name[len(self.prefix):]
else:
return full_name
class FeatureBranchManager(BranchManager):
identifier = 'feature'
prefix = 'feature/'
class ReleaseBranchManager(BranchManager):
identifier = 'release'
prefix = 'release/'
def finish(self, name):
self.merge(name, self.gitflow.master_name(),
'Finished %(identifier)s %(short_name)s.')
self.merge(name, self.gitflow.develop_name(),
'Finished %(identifier)s %(short_name)s.')
self.delete(name)
class HotfixBranchManager(BranchManager):
identifier = 'hotfix'
prefix = 'hotfix/'
def default_base(self):
return self.gitflow.master_name()
def finish(self, name):
self.merge(name, self.gitflow.master_name(),
'Finished %(identifier)s %(short_name)s.')
self.merge(name, self.gitflow.develop_name(),
'Finished %(identifier)s %(short_name)s.')
self.delete(name)
class SupportBranchManager(BranchManager):
identifier = 'support'
prefix = 'support/'
def default_base(self):
return self.gitflow.master_name()
def finish(self, name):
raise NotImplementedError("Finishing support branches does not make "
"any sense.")