Philip Kirkbride
Philip Kirkbride

Reputation: 22879

Git pre-commit running after commit?

I'm trying to run a format command on code before it's committed. I ether want it to not allow commit if unformatted or automatically do it before committing.

What happens now is the commit happens and the formatting is applied after the commit as unstaged changes.

Here is my .git/hooks/pre-commit:

#!/bin/bash

go fmt src/*.go

Upvotes: 1

Views: 1872

Answers (2)

Roland Smith
Roland Smith

Reputation: 43505

After formatting, add the files to the index:

#!/bin/bash

go fmt src/*.go
git add src/*.go
exit 0

It would probably be a good idea to mention this hook in your README. Just so you don't forget.

A possibly more serious point is that your edits to the code for this commit will be indistinguishable from go fmt changes.

Upvotes: 4

Philip Kirkbride
Philip Kirkbride

Reputation: 22879

After reading Ian's code I had an idea which seems to be working.

#!/bin/bash

go fmt src/*.go
git add src/*.go

Upvotes: 0

Related Questions