python - How do I access the contents and names of files being committed from within a Git hook script? -
i'm bit confused how implement git hooks correctly, , cannot figure out how access type of information need within script. have little experience scripting/using python.
i want access filenames of files (and later contents of file) committed in pre-commit hook, can check if match naming convention. i've seen posts such 1 git server hook: contents of files being pushed?, poster mentions how got list of files calling git diff --cached --name-status --diff-filter=am
.
i'm sorry if stupid question, how call line within script , set equal something? recognize line git command i'm confused how translates coding it. in python?
here's have template pre-commit. test print , in python.
#!/usr/bin/env python import sys print("\nerror details\n")
git diff-index --name-status head | grep '^[ma]'
that's reliable way know. prints out names m or prefix, followed whitespace, followed name, indicate whether or not file "modified" or "added."
there magic, though. recommend:
git stash --keep-index git diff-index --name-status head | grep '^[ma]' git reset --hard git stash pop --quiet --index
this give list of names in staging area (by stashing changes since last git add
command) , restore workspace afterward. since staging area, , not workspace, you're commit, want.
i have program @ https://github.com/elfsternberg/pre-commit-stash
it's written in hy, dialect of python people barely know or can read. hy come hy2py transpiler, though, if need it, script show how it's done.
Comments
Post a Comment