ftfy.sh 3.6 KB

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