observing / pre-commit

Automatically installs a git pre-commit script in your git repository which runs your `npm test` on pre-commit
MIT License
1.88k stars 150 forks source link

Add an option to stash changes before running hooks #63

Open kevinoid opened 8 years ago

kevinoid commented 8 years ago

In order to ensure any pre-commit tests are run against the files as they will appear in the commit, this PR adds an option to run git stash to save any changes to the working tree before running the scripts, then restore any changes after the scripts finish.

The save/restore procedure is based on Chris Torek's StackOverflow answer to a question asking how to do exactly this. This implementation does not do a hard reset before restoring the stash by default, since it assumes that if scripts modify tracked files the changes should be kept. But it does provide this behavior, and git clean, as configurable options.

It follows the convention requested in #4 by making the new stash option default to off. Although there are no known implementation issues (with the exception of the git bug noted in Torek's SO answer), current scripts may expect modified/untracked files to exist or modify untracked files in a way which prevents applying the stash, making default-on behavior backwards-incompatible.

The tests are split into a separate file. Since each stash option is tested against a project repository and a clean/reset is done between each test, the tests are somewhat slow. By splitting the tests into a separate file, we can avoid running them by default. They can instead be run as test-stash, as part of test-all, and as part of test-travis.

This commit is based off of the work of Christopher Hiller in #47, although the implementation differs significantly due to the use of Promises in place of async, which I found to be significantly clearer, more flexible, and they make the tests significantly more concise.

Thanks for considering, Kevin

Fixes: #4

3rd-Eden commented 8 years ago

Thanks for your pull request, @Swaagie Any opinions?