3

I'm working on a rather interesting project, attempting to do the following:

Whenever a developer pushes a commit to a remote branch, execute a githook that will lookup its associated QA branch via an issue tracking system, then merge that commit into the QA branch.

It's important that the commit to the remote branch (mirroring the developer's local branch) be rejected if there are any conflicts on the QA branch.

The problem I'm running into is that git-merge (and pull, checkout, etc.) must be run in a work tree, which causes the githook to fail.

Is there a (relatively) simple way to merge one branch (or commit) into another via an update githook? My reasoning being that the remote, while not being a working tree, obviously has all of the data it needs to do this, just not the command (as far as I've been able to find).

If there isn't a straightforward command, my next thought is to create a temporary clone of the remote in which I can perform the merge operation:

(psuedocode)
mkdir $GIT_DIR/tempRepo
cd $GIT_DIR/tempRepo
git clone -b $QA_BRANCH --local $PARENT_DIR $CURRENT_DIR
git merge $WHATEVER
#cleanup

If any part of this fails, I reject the update (hopefully with some useful info to the developer so he/she can fix it). I've played with this solution a bit and while it appears to work fine via my script, it fails when running on a real remote (I don't know why yet, but while typing this, this answer popped up that may help - getting "fatal: not a git repository: '.'" when using post-update hook to execute 'git pull' on another repo)

I'd love any insight from the git gurus around here, I'm still fairly new to the plumbing that underpins this whole system, so if I'm on the right (or wrong) track, or if there are some magical commands or gotchas I should know about, I'd appreciate it.

Thanks in advance!

Community
  • 1
  • 1
Matthew W
  • 197
  • 1
  • 8

1 Answers1

0

The update hook is a server-side one, which means it is not intended to work in a working tree, since you generally push to a bare repository.

Your scenario is much more suited to a CI tool, like for instance Jenkins (or any others, TeamCity, Travis-CI, CICircle, etc...)

You can setup a Jenkins job to detect any new commit on branches, as described in this answer.
A Jenkins job (with the JENKINS Git Plugin) will clone the repo and make a working tree, and then can do any operation that you want (and have scripted), including a merge.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250