Skip to content

Commit e19bad9

Browse files
author
Kai Liu
committed
Fix some "make format" issue
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
1 parent 6d6fb70 commit e19bad9

File tree

1 file changed

+30
-4
lines changed

1 file changed

+30
-4
lines changed

build_tools/format-diff.sh

+30-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#!/bin/bash
2-
set -e
32
# If clang_format_diff.py command is not specfied, we assume we are able to
43
# access directly without any path.
54
if [ -z $CLANG_FORMAT_DIFF ]
@@ -12,7 +11,7 @@ if ! which $CLANG_FORMAT_DIFF &> /dev/null
1211
then
1312
echo "You didn't have clang-format-diff.py available in your computer!"
1413
echo "You can download it by running: "
15-
echo " curl https://fburl.com/clang-format-diff"
14+
echo " curl http://goo.gl/iUW1u2"
1615
exit 128
1716
fi
1817

@@ -49,8 +48,22 @@ fi
4948
# fi
5049
# fi
5150

52-
# Check the format of recently changed lines,
53-
diffs=$(git diff -U0 HEAD^ | $CLANG_FORMAT_DIFF -p 1)
51+
set -e
52+
53+
uncommitted_code=`git diff HEAD`
54+
55+
# If there's no uncommitted changes, we assume user are doing post-commit
56+
# format check, in which case we'll check the modified lines from latest commit.
57+
# Otherwise, we'll check format of the uncommitted code only.
58+
format_last_commit=0
59+
if [ -z "$uncommitted_code" ]
60+
then
61+
# Check the format of last commit
62+
diffs=$(git diff -U0 HEAD^ | $CLANG_FORMAT_DIFF -p 1)
63+
else
64+
# Check the format of uncommitted lines,
65+
diffs=$(git diff -U0 HEAD | $CLANG_FORMAT_DIFF -p 1)
66+
fi
5467

5568
if [ -z "$diffs" ]
5669
then
@@ -81,3 +94,16 @@ fi
8194

8295
# Do in-place format adjustment.
8396
git diff -U0 HEAD^ | $CLANG_FORMAT_DIFF -i -p 1
97+
echo "Files reformatted!"
98+
99+
# Amend to last commit if user do the post-commit format check
100+
if [ -z "$uncommitted_code" ]; then
101+
echo -e "Would you like to amend the changes to last commit (`git log HEAD --oneline | head -1`)? (y/n): \c"
102+
read to_amend
103+
104+
if [ "$to_amend" == "y" ]
105+
then
106+
git commit -a --amend --reuse-message HEAD
107+
echo "Amended to last commit"
108+
fi
109+
fi

0 commit comments

Comments
 (0)