Prevent git committers from creating specific directories

Is there a way to prevent git committers from creating a directory in the repository.

We have some developers who save non-installation files in a directory structure. Files to be in

a/b/c/file 

Finish in

  a/a/b/c/file 

I just want to prevent the creation of

  a/a/* 

This should happen appropriately so that they know that their files have not been executed ... so just ignoring a/a/* not enough.

Is there any way to do this?

EDIT

Several people have indicated that committers should be "trained" so as not to. There are several disadvantages:

  • Committers are human and make mistakes; training does not stop it.
  • This is not the kind of information that would consolidate itself at the head of a new rental during training (waaay, to describe in detail).
  • Basically, this happens during mergers from one branch to another, so the files are not created manually, but they are dragged into a larger set of changes.

Thus, the need for an automatic approach.

+4
source share
3 answers

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.

+5
source

Submodules (as suggested in the commentary) are an option, but this does not prevent the user from creating a/a , it can just stop him from inserting unrelated directories.

You can make a git hook that checks that there are no files in the a/a/ folder, and reject the click if that happens. Checkout git intercepts there: http://git-scm.com/book/en/Customizing-Git-Git-Hooks

You are probably looking for a pre-receive hook. Then you only need to write your own script - it should not be too complicated.

+2
source

This will prevent the creation of a subdirectory a , but this is not ideal, and the best answer is probably training:

 ln -s . a git add a git commit -m "Create top-level self-referential symlink to stifle subdirectory creation" 
+2
source

Source: https://habr.com/ru/post/1486904/


All Articles