• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/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
31vpx_style() {
32  astyle --style=bsd --min-conditional-indent=0 --break-blocks \
33         --pad-oper --pad-header --unpad-paren \
34         --align-pointer=name \
35         --indent-preprocessor --convert-tabs --indent-labels \
36         --suffix=none --quiet "$@"
37  sed -i "" 's/[[:space:]]\{1,\},/,/g' "$@"
38}
39
40
41apply() {
42  [ $INTERSECT_RESULT -ne 0 ] && patch -p1 < "$1"
43}
44
45
46commit() {
47  LAST_CHANGEID=$(git show | awk '/Change-Id:/{print $2}')
48  if [ -z "$LAST_CHANGEID" ]; then
49    log "HEAD doesn't have a Change-Id, unable to generate a new commit"
50    exit 1
51  fi
52
53  # Build a deterministic Change-Id from the parent's
54  NEW_CHANGEID=${LAST_CHANGEID}-styled
55  NEW_CHANGEID=I$(echo $NEW_CHANGEID | git hash-object --stdin)
56
57  # Commit, preserving authorship from the parent commit.
58  git commit -a -C HEAD > /dev/null
59  git commit --amend -F- << EOF
60Cosmetic: Fix whitespace in change ${LAST_CHANGEID:0:9}
61
62Change-Id: ${NEW_CHANGEID}
63EOF
64}
65
66
67show_commit_msg_diff() {
68  if [ $DIFF_MSG_RESULT -ne 0 ]; then
69    log "Modified commit message:"
70    diff -u "$ORIG_COMMIT_MSG" "$NEW_COMMIT_MSG" | tail -n +3
71  fi
72}
73
74
75amend() {
76  show_commit_msg_diff
77  if [ $DIFF_MSG_RESULT -ne 0 ] || [ $INTERSECT_RESULT -ne 0 ]; then
78    git commit -a --amend -F "$NEW_COMMIT_MSG"
79  fi
80}
81
82
83diff_msg() {
84  git log -1 --format=%B > "$ORIG_COMMIT_MSG"
85  "${dirname_self}"/wrap-commit-msg.py \
86      < "$ORIG_COMMIT_MSG" > "$NEW_COMMIT_MSG"
87  cmp -s "$ORIG_COMMIT_MSG" "$NEW_COMMIT_MSG"
88  DIFF_MSG_RESULT=$?
89}
90
91
92# Temporary files
93ORIG_DIFF=orig.diff.$$
94MODIFIED_DIFF=modified.diff.$$
95FINAL_DIFF=final.diff.$$
96ORIG_COMMIT_MSG=orig.commit-msg.$$
97NEW_COMMIT_MSG=new.commit-msg.$$
98CLEAN_FILES="${ORIG_DIFF} ${MODIFIED_DIFF} ${FINAL_DIFF}"
99CLEAN_FILES="${CLEAN_FILES} ${ORIG_COMMIT_MSG} ${NEW_COMMIT_MSG}"
100
101# Preconditions
102[ $# -lt 2 ] || usage
103
104# Check that astyle supports pad-header and align-pointer=name
105if ! astyle --pad-header --align-pointer=name < /dev/null; then
106  log "Install astyle v1.24 or newer"
107  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
122for f in $(git diff HEAD^ --name-only -M90 --diff-filter=AM \
123           | grep '\.[ch]$'); do
124  case "$f" in
125    third_party/*) continue;;
126    nestegg/*) continue;;
127  esac
128  vpx_style "$f"
129done
130git diff --no-color --no-ext-diff > "${MODIFIED_DIFF}"
131
132# Intersect the two diffs
133"${dirname_self}"/intersect-diffs.py \
134    "${ORIG_DIFF}" "${MODIFIED_DIFF}" > "${FINAL_DIFF}"
135INTERSECT_RESULT=$?
136git reset --hard >/dev/null
137
138# Fixup the commit message
139diff_msg
140
141# Handle options
142if [ -n "$1" ]; then
143  case "$1" in
144    -h|--help) usage;;
145    -n|--dry-run) cat "${FINAL_DIFF}"; show_commit_msg_diff;;
146    --commit) apply "${FINAL_DIFF}"; commit;;
147    --amend) apply "${FINAL_DIFF}"; amend;;
148    --msg-only) amend;;
149    *) usage;;
150  esac
151else
152  apply "${FINAL_DIFF}"
153  if ! git diff --quiet; then
154    log "Formatting changes applied, verify and commit."
155    log "See also: http://www.webmproject.org/code/contribute/conventions/"
156    git diff --stat
157  fi
158fi
159
160rm -f ${CLEAN_FILES}
161