|
| 1 | +#!/bin/bash |
| 2 | +set -e |
| 3 | +# If clang_format_diff.py command is not specfied, we assume we are able to |
| 4 | +# access directly without any path. |
| 5 | +if [ -z $CLANG_FORMAT_DIFF ] |
| 6 | +then |
| 7 | +CLANG_FORMAT_DIFF="clang-format-diff.py" |
| 8 | +fi |
| 9 | + |
| 10 | +# Check clang-format-diff.py |
| 11 | +if ! which $CLANG_FORMAT_DIFF &> /dev/null |
| 12 | +then |
| 13 | + echo "You didn't have clang-format-diff.py available in your computer!" |
| 14 | + echo "You can download it by running: " |
| 15 | + echo " curl https://fburl.com/clang-format-diff" |
| 16 | + exit 128 |
| 17 | +fi |
| 18 | + |
| 19 | +# Check argparse, a library that clang-format-diff.py requires. |
| 20 | +python 2>/dev/null << EOF |
| 21 | +import argparse |
| 22 | +EOF |
| 23 | + |
| 24 | +if [ "$?" != 0 ] |
| 25 | +then |
| 26 | + echo "To run clang-format-diff.py, we'll need the library "argparse" to be" |
| 27 | + echo "installed. You can try either of the follow ways to install it:" |
| 28 | + echo " 1. Manually download argparse: https://pypi.python.org/pypi/argparse" |
| 29 | + echo " 2. easy_install argparse (if you have easy_install)" |
| 30 | + echo " 3. pip install argparse (if you have pip)" |
| 31 | + exit 129 |
| 32 | +fi |
| 33 | + |
| 34 | +# TODO(kailiu) following work is not complete since we still need to figure |
| 35 | +# out how to add the modified files done pre-commit hook to git's commit index. |
| 36 | +# |
| 37 | +# Check if this script has already been added to pre-commit hook. |
| 38 | +# Will suggest user to add this script to pre-commit hook if their pre-commit |
| 39 | +# is empty. |
| 40 | +# PRE_COMMIT_SCRIPT_PATH="`git rev-parse --show-toplevel`/.git/hooks/pre-commit" |
| 41 | +# if ! ls $PRE_COMMIT_SCRIPT_PATH &> /dev/null |
| 42 | +# then |
| 43 | +# echo "Would you like to add this script to pre-commit hook, which will do " |
| 44 | +# echo -n "the format check for all the affected lines before you check in (y/n):" |
| 45 | +# read add_to_hook |
| 46 | +# if [ "$add_to_hook" == "y" ] |
| 47 | +# then |
| 48 | +# ln -s `git rev-parse --show-toplevel`/build_tools/format-diff.sh $PRE_COMMIT_SCRIPT_PATH |
| 49 | +# fi |
| 50 | +# fi |
| 51 | + |
| 52 | +# Check the format of recently changed lines, |
| 53 | +diffs=$(git diff -U0 HEAD^ | $CLANG_FORMAT_DIFF -p 1) |
| 54 | + |
| 55 | +if [ -z "$diffs" ] |
| 56 | +then |
| 57 | + echo "Nothing needs to be reformatted!" |
| 58 | + exit 0 |
| 59 | +fi |
| 60 | + |
| 61 | +# Highlight the insertion/deletion from the clang-format-diff.py's output |
| 62 | +COLOR_END="\033[0m" |
| 63 | +COLOR_RED="\033[0;31m" |
| 64 | +COLOR_GREEN="\033[0;32m" |
| 65 | + |
| 66 | +echo -e "Detect lines that doesn't follow the format rules:\r" |
| 67 | +# Add the color to the diff. lines added will be green; lines removed will be red. |
| 68 | +echo "$diffs" | |
| 69 | + sed -e "s/\(^-.*$\)/`echo -e \"$COLOR_RED\1$COLOR_END\"`/" | |
| 70 | + sed -e "s/\(^+.*$\)/`echo -e \"$COLOR_GREEN\1$COLOR_END\"`/" |
| 71 | +echo -e "Would you like to fix the format automatically (y/n): \c" |
| 72 | + |
| 73 | +# Make sure under any mode, we can read user input. |
| 74 | +exec < /dev/tty |
| 75 | +read to_fix |
| 76 | + |
| 77 | +if [ "$to_fix" != "y" ] |
| 78 | +then |
| 79 | + exit 1 |
| 80 | +fi |
| 81 | + |
| 82 | +# Do in-place format adjustment. |
| 83 | +git diff -U0 HEAD^ | $CLANG_FORMAT_DIFF -i -p 1 |
0 commit comments