Skip to content

Instantly share code, notes, and snippets.

@Dierk
Created February 9, 2012 23:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Dierk/1784418 to your computer and use it in GitHub Desktop.
Save Dierk/1784418 to your computer and use it in GitHub Desktop.
a pre-receive hook to allow both: feature branches and agile CI
// a git pre-receive hook
// that automatically merges all pushes to feature branches
// into a dedicated continuous-integration (ci) branch.
// Since we cannot merge in a bare repo, we work on a temporary clone.
// Dierk Koenig
def ciBranch = 'master'
def mergeName = 'merge'
def hereDirPath = new File('.').canonicalPath
def tempDir = new File('../temp')
def mergeDir = new File(tempDir, mergeName)
System.in.eachLine { line ->
def (parent, commit, ref) = line.split()
def branch = ref.split('/').last()
// pushes to the ciBranch are not merged - cut endless loop
if (branch == ciBranch) System.exit(0)
println "branch $branch was pushed: auto-merge into $ciBranch"
cmd "rm -rf $tempDir.canonicalPath"
tempDir.mkdirs()
cmd "git clone -b $ciBranch $hereDirPath $mergeName", tempDir
cmd "git merge $commit", mergeDir
cmd "git push origin $ciBranch", mergeDir
println "done"
}
void cmd(String command, File dir = null) {
Process proc = command.execute([], dir)
if (dir) println "in dir: $dir.canonicalPath"
println command
proc.waitFor()
def out = proc.in.text; if (out) println out
def err = proc.err.text; if (err) println "! $err"
if (proc.exitValue()) System.exit(1)
}
@Dierk
Copy link
Author

Dierk commented Feb 10, 2012

a) there are a lot of scripts where an additional jvm startup time (~ 1 second on my machine) is not relevant. The above is one example.
b) groovyserv eliminates even this additional second and has always worked reliably for me.

@gjoseph
Copy link

gjoseph commented Feb 14, 2012

Yeah, I thought a pre-receive would block the client which is pushing. Now if you tell me you're using groovyserv for your hooks… sounds like I'm gonna have to convert my hooks ! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment