Reputation: 377
I'm working on a pre-receive hook. I want all the files to be named in UPPER CASE. If a file doesn't respect this condition we should stop the push and ask the user to change the file name. I tried
if [[ "$fileName" =~ [a-z] ]]; then
echo -e "file should be in upper Case" >&2
exit 1;
fi
The push is stopped after a user changes the filename with
git mv -f tes TES
and I always get the error that the file is lowercase. Should I delete the file from the last commit and commit again with new name?
Upvotes: 0
Views: 334
Reputation: 45659
It sounds like the hook is working. However, that doesn't remove the commit with the lowercase filename from the history of the local repo's ref.
If the user is just pushing a single commit, then they can fix the filenames and then
git commit --amend
More commonly, a user might push multiple commits at once. Assuming your hook checks all commits (if not, lowercase names will leak in to your history over time), the user will have to fix the entire history before pushing. This can be done with git filter-branch
. To clean up a branch called the_branch
so it can be pushed:
ucfn.sh
So
git filter-branch --tree-filter=ucfn.sh -- the_branch
Or maybe
git filter-branch --tree-filter=ucfn.sh -- origin/the_branch..the_branch
(You can make this run faster (if there are lots of new commits, or if the work tree is quite large) by switching to an index-filter
, but the script is harder to write because you have no work tree in an index-filter
.)
The best thing is to help your users avoid this condition, by providing them with a pre-commit hook that checks whether the push will be accepted, and rejects the commit otherwise. Like any local hook, they'd have to choose to set it up and you can't force them to use it - so you still need the post-receive hook - but this can stop your users from getting into a corner where they have a huge clean-up before they can push.
Upvotes: 2