-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdiff-git-commits
More file actions
executable file
·115 lines (96 loc) · 2.09 KB
/
diff-git-commits
File metadata and controls
executable file
·115 lines (96 loc) · 2.09 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/bin/bash -eu
#
# Diff two comits or patches and colorize the results
#
function out()
{
local rc=$?
trap - INT TERM EXIT HUP
rm -f "${TMP1}" "${TMP2}"
if [ "${rc}" -ne 0 ] ; then
echo "Script failed" >&2
fi
exit "${rc}"
}
function usage()
{
cat <<EOF
Usage: diff-git-commits [-h] [--no-colors] [-w NUM] COMMIT [COMMIT]
Show the differences between two commits side-by-side and color-highlight the
differences. If only one commit is provided, the script tries to parse the
first commit message for the hash of the second commit.
Positional arguments:
COMMIT A git commit hash or a patch file.
Optional arguments:
-h, --help Show this help text and exit.
--no-colors
-w, --width NUM Output at most NUM print columns. If not specified, uses the
width of the current terminal (tput cols).
EOF
}
diff=colordiff
width=$(tput cols)
commit1=
commit2=
while [ $# -gt 0 ] ; do
case "${1}" in
-h|--help)
usage
exit
;;
--no-colors)
diff=diff
;;
-w|--width)
width=${2}
shift
;;
*)
if [ -z "${commit1}" ] ; then
commit1=${1}
elif [ -z "${commit2}" ] ; then
commit2=${1}
else
echo "Invalid argument: ${1}" >&2
exit 1
fi
;;
esac
shift
done
if [ -z "${commit1}" ] ; then
usage
exit 1
fi
TMP1=
TMP2=
trap out EXIT INT TERM HUP
if [ -e "${commit1}" ] ; then
patch1=${commit1}
else
TMP1=$(mktemp)
git show "${commit1}" > "${TMP1}"
patch1=${TMP1}
fi
if [ -z "${commit2}" ] ; then
show_opts=()
commit2=$(grep -P '(cherry picked|(back|forward )ported) from commit [0-9a-f]{40}' \
"${patch1}" | tail -1 | grep -oP '[0-9a-f]{40}' || true)
fi
if [ -z "${commit2}" ] ; then
show_opts=("-R")
commit2=$(grep -P 'This reverts commit [0-9a-f]{40}' \
"${patch1}" | grep -oP '[0-9a-f]{40}' || true)
fi
if [ -z "${commit2}" ] ; then
echo "Unable to find second commit hash in first commit message" >&2
exit 1
fi
if [ -e "${commit2}" ] ; then
patch2=${commit2}
else
TMP2=$(mktemp)
git show "${show_opts[@]}" "${commit2}" > "${TMP2}"
patch2=${TMP2}
fi
"${diff}" -y -W "${width}" "${patch1}" "${patch2}" || true