-
Notifications
You must be signed in to change notification settings - Fork 0
/
git-multistatus
executable file
·53 lines (44 loc) · 1.46 KB
/
git-multistatus
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
#!/usr/bin/env python3
import subprocess
import sys
if len(sys.argv) > 1:
upstreams = sys.argv[1:]
else:
print("Must supply upstream branch as arg")
sys.exit(1)
merged = []
_distance_cache = {}
def commit_distance(trunk, leaf):
if (trunk, leaf) in _distance_cache:
return _distance_cache[(trunk, leaf)]
val = int(subprocess.check_output(["git", "rev-list", leaf, "^" + trunk, "--count"]).decode().strip())
_distance_cache[(trunk, leaf)] = val
return val
for line in subprocess.check_output(["git", "branch"]).decode().split("\n"):
tag = line.strip()
if not tag:
continue
if tag[:3] == "* (":
if tag[:8] == "* (HEAD ":
tag = "* HEAD"
else:
continue
head = False
ref = tag
if ref[:2] == "* ":
head = True
ref = tag[2:]
tag ="*" + ref
# find the closest upstream source branch for our ref
if len(upstreams) > 1:
upstreams.sort(key=lambda x: (commit_distance(x, ref), commit_distance(ref, x)))
upstream = upstreams[0]
behind = commit_distance(ref, upstream)
tag = "%s(%d)" % (tag, behind)
if subprocess.call(["git", "merge-base", "--is-ancestor", ref, upstream]) == 0:
merged.append(tag)
else:
print("==>", tag)
print(subprocess.check_output(["git", "log", "%s..%s" % (upstream, ref), "--reverse", "--format=tformat: %h %s"]).decode())
if merged:
print("Merged:", " ".join(merged))