blob: 315da1af5dffcf4270ae107338243b5671b172c9 [file] [log] [blame]
John Koleszara3ff6252012-08-08 09:28:01 -07001#!/bin/sh
2self="$0"
3dirname_self=$(dirname "$self")
4
5usage() {
6 cat <<EOF >&2
7Usage: $self [option]
8
9This script applies a whitespace transformation to the commit at HEAD. If no
10options are given, then the modified files are left in the working tree.
11
12Options:
13 -h, --help Shows this message
14 -n, --dry-run Shows a diff of the changes to be made.
15 --amend Squashes the changes into the commit at HEAD
16 This option will also reformat the commit message.
17 --commit Creates a new commit containing only the whitespace changes
18 --msg-only Reformat the commit message only, ignore the patch itself.
19
20EOF
21 rm -f ${CLEAN_FILES}
22 exit 1
23}
24
25
26log() {
27 echo "${self##*/}: $@" >&2
28}
29
30
Yaowu Xuf883b422016-08-30 14:01:10 -070031aom_style() {
John Koleszar84b1fbf2012-08-08 09:34:44 -070032 for f; do
33 case "$f" in
34 *.h|*.c|*.cc)
James Zern7dcd4992016-08-04 20:17:09 -070035 clang-format -i --style=file "$f"
John Koleszar84b1fbf2012-08-08 09:34:44 -070036 ;;
37 esac
38 done
John Koleszara3ff6252012-08-08 09:28:01 -070039}
40
41
42apply() {
43 [ $INTERSECT_RESULT -ne 0 ] && patch -p1 < "$1"
44}
45
46
47commit() {
48 LAST_CHANGEID=$(git show | awk '/Change-Id:/{print $2}')
49 if [ -z "$LAST_CHANGEID" ]; then
50 log "HEAD doesn't have a Change-Id, unable to generate a new commit"
51 exit 1
52 fi
53
54 # Build a deterministic Change-Id from the parent's
55 NEW_CHANGEID=${LAST_CHANGEID}-styled
56 NEW_CHANGEID=I$(echo $NEW_CHANGEID | git hash-object --stdin)
57
58 # Commit, preserving authorship from the parent commit.
59 git commit -a -C HEAD > /dev/null
60 git commit --amend -F- << EOF
61Cosmetic: Fix whitespace in change ${LAST_CHANGEID:0:9}
62
63Change-Id: ${NEW_CHANGEID}
64EOF
65}
66
67
68show_commit_msg_diff() {
69 if [ $DIFF_MSG_RESULT -ne 0 ]; then
70 log "Modified commit message:"
71 diff -u "$ORIG_COMMIT_MSG" "$NEW_COMMIT_MSG" | tail -n +3
72 fi
73}
74
75
76amend() {
77 show_commit_msg_diff
78 if [ $DIFF_MSG_RESULT -ne 0 ] || [ $INTERSECT_RESULT -ne 0 ]; then
79 git commit -a --amend -F "$NEW_COMMIT_MSG"
80 fi
81}
82
83
84diff_msg() {
85 git log -1 --format=%B > "$ORIG_COMMIT_MSG"
86 "${dirname_self}"/wrap-commit-msg.py \
87 < "$ORIG_COMMIT_MSG" > "$NEW_COMMIT_MSG"
88 cmp -s "$ORIG_COMMIT_MSG" "$NEW_COMMIT_MSG"
89 DIFF_MSG_RESULT=$?
90}
91
92
93# Temporary files
94ORIG_DIFF=orig.diff.$$
95MODIFIED_DIFF=modified.diff.$$
96FINAL_DIFF=final.diff.$$
97ORIG_COMMIT_MSG=orig.commit-msg.$$
98NEW_COMMIT_MSG=new.commit-msg.$$
99CLEAN_FILES="${ORIG_DIFF} ${MODIFIED_DIFF} ${FINAL_DIFF}"
100CLEAN_FILES="${CLEAN_FILES} ${ORIG_COMMIT_MSG} ${NEW_COMMIT_MSG}"
101
102# Preconditions
103[ $# -lt 2 ] || usage
104
James Zern7dcd4992016-08-04 20:17:09 -0700105if ! clang-format -version >/dev/null 2>&1; then
106 log "clang-format not found"
John Koleszara3ff6252012-08-08 09:28:01 -0700107 exit 1
108fi
109
110if ! git diff --quiet HEAD; then
111 log "Working tree is dirty, commit your changes first"
112 exit 1
113fi
114
115# Need to be in the root
116cd "$(git rev-parse --show-toplevel)"
117
118# Collect the original diff
119git show > "${ORIG_DIFF}"
120
121# Apply the style guide on new and modified files and collect its diff
John Koleszar84b1fbf2012-08-08 09:34:44 -0700122for f in $(git diff HEAD^ --name-only -M90 --diff-filter=AM); do
John Koleszara3ff6252012-08-08 09:28:01 -0700123 case "$f" in
124 third_party/*) continue;;
John Koleszara3ff6252012-08-08 09:28:01 -0700125 esac
Yaowu Xuf883b422016-08-30 14:01:10 -0700126 aom_style "$f"
John Koleszara3ff6252012-08-08 09:28:01 -0700127done
128git diff --no-color --no-ext-diff > "${MODIFIED_DIFF}"
129
130# Intersect the two diffs
131"${dirname_self}"/intersect-diffs.py \
132 "${ORIG_DIFF}" "${MODIFIED_DIFF}" > "${FINAL_DIFF}"
133INTERSECT_RESULT=$?
134git reset --hard >/dev/null
135
136# Fixup the commit message
137diff_msg
138
139# Handle options
140if [ -n "$1" ]; then
141 case "$1" in
142 -h|--help) usage;;
143 -n|--dry-run) cat "${FINAL_DIFF}"; show_commit_msg_diff;;
144 --commit) apply "${FINAL_DIFF}"; commit;;
145 --amend) apply "${FINAL_DIFF}"; amend;;
146 --msg-only) amend;;
147 *) usage;;
148 esac
149else
150 apply "${FINAL_DIFF}"
151 if ! git diff --quiet; then
152 log "Formatting changes applied, verify and commit."
153 log "See also: http://www.webmproject.org/code/contribute/conventions/"
154 git diff --stat
155 fi
156fi
157
158rm -f ${CLEAN_FILES}