Skip to content

Commit 5bde67f

Browse files
authored
Merge pull request #2095 from matthewdale/godriver3543-precommit-licenseck
GODRIVER-3543 Check license headers in pre-commit.
2 parents 3b0d247 + a12b92c commit 5bde67f

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

.pre-commit-config.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,9 @@ repos:
6565
require_serial: true
6666
pass_filenames: false
6767
entry: etc/golangci-lint.sh
68+
69+
- id: check-licenses
70+
name: check-licenses
71+
language: system
72+
types: [go]
73+
entry: etc/check_license.sh

etc/check_license.sh

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ copyright=$"// Copyright (C) MongoDB, Inc. $year-present.
88
// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
99
"
1010

11-
add_copyright() {
11+
check_or_add_copyright() {
1212
file=$1
1313

1414
# Check if first 24 bytes match first 24 bytes of copyright notice.
@@ -54,10 +54,23 @@ do
5454
esac
5555
done
5656

57-
# Find all .go files and try to write a license notice.
58-
GO_FILES=$(find . -type f -name "*.go" -print)
57+
# Shift script arguments so $1 contains only optional filename args.
58+
# E.g. check_license.sh -a ./mongo/cursor.go
59+
shift "$((OPTIND - 1))"
60+
FILES=$1
5961

60-
for file in $GO_FILES
62+
# If no filenames were passed, find all .go files and try to write a license
63+
# notice.
64+
if [ -z "$FILES" ]; then
65+
FILES=$(find . -type f -name "*.go" -print)
66+
fi
67+
68+
for file in $FILES
6169
do
62-
add_copyright "$file"
70+
# Skip any files that aren't .go files.
71+
if [[ ! "$file" =~ .go$ ]]; then
72+
continue
73+
fi
74+
75+
check_or_add_copyright "$file"
6376
done

0 commit comments

Comments
 (0)