]>
git.wh0rd.org - home.git/blob - .bin/git-rb-all
3 """Helper to rebase all local branches."""
7 from pathlib
import Path
13 PROG
= os
.path
.basename(__file__
)
17 """Terminal escape sequences."""
23 _BLACK
, _RED
, _GREEN
, _YELLOW
, _BLUE
, _MAGENTA
, _CYAN
, _WHITE
= range(0, 8)
26 FG_BLACK
= str(_FG
+ _BLACK
)
27 FG_RED
= str(_FG
+ _RED
)
28 FG_GREEN
= str(_FG
+ _GREEN
)
29 FG_YELLOW
= str(_FG
+ _YELLOW
)
30 FG_BLUE
= str(_FG
+ _BLUE
)
31 FG_MAGENTA
= str(_FG
+ _MAGENTA
)
32 FG_CYAN
= str(_FG
+ _CYAN
)
33 FG_WHITE
= str(_FG
+ _WHITE
)
39 _combine
= lambda *args
: Terminal
.CSI_PREFIX
+ ';'.join(args
) + Terminal
.SGR_SUFFIX
40 NORMAL
= _combine(Terminal
.NORMAL
)
41 GOOD
= _combine(Terminal
.FG_GREEN
)
42 WARN
= _combine(Terminal
.FG_YELLOW
)
43 BAD
= _combine(Terminal
.FG_RED
)
44 HILITE
= _combine(Terminal
.BOLD
, Terminal
.FG_CYAN
)
45 BRACKET
= _combine(Terminal
.BOLD
, Terminal
.FG_BLUE
)
49 """Show an error |msg| then exit."""
50 print(f
'{Color.BAD}{PROG}: error: {msg}{Color.NORMAL}', file=sys
.stderr
)
54 def git(args
, **kwargs
):
56 kwargs
.setdefault('check', True)
57 kwargs
.setdefault('capture_output', True)
58 kwargs
.setdefault('encoding', 'utf-8')
59 #print('+', 'git', *args)
60 return subprocess
.run(['git'] + args
, **kwargs
)
63 def rebase_inprogress():
64 """Determine whether a rebase is already in progress."""
65 output
= git(['rev-parse', '--git-path', 'rebase-merge']).stdout
.strip()
66 if Path(output
).exists():
69 output
= git(['rev-parse', '--git-path', 'rebase-apply']).stdout
.strip()
70 if Path(output
).exists():
76 class AppendOption(argparse
.Action
):
77 """Append the command line option (with no arguments) to dest.
79 parser.add_argument('-b', '--barg', dest='out', action='append_option')
80 options = parser.parse_args(['-b', '--barg'])
81 options.out == ['-b', '--barg']
84 def __init__(self
, option_strings
, dest
, **kwargs
):
86 raise ValueError('nargs is not supported for append_option action')
87 super().__init
__(option_strings
, dest
, nargs
=0, **kwargs
)
89 def __call__(self
, parser
, namespace
, values
, option_string
=None):
90 if getattr(namespace
, self
.dest
, None) is None:
91 setattr(namespace
, self
.dest
, [])
92 getattr(namespace
, self
.dest
).append(option_string
)
97 parser
= argparse
.ArgumentParser(description
=__doc__
)
99 '--catchup', action
='store_true',
100 help='run git-rb-catchup when rebasing')
102 '-q', '--quiet', dest
='git_options', action
=AppendOption
, default
=['-q'],
103 help='passthru to git rebase')
108 """The main entry point for scripts."""
109 parser
= get_parser()
110 opts
= parser
.parse_args(argv
)
112 # Skip if rebase is in progress.
113 if rebase_inprogress():
114 fatal('skipping due to active rebase')
116 # Switch to the top dir in case the working dir doesn't exist in every branch.
117 topdir
= git(['rev-parse', '--show-toplevel']).stdout
.strip()
121 # ||m|refs/remotes/origin/master|ahead 2, behind 203
122 # *||master|refs/remotes/origin/master|ahead 1
123 # |/usr/local/src/gnu/gdb/build/release|release||behind 10
126 ['for-each-ref', '--format=%(HEAD)|%(worktreepath)|%(refname:short)|%(upstream)|%(upstream:track,nobracket)',
127 'refs/heads/*']).stdout
.splitlines()
133 head
, worktreepath
, branch
, tracking
, ahead_behind
= line
.split('|')
134 branch_width
= max(branch_width
, len(branch
))
137 if not (worktreepath
and worktreepath
!= topdir
):
139 # If there are no branches to rebase, go silently.
143 fatal('unable to resolve current branch')
148 head
, worktreepath
, branch
, tracking
, ahead_behind
= line
.split('|')
150 # If it's a branch in another worktree, ignore it.
151 if worktreepath
and worktreepath
!= topdir
:
154 print(f
'{Color.BRACKET}### {Color.GOOD}{branch:{branch_width}}{Color.NORMAL} ',
157 print(f
'{Color.WARN}skipping{Color.NORMAL} due to missing merge branch')
160 m
= re
.match(r
'ahead ([0-9]+)', ahead_behind
)
161 ahead
= int(m
.group(1)) if m
else 0
162 m
= re
.search(r
'behind ([0-9]+)', ahead_behind
)
163 behind
= int(m
.group(1)) if m
else 0
168 git(['update-ref', f
'refs/heads/{branch}', tracking
])
169 print('fast forwarded')
173 output
= git(['status', '--porcelain']).stdout
174 is_dirty
= any(x
for x
in output
.splitlines() if '?' not in x
[0:2])
176 print(f
'{Color.BAD}unable to rebase: tree is dirty{Color.NORMAL}')
179 print(f
'rebasing [{ahead_behind}] ', end
='', flush
=True)
180 git(['checkout', '-q', branch
])
183 result
= git(['rb-catchup'], capture_output
=False, check
=False)
185 result
= git(['rebase'] + opts
.git_options
, check
=False)
186 if result
.returncode
:
187 git(['rebase', '--abort'])
188 print(f
'{Color.BAD}failed{Color.NORMAL}\n' + result
.stdout
.strip())
192 git(['checkout', '-q', curr_state
])
196 if __name__
== '__main__':
197 sys
.exit(main(sys
.argv
[1:]))