0

We have gitlab set up on our own server. I need a server side hook that will read each commit message and add the branch name on which the commit is made at the beginning of the message. Is it possible to modify the commit message during push? I read that it is possible to modify commit messages on the client side link but can it be done on server side.

Any help.

3
  • I see two problems with this. First, determining the appropriate branch name will not be straight-forward. Second, even if you can do it, the result will be that you have different commits on the remote than you do on the local - because the commit message is part of the commit identity. So instead... Commented May 18, 2017 at 18:50
  • ...you should probably have your hook detect whether the pushed commits adhere to standards and reject the push if not; then train your people to adhere to the standard (in part by rejecting their pushes when they don't). This does still require that you be able to read a message and tell if it conforms. But this needn't be perfect. Require the message to start with something in the format of your branch names, and as long as your developers aren't children who spitefully put bad info in, it should be ok. Commented May 18, 2017 at 18:51
  • 1
    Seems such tagging is not very necessary. You can use git branch --contains <sha-1> to list all local branches that contain a commit. Or git branch -r --contains <sha-1> to list all remote branches containing a commit. This way you can get the information about which branches a commit has been pushed to without the need of any hooks. Commented May 19, 2017 at 13:20

1 Answer 1

5

It is a veeeery bad idea to modify commits in any way on push. The message is part of the commit ID and thus you will essential make the commit a different commit with a different SHA if you would do such a thing.

Furthermore, a commit does not belong to any branch. A commit can be part of the history of 0, 1 or any other amount of branches. You can only determine to which branches (plural) a commit belongs at the time you are looking. This can change any time basically.

On client side there are hooks that pre-format the commit message or post-process the commit message before / after the editor is invoked when a commit is created, but at this point the commit is not yet present and thus you influence how it will be created, but do not change it which really would be a terrible idea due to several reasons.

What you could do in a server side hook is you could add notes to the commits where you mention the branches to which the commit belongs at push time in a post-receive hook. Notes attached to commits do not change the commits themselves.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.