|
1 |
| -#!/bin/bash |
2 |
| - |
3 |
| -# git pre-commit hook that runs an clang-format stylecheck. |
4 |
| -# Features: |
5 |
| -# - abort commit when commit does not comply with the style guidelines |
6 |
| -# - create a patch of the proposed style changes |
7 |
| - |
8 |
| -# modifications for clang-format by rene.milk@wwu.de |
9 |
| -# This file is part of a set of unofficial pre-commit hooks available |
10 |
| -# at github. |
11 |
| -# Link: https://github.com/githubbrowser/Pre-commit-hooks |
12 |
| -# Contact: David Martin, david.martin.mailbox@googlemail.com |
13 |
| - |
14 |
| - |
15 |
| -################################################################## |
16 |
| -# SETTINGS |
17 |
| -# set path to clang-format binary |
18 |
| -if [ -z "$CLANG_FORMAT" ]; then |
19 |
| - CLANG_FORMAT=$(type -p clang-format) |
20 |
| -fi |
21 |
| - |
22 |
| -# remove any older patches from previous commits. Set to true or false. |
23 |
| -# DELETE_OLD_PATCHES=false |
24 |
| -DELETE_OLD_PATCHES=false |
25 |
| - |
26 |
| -# only parse files with the extensions in FILE_EXTS. Set to true or false. |
27 |
| -# if false every changed file in the commit will be parsed with clang-format. |
28 |
| -# if true only files matching one of the extensions are parsed with clang-format. |
29 |
| -# PARSE_EXTS=true |
30 |
| -PARSE_EXTS=true |
31 |
| - |
32 |
| -# file types to parse. Only effective when PARSE_EXTS is true. |
33 |
| -# FILE_EXTS=".c .h .cpp .hpp" |
34 |
| -FILE_EXTS=".c .h .cpp .hpp .cc .hh .cxx .hxx .m .java .js" |
35 |
| - |
36 |
| -################################################################## |
37 |
| -# There should be no need to change anything below this line. |
38 |
| - |
39 |
| -# Reference: http://stackoverflow.com/questions/1055671/how-can-i-get-the-behavior-of-gnus-readlink-f-on-a-mac |
40 |
| -canonicalize_filename () { |
41 |
| - local target_file=$1 |
42 |
| - local physical_directory="" |
43 |
| - local result="" |
44 |
| - |
45 |
| - # Need to restore the working directory after work. |
46 |
| - pushd `pwd` > /dev/null |
47 |
| - |
48 |
| - cd "$(dirname "$target_file")" |
49 |
| - target_file=`basename $target_file` |
50 |
| - |
51 |
| - # Iterate down a (possible) chain of symlinks |
52 |
| - while [ -L "$target_file" ] |
53 |
| - do |
54 |
| - target_file=$(readlink "$target_file") |
55 |
| - cd "$(dirname "$target_file")" |
56 |
| - target_file=$(basename "$target_file") |
57 |
| - done |
58 |
| - |
59 |
| - # Compute the canonicalized name by finding the physical path |
60 |
| - # for the directory we're in and appending the target file. |
61 |
| - physical_directory=`pwd -P` |
62 |
| - result="$physical_directory"/"$target_file" |
63 |
| - |
64 |
| - # restore the working directory after work. |
65 |
| - popd > /dev/null |
66 |
| - |
67 |
| - echo "$result" |
68 |
| -} |
69 |
| - |
70 |
| -# exit on error |
71 |
| -set -e |
72 |
| - |
73 |
| -# check whether the given file matches any of the set extensions |
74 |
| -matches_extension() { |
75 |
| - local filename=$(basename "$1") |
76 |
| - local extension=".${filename##*.}" |
77 |
| - local ext |
78 |
| - |
79 |
| - for ext in $FILE_EXTS; do [[ "$ext" == "$extension" ]] && return 0; done |
80 |
| - |
81 |
| - return 1 |
82 |
| -} |
83 |
| - |
84 |
| -# necessary check for initial commit |
85 |
| -if git rev-parse --verify HEAD >/dev/null 2>&1 ; then |
86 |
| - against=HEAD |
87 |
| -else |
88 |
| - # Initial commit: diff against an empty tree object |
89 |
| - against=4b825dc642cb6eb9a060e54bf8d69288fbee4904 |
90 |
| -fi |
91 |
| - |
92 |
| -if [ ! -x "$CLANG_FORMAT" ] ; then |
93 |
| - printf "Error: clang-format executable not found.\n" |
94 |
| - printf "Set the correct path in $(canonicalize_filename "$0").\n" |
95 |
| - exit 1 |
96 |
| -fi |
97 |
| - |
98 |
| -# create a random filename to store our generated patch |
99 |
| -prefix="pre-commit-clang-format" |
100 |
| -suffix="$(date +%s)" |
101 |
| -patch="/tmp/$prefix-$suffix.patch" |
102 |
| - |
103 |
| -# clean up any older clang-format patches |
104 |
| -$DELETE_OLD_PATCHES && rm -f /tmp/$prefix*.patch |
105 |
| - |
106 |
| -# create one patch containing all changes to the files |
107 |
| -git diff-index --cached --diff-filter=ACMR --name-only $against -- | while read file; |
108 |
| -do |
109 |
| - # ignore file if we do check for file extensions and the file |
110 |
| - # does not match any of the extensions specified in $FILE_EXTS |
111 |
| - if $PARSE_EXTS && ! matches_extension "$file"; then |
112 |
| - continue; |
113 |
| - fi |
114 |
| - |
115 |
| - # clang-format our sourcefile, create a patch with diff and append it to our $patch |
116 |
| - # The sed call is necessary to transform the patch from |
117 |
| - # --- $file timestamp |
118 |
| - # +++ - timestamp |
119 |
| - # to both lines working on the same file and having a a/ and b/ prefix. |
120 |
| - # Else it can not be applied with 'git apply'. |
121 |
| - "$CLANG_FORMAT" -style=file "$file" | \ |
122 |
| - diff -u "$file" - | \ |
123 |
| - sed -e "1s|--- |--- a/|" -e "2s|+++ -|+++ b/$file|" >> "$patch" |
124 |
| -done |
125 |
| - |
126 |
| -# if no patch has been generated all is ok, clean up the file stub and exit |
127 |
| -if [ ! -s "$patch" ] ; then |
128 |
| - rm -f "$patch" |
129 |
| - exit 0 |
130 |
| -fi |
131 |
| - |
132 |
| -# a patch has been created, notify the user and exit |
133 |
| -printf "\nThe following differences were found between the code to commit " |
134 |
| -printf "and the clang-format rules:\n\n" |
135 |
| -cat "$patch" |
136 |
| - |
137 |
| -printf "\nYou can apply these changes with:\n git apply $patch\n" |
138 |
| -printf "(may need to be called from the root directory of your repository)\n" |
139 |
| -printf "Aborting commit. Apply changes and commit again or skip checking with" |
140 |
| -printf " --no-verify (not recommended).\n" |
141 |
| - |
142 |
| -exit 1 |
| 1 | +#!/bin/sh |
| 2 | +# https://github.com/shyiko/ktlint pre-commit hook |
| 3 | +git diff --name-only --cached --relative | grep '\.kt[s"]\?$' | xargs ktlint --android --relative . |
| 4 | +if [ $? -ne 0 ]; then exit 1; fi |
0 commit comments