1

The user and group are always changed to root:root if I execute git pull or git checkout

So I created the file .git/hooks/post-merge with the following content:

#!/bin/sh

chown -R mycompany:www /srv/www/vhosts/mycompany

exec git-update-server-info

The file has execute permission

But it does not work, nothing changes after git pull was called.

Black
  • 18,150
  • 39
  • 158
  • 271
  • 1
    It's an example like a in gitlab or github when you see merge. - shows what lines are removed, + shows what lines are added. – Ilko Mar 06 '19 at 12:36
  • Ok I see, so his example does not work. I removed it from my question. – Black Mar 06 '19 at 12:38
  • You can try his example, just remove `+` and `-` and `- git pull HUB master` line and try it. Also change branch names. – Ilko Mar 06 '19 at 12:43
  • Does not work. Why is my code not getting called? – Black Mar 06 '19 at 15:15

1 Answers1

1

First thing to check: is your hook executable?
As in this example:

Run chmod +x post-merge to make it executable then put it into .git/hooks/.

Then add at least an echo in it, to check if it is executed or not.

Note that the hook will only run after a successful merge, meaning if the pull result in a fast-forward merge (HEAD moves, no actual merge needed) the hook would not run.

An alternative would be to try and use setuid/setgid to force to process (Git, as root) to run as the right user (pre-requisite: chown to set the right user/group for those files)

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Thx for your answer. Yes the file is executable as I wrote earlier, I also added echo statements but nothing is printed. – Black Mar 07 '19 at 19:15
  • so you say if `git pull` returns `Already up-to-date` then the hook will not get called? Only if there will be new files? – Black Mar 08 '19 at 07:38
  • @Black Inly if there are new files *to be merged*. If the pull only add new commits on top of your commits (fast-forward merge), the hook would not need to run. – VonC Mar 08 '19 at 07:40
  • but thats also a use case where I need it to run, because my file owner and group are always overwritten with `root:root`. Isn't it possible? – Black Mar 08 '19 at 07:45
  • @Black Then you would need to define an alias that would chain the pull and the chown. – VonC Mar 08 '19 at 07:49
  • no this is not a good idea, since I have multiple projects on the same server which have other users. This would change their users to the one defined in my alias if I execute `git pull`. – Black Mar 08 '19 at 07:53
  • Maybe I have to edit the git source code to change the behaviour. But this would be really hacky. So I guess I can accept your solution. – Black Mar 08 '19 at 07:55
  • 1
    @Black Would a `setgid` be more practical? https://stackoverflow.com/a/29838913/6309: at least the group would be correct. – VonC Mar 08 '19 at 07:55
  • Thx for `setgid` interesting. But It won't solve my problem because the user is the problem. My NetBeans can't overwrite files with user root if I synch or upload, but if I do `git pull` then all changed files will be set to user root. Another solution would be to change to the user of the project with `su`, but this is not comfortable enough for me. – Black Mar 08 '19 at 07:58
  • @Black Maybe this can be appliued to uid as well: https://linuxconfig.org/how-to-use-special-permissions-the-setuid-setgid-and-sticky-bits – VonC Mar 08 '19 at 08:00