Skip to content

Commit 02cbc0b

Browse files
committed
make: format-commit for git-clang-format
Add `format-commit` and `install-pre-commit-hook` make targets. First one will format all committed changes against current `.clang-format`. The latter one will execute `make format-commit` on `pre-commit` hook.
1 parent 8bc8038 commit 02cbc0b

13 files changed

+1797
-3
lines changed

β€Ž.clang-formatβ€Ž

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
---
2+
BasedOnStyle: Google
3+
AlignAfterOpenBracket: 'true'
4+
AlignConsecutiveAssignments: 'false'
5+
AlignEscapedNewlinesLeft: 'false'
6+
AllowShortIfStatementsOnASingleLine: 'false'
7+
AllowShortFunctionsOnASingleLine: Inline
8+
AllowShortIfStatementsOnASingleLine: 'false'
9+
AllowShortLoopsOnASingleLine: 'false'
10+
AllowAllParametersOfDeclarationOnNextLine: 'true'
11+
AlwaysBreakTemplateDeclarations: 'true'
12+
BinPackArguments: 'false'
13+
BinPackParameters: 'false'
14+
ColumnLimit: '80'
15+
IndentWidth: '2'
16+
MaxEmptyLinesToKeep: '2'
17+
NamespaceIndentation: None
18+
PointerAlignment: Left
19+
SpaceAfterCStyleCast: 'true'
20+
SpaceBeforeAssignmentOperators: 'true'
21+
SpacesBeforeTrailingComments: '2'
22+
TabWidth: '2'
23+
UseTab: Never
24+
25+
...

β€Ž.gitignoreβ€Ž

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,6 @@ test.tap
8787
# Xcode workspaces and project folders
8888
*.xcodeproj
8989
*.xcworkspace
90+
tools/format/clang-format-*
91+
!tools/format/clang-format-*.sha1
92+
tools/format/external_bin

β€ŽCONTRIBUTING.mdβ€Ž

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ copy locally.
3939
$ git clone git@github.com:username/node.git
4040
$ cd node
4141
$ git remote add upstream git://github.com/nodejs/node.git
42+
$ # Either install `clang-format` (version 3.7.0 or 3.8.0) or download one with
43+
$ # `make download-clang-format`
44+
$ make install-pre-commit-hook
4245
```
4346

4447
#### Which branch?

β€ŽMakefileβ€Ž

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -531,8 +531,62 @@ CPPLINT_FILES = $(filter-out $(CPPLINT_EXCLUDE), $(wildcard \
531531
tools/icu/*.h \
532532
))
533533

534-
cpplint:
535-
@$(PYTHON) tools/cpplint.py $(CPPLINT_FILES)
534+
GOOGLE_DOWNLOAD ?= tools/format/download_from_google_storage
535+
GOOGLE_DOWNLOAD_FLAGS ?= --no_resume --no_auth
536+
537+
tools/format/clang-format-mac: tools/format/clang-format-mac.sha1
538+
@echo "Downloading clang-format, may take some time"
539+
@$(GOOGLE_DOWNLOAD) $(GOOGLE_DOWNLOAD_FLAGS) --platform=darwin \
540+
--bucket chromium-clang-format -s $<
541+
542+
tools/format/clang-format-win.exe: tools/format/clang-format-win.exe.sha1
543+
@echo "Downloading clang-format, may take some time"
544+
@$(GOOGLE_DOWNLOAD) $(GOOGLE_DOWNLOAD_FLAGS) --platform=win32 \
545+
--bucket chromium-clang-format -s $<
546+
547+
tools/format/clang-format-linux64: tools/format/clang-format-linux64.sha1
548+
@echo "Downloading clang-format, may take some time"
549+
@$(GOOGLE_DOWNLOAD) $(GOOGLE_DOWNLOAD_FLAGS) --platform=linux* \
550+
--bucket chromium-clang-format -s $<
551+
552+
ifeq ($(OSTYPE),darwin)
553+
CLANG_EXTERNAL_FORMAT ?= tools/format/clang-format-mac
554+
else ifeq ($(OSTYPE),linux)
555+
CLANG_EXTERNAL_FORMAT ?= tools/format/clang-format-linux64
556+
else
557+
CLANG_EXTERNAL_FORMAT ?= tools/format/clang-format-win.exe
558+
endif
559+
560+
ifneq ("$(wildcard $(CLANG_EXTERNAL_FORMAT))", "")
561+
CLANG_FORMAT ?= $(CLANG_EXTERNAL_FORMAT)
562+
else
563+
CLANG_FORMAT ?= clang-format
564+
endif
565+
566+
CLANG_LINT ?= ./tools/format/git-clang-format --binary $(CLANG_FORMAT)
567+
CLANG_LINT_BASE ?= origin/master
568+
CLANG_LINT_COMMIT ?= $(shell git merge-base @ $(CLANG_LINT_BASE))
569+
570+
download-clang-format: $(CLANG_EXTERNAL_FORMAT)
571+
572+
check-clang-format-version:
573+
@($(CLANG_FORMAT) -version | grep -q "3\.[78]\.0") || \
574+
(echo "Wrong clang-format version, expected either 3.7.0 or 3.8.0" && \
575+
exit 1)
576+
577+
cpplint: check-clang-format-version
578+
@echo "Formatting against $(CLANG_LINT_COMMIT)"
579+
@if [[ $$($(CLANG_LINT) $(CLANG_LINT_COMMIT) --diff -- $(CPPLINT_FILES) | grep "diff ") ]]; then \
580+
echo "Lint issues detected, please run \"make format-commit\""; \
581+
exit 1;\
582+
fi
583+
584+
format-commit: check-clang-format-version
585+
@$(CLANG_LINT) -- $(CPPLINT_FILES)
586+
587+
install-pre-commit-hook: check-clang-format-version
588+
@echo "#!/bin/sh\nexec make format-commit" > .git/hooks/pre-commit
589+
@chmod +x .git/hooks/pre-commit
536590

537591
lint: jslint cpplint
538592

@@ -541,4 +595,5 @@ lint: jslint cpplint
541595
dynamiclib test test-all test-addons build-addons website-upload pkg \
542596
blog blogclean tar binary release-only bench-http-simple bench-idle \
543597
bench-all bench bench-misc bench-array bench-buffer bench-net \
544-
bench-http bench-fs bench-tls cctest run-ci
598+
bench-http bench-fs bench-tls cctest run-ci format-commit \
599+
install-pre-commit-hook download-clang-format check-clang-format-version
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
acc9e19e04ad4cf6be067366d2522348cd160ae1
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
e66adcd1631b8d80650e7b15106033b04c9c2212
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
dd661c89fa003a9ebd57d59a7fea346c1fc87f79
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/usr/bin/env bash
2+
# Copyright (c) 2009 The Chromium Authors. All rights reserved.
3+
# Use of this source code is governed by a BSD-style license that can be
4+
# found in the LICENSE file.
5+
6+
base_dir=$(dirname "$0")
7+
8+
PYTHONDONTWRITEBYTECODE=1 exec python "$base_dir/download_from_google_storage.py" "$@"
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
@echo off
2+
:: Copyright (c) 2012 The Chromium Authors. All rights reserved.
3+
:: Use of this source code is governed by a BSD-style license that can be
4+
:: found in the LICENSE file.
5+
6+
:: This is required with cygwin only.
7+
PATH=%~dp0;%PATH%
8+
9+
:: Defer control.
10+
%~dp0python "%~dp0\download_from_google_storage.py" %*

0 commit comments

Comments
Β (0)