-
Notifications
You must be signed in to change notification settings - Fork 1
/
review_branch.sh
executable file
·126 lines (105 loc) · 3.66 KB
/
review_branch.sh
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/bin/bash
################################################################################
#
# Description:
# ------------
# Script to review a git branch by comparing the changes made in each commit
# of the branch, starting with the oldest commit. The base (commit from which
# the branch under review has been created) is assumed to be
# 'upstream/master'.
#
# Usage:
# ------
# (first make sure that the script is executable)
# $> /path/to/review_branch.sh branch_name
# If a graphical diff tool has been configured in gitconfig, then that will be
# launched (for each file changed in each commit), or else simple 'git diff'
# will be done as a fallback.
# The user will have to give manual confirmation before beginning the diff of
# a new commit, for clear distinction between changes made in separate
# commits.
#
################################################################################
DEFAULT_BASE="upstream/master"
# Confirm that we are in a git repository
GIT_TOP=$(git rev-parse --show-toplevel)
RC=$?
if [ $RC != "0" ]; then
exit $RC
fi
# Get the branch name
if [ $# -eq 0 ]; then
# Use the current branch if possible
BRANCH_NAME=$(git symbolic-ref --short HEAD 2>/dev/null)
if [ $? != "0" ]; then
echo "Cannot determine branch to review. Aborting."
exit 1
fi
elif [ $# -eq 1 ]; then
BRANCH_NAME=$1
# TODO: validate given branch name
else
echo "Too many arguments. Aborting."
exit 1
fi
# If a diff tool has been configured, then use that; otherwise just do a simple
# git diff.
DIFF_TOOL=$(git config diff.tool)
if [ -z $DIFF_TOOL ]; then
GD="git diff"
else
# Check if the diff tool exists
type $DIFF_TOOL &>/dev/null
if [ $? != "0" ]; then
echo "$DIFF_TOOL: no such diff tool exists."
GD="git diff"
else
GD="git difftool --no-prompt"
fi
fi
BASE=$DEFAULT_BASE
COMMITS_LIST=(`git log --oneline $BASE..$BRANCH_NAME | cut -d" " -f1`)
echo "Found ${#COMMITS_LIST[@]} commits in branch $BRANCH_NAME"
for (( i=${#COMMITS_LIST[@]}-1, n=1; i>=0; --i, ++n ))
do
COMMIT_SHORT_HASH=$(git log --format="%h" -n1 ${COMMITS_LIST[$i]})
COMMIT_MSG_SUBJECT=$(git log --format="%s" -n1 ${COMMITS_LIST[$i]})
COMMIT_MSG_BODY=$(git log --format="%b" -n1 ${COMMITS_LIST[$i]})
# echo -e " $n. \e[1m$COMMIT_HASH_AND_SUBJECT\e[0m"
echo -e " $n. (\e[34m$COMMIT_SHORT_HASH\e[0m) \e[1m$COMMIT_MSG_SUBJECT\e[0m"
if [ -n "$COMMIT_MSG_BODY" ]; then
# Split the commit message body into individual lines and print each
# separately. This is needed so that we can indent each line of the
# commit messsage body equally.
mapfile -t COMMIT_MSG_BODY_LINES <<< "$COMMIT_MSG_BODY"
for (( j=0; j<${#COMMIT_MSG_BODY_LINES[@]}; ++j ))
do
if [ -z "${COMMIT_MSG_BODY_LINES[$j]}" ]; then
# There's no need to indent blank lines
echo ""
else
echo " ${COMMIT_MSG_BODY_LINES[$j]}"
fi
done
fi
echo -ne " \e[35m... Press 'r' to review, 's' to skip\e[0m"
# Silently read a single character
read -s -n 1 C
# Treat 'Enter' as 'r'
if [ -z $C ]; then
C='r'
fi
while [ $C != 'r' ] && [ $C != 'R' ] && [ $C != 's' ] && [ $C != 'S' ];
do
read -s -n 1 C
if [ -z $C ]; then
C='r'
fi
done
if [ $C == 'r' ] || [ $C == 'R' ]; then
$GD ${COMMITS_LIST[$i]}~1 ${COMMITS_LIST[$i]}
fi
# Remove the "Press 'r' to review..." line
echo -e "\e[1A" # Go up one line
echo -en "\e[0K" # Clear the line
done