2016-09-30 5 views
1

私の目標は、コミット時にgoソースコードのフォーマットを強制することです。ファイル/ファイルセットの上にgo fmtを実行すると変更があるかどうかを調べる方法はありますか?私は考えることができる唯一の技術はにある:goコードがフォーマットされているかどうかを調べること

  • トン
  • 実行がソース
  • 上のfmt行く現在の時刻が最後のmodの日付のいずれかどうかを確認するために、ソースファイルを反復>t

これを実行するためのツール/スクリプトを作成し、サークルCIビルド中に実行することができます。私は先に進む前に私が車輪を再発明していないことを確認したかったのです。

> gofmt -l . 

そして、受信したリストを渡します。そのフォーマットのような

何かをgofmt's`異なり

-lリストファイル:

+1

を見つけましたか? – coredump

+4

これは 'gofmt'プログラムに組み込まれています:' gofmt -l ... ' –

+0

タイムスタンプがあっても、ファイルが変更されていないことをいつも前と後に' md5sum'を実行できます。 –

答えて

0

gofmt -hによると、あなたは-lオプションを使用することができますさらにファイルの。

1

無条件にそれをフォーマットしないのはなぜ私がこの前commitフックenter link description here

1 #!/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 
13 gofiles=$(git diff --cached --name-only --diff-filter=ACM | grep '\.go$') 
14 [ -z "$gofiles" ] && exit 0 
15 
16 unformatted=$(gofmt -l $gofiles) 
17 [ -z "$unformatted" ] && exit 0 
18 
19 # Some files are not gofmt'd. Print message and fail. 
20 
21 echo >&2 "Go files must be formatted with gofmt. Please run:" 
22 for fn in $unformatted; do 
23  echo >&2 " gofmt -w $PWD/$fn" 
24 done 
25 
26 exit 1 
関連する問題