-
Notifications
You must be signed in to change notification settings - Fork 0
/
git-log-divergence
executable file
·102 lines (83 loc) · 2.66 KB
/
git-log-divergence
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#!/usr/bin/env python3
# Author: Jan Larres <[email protected]>
# License: MIT/X11
import argparse
import logging
import sys
from subprocess import CalledProcessError, check_output, run
logging.basicConfig(format="%(message)s", level=logging.INFO)
log = logging.getLogger(__name__)
def main(args: argparse.Namespace) -> None:
if args.verbose:
log.setLevel(logging.DEBUG)
revs = args.rev
match len(revs):
case 0:
revs = ["@{upstream}", "HEAD"]
case 1:
revs += ["HEAD"]
cmd = ["git", "merge-base", "--octopus", "--all", *revs]
log.debug("merge-base command: '%s'", " ".join(cmd))
try:
merge_bases = check_output(cmd).decode(sys.stdout.encoding).splitlines()
except CalledProcessError as e:
die("Failed to find merge bases", args.verbose, e)
exclusions = [f"{base}^" for base in merge_bases]
log.debug("Exclusions: '%s'", " ".join(exclusions))
rev_args = [*revs, "--not", *exclusions]
extra_args = ["--topo-order", "--boundary"]
if args.simplify:
extra_args.append("--simplify-by-decoration")
if args.long:
extra_args.append(
'--pretty=tformat:"'
"%C(auto,yellow)%h"
" %C(auto,blue)%ad"
" %C(auto,green)%<(15,trunc)%an"
" %C(auto)%d"
' %Creset%s"'
)
if args.print_args:
print(" ".join(extra_args + rev_args))
sys.exit(0)
log_cmd = ["git", "log", "--oneline", "--decorate", "--graph"]
log_cmd += extra_args + rev_args
log.debug("Full command: '%s'", " ".join(log_cmd))
run(log_cmd, check=True)
def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(description="Log graph of branch diverge")
parser.add_argument("rev", nargs="*", help="Revisions to log")
parser.add_argument(
"-v",
"--verbose",
action="store_true",
default=False,
help="increase output verbosity",
)
parser.add_argument(
"-s",
"--simplify",
action="store_true",
default=False,
help="only show commits with symbolic refs",
)
parser.add_argument(
"-p",
"--print-args",
action="store_true",
default=False,
help="print revision args instead of running git log",
)
parser.add_argument(
"-l",
"--long",
action="store_true",
default=False,
help="print revisions with a longer format",
)
return parser.parse_args()
def die(msg: str, verbose: bool, e=None) -> None:
log.error(msg, exc_info=e if verbose else None)
sys.exit(1)
if __name__ == "__main__":
main(parse_args())