Repository navigation
Expand file tree
/
Copy pathgit-review
More file actions
executable file
·98 lines (90 loc) · 3.16 KB
/
Copy pathgit-review
File metadata and controls
executable file
·98 lines (90 loc) · 3.16 KB
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
#!/bin/sh -e
# Review commits added since the upstream base, oldest first, one commit per screen.
#
# git review step through commits one by one (Enter=next, p=prev, N=jump, a=rest, m=merges, q=quit)
# git review -l numbered summary of the commits, no diff
# git review -s numbered summary with per-commit diffstat
# git review -p whole stream in one pager (git log --reverse -p)
# git review -n 3 show commit #3 with patch
# git review <range> any git range, e.g. origin/main..feature, HEAD~3.., v1.2..
mode=step
range=
target=
rev_opts=--no-merges
while test $# -gt 0; do
case $1 in
-h | --help) sed -n '4,9p' "$0" | sed 's/^# \{0,1\}//'; exit 0 ;;
-l) mode=list; shift ;;
-s) mode=stat; shift ;;
-p) mode=stream; shift ;;
-n) mode=show; target=$2; shift 2 ;;
--merges) rev_opts=; shift ;;
*) range=$1; shift ;;
esac
done
test -n "$range" || range="$(git-upstream-base)..HEAD"
commits=$(git rev-list --reverse $rev_opts "$range")
test -n "$commits" || { echo "Nothing to review in $range"; exit 0; }
total=$(printf '%s\n' "$commits" | wc -l | tr -d ' ')
pager=${REVIEW_PAGER:-}
if test -z "$pager"; then
# shellcheck disable=SC2209 # the value is a pager command line, not command output
if test -t 1; then pager='less -F -X -R'; else pager=cat; fi
fi
case $pager in *less*) color=always ;; *) color=auto ;; esac
nth() { printf '%s\n' "$commits" | sed -n "$1p"; }
view() {
{
printf '=== commit %d/%d ===\n' "$1" "$total"
git show --format=fuller --stat -p --color=$color "$(nth "$1")"
} | $pager
}
if test "$mode" = stat; then flags='--stat'; else flags=''; fi
case $mode in
list | stat)
i=1
for sha in $commits; do
printf '%2d. ' "$i"
git show --no-patch --format='%h %s' $flags "$sha"
i=$((i + 1))
done
exit 0
;;
stream) git log --reverse -p $rev_opts --color=$color "$range" | $pager; exit 0 ;;
show)
if test -z "$target"; then echo "-n needs a commit number"; exit 1; fi
if test "$target" -lt 1 || test "$target" -gt "$total"; then
echo "commit #$target is outside 1..$total"; exit 1
fi
view "$target"
exit 0
;;
esac
i=1
while :; do
view "$i"
if test "$i" -ge "$total"; then break; fi
printf 'commit %d/%d Enter=next p=prev N=jump a=rest m=merges q=quit: ' "$i" "$total"
ans=
if test -t 0; then IFS= read -r ans </dev/tty || break; else break; fi
case $ans in
q | Q) break ;;
a | A)
j=$i
while test "$j" -le "$total"; do
view "$j"
j=$((j + 1))
done
break ;;
m | M)
if test -n "$rev_opts"; then rev_opts=; else rev_opts=--no-merges; fi
commits=$(git rev-list --reverse $rev_opts "$range")
total=$(printf '%s\n' "$commits" | wc -l | tr -d ' ')
i=1 ;;
p | P) if test "$i" -gt 1; then i=$((i - 1)); fi ;;
'') i=$((i + 1)) ;;
*[0-9])
if test "$ans" -ge 1 && test "$ans" -le "$total"; then i=$ans; fi
;;
esac
done