Hacker News new | past | comments | ask | show | jobs | submit login

This is my pre-commit hook for Django projects: https://gist.github.com/858223

It just runs pyflakes and does some sanity checking. Best of all, it doesn't go around stashing files or otherwise messing with your working directory or repository. It just uses some git plumbing commands to copy the current index (what you're about to commit) to a temporary directory.

I can't remember where I copied it from but it works perfectly, only copying whatever I'm about to commit:

     git diff --cached --name-only --diff-filter=ACMR | xargs git checkout-index --prefix=$TMPDIR/ --
I can't say how many times this saved me from leaving a pdb.set_trace() in a view somewhere...



Cool. We had looked at doing it that way, but decided that we wanted to have the flexibility of accessing the entire repo so that we can run things like "./manage.py validate" and quick unit tests.

A leftover pdb.set_trace() that snuck into production is the exact reason we first added this :)


So wouldn't:

git checkout-index --prefix=../temp/ --all

work?


Absolutely. Stashing seemed like the cleaner choice to me, but either would work.


That's pretty cool. I've been using something a bit similar for a while too, just to run my changes through PEP8 (http://tech.myemma.com/python-pep8-git-hooks/). Mine uses the output of `git status --porcelain` to get the changed files, and writes them to a temporary directory with `git show`. Similar result to yours, but I think I like your `git diff` way better.


You seem to suggest that the index consists of only the files that were modified / added in the commit, which is not the case.


>> the current index (what you're about to commit)

[snip]

> You seem to suggest that the index consists of only the files that were modified / added in the commit, which is not the case.

To see that the parent's parenthesis doesn't suggest that, consider that in Git you commit the state of the whole tree and not just the modified / added files. The Git manual puts it this way:

[git commit] Stores the current contents of the index in a new commit along with a log message from the user describing the changes.

EDIT: Sorry, now I see what you mean:

>> only copying whatever I'm about to commit

... while the code actually only copies those files that have changes about to be committed.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: