-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathrebase-all.py
executable file
·48 lines (38 loc) · 1.46 KB
/
rebase-all.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
42
43
44
45
46
47
48
#!/usr/bin/env python3
"""
Rebase all local branches in a repository.
"""
from subprocess import PIPE, run
def main():
# Check for clean local working tree
status_res = run(["git", "status", "--short"], stdout=PIPE)
entries = [e.strip() for e in status_res.stdout.decode("utf-8").split("\n") if e]
for entry in entries:
[status, path] = entry.split(" ")
if status != "??":
print("Working directory is not clean")
# List unmerged Git branches
branch_list_res = run(["git", "branch", "--no-merged"], stdout=PIPE)
if branch_list_res.returncode:
raise "Listing remote branches failed"
branch_list = [
b.decode("utf-8").strip() for b in branch_list_res.stdout.strip().split(b"\n")
]
# Rebase each branch in turn
onto_branch = "master"
for branch in branch_list:
co_result = run(["git", "checkout", branch], stdout=PIPE)
if co_result.returncode:
print("{} - Checkout failed".format(branch))
return
rebase_result = run(["git", "rebase", onto_branch], stdout=PIPE)
if rebase_result.returncode:
abort_result = run(["git", "rebase", "--abort"])
if abort_result.returncode:
print("Rebasing {} failed".format(abort_result))
return
print("{} - Auto-rebase failed".format(branch))
else:
print("{} - Rebased".format(branch))
if __name__ == "__main__":
main()