Skip to content

Commit d702d80

Browse files
author
Kai Liu
committed
A script that automatically reformat affected lines
Summary: Added a script that reformat only the affected lines in a given diff. I planned to make that file as pre-commit hook but looks it's a little bit more difficult than I thought. Since I don't want to spend too much time on this task right now, I eventually added a "make command" to achieve this with a few additional key strokes. Also make the clang-format solely inherited from Google's style -- there are still debates on some of the style issues, but we can address them later once we reach a consensus. Test Plan: Did some ugly format change and ran "make format", all affected lines are formatted as expected. Reviewers: igor, sdong, haobo CC: leveldb Differential Revision: https://reviews.facebook.net/D15147
1 parent fbbf0d1 commit d702d80

File tree

3 files changed

+93
-43
lines changed

3 files changed

+93
-43
lines changed

.clang-format

-42
Original file line numberDiff line numberDiff line change
@@ -2,46 +2,4 @@
22
# http://clang.llvm.org/docs/ClangFormatStyleOptions.html
33
---
44
BasedOnStyle: Google
5-
AccessModifierOffset: -1
6-
ConstructorInitializerIndentWidth: 4
7-
AlignEscapedNewlinesLeft: true
8-
AlignTrailingComments: true
9-
AllowAllParametersOfDeclarationOnNextLine: true
10-
AllowShortIfStatementsOnASingleLine: false
11-
AllowShortLoopsOnASingleLine: false
12-
AlwaysBreakTemplateDeclarations: true
13-
AlwaysBreakBeforeMultilineStrings: true
14-
BreakBeforeBinaryOperators: false
15-
BreakConstructorInitializersBeforeComma: false
16-
BinPackParameters: false
17-
ColumnLimit: 80
18-
ConstructorInitializerAllOnOneLineOrOnePerLine: true
19-
DerivePointerBinding: true
20-
ExperimentalAutoDetectBinPacking: true
21-
IndentCaseLabels: false
22-
MaxEmptyLinesToKeep: 1
23-
NamespaceIndentation: None
24-
ObjCSpaceBeforeProtocolList: false
25-
PenaltyBreakBeforeFirstCallParameter: 10
26-
PenaltyBreakComment: 60
27-
PenaltyBreakString: 1000
28-
PenaltyBreakFirstLessLess: 20
29-
PenaltyExcessCharacter: 1000000
30-
PenaltyReturnTypeOnItsOwnLine: 200
31-
PointerBindsToType: true
32-
SpacesBeforeTrailingComments: 2
33-
Cpp11BracedListStyle: true
34-
Standard: Cpp11
35-
IndentWidth: 2
36-
TabWidth: 8
37-
UseTab: Never
38-
BreakBeforeBraces: Attach
39-
IndentFunctionDeclarationAfterType: false
40-
SpacesInParentheses: false
41-
SpacesInAngles: false
42-
SpaceInEmptyParentheses: false
43-
SpacesInCStyleCastParentheses: false
44-
SpaceAfterControlStatementKeyword: true
45-
SpaceBeforeAssignmentOperators: true
46-
ContinuationIndentWidth: 4
475
...

Makefile

+10-1
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ endif # PLATFORM_SHARED_EXT
135135
all: $(LIBRARY) $(PROGRAMS) $(SHARED)
136136

137137
.PHONY: blackbox_crash_test check clean coverage crash_test ldb_tests \
138-
release tags valgrind_check whitebox_crash_test
138+
release tags valgrind_check whitebox_crash_test format
139139

140140
release:
141141
$(MAKE) clean
@@ -196,6 +196,9 @@ tags:
196196
ctags * -R
197197
cscope -b `find . -name '*.cc'` `find . -name '*.h'`
198198

199+
format:
200+
build_tools/format-diff.sh
201+
199202
# ---------------------------------------------------------------------------
200203
# Unit tests and tools
201204
# ---------------------------------------------------------------------------
@@ -411,6 +414,12 @@ DEPFILES = $(filter-out util/build_version.d,$(SOURCES:.cc=.d))
411414

412415
depend: $(DEPFILES)
413416

417+
# if the make goal is either "clean" or "format", we shouldn't
418+
# try to import the *.d files.
419+
# TODO(kailiu) The unfamiliarity of Make's conditions leads to the ugly
420+
# working solution.
414421
ifneq ($(MAKECMDGOALS),clean)
422+
ifneq ($(MAKECMDGOALS),format)
415423
-include $(DEPFILES)
416424
endif
425+
endif

build_tools/format-diff.sh

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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

Comments
 (0)