robmadole / jig

Check your code for stuff before you git commit
http://pythonhosted.org/jig/
BSD 2-Clause "Simplified" License
34 stars 4 forks source link

Plugins should function from staged changes instead of the filesystem #3

Open robmadole opened 11 years ago

robmadole commented 11 years ago

As reported by @amorriso in https://github.com/robmadole/jig-plugins/issues/3

FYI, all the pre-commit scripts only verify files on disk. These should be changed to verify files that are actually staged.

One solution was to stage changes so the files on disk were pristine and matched the index.

import subprocess

Stash any changes to the working tree that are not going to be committed

subprocess.call(['git', 'stash', 'save', '--keep-index', '-q'], stdout=subprocess.PIPE)

... execute pre-commit stuff

Unstash changes to the working tree that we had stashed

subprocess.call(['git', 'reset', '--hard'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
subprocess.call(['git', 'stash', 'pop', '--quiet', '--index'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)

One concern I have for this is sometimes stash misbehaves and the Git Python library can also be temperamental. However this proof of concept is a good start.

ghost commented 10 years ago

Hi!

Before I found jig, I wrote git-pre-commit-hook. git-pre-commit-hook depends from git-pre-commit-hook-utils. Maybe, git-pre-commit-hook-utils can help with this problem. Additional features: works with initial commit (jig now write message that one commit required) and with "strange" filenames.

robmadole commented 10 years ago

Oh nice. I have an implementation that fixes this issue sitting in the ci branch but it's not ready for release yet. I will most certainly checkout your projects and borrow your better ideas. Thanks for sharing.