-
Notifications
You must be signed in to change notification settings - Fork 0
/
branch_checkout.py
executable file
·41 lines (35 loc) · 1.19 KB
/
branch_checkout.py
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
#!/usr/bin/env python3
"""
Outputs a list of branches that then give you a choice of which one to checkout by index
"""
import subprocess
import sys
import re
from devtools import sformat
def get_branch():
print()
current_branch = subprocess.run('git rev-parse --abbrev-ref HEAD', shell=True, stdout=subprocess.PIPE).stdout.decode().rstrip('\n')
output = subprocess.run('git branchmin', shell=True, stdout=subprocess.PIPE)
branch_names = {}
for i, branch_info in enumerate(output.stdout.decode().split('\n')):
if not branch_info:
continue
name, desc = branch_info.split(',', 1)
branch_names[i] = name
colour = sformat.blue if name == current_branch else sformat.yellow
print(sformat(f'{i:>5}: {name:<40}', colour), sformat(f'{desc}', sformat.green))
print()
try:
id = input('ID? ')
except EOFError:
sys.exit()
except KeyboardInterrupt:
return
try:
selected_branch = branch_names[int(id)]
except (KeyError, ValueError):
print('No branch with that ID')
else:
subprocess.run(f'git checkout {selected_branch}', shell=True)
if __name__ == '__main__':
get_branch()