Consumer education with a 2x4 key should be the first choice. For a technological solution, you can prevent them from committing such commits locally with a suitable commit. However, they probably also will not be able to set the hook. Thus, you have to implement a hook that rejects their click. You can do this with the update hook, which can reject commits before they are written to the central repository. Here is a script hook like this:
#!/bin/sh # # update hook - args: refname sha1-old sha1-new ref="$1" old="$2" new="$3" if [ -z "$ref" -o -z "$old" -o -z "$new" ]; then echo >&2 "usage: $0 <ref> <oldrev> <newrev>" exit 1 fi for badpath in a/aa/z/x; do if git ls-tree $new:$badpath >/dev/null; then bogus=$(git ls-tree -r --name-only $new $badpath) echo >&2 "REJECTED DUE TO BOGUS PATH \"$bogus\"" exit 1 fi done exit 0
If we create a simple bare repository and add it as hooks / update and make it executable (we donβt need to do this on windows - interceptors are executed anyway). Then clone the repository and add the forbidden path (for example, the file a / b / c /, as in your example). This is what I get when I push this commit:
C:\Users\Pat\AppData\Local\Temp\x\L>git push origin master Counting objects: 9, done. Delta compression using up to 2 threads. Compressing objects: 100% (2/2), done. Writing objects: 100% (7/7), 423 bytes | 0 bytes/s, done. Total 7 (delta 0), reused 0 (delta 0) remote: REJECTED DUE TO BOGUS PATH "a/a/b/c/file" remote: error: hook declined to update refs/heads/master To C:/Users/Pat/AppData/Local/Temp/x/R ! [remote rejected] master -> master (hook declined) error: failed to push some refs to 'C:/Users/Pat/AppData/Local/Temp/x/R'
This illustrates two things. Firstly, these hook scripts are actually very easy to create and test in some kind of temporary storage. Everything that you echo to stderr or stdout is copied to the user. Secondly - all this works on windows too (when using Git for Windows at least).
Commit capture works in the local user repository and will be less annoying, but you will have the problem of ensuring that all local clones have the correct interceptors. It is much easier to handle on the server.
source share