7
7
8
8
from __future__ import unicode_literals
9
9
10
+ try :
11
+ from StringIO import StringIO
12
+ except ImportError :
13
+ from io import StringIO
14
+
10
15
from clint .textui import colored
11
16
12
17
from gitless import core
16
21
17
22
def parser (subparsers , _ ):
18
23
"""Adds the branch parser to the given subparsers object."""
24
+ desc = 'list, create, edit or delete branches'
19
25
branch_parser = subparsers .add_parser (
20
- 'branch' , help = 'list, create, edit or delete branches' )
26
+ 'branch' , help = desc , description = desc . capitalize () )
21
27
branch_parser .add_argument (
22
28
'-r' , '--remote' ,
23
29
help = 'list remote branches in addition to local branches' ,
24
30
action = 'store_true' )
25
31
26
32
branch_parser .add_argument (
27
- '-c' , '--create' , nargs = '+' , help = 'create branch(es)' , dest = 'create_b' )
33
+ '-c' , '--create' , nargs = '+' , help = 'create branch(es)' , dest = 'create_b' ,
34
+ metavar = 'branch' )
28
35
branch_parser .add_argument (
29
36
'-dp' , '--divergent-point' ,
30
37
help = 'the commit from where to \' branch out\' (only relevant if a new '
31
38
'branch is created; defaults to HEAD)' , default = 'HEAD' ,
32
39
dest = 'dp' )
33
40
branch_parser .add_argument (
34
- '-d' , '--delete' , nargs = '+' , help = 'delete branch(es)' , dest = 'delete_b' )
41
+ '-d' , '--delete' , nargs = '+' , help = 'delete branch(es)' , dest = 'delete_b' ,
42
+ metavar = 'branch' )
35
43
44
+ branch_parser .add_argument (
45
+ '-sh' , '--set-head' , help = 'set the head of the current branch' ,
46
+ dest = 'new_head' , metavar = 'commit_id' )
36
47
branch_parser .add_argument (
37
48
'-su' , '--set-upstream' ,
38
49
help = 'set the upstream branch of the current branch' ,
39
- dest = 'upstream_b' )
50
+ dest = 'upstream_b' , metavar = 'branch' )
40
51
branch_parser .add_argument (
41
52
'-uu' , '--unset-upstream' ,
42
53
help = 'unset the upstream branch of the current branch' ,
43
54
action = 'store_true' )
55
+
56
+ branch_parser .add_argument (
57
+ '-v' , '--verbose' , help = 'be verbose, will output the head of each branch' ,
58
+ action = 'store_true' )
44
59
branch_parser .set_defaults (func = main )
45
60
46
61
@@ -54,35 +69,38 @@ def main(args, repo):
54
69
ret = _do_set_upstream (args .upstream_b , repo )
55
70
elif args .unset_upstream :
56
71
ret = _do_unset_upstream (repo )
72
+ elif args .new_head :
73
+ ret = _do_set_head (args .new_head , repo )
57
74
else :
58
- _do_list (repo , args .remote )
75
+ _do_list (repo , args .remote , v = args . verbose )
59
76
60
77
return ret
61
78
62
79
63
- def _do_list (repo , list_remote ):
80
+ def _do_list (repo , list_remote , v = False ):
64
81
pprint .msg ('List of branches:' )
65
- pprint .exp ('do gl branch -c <b> to create branch b' )
66
- pprint .exp ('do gl branch -d <b> to delete branch b' )
67
- pprint .exp ('do gl switch <b> to switch to branch b' )
82
+ pprint .exp ('do gl branch -c b to create branch b' )
83
+ pprint .exp ('do gl branch -d b to delete branch b' )
84
+ pprint .exp ('do gl switch b to switch to branch b' )
68
85
pprint .exp ('* = current branch' )
69
86
pprint .blank ()
70
87
88
+
71
89
for b in (repo .lookup_branch (n ) for n in repo .listall_branches ()):
72
90
current_str = '*' if b .is_current else ' '
73
- upstream_str = ''
74
- try :
75
- upstream_str = '(upstream is {0})' .format (b .upstream_name )
76
- except KeyError :
77
- pass
91
+ upstream_str = '(upstream is {0})' .format (b .upstream ) if b .upstream else ''
78
92
color = colored .green if b .is_current else colored .yellow
79
93
pprint .item (
80
94
'{0} {1} {2}' .format (current_str , color (b .branch_name ), upstream_str ))
95
+ if v :
96
+ pprint .item (' ➜ head is {0}' .format (_ci_str (b .head )))
81
97
82
98
if list_remote :
83
99
for r in repo .remotes :
84
100
for b in (r .lookup_branch (n ) for n in r .listall_branches ()):
85
101
pprint .item (' {0}' .format (colored .yellow (b .branch_name )))
102
+ if v :
103
+ pprint .item (' ➜ head is {0}' .format (_ci_str (b .head )))
86
104
87
105
88
106
def _do_create (create_b , dp , repo ):
@@ -91,7 +109,7 @@ def _do_create(create_b, dp, repo):
91
109
try :
92
110
target = repo .revparse_single (dp )
93
111
except KeyError :
94
- raise ValueError ('Invalid divergent point " {0}" ' .format (dp ))
112
+ raise ValueError ('Invalid divergent point {0}' .format (dp ))
95
113
96
114
for b_name in create_b :
97
115
r = repo
@@ -142,7 +160,7 @@ def _do_delete(delete_b, repo):
142
160
except core .BranchIsCurrentError as e :
143
161
pprint .err (e )
144
162
pprint .err_exp (
145
- 'do gl branch <b> to create or switch to another branch b and then '
163
+ 'do gl branch b to create or switch to another branch b and then '
146
164
'gl branch -d {0} to remove branch {0}' .format (b ))
147
165
errors_found = True
148
166
@@ -161,3 +179,22 @@ def _do_unset_upstream(repo):
161
179
curr_b .upstream = None
162
180
pprint .ok ('Upstream unset for current branch {0}' .format (curr_b ))
163
181
return True
182
+
183
+
184
+ def _do_set_head (commit_id , repo ):
185
+ try :
186
+ commit = repo .revparse_single (commit_id )
187
+ except KeyError :
188
+ raise ValueError ('Invalid head {0}' .format (commit_id ))
189
+
190
+ curr_b = repo .current_branch
191
+ curr_b .head = commit .id
192
+ pprint .ok (
193
+ 'Head of current branch {0} is now {1}' .format (curr_b , _ci_str (commit )))
194
+ return True
195
+
196
+
197
+ def _ci_str (ci ):
198
+ ci_str = StringIO ()
199
+ pprint .commit (ci , compact = True , stream = ci_str .write )
200
+ return ci_str .getvalue ().strip ()
0 commit comments