FTFY: an automated style corrector
This is a utility for applying a limited amount of style correction on
a change-by-change basis. Rather than a big-bang reformatting, this
tool attempts to only correct the style in diff hunks that you touch.
This should make the cosmetic changes small enough that we can mix them
with functional changes without destroying the diffs, and there's an
escape hatch for separating the reformatting to a second commit for
purists and cases where it hurts readability.
At this time, the script requires a clean working tree, so run it after
you've commited your changes. Run without arguments, the style
corrections will be applied and left unstaged in your working copy. It
also supports the --amend option, which will automatically amend your
HEAD with the corrected style, and --commit, which will create a new
change dependent on your HEAD that contains only the whitespace changes.
There are a number of ways this could be applied in an automated manner
if this proves to be useful, either on a project-wide or per-user
basis. This doesn't buy anything in terms of real code quality, the
intent here would be to keep formatting nits out of review comments in
favor of more meaningful ones and help people whose habitual style
doesn't match the baseline.
Requires astyle[1] 1.24 or newer.
[1]: http://astyle.sourceforge.net/
Change-Id: I2fb3434de8479655e9811f094029bb90e5d757e1
2012-03-28 01:28:41 +02:00
|
|
|
#!/bin/sh
|
|
|
|
self="$0"
|
2012-03-29 01:41:16 +02:00
|
|
|
dirname_self=$(dirname "$self")
|
FTFY: an automated style corrector
This is a utility for applying a limited amount of style correction on
a change-by-change basis. Rather than a big-bang reformatting, this
tool attempts to only correct the style in diff hunks that you touch.
This should make the cosmetic changes small enough that we can mix them
with functional changes without destroying the diffs, and there's an
escape hatch for separating the reformatting to a second commit for
purists and cases where it hurts readability.
At this time, the script requires a clean working tree, so run it after
you've commited your changes. Run without arguments, the style
corrections will be applied and left unstaged in your working copy. It
also supports the --amend option, which will automatically amend your
HEAD with the corrected style, and --commit, which will create a new
change dependent on your HEAD that contains only the whitespace changes.
There are a number of ways this could be applied in an automated manner
if this proves to be useful, either on a project-wide or per-user
basis. This doesn't buy anything in terms of real code quality, the
intent here would be to keep formatting nits out of review comments in
favor of more meaningful ones and help people whose habitual style
doesn't match the baseline.
Requires astyle[1] 1.24 or newer.
[1]: http://astyle.sourceforge.net/
Change-Id: I2fb3434de8479655e9811f094029bb90e5d757e1
2012-03-28 01:28:41 +02:00
|
|
|
|
|
|
|
usage() {
|
|
|
|
cat <<EOF >&2
|
|
|
|
Usage: $self [option]
|
|
|
|
|
|
|
|
This script applies a whitespace transformation to the commit at HEAD. If no
|
|
|
|
options are given, then the modified files are left in the working tree.
|
|
|
|
|
|
|
|
Options:
|
2012-03-29 01:41:16 +02:00
|
|
|
-h, --help Shows this message
|
FTFY: an automated style corrector
This is a utility for applying a limited amount of style correction on
a change-by-change basis. Rather than a big-bang reformatting, this
tool attempts to only correct the style in diff hunks that you touch.
This should make the cosmetic changes small enough that we can mix them
with functional changes without destroying the diffs, and there's an
escape hatch for separating the reformatting to a second commit for
purists and cases where it hurts readability.
At this time, the script requires a clean working tree, so run it after
you've commited your changes. Run without arguments, the style
corrections will be applied and left unstaged in your working copy. It
also supports the --amend option, which will automatically amend your
HEAD with the corrected style, and --commit, which will create a new
change dependent on your HEAD that contains only the whitespace changes.
There are a number of ways this could be applied in an automated manner
if this proves to be useful, either on a project-wide or per-user
basis. This doesn't buy anything in terms of real code quality, the
intent here would be to keep formatting nits out of review comments in
favor of more meaningful ones and help people whose habitual style
doesn't match the baseline.
Requires astyle[1] 1.24 or newer.
[1]: http://astyle.sourceforge.net/
Change-Id: I2fb3434de8479655e9811f094029bb90e5d757e1
2012-03-28 01:28:41 +02:00
|
|
|
-n, --dry-run Shows a diff of the changes to be made.
|
|
|
|
--amend Squashes the changes into the commit at HEAD
|
2012-03-29 01:41:16 +02:00
|
|
|
This option will also reformat the commit message.
|
FTFY: an automated style corrector
This is a utility for applying a limited amount of style correction on
a change-by-change basis. Rather than a big-bang reformatting, this
tool attempts to only correct the style in diff hunks that you touch.
This should make the cosmetic changes small enough that we can mix them
with functional changes without destroying the diffs, and there's an
escape hatch for separating the reformatting to a second commit for
purists and cases where it hurts readability.
At this time, the script requires a clean working tree, so run it after
you've commited your changes. Run without arguments, the style
corrections will be applied and left unstaged in your working copy. It
also supports the --amend option, which will automatically amend your
HEAD with the corrected style, and --commit, which will create a new
change dependent on your HEAD that contains only the whitespace changes.
There are a number of ways this could be applied in an automated manner
if this proves to be useful, either on a project-wide or per-user
basis. This doesn't buy anything in terms of real code quality, the
intent here would be to keep formatting nits out of review comments in
favor of more meaningful ones and help people whose habitual style
doesn't match the baseline.
Requires astyle[1] 1.24 or newer.
[1]: http://astyle.sourceforge.net/
Change-Id: I2fb3434de8479655e9811f094029bb90e5d757e1
2012-03-28 01:28:41 +02:00
|
|
|
--commit Creates a new commit containing only the whitespace changes
|
2012-03-29 01:41:16 +02:00
|
|
|
--msg-only Reformat the commit message only, ignore the patch itself.
|
|
|
|
|
FTFY: an automated style corrector
This is a utility for applying a limited amount of style correction on
a change-by-change basis. Rather than a big-bang reformatting, this
tool attempts to only correct the style in diff hunks that you touch.
This should make the cosmetic changes small enough that we can mix them
with functional changes without destroying the diffs, and there's an
escape hatch for separating the reformatting to a second commit for
purists and cases where it hurts readability.
At this time, the script requires a clean working tree, so run it after
you've commited your changes. Run without arguments, the style
corrections will be applied and left unstaged in your working copy. It
also supports the --amend option, which will automatically amend your
HEAD with the corrected style, and --commit, which will create a new
change dependent on your HEAD that contains only the whitespace changes.
There are a number of ways this could be applied in an automated manner
if this proves to be useful, either on a project-wide or per-user
basis. This doesn't buy anything in terms of real code quality, the
intent here would be to keep formatting nits out of review comments in
favor of more meaningful ones and help people whose habitual style
doesn't match the baseline.
Requires astyle[1] 1.24 or newer.
[1]: http://astyle.sourceforge.net/
Change-Id: I2fb3434de8479655e9811f094029bb90e5d757e1
2012-03-28 01:28:41 +02:00
|
|
|
EOF
|
|
|
|
rm -f ${CLEAN_FILES}
|
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
log() {
|
|
|
|
echo "${self##*/}: $@" >&2
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
vpx_style() {
|
|
|
|
astyle --style=bsd --min-conditional-indent=0 --break-blocks \
|
|
|
|
--pad-oper --pad-header --unpad-paren \
|
|
|
|
--align-pointer=name \
|
|
|
|
--indent-preprocessor --convert-tabs --indent-labels \
|
|
|
|
--suffix=none --quiet "$@"
|
|
|
|
sed -i 's/[[:space:]]\{1,\},/,/g' "$@"
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
apply() {
|
2012-03-29 01:41:16 +02:00
|
|
|
[ $INTERSECT_RESULT -ne 0 ] && patch -p1 < "$1"
|
FTFY: an automated style corrector
This is a utility for applying a limited amount of style correction on
a change-by-change basis. Rather than a big-bang reformatting, this
tool attempts to only correct the style in diff hunks that you touch.
This should make the cosmetic changes small enough that we can mix them
with functional changes without destroying the diffs, and there's an
escape hatch for separating the reformatting to a second commit for
purists and cases where it hurts readability.
At this time, the script requires a clean working tree, so run it after
you've commited your changes. Run without arguments, the style
corrections will be applied and left unstaged in your working copy. It
also supports the --amend option, which will automatically amend your
HEAD with the corrected style, and --commit, which will create a new
change dependent on your HEAD that contains only the whitespace changes.
There are a number of ways this could be applied in an automated manner
if this proves to be useful, either on a project-wide or per-user
basis. This doesn't buy anything in terms of real code quality, the
intent here would be to keep formatting nits out of review comments in
favor of more meaningful ones and help people whose habitual style
doesn't match the baseline.
Requires astyle[1] 1.24 or newer.
[1]: http://astyle.sourceforge.net/
Change-Id: I2fb3434de8479655e9811f094029bb90e5d757e1
2012-03-28 01:28:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
commit() {
|
|
|
|
LAST_CHANGEID=$(git show | awk '/Change-Id:/{print $2}')
|
|
|
|
if [ -z "$LAST_CHANGEID" ]; then
|
|
|
|
log "HEAD doesn't have a Change-Id, unable to generate a new commit"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Build a deterministic Change-Id from the parent's
|
|
|
|
NEW_CHANGEID=${LAST_CHANGEID}-styled
|
|
|
|
NEW_CHANGEID=I$(echo $NEW_CHANGEID | git hash-object --stdin)
|
|
|
|
|
|
|
|
# Commit, preserving authorship from the parent commit.
|
|
|
|
git commit -a -C HEAD > /dev/null
|
|
|
|
git commit --amend -F- << EOF
|
|
|
|
Cosmetic: Fix whitespace in change ${LAST_CHANGEID:0:9}
|
|
|
|
|
|
|
|
Change-Id: ${NEW_CHANGEID}
|
|
|
|
EOF
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-03-29 01:41:16 +02:00
|
|
|
show_commit_msg_diff() {
|
|
|
|
if [ $DIFF_MSG_RESULT -ne 0 ]; then
|
|
|
|
log "Modified commit message:"
|
|
|
|
diff -u "$ORIG_COMMIT_MSG" "$NEW_COMMIT_MSG" | tail -n +3
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
|
FTFY: an automated style corrector
This is a utility for applying a limited amount of style correction on
a change-by-change basis. Rather than a big-bang reformatting, this
tool attempts to only correct the style in diff hunks that you touch.
This should make the cosmetic changes small enough that we can mix them
with functional changes without destroying the diffs, and there's an
escape hatch for separating the reformatting to a second commit for
purists and cases where it hurts readability.
At this time, the script requires a clean working tree, so run it after
you've commited your changes. Run without arguments, the style
corrections will be applied and left unstaged in your working copy. It
also supports the --amend option, which will automatically amend your
HEAD with the corrected style, and --commit, which will create a new
change dependent on your HEAD that contains only the whitespace changes.
There are a number of ways this could be applied in an automated manner
if this proves to be useful, either on a project-wide or per-user
basis. This doesn't buy anything in terms of real code quality, the
intent here would be to keep formatting nits out of review comments in
favor of more meaningful ones and help people whose habitual style
doesn't match the baseline.
Requires astyle[1] 1.24 or newer.
[1]: http://astyle.sourceforge.net/
Change-Id: I2fb3434de8479655e9811f094029bb90e5d757e1
2012-03-28 01:28:41 +02:00
|
|
|
amend() {
|
2012-03-29 01:41:16 +02:00
|
|
|
show_commit_msg_diff
|
|
|
|
if [ $DIFF_MSG_RESULT -ne 0 ] || [ $INTERSECT_RESULT -ne 0 ]; then
|
|
|
|
git commit -a --amend -F "$NEW_COMMIT_MSG"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
diff_msg() {
|
|
|
|
git log -1 --format=%B > "$ORIG_COMMIT_MSG"
|
|
|
|
"${dirname_self}"/wrap-commit-msg.py \
|
|
|
|
< "$ORIG_COMMIT_MSG" > "$NEW_COMMIT_MSG"
|
|
|
|
cmp -s "$ORIG_COMMIT_MSG" "$NEW_COMMIT_MSG"
|
|
|
|
DIFF_MSG_RESULT=$?
|
FTFY: an automated style corrector
This is a utility for applying a limited amount of style correction on
a change-by-change basis. Rather than a big-bang reformatting, this
tool attempts to only correct the style in diff hunks that you touch.
This should make the cosmetic changes small enough that we can mix them
with functional changes without destroying the diffs, and there's an
escape hatch for separating the reformatting to a second commit for
purists and cases where it hurts readability.
At this time, the script requires a clean working tree, so run it after
you've commited your changes. Run without arguments, the style
corrections will be applied and left unstaged in your working copy. It
also supports the --amend option, which will automatically amend your
HEAD with the corrected style, and --commit, which will create a new
change dependent on your HEAD that contains only the whitespace changes.
There are a number of ways this could be applied in an automated manner
if this proves to be useful, either on a project-wide or per-user
basis. This doesn't buy anything in terms of real code quality, the
intent here would be to keep formatting nits out of review comments in
favor of more meaningful ones and help people whose habitual style
doesn't match the baseline.
Requires astyle[1] 1.24 or newer.
[1]: http://astyle.sourceforge.net/
Change-Id: I2fb3434de8479655e9811f094029bb90e5d757e1
2012-03-28 01:28:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
# Temporary files
|
|
|
|
ORIG_DIFF=orig.diff.$$
|
|
|
|
MODIFIED_DIFF=modified.diff.$$
|
|
|
|
FINAL_DIFF=final.diff.$$
|
2012-03-29 01:41:16 +02:00
|
|
|
ORIG_COMMIT_MSG=orig.commit-msg.$$
|
|
|
|
NEW_COMMIT_MSG=new.commit-msg.$$
|
FTFY: an automated style corrector
This is a utility for applying a limited amount of style correction on
a change-by-change basis. Rather than a big-bang reformatting, this
tool attempts to only correct the style in diff hunks that you touch.
This should make the cosmetic changes small enough that we can mix them
with functional changes without destroying the diffs, and there's an
escape hatch for separating the reformatting to a second commit for
purists and cases where it hurts readability.
At this time, the script requires a clean working tree, so run it after
you've commited your changes. Run without arguments, the style
corrections will be applied and left unstaged in your working copy. It
also supports the --amend option, which will automatically amend your
HEAD with the corrected style, and --commit, which will create a new
change dependent on your HEAD that contains only the whitespace changes.
There are a number of ways this could be applied in an automated manner
if this proves to be useful, either on a project-wide or per-user
basis. This doesn't buy anything in terms of real code quality, the
intent here would be to keep formatting nits out of review comments in
favor of more meaningful ones and help people whose habitual style
doesn't match the baseline.
Requires astyle[1] 1.24 or newer.
[1]: http://astyle.sourceforge.net/
Change-Id: I2fb3434de8479655e9811f094029bb90e5d757e1
2012-03-28 01:28:41 +02:00
|
|
|
CLEAN_FILES="${ORIG_DIFF} ${MODIFIED_DIFF} ${FINAL_DIFF}"
|
2012-03-29 01:41:16 +02:00
|
|
|
CLEAN_FILES="${CLEAN_FILES} ${ORIG_COMMIT_MSG} ${NEW_COMMIT_MSG}"
|
FTFY: an automated style corrector
This is a utility for applying a limited amount of style correction on
a change-by-change basis. Rather than a big-bang reformatting, this
tool attempts to only correct the style in diff hunks that you touch.
This should make the cosmetic changes small enough that we can mix them
with functional changes without destroying the diffs, and there's an
escape hatch for separating the reformatting to a second commit for
purists and cases where it hurts readability.
At this time, the script requires a clean working tree, so run it after
you've commited your changes. Run without arguments, the style
corrections will be applied and left unstaged in your working copy. It
also supports the --amend option, which will automatically amend your
HEAD with the corrected style, and --commit, which will create a new
change dependent on your HEAD that contains only the whitespace changes.
There are a number of ways this could be applied in an automated manner
if this proves to be useful, either on a project-wide or per-user
basis. This doesn't buy anything in terms of real code quality, the
intent here would be to keep formatting nits out of review comments in
favor of more meaningful ones and help people whose habitual style
doesn't match the baseline.
Requires astyle[1] 1.24 or newer.
[1]: http://astyle.sourceforge.net/
Change-Id: I2fb3434de8479655e9811f094029bb90e5d757e1
2012-03-28 01:28:41 +02:00
|
|
|
|
|
|
|
# Preconditions
|
|
|
|
[ $# -lt 2 ] || usage
|
|
|
|
|
|
|
|
if ! git diff --quiet HEAD; then
|
|
|
|
log "Working tree is dirty, commit your changes first"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Need to be in the root
|
|
|
|
cd "$(git rev-parse --show-toplevel)"
|
|
|
|
|
|
|
|
# Collect the original diff
|
|
|
|
git show > "${ORIG_DIFF}"
|
|
|
|
|
2012-04-04 00:59:37 +02:00
|
|
|
# Apply the style guide on new and modified files and collect its diff
|
|
|
|
for f in $(git diff HEAD^ --name-only -M90 --diff-filter=AM
|
|
|
|
| grep '\.[ch]$'); do
|
FTFY: an automated style corrector
This is a utility for applying a limited amount of style correction on
a change-by-change basis. Rather than a big-bang reformatting, this
tool attempts to only correct the style in diff hunks that you touch.
This should make the cosmetic changes small enough that we can mix them
with functional changes without destroying the diffs, and there's an
escape hatch for separating the reformatting to a second commit for
purists and cases where it hurts readability.
At this time, the script requires a clean working tree, so run it after
you've commited your changes. Run without arguments, the style
corrections will be applied and left unstaged in your working copy. It
also supports the --amend option, which will automatically amend your
HEAD with the corrected style, and --commit, which will create a new
change dependent on your HEAD that contains only the whitespace changes.
There are a number of ways this could be applied in an automated manner
if this proves to be useful, either on a project-wide or per-user
basis. This doesn't buy anything in terms of real code quality, the
intent here would be to keep formatting nits out of review comments in
favor of more meaningful ones and help people whose habitual style
doesn't match the baseline.
Requires astyle[1] 1.24 or newer.
[1]: http://astyle.sourceforge.net/
Change-Id: I2fb3434de8479655e9811f094029bb90e5d757e1
2012-03-28 01:28:41 +02:00
|
|
|
case "$f" in
|
|
|
|
third_party/*) continue;;
|
|
|
|
nestegg/*) continue;;
|
|
|
|
esac
|
|
|
|
vpx_style "$f"
|
|
|
|
done
|
|
|
|
git diff --no-color --no-ext-diff > "${MODIFIED_DIFF}"
|
|
|
|
|
|
|
|
# Intersect the two diffs
|
2012-03-29 01:41:16 +02:00
|
|
|
"${dirname_self}"/intersect-diffs.py \
|
FTFY: an automated style corrector
This is a utility for applying a limited amount of style correction on
a change-by-change basis. Rather than a big-bang reformatting, this
tool attempts to only correct the style in diff hunks that you touch.
This should make the cosmetic changes small enough that we can mix them
with functional changes without destroying the diffs, and there's an
escape hatch for separating the reformatting to a second commit for
purists and cases where it hurts readability.
At this time, the script requires a clean working tree, so run it after
you've commited your changes. Run without arguments, the style
corrections will be applied and left unstaged in your working copy. It
also supports the --amend option, which will automatically amend your
HEAD with the corrected style, and --commit, which will create a new
change dependent on your HEAD that contains only the whitespace changes.
There are a number of ways this could be applied in an automated manner
if this proves to be useful, either on a project-wide or per-user
basis. This doesn't buy anything in terms of real code quality, the
intent here would be to keep formatting nits out of review comments in
favor of more meaningful ones and help people whose habitual style
doesn't match the baseline.
Requires astyle[1] 1.24 or newer.
[1]: http://astyle.sourceforge.net/
Change-Id: I2fb3434de8479655e9811f094029bb90e5d757e1
2012-03-28 01:28:41 +02:00
|
|
|
"${ORIG_DIFF}" "${MODIFIED_DIFF}" > "${FINAL_DIFF}"
|
|
|
|
INTERSECT_RESULT=$?
|
|
|
|
git reset --hard >/dev/null
|
|
|
|
|
2012-03-29 01:41:16 +02:00
|
|
|
# Fixup the commit message
|
|
|
|
diff_msg
|
|
|
|
|
|
|
|
# Handle options
|
|
|
|
if [ -n "$1" ]; then
|
|
|
|
case "$1" in
|
|
|
|
-h|--help) usage;;
|
|
|
|
-n|--dry-run) cat "${FINAL_DIFF}"; show_commit_msg_diff;;
|
|
|
|
--commit) apply "${FINAL_DIFF}"; commit;;
|
|
|
|
--amend) apply "${FINAL_DIFF}"; amend;;
|
|
|
|
--msg-only) amend;;
|
|
|
|
*) usage;;
|
|
|
|
esac
|
|
|
|
else
|
|
|
|
apply "${FINAL_DIFF}"
|
|
|
|
if ! git diff --quiet; then
|
|
|
|
log "Formatting changes applied, verify and commit."
|
|
|
|
log "See also: http://www.webmproject.org/code/contribute/conventions/"
|
|
|
|
git diff --stat
|
FTFY: an automated style corrector
This is a utility for applying a limited amount of style correction on
a change-by-change basis. Rather than a big-bang reformatting, this
tool attempts to only correct the style in diff hunks that you touch.
This should make the cosmetic changes small enough that we can mix them
with functional changes without destroying the diffs, and there's an
escape hatch for separating the reformatting to a second commit for
purists and cases where it hurts readability.
At this time, the script requires a clean working tree, so run it after
you've commited your changes. Run without arguments, the style
corrections will be applied and left unstaged in your working copy. It
also supports the --amend option, which will automatically amend your
HEAD with the corrected style, and --commit, which will create a new
change dependent on your HEAD that contains only the whitespace changes.
There are a number of ways this could be applied in an automated manner
if this proves to be useful, either on a project-wide or per-user
basis. This doesn't buy anything in terms of real code quality, the
intent here would be to keep formatting nits out of review comments in
favor of more meaningful ones and help people whose habitual style
doesn't match the baseline.
Requires astyle[1] 1.24 or newer.
[1]: http://astyle.sourceforge.net/
Change-Id: I2fb3434de8479655e9811f094029bb90e5d757e1
2012-03-28 01:28:41 +02:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
rm -f ${CLEAN_FILES}
|