Skip to content

Commit

Permalink
Fix some "make format" issue
Browse files Browse the repository at this point in the history
Summary:
* make sure when some pre-check fails, the script won't halt immediately.
* change fburl to google's short url.
* Fix a bug in this script: now it checks the uncommitted code only.

Test Plan: Ran the script under differnet environments.

Reviewers: igor

Reviewed By: igor

CC: leveldb

Differential Revision: https://reviews.facebook.net/D15231
  • Loading branch information
Kai Liu committed Jan 16, 2014
1 parent 6d6fb70 commit e19bad9
Showing 1 changed file with 30 additions and 4 deletions.
34 changes: 30 additions & 4 deletions build_tools/format-diff.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/bin/bash
set -e
# If clang_format_diff.py command is not specfied, we assume we are able to
# access directly without any path.
if [ -z $CLANG_FORMAT_DIFF ]
Expand All @@ -12,7 +11,7 @@ if ! which $CLANG_FORMAT_DIFF &> /dev/null
then
echo "You didn't have clang-format-diff.py available in your computer!"
echo "You can download it by running: "
echo " curl https://fburl.com/clang-format-diff"
echo " curl http://goo.gl/iUW1u2"
exit 128
fi

Expand Down Expand Up @@ -49,8 +48,22 @@ fi
# fi
# fi

# Check the format of recently changed lines,
diffs=$(git diff -U0 HEAD^ | $CLANG_FORMAT_DIFF -p 1)
set -e

uncommitted_code=`git diff HEAD`

# If there's no uncommitted changes, we assume user are doing post-commit
# format check, in which case we'll check the modified lines from latest commit.
# Otherwise, we'll check format of the uncommitted code only.
format_last_commit=0
if [ -z "$uncommitted_code" ]
then
# Check the format of last commit
diffs=$(git diff -U0 HEAD^ | $CLANG_FORMAT_DIFF -p 1)
else
# Check the format of uncommitted lines,
diffs=$(git diff -U0 HEAD | $CLANG_FORMAT_DIFF -p 1)
fi

if [ -z "$diffs" ]
then
Expand Down Expand Up @@ -81,3 +94,16 @@ fi

# Do in-place format adjustment.
git diff -U0 HEAD^ | $CLANG_FORMAT_DIFF -i -p 1
echo "Files reformatted!"

# Amend to last commit if user do the post-commit format check
if [ -z "$uncommitted_code" ]; then
echo -e "Would you like to amend the changes to last commit (`git log HEAD --oneline | head -1`)? (y/n): \c"
read to_amend

if [ "$to_amend" == "y" ]
then
git commit -a --amend --reuse-message HEAD
echo "Amended to last commit"
fi
fi

0 comments on commit e19bad9

Please sign in to comment.