Repository navigation
Expand file tree
/
Copy path__init__.py
More file actions
710 lines (630 loc) · 25.2 KB
/
Copy path__init__.py
File metadata and controls
710 lines (630 loc) · 25.2 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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import (absolute_import, division, print_function,
unicode_literals)
"""
git-flow
.. program:: git flow
.. cmdoption:: -v, --verbose
Produce more output.
.. cmdoption:: -h, --help
Print usage, help and information on the available commands.
"""
#
# This file is part of `gitflow`.
# Copyright (c) 2010-2011 Vincent Driessen
# Copyright (c) 2012-2013 Hartmut Goebel
# Copyright (c) 2015 Christian Assing
# Distributed under a BSD-like license. For full terms see the file LICENSE.txt
#
import argparse
from gitflow.core import GitFlow, info, GitCommandError
from gitflow.util import itersubclasses
from gitflow.exceptions import (GitflowError,
NotInitialized, BranchTypeExistsError,
BaseNotOnBranch)
__copyright__ = "2010-2011 Vincent Driessen; 2012-2013 Hartmut Goebel"
__license__ = "BSD"
def die(*texts):
raise SystemExit('\n'.join(map(str, texts)))
class NotEmpty(argparse.Action):
def __call__(self, parser, namespace, values, option_string=None):
if not values:
raise argparse.ArgumentError(self, 'must not by empty.')
setattr(namespace, self.dest, values)
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`.
"""
@classmethod
def register_parser(cls, parent):
raise NotImplementedError("Implement this method in a subclass.")
@staticmethod
def run(args):
raise NotImplementedError("Implement this method in a subclass.")
class VersionCommand(GitFlowCommand):
@classmethod
def register_parser(cls, parent):
p = parent.add_parser('version', help='Show the version of gitflow.')
p.set_defaults(func=cls.run)
@staticmethod
def run(args):
from gitflow import __version__
print(__version__)
class StatusCommand(GitFlowCommand):
@classmethod
def register_parser(cls, parent):
p = parent.add_parser('status', help='Show some status.')
p.set_defaults(func=cls.run)
@staticmethod
def run(args):
gitflow = GitFlow()
for name, hexsha, is_active_branch in gitflow.status():
if is_active_branch:
prefix = '*'
else:
prefix = ' '
info('%s %s: %s' % (prefix, name, hexsha[:7]))
class InitCommand(GitFlowCommand):
@classmethod
def register_parser(cls, 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.add_argument('-d', '--defaults', action='store_true',
dest='use_defaults',
help='Use default branch naming conventions and prefixes.')
p.set_defaults(func=cls.run)
return p
@staticmethod
def run(args):
from . import _init
_init.run_default(args)
class FeatureCommand(GitFlowCommand):
@classmethod
def register_parser(cls, parent):
p = parent.add_parser('feature', help='Manage your feature branches.')
sub = p.add_subparsers(title='Actions')
cls.register_list(sub)
cls.register_start(sub)
cls.register_finish(sub)
cls.register_checkout(sub)
cls.register_diff(sub)
cls.register_rebase(sub)
cls.register_publish(sub)
cls.register_pull(sub)
cls.register_track(sub)
# - list
@classmethod
def register_list(cls, parent):
p = parent.add_parser('list',
help='List all existing feature branches '
'in the local repository.')
p.set_defaults(func=cls.run_list)
p.add_argument(
'-v', '--verbose', action='store_true',
help='Be verbose (more output).')
@staticmethod
def run_list(args):
gitflow = GitFlow()
gitflow.start_transaction()
gitflow.list('feature', 'name', use_tagname=False,
verbose=args.verbose)
# - start
@classmethod
def register_start(cls, parent):
p = parent.add_parser('start', help='Start a new feature branch.')
p.set_defaults(func=cls.run_start)
p.add_argument(
'-F', '--fetch', action='store_true',
help='Fetch from origin before performing local operation.')
p.add_argument('name', action=NotEmpty)
p.add_argument('base', nargs='?')
@staticmethod
def run_start(args):
gitflow = GitFlow()
# :fixme: Why does the sh-version not require a clean working dir?
# NB: `args.name` is required since the branch must not yet exist
# :fixme: get default value for `base`
gitflow.start_transaction(
'create feature branch %s (from %s)' % (args.name, args.base))
try:
branch = gitflow.create('feature', args.name, args.base,
fetch=args.fetch)
except (NotInitialized, BaseNotOnBranch):
# printed in main()
raise
except Exception as e:
die("Could not create feature branch %r" % args.name, e)
print()
print("Summary of actions:")
print("- A new branch", branch, "was created, based on", args.base)
print("- You are now on branch", branch)
print("")
print("Now, start committing on your feature. When done, use:")
print("")
print(" git flow feature finish", args.name)
print()
# - finish
@classmethod
def register_finish(cls, parent):
p = parent.add_parser('finish', help='Finish a feature branch.')
p.set_defaults(func=cls.run_finish)
p.add_argument(
'-F', '--fetch', action='store_true',
help='Fetch from origin before performing local operation.')
p.add_argument(
'-r', '--rebase', action='store_true',
help='Finish branch by rebasing first.')
p.add_argument(
'-k', '--keep', action='store_true',
help='Keep branch after performing finish.')
p.add_argument(
'-D', '--force-delete', action='store_true',
help='Force delete feature branch after finish.')
p.add_argument('nameprefix', nargs='?')
@staticmethod
def run_finish(args):
gitflow = GitFlow()
name = gitflow.nameprefix_or_current('feature', args.nameprefix)
gitflow.start_transaction('finishing feature branch %s' % name)
gitflow.finish('feature', name,
fetch=args.fetch, rebase=args.rebase,
keep=args.keep, force_delete=args.force_delete,
tagging_info=None)
# - checkout
@classmethod
def register_checkout(cls, parent):
p = parent.add_parser(
'checkout',
help='Check out (switch to) the given feature branch.')
p.set_defaults(func=cls.run_checkout)
p.add_argument('nameprefix', action=NotEmpty)
@staticmethod
def run_checkout(args):
gitflow = GitFlow()
# NB: Does not default to the current branch as `nameprefix` is required
name = gitflow.nameprefix_or_current('feature', args.nameprefix)
gitflow.start_transaction('checking out feature branch %s' % name)
gitflow.checkout('feature', name)
# - diff
@classmethod
def register_diff(cls, parent):
p = parent.add_parser(
'diff',
help='Show a diff of changes since this feature branched off.')
p.set_defaults(func=cls.run_diff)
p.add_argument('nameprefix', nargs='?')
@staticmethod
def run_diff(args):
gitflow = GitFlow()
name = gitflow.nameprefix_or_current('feature', args.nameprefix)
gitflow.start_transaction('diff for feature branch %s' % name)
gitflow.diff('feature', name)
# - rebase
@classmethod
def register_rebase(cls, parent):
p = parent.add_parser(
'rebase',
help='Rebase a feature branch on top of develop.')
p.set_defaults(func=cls.run_rebase)
p.add_argument(
'-i', '--interactive', action='store_true',
help='Start an interactive rebase.')
p.add_argument('nameprefix', nargs='?')
@staticmethod
def run_rebase(args):
gitflow = GitFlow()
name = gitflow.nameprefix_or_current('feature', args.nameprefix)
gitflow.start_transaction('rebasing feature branch %s' % name)
gitflow.rebase('feature', name, args.interactive)
# - publish
@classmethod
def register_publish(cls, parent):
p = parent.add_parser(
'publish',
help='Publish this feature branch to origin.')
p.set_defaults(func=cls.run_publish)
p.add_argument('nameprefix', nargs='?')
@staticmethod
def run_publish(args):
gitflow = GitFlow()
name = gitflow.nameprefix_or_current('feature', args.nameprefix)
gitflow.start_transaction('publishing feature branch %s' % name)
branch = gitflow.publish('feature', name)
print()
print("Summary of actions:")
print("- A new remote branch '%s' was created" % branch)
print("- The local branch '%s' was configured to track the remote branch" % branch)
print("- You are now on branch '%s'" % branch)
print()
# - pull
@classmethod
def register_pull(cls, parent):
p = parent.add_parser(
'pull',
help='Pull a feature branch from a remote peer.')
p.set_defaults(func=cls.run_pull)
p.add_argument('remote', action=NotEmpty,
help="Remote repository to pull from.")
p.add_argument(
'name', nargs='?',
help='Name of the feature branch to pull. '
'Defaults to the current branch, if it is a feature branch.')
# :todo: implement --prefix
# p.add-argument('-p', '--prefix',
# help='Alternative remote feature branch name prefix.')
@staticmethod
def run_pull(args):
gitflow = GitFlow()
name = gitflow.name_or_current('feature', args.name, must_exist=False)
gitflow.start_transaction('pulling remote feature branch %s '
'into local banch %s' % (args.remote, name))
gitflow.pull('feature', args.remote, name)
# - track
@classmethod
def register_track(cls, parent):
p = parent.add_parser(
'track',
help='Track a feature branch from origin.')
p.set_defaults(func=cls.run_track)
p.add_argument('name', action=NotEmpty)
@staticmethod
def run_track(args):
gitflow = GitFlow()
# NB: `args.name` is required since the branch must not yet exist
gitflow.start_transaction('tracking remote feature branch %s'
% args.name)
branch = gitflow.track('feature', args.name)
print()
print("Summary of actions:")
print("- A new remote tracking branch '%s' was created" % branch)
print("- You are now on branch '%s'" % branch)
print()
class ReleaseCommand(GitFlowCommand):
@classmethod
def register_parser(cls, parent):
p = parent.add_parser('release', help='Manage your release branches.')
p.add_argument(
'-v', '--verbose', action='store_true',
help='Be verbose (more output).')
sub = p.add_subparsers(title='Actions')
cls.register_list(sub)
cls.register_start(sub)
cls.register_finish(sub)
cls.register_publish(sub)
cls.register_track(sub)
# - list
@classmethod
def register_list(cls, parent):
p = parent.add_parser('list',
help='Lists all existing release branches '
'in the local repository.')
p.set_defaults(func=cls.run_list)
p.add_argument(
'-v', '--verbose', action='store_true',
help='Be verbose (more output).')
@staticmethod
def run_list(args):
gitflow = GitFlow()
gitflow.start_transaction()
gitflow.list('release', 'version', use_tagname=True,
verbose=args.verbose)
# - start
@classmethod
def register_start(cls, parent):
p = parent.add_parser('start', help='Start a new release branch.')
p.set_defaults(func=cls.run_start)
p.add_argument(
'-F', '--fetch', action='store_true',
# :todo: get "origin" from config
help='Fetch from origin before performing local operation.')
p.add_argument('version', action=NotEmpty)
p.add_argument('base', nargs='?')
@staticmethod
def run_start(args):
gitflow = GitFlow()
# NB: `args.version` is required since the branch must not yet exist
# :fixme: get default value for `base`
gitflow.start_transaction('create release branch %s (from %s)' % (args.version, args.base))
try:
gitflow.create('release', args.version, args.base, fetch=args.fetch)
except (NotInitialized, BranchTypeExistsError, BaseNotOnBranch):
# printed in main()
raise
except Exception as e:
die("Could not create release branch %r" % args.version, e)
print("Follow-up actions:")
print("- Bump the version number now!")
print("- Start committing last-minute fixes in preparing your release")
print("- When done, run:")
print()
print(" git flow release finish", args.version)
# - finish
@classmethod
def register_finish(cls, parent):
p = parent.add_parser('finish', help='Finish a release branch.')
p.set_defaults(func=cls.run_finish)
p.add_argument(
'-F', '--fetch', action='store_true',
help='Fetch from origin before performing local operation.')
p.add_argument(
'-p', '--push', action='store_true',
# :todo: get "origin" from config
help="Push to origin after performing finish.")
p.add_argument(
'-k', '--keep', action='store_true',
help='Keep branch after performing finish.')
p.add_argument('version', nargs='?')
g = p.add_argument_group('tagging options')
g.add_argument(
'-n', '--notag', action='store_true',
help="Don't tag this release.")
g.add_argument(
'-m', '--message',
help="Use the given tag message.")
g.add_argument(
'-s', '--sign', action='store_true',
help="Sign the release tag cryptographically.")
g.add_argument(
'-u', '--signingkey',
help="Use the given GPG-key for the digital signature "
"instead of the default git uses (implies -s).")
@staticmethod
def run_finish(args):
gitflow = GitFlow()
version = gitflow.name_or_current('release', args.version)
gitflow.start_transaction('finishing release branch %s' % version)
tagging_info = None
if not args.notag:
tagging_info = dict(
sign=args.sign or args.signingkey,
signingkey=args.signingkey,
message=args.message)
gitflow.finish(
'release', version,
fetch=args.fetch, rebase=False,
keep=args.keep, force_delete=False,
tagging_info=tagging_info)
# - publish
@classmethod
def register_publish(cls, parent):
p = parent.add_parser(
'publish',
help='Publish this release branch to origin.')
p.set_defaults(func=cls.run_publish)
p.add_argument('version', nargs='?')
@staticmethod
def run_publish(args):
gitflow = GitFlow()
version = gitflow.name_or_current('release', args.version)
gitflow.start_transaction('publishing release branch %s' % version)
branch = gitflow.publish('release', version)
print()
print("Summary of actions:")
print("- A new remote branch '%s' was created" % branch)
print("- The local branch '%s' was configured to track the remote branch" % branch)
print("- You are now on branch '%s'" % branch)
print()
# - track
@classmethod
def register_track(cls, parent):
p = parent.add_parser(
'track',
help='Track a release branch from origin.')
p.set_defaults(func=cls.run_track)
p.add_argument('version', action=NotEmpty)
@staticmethod
def run_track(args):
gitflow = GitFlow()
# NB: `args.version` is required since the branch must not yet exist
gitflow.start_transaction('tracking remote release branch %s'
% args.version)
branch = gitflow.track('release', args.version)
print()
print("Summary of actions:")
print("- A new remote tracking branch '%s' was created" % branch)
print("- You are now on branch '%s'" % branch)
print()
class HotfixCommand(GitFlowCommand):
@classmethod
def register_parser(cls, parent):
p = parent.add_parser('hotfix', help='Manage your hotfix branches.')
p.add_argument(
'-v', '--verbose', action='store_true',
help='Be verbose (more output).')
sub = p.add_subparsers(title='Actions')
cls.register_list(sub)
cls.register_start(sub)
cls.register_finish(sub)
cls.register_publish(sub)
# - list
@classmethod
def register_list(cls, parent):
p = parent.add_parser('list',
help='Lists all existing hotfix branches '
'in the local repository.')
p.set_defaults(func=cls.run_list)
p.add_argument(
'-v', '--verbose', action='store_true',
help='Be verbose (more output).')
@staticmethod
def run_list(args):
gitflow = GitFlow()
gitflow.start_transaction()
gitflow.list('hotfix', 'version', use_tagname=True,
verbose=args.verbose)
# - start
@classmethod
def register_start(cls, parent):
p = parent.add_parser('start', help='Start a new hotfix branch.')
p.set_defaults(func=cls.run_start)
p.add_argument(
'-F', '--fetch', action='store_true',
# :todo: get "origin" from config
help='Fetch from origin before performing local operation.')
p.add_argument('version', action=NotEmpty)
p.add_argument('base', nargs='?')
@staticmethod
def run_start(args):
gitflow = GitFlow()
# NB: `args.version` is required since the branch must not yet exist
# :fixme: get default value for `base`
gitflow.start_transaction('create hotfix branch %s (from %s)' % (args.version, args.base))
try:
branch = gitflow.create('hotfix', args.version, args.base,
fetch=args.fetch)
except (NotInitialized, BranchTypeExistsError, BaseNotOnBranch):
# printed in main()
raise
except Exception as e:
die("Could not create hotfix branch %r" % args.version, e)
print()
print("Summary of actions:")
print("- A new branch", branch, "was created, based on", args.base)
print("- You are now on branch", branch)
print("")
print("Follow-up actions:")
print("- Bump the version number now!")
print("- Start committing your hot fixes")
print("- When done, run:")
print()
print(" git flow hotfix finish", args.version)
# - finish
@classmethod
def register_finish(cls, parent):
p = parent.add_parser('finish', help='Finish a hotfix branch.')
p.set_defaults(func=cls.run_finish)
p.add_argument(
'-F', '--fetch', action='store_true',
help='Fetch from origin before performing local operation.')
p.add_argument(
'-p', '--push', action='store_true',
# :todo: get "origin" from config
help="Push to origin after performing finish.")
p.add_argument(
'-k', '--keep', action='store_true',
help='Keep branch after performing finish.')
p.add_argument('version', nargs='?')
g = p.add_argument_group('tagging options')
g.add_argument('-n', '--notag', action='store_true',
help="Don't tag this hotfix.")
g.add_argument('-m', '--message',
help="Use the given tag message.")
g.add_argument(
'-s', '--sign', action='store_true',
help="Sign the hotfix tag cryptographically.")
g.add_argument(
'-u', '--signingkey',
help="Use this given GPG-key for the digital signature "
"instead of the default git uses (implies -s).")
@staticmethod
def run_finish(args):
gitflow = GitFlow()
version = gitflow.name_or_current('hotfix', args.version)
gitflow.start_transaction('finishing hotfix branch %s' % version)
tagging_info = None
if not args.notag:
tagging_info = dict(
sign=args.sign or args.signingkey,
signingkey=args.signingkey,
message=args.message)
gitflow.finish(
'hotfix', version,
fetch=args.fetch, rebase=False,
keep=args.keep, force_delete=False,
tagging_info=tagging_info)
# - publish
@classmethod
def register_publish(cls, parent):
p = parent.add_parser(
'publish',
help='Publish this hotfix branch to origin.')
p.set_defaults(func=cls.run_publish)
p.add_argument('version', nargs='?')
@staticmethod
def run_publish(args):
gitflow = GitFlow()
version = gitflow.name_or_current('hotfix', args.version)
gitflow.start_transaction('publishing hotfix branch %s' % version)
branch = gitflow.publish('hotfix', version)
print()
print("Summary of actions:")
print("- A new remote branch '%s' was created" % branch)
print("- The local branch '%s' was configured to track the remote branch" % branch)
print("- You are now on branch '%s'" % branch)
print()
class SupportCommand(GitFlowCommand):
@classmethod
def register_parser(cls, parent):
p = parent.add_parser('support', help='Manage your support branches.')
p.add_argument(
'-v', '--verbose', action='store_true',
help='Be verbose (more output).')
sub = p.add_subparsers(title='Actions')
cls.register_list(sub)
cls.register_start(sub)
# - list
@classmethod
def register_list(cls, parent):
p = parent.add_parser('list',
help='Lists all existing support branches '
'in the local repository.')
p.set_defaults(func=cls.run_list)
p.add_argument(
'-v', '--verbose', action='store_true',
help='Be verbose (more output).')
@staticmethod
def run_list(args):
gitflow = GitFlow()
gitflow.start_transaction()
gitflow.list('support', 'version', use_tagname=True,
verbose=args.verbose)
# - start
@classmethod
def register_start(cls, parent):
p = parent.add_parser('start', help='Start a new support branch.')
p.set_defaults(func=cls.run_start)
p.add_argument(
'-F', '--fetch', action='store_true',
help='Fetch from origin before performing local operation.')
p.add_argument('name', action=NotEmpty)
p.add_argument('base', nargs='?')
@staticmethod
def run_start(args):
gitflow = GitFlow()
# NB: `args.name` is required since the branch must not yet exist
# :fixme: get default value for `base`
gitflow.start_transaction('create support branch %s (from %s)' % (args.name, args.base))
try:
branch = gitflow.create('support', args.name, args.base,
fetch=args.fetch)
except (NotInitialized, BranchTypeExistsError, BaseNotOnBranch):
# printed in main()
raise
except Exception as e:
die("Could not create support branch %r" % args.name, e)
print()
print("Summary of actions:")
print("- A new branch", branch, "was created, based on", args.base)
print("- You are now on branch", branch)
print("")
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()
try:
args.func(args)
except KeyboardInterrupt:
raise SystemExit('Aborted by user request.')
if __name__ == '__main__':
try:
main()
except (GitflowError, GitCommandError) as e:
raise SystemExit('Error: %s' % e)