blob: 242159f04af5436702e0880dedd00c8b6796498f [file] [log] [blame]
Andrew Gerrand1395d3d2012-11-15 19:58:49 +01001#!/bin/sh
2# Copyright 2012 The Go Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style
4# license that can be found in the LICENSE file.
5
6# git gofmt pre-commit hook
7#
8# To use, store as .git/hooks/pre-commit inside your repository and make sure
9# it has execute permissions.
10#
11# This script does not handle file names that contain spaces.
12
Derek Shockey90fea9d2016-02-23 21:17:25 -080013gofiles=$(git diff --cached --name-only --diff-filter=ACM | grep '\.go$')
Andrew Gerrand1395d3d2012-11-15 19:58:49 +010014[ -z "$gofiles" ] && exit 0
15
16unformatted=$(gofmt -l $gofiles)
17[ -z "$unformatted" ] && exit 0
18
19# Some files are not gofmt'd. Print message and fail.
20
21echo >&2 "Go files must be formatted with gofmt. Please run:"
22for fn in $unformatted; do
23 echo >&2 " gofmt -w $PWD/$fn"
24done
25
26exit 1