2

We need to write a client-side Git hook to detect a new branch creation from master. Whenever a new branch is created, a folder needs to be deleted from the branch.

I am not sure which hook is the best place to do this check or how to identify if the branch has just been created.

Kashif Nazar
  • 20,775
  • 5
  • 29
  • 46

1 Answers1

1

A client hook is tricky, as:

  • it can be bypassed by a client, and
  • is not easily deployed on all clients.

A server hook is easier (update hook), since it receives a zero sha for new refs.
That same hook can list the content of a commit

git diff-tree --no-commit-id --name-only -r <SHA1>
# or
git ls-tree -d --name-only -r <SHA1>

If a specific folder is still there, it can reject the push with an helpful message.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Thanks for the answer VonC. I am sorry I didn't mention the whole scenario. Our requirement is to delete a directory from a branch that has just been created; and file structure isn't available on the server. That's why we are looking for client hooks. Secondly, if there is a work around to delete the directory from the refs, there is another concern. Shouldn't the directory be deleted before staging it? In my understanding, server side hooks will come into act only when the newly created branch is pushed. – Kashif Nazar Jul 03 '15 at 06:14
  • 1
    @KashifNazar "In my understanding, server side hooks will come into act only when the newly created branch is pushed." that is true, which means either the folder will have been deleted, or it would still be present and the push will be rejected (policy enforced) – VonC Jul 03 '15 at 06:35
  • 1
    @KashifNazar "Shouldn't the directory be deleted before staging it?": sure: git rm does both (delete and stage the deletion), or rm + git add -u will also stage the deletion. – VonC Jul 03 '15 at 06:37
  • thanks again. We are looking for a 'do-the-thing' solution rather than policy enforcement. – Kashif Nazar Jul 03 '15 at 06:42