git: Allow specific files in a commit to be allowed via push to the remote repository based on certain criteria

StackOverflow https://stackoverflow.com/questions/18121005

  •  23-06-2022
  •  | 
  •  

Question

I have a situation wherein my pre-receive hook script needs to allow only those files in commits whose name starts with Tag_

For example, I have a commit which contains 3 files,

Tag_a
Tagb
Tag_c

now my pre-receive script should allow only Tag_a and Tag_b but not Tagb.

I read somewhere on Stack Overflow that if we reject a file and allow the other files to be pushed, then the hash (sha1) value of the commit may change.

So my question is, is there a way to achieve this?

Was it helpful?

Solution 2

A pre-receive or update hook can only accept or reject an entire ref-name. It's an all or nothing deal. You can inspect the proposed tree(s), but you'll have to figure out what the changes from the previous place the ref-name pointed-to might be.

For instance, if you get something saying refs/heads/zog is moving from d4df144c36f26c7887e5f8a74812f06a3b0338ec to 5e013711f5d6eb3f643ef562d49a131852aa4aa1, you'll need to see all the commits being added (or removed) between those points, and see what tree changes are being made in each commit. You can then decide that it's OK for the branch name to change, or that it is not OK, and that's basically it. The new commit might have the old one as its (single) parent, or it might have another new commit as its parent, which in turn has a third new commit, etc. (And any new commit may be a merge with multiple parents.)

OTHER TIPS

Don't be sneaky. Just reject the whole commit, user should --amend commit to exclude disallowed files and try to push again.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top