数日経って修正が落ち着いたのでまとめる。
#!/bin/sh ./Pods/SwiftFormat/CommandLineTool/swiftformat --lint --quiet --config ./.swiftformat $(git diff --cached --diff-filter=d --name-only | grep .swift)
解説
pre-commit hook
commit操作を開始したあとcommitメッセージの処理の前に実行されるhook。ここで0でない値を返すとcommitが中断される。
実行時のパスはリポジトリのルート。
https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks#_committing_workflow_hooks
SwiftFormat
./Pods/SwiftFormat/CommandLineTool/swiftformat
- CocoaPodsを使っていたら
${PODS_ROOT}/SwiftFormat/CommandLineTool/swiftformat
にexecutableが入っている。 - https://github.com/nicklockwood/SwiftFormat/blob/master/CommandLineTool/swiftformat
- CocoaPodsを使っていたら
--lint
- フォーマットと同じように動くがファイルは変更しない
--dryrun
と違って適用されるルールがあると0でない値を返す- https://github.com/nicklockwood/SwiftFormat#linting
--quiet
- エラー以外は出力しない
--config
- 引数を設定ファイルとして読む
- https://github.com/nicklockwood/SwiftFormat#config-file
git diff
--cached
--diff-filter
- diffの種類によってフィルターする
- Added (
A
), Copied (C
), Deleted (D
), Modified (M
)など
- Added (
- 小文字にすると除外できる
- https://git-scm.com/docs/git-diff#Documentation/git-diff.txt---diff-filterACDMRTUXB82308203
- diffの種類によってフィルターする
--name-only