Skip to content

Commit a4ca79a

Browse files
rabbahdubee
authored andcommitted
Git pre-commit hook to format src. (#324)
1 parent c8af54d commit a4ca79a

1 file changed

Lines changed: 38 additions & 0 deletions

File tree

tools/git/pre-commit-gofmt.sh

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#!/usr/bin/env bash
2+
#
3+
# Licensed to the Apache Software Foundation (ASF) under one or more
4+
# contributor license agreements. See the NOTICE file distributed with
5+
# this work for additional information regarding copyright ownership.
6+
# The ASF licenses this file to You under the Apache License, Version 2.0
7+
# (the "License"); you may not use this file except in compliance with
8+
# the License. You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an "AS IS" BASIS,
14+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
# See the License for the specific language governing permissions and
16+
# limitations under the License.
17+
#
18+
19+
set -e
20+
21+
ROOT_DIR="$(git rev-parse --show-toplevel)"
22+
23+
set +e
24+
FILE_EXT=".go"
25+
STAGED_FILES=$(git diff --name-only --no-color --diff-filter=d --exit-code -- "${ROOT_DIR}/*$FILE_EXT")
26+
STAGED_FILES_DETECTED=$?
27+
set -e
28+
29+
if [ "${STAGED_FILES_DETECTED}" -eq 1 ]; then
30+
# Re-format and re-add all staged files
31+
for FILE in ${STAGED_FILES}
32+
do
33+
gofmt -s -w "${ROOT_DIR}/${FILE}"
34+
git add -- "${ROOT_DIR}/${FILE}"
35+
done
36+
fi
37+
38+
exit 0

0 commit comments

Comments
 (0)