Repository navigation
Expand file tree
/
Copy path__init__.py
More file actions
216 lines (173 loc) · 7.01 KB
/
Copy path__init__.py
File metadata and controls
216 lines (173 loc) · 7.01 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
#!/usr/bin/env python
"""
git-flow
.. program:: git flow
.. cmdoption:: -v, --verbose
Produce more output.
.. cmdoption:: -h, --help
Print usage, help and information on the available commands.
"""
import argparse
from gitflow.core import GitFlow
from gitflow.util import itersubclasses
class GitFlowCommand(object):
"""
This is just an empty class to serve as the base class for all command line
level sub commands. Since the git-flow loader will auto-detect all
subclasses, implementing a new subcommand is as easy as subclassing the
:class:`GitFlowCommand`.
"""
def register_parser(self, parent):
raise NotImplementedError("Implement this method in a subclass.")
def run(self, args):
raise NotImplementedError("Implement this method in a subclass.")
class InitCommand(GitFlowCommand):
def register_parser(self, parent):
p = parent.add_parser('init',
help='Initialize a repository for gitflow.')
p.add_argument('-f', '--force', action='store_true',
help='force reinitialization of the gitflow preferences')
p.set_defaults(func=self.run)
return p
def run(self, args):
print('-------------------')
print('Init has been run!')
print(args)
print('-------------------')
class FeatureCommand(GitFlowCommand):
def register_parser(self, parent):
p = parent.add_parser('feature', help='Manage your feature branches.')
sub = p.add_subparsers(title='Actions')
# list
p = sub.add_parser('list', help='List all feature branches.')
p.set_defaults(func=self.run_list)
p.add_argument('-v', '--verbose', action='store_true',
help='Be verbose (more output).')
# start
p = sub.add_parser('start', help='Start a new feature branch.')
p.set_defaults(func=self.run_start)
p.add_argument('-F', '--fetch', action='store_true',
help='Fetch from origin before performing local operation.')
p.add_argument('name')
p.add_argument('base', nargs='?')
# finish
p = sub.add_parser('finish', help='Finish a feature branch.')
p.set_defaults(func=self.run_finish)
p.add_argument('-r', '--rebase', action='store_true',
help='Finish branch by rebasing first.')
p.add_argument('-F', '--fetch', action='store_true',
help='Fetch from origin before performing local operation.')
p.add_argument('nameprefix')
# publish
p = sub.add_parser('publish',
help='Publish this feature branch to origin.')
p.set_defaults(func=self.run_publish)
p.add_argument('name')
# track
p = sub.add_parser('track',
help='Track a feature branch from origin.')
p.set_defaults(func=self.run_track)
p.add_argument('name')
# diff
p = sub.add_parser('diff',
help='Show a diff of changes since this feature branched off.')
p.set_defaults(func=self.run_diff)
p.add_argument('nameprefix', nargs='?')
# rebase
p = sub.add_parser('rebase',
help='Rebase a feature branch on top of develop.')
p.set_defaults(func=self.run_rebase)
p.add_argument('-i', '--interactive', action='store_true',
help='Start an interactive rebase.')
p.add_argument('nameprefix', nargs='?')
# checkout
p = sub.add_parser('checkout',
help='Check out the given feature branch.')
p.set_defaults(func=self.run_checkout)
p.add_argument('nameprefix')
# pull
p = sub.add_parser('pull',
help='Pull a feature branch from a remote peer.')
p.set_defaults(func=self.run_pull)
p.add_argument('remote')
p.add_argument('name', nargs='?')
def run_list(self, args):
gitflow = GitFlow()
gitflow.start_transaction()
manager = gitflow.managers['feature']
branches = manager.list()
if not branches:
warn('No feature branches exist.')
warn('')
warn('You can start a new feature branch:')
warn('')
warn(' git flow feature start <name> [<base>]')
warn('')
raise SystemExit()
# determine the longest branch name
lenfunc = lambda b: len(b.name)
width = max(map(lenfunc, branches)) - len(manager.prefix) + 3
develop_sha = gitflow.develop().commit.hexsha
for branch in branches:
is_active = gitflow.repo.active_branch == branch
if is_active:
prefix = '* '
else:
prefix = ' '
name = manager.shorten(branch.name)
extra_info = ''
if args.verbose:
name = name.ljust(width)
branch_sha = branch.commit.hexsha
base_sha = gitflow.repo.git.merge_base(develop_sha, branch_sha)
if branch_sha == develop_sha:
extra_info = '(no commits yet)'
elif base_sha == branch_sha:
extra_info = '(is behind develop, may ff)'
elif base_sha == develop_sha:
extra_info = '(based on latest develop)'
else:
extra_info = '(may be rebased)'
print(prefix + name + extra_info)
def run_start(self, args):
gitflow = GitFlow()
gitflow.start_transaction('create feature branch %s (from %s)' % \
(args.name, args.base))
gitflow.create('feature', args.name, args.base)
def run_finish(self, args):
gitflow = GitFlow()
gitflow.start_transaction('finishing feature branch %s'
% args.nameprefix)
gitflow.finish('feature', args.nameprefix)
def run_publish(self, args): pass
def run_track(self, args): pass
def run_diff(self, args): pass
def run_rebase(self, args): pass
def run_checkout(self, args): pass
def run_pull(self, args): pass
class ReleaseCommand(GitFlowCommand):
def register_parser(self, parent):
p = parent.add_parser('release', help='Manage your release branches.')
p.add_argument('-v', '--verbose', action='store_true',
help='be verbose (more output)')
p.set_defaults(func=self.run)
return p
def run(self, args):
print 'release ran'
print(args)
class VersionCommand(GitFlowCommand):
def register_parser(self, parent):
p = parent.add_parser('version', help='Show the version of gitflow.')
p.set_defaults(func=self.run)
def run(self, args):
from gitflow import __version__
print(__version__)
def main():
parser = argparse.ArgumentParser(prog='git flow')
placeholder = parser.add_subparsers(title='Subcommands')
for cls in itersubclasses(GitFlowCommand):
cls().register_parser(placeholder)
args = parser.parse_args()
args.func(args)
if __name__ == '__main__':
main()