@ -26,16 +26,30 @@ while getopts ':ch' OPTION; do
esac
esac
done
done
if [ -z $CLANG_FORMAT_DIFF ]
REPO_ROOT = " $( git rev-parse --show-toplevel) "
then
CLANG_FORMAT_DIFF = "clang-format-diff.py"
fi
# Check clang-format-diff.py
if [ " $CLANG_FORMAT_DIFF " ] ; then
if ! which $CLANG_FORMAT_DIFF & > /dev/null
echo " Note: CLANG_FORMAT_DIFF=' $CLANG_FORMAT_DIFF ' "
then
# Dry run to confirm dependencies like argparse
if [ ! -f ./clang-format-diff.py ]
if $CLANG_FORMAT_DIFF --help >/dev/null < /dev/null; then
then
true #Good
else
exit 128
fi
else
# First try directly executing the possibilities
if clang-format-diff.py --help & > /dev/null < /dev/null; then
CLANG_FORMAT_DIFF = clang-format-diff.py
elif $REPO_ROOT /clang-format-diff.py --help & > /dev/null < /dev/null; then
CLANG_FORMAT_DIFF = $REPO_ROOT /clang-format-diff.py
else
# This probably means we need to directly invoke the interpreter.
# But first find clang-format-diff.py
if [ -f " $REPO_ROOT /clang-format-diff.py " ] ; then
CFD_PATH = " $REPO_ROOT /clang-format-diff.py "
elif which clang-format-diff.py & > /dev/null; then
CFD_PATH = " $( which clang-format-diff.py) "
else
echo "You didn't have clang-format-diff.py and/or clang-format available in your computer!"
echo "You didn't have clang-format-diff.py and/or clang-format available in your computer!"
echo "You can download clang-format-diff.py by running: "
echo "You can download clang-format-diff.py by running: "
echo " curl --location http://goo.gl/iUW1u2 -o ${ CLANG_FORMAT_DIFF } "
echo " curl --location http://goo.gl/iUW1u2 -o ${ CLANG_FORMAT_DIFF } "
@ -48,23 +62,11 @@ then
echo " Then, move both files (i.e. ${ CLANG_FORMAT_DIFF } and clang-format) to some directory within PATH= ${ PATH } "
echo " Then, move both files (i.e. ${ CLANG_FORMAT_DIFF } and clang-format) to some directory within PATH= ${ PATH } "
echo " and make sure ${ CLANG_FORMAT_DIFF } is executable. "
echo " and make sure ${ CLANG_FORMAT_DIFF } is executable. "
exit 128
exit 128
else
if [ -x ./clang-format-diff.py ]
then
PATH = $PATH :.
else
CLANG_FORMAT_DIFF = "python ./clang-format-diff.py"
fi
fi
fi
# Check argparse pre-req on interpreter, or it will fail
fi
if echo import argparse | ${ PYTHON :- python3 } ; then
true # Good
# Check argparse, a library that clang-format-diff.py requires.
else
python 2>/dev/null << EOF
import argparse
EOF
if [ " $? " != 0 ]
then
echo "To run clang-format-diff.py, we'll need the library " argparse" to be"
echo "To run clang-format-diff.py, we'll need the library " argparse" to be"
echo "installed. You can try either of the follow ways to install it:"
echo "installed. You can try either of the follow ways to install it:"
echo " 1. Manually download argparse: https://pypi.python.org/pypi/argparse"
echo " 1. Manually download argparse: https://pypi.python.org/pypi/argparse"
@ -72,6 +74,28 @@ then
echo " 3. pip install argparse (if you have pip)"
echo " 3. pip install argparse (if you have pip)"
exit 129
exit 129
fi
fi
# Unfortunately, some machines have a Python2 clang-format-diff.py
# installed but only a Python3 interpreter installed. Rather than trying
# different Python versions that might be installed, we can try migrating
# the code to Python3 if it looks like Python2
if grep -q "print '" " $CFD_PATH " && \
${ PYTHON :- python3 } --version | grep -q 'ython 3' ; then
if [ ! -f " $REPO_ROOT /.py3/clang-format-diff.py " ] ; then
echo " Migrating $CFD_PATH to Python3 in a hidden file "
mkdir -p " $REPO_ROOT /.py3 "
${ PYTHON :- python3 } -m lib2to3 -w -n -o " $REPO_ROOT /.py3 " " $CFD_PATH " > /dev/null || exit 128
fi
CFD_PATH = " $REPO_ROOT /.py3/clang-format-diff.py "
fi
CLANG_FORMAT_DIFF = " ${ PYTHON :- python3 } $CFD_PATH "
# This had better work after all those checks
if $CLANG_FORMAT_DIFF --help >/dev/null < /dev/null; then
true #Good
else
exit 128
fi
fi
fi
# TODO(kailiu) following work is not complete since we still need to figure
# TODO(kailiu) following work is not complete since we still need to figure
# out how to add the modified files done pre-commit hook to git's commit index.
# out how to add the modified files done pre-commit hook to git's commit index.