Skip to content

Instantly share code, notes, and snippets.

@jeantil
Created March 8, 2012 11:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save jeantil/2000521 to your computer and use it in GitHub Desktop.
Save jeantil/2000521 to your computer and use it in GitHub Desktop.
gitblit hooks to sync with subgit
/*
* Copyright 2011 Jean Helou.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import com.gitblit.GitBlit
import com.gitblit.Keys
import com.gitblit.models.RepositoryModel
import com.gitblit.models.UserModel
import com.gitblit.utils.JGitUtils
import org.eclipse.jgit.lib.Repository
import org.eclipse.jgit.revwalk.RevCommit
import org.eclipse.jgit.transport.ReceiveCommand
import org.eclipse.jgit.transport.ReceiveCommand.Result
import org.slf4j.Logger
import java.io.File
/**
* Sample Gitblit Post-Receive Hook: jenkins
*
* The Post-Receive hook is executed AFTER the pushed commits have been applied
* to the Git repository. This is the appropriate point to trigger an
* integration build or to send a notification.
*
* This script is only executed when pushing to *Gitblit*, not to other Git
* tooling you may be using.
*
* If this script is specified in *groovy.postReceiveScripts* of gitblit.properties
* or web.xml then it will be executed by any repository when it receives a
* push. If you choose to share your script then you may have to consider
* tailoring control-flow based on repository access restrictions.
*
* Scripts may also be specified per-repository in the repository settings page.
* Shared scripts will be excluded from this list of available scripts.
*
* This script is dynamically reloaded and it is executed within it's own
* exception handler so it will not crash another script nor crash Gitblit.
*
* Bound Variables:
* gitblit Gitblit Server com.gitblit.GitBlit
* repository Gitblit Repository com.gitblit.models.RepositoryModel
* user Gitblit User com.gitblit.models.UserModel
* commands JGit commands Collection<org.eclipse.jgit.transport.ReceiveCommand>
* url Base url for Gitblit String
* logger Logger instance org.slf4j.Logger
*
*/
logger.info("subgit post-receive hook triggered by ${user.username} for ${repository.name} repository")
/* Bind env */
def env = System.getenv()
/* Extract repository path from jgit repo */
Repository repo = gitblit.getRepository(repository.name)
String repoPath=repo.directory.canonicalPath
logger.info("Subgit hook triggered on ${repo.directory.canonicalPath}")
/* Prepare subgit command */
String java_home= env['JAVA_HOME']
String java=java_home+File.separator+'bin'+File.separator+'java'
String classpath=${SUBGIT_CLASSPATH}
String main_class='org.tmatesoft.translator.SubGitHook'
def command = ["${java}","-noverify","-Xint","-cp","${classpath}","${main_class}","post-receive","\"${repoPath}\""] // Create the String
logger.info("executing :"+"${java} -noverify -Xint -cp ${classpath} ${main_class} pre-receive \"${repoPath}\"")
def proc = command.execute() // Call *execute* on the string
proc.waitFor() // Wait for the command to finish
logger.info("subgit stderr: ${proc.err.text}")
logger.info("subgit stdout: ${proc.in.text}")
logger.info("subgit return code : ${ proc.exitValue()}")
if(proc.exitValue()!=0) return false
return true
/*
* Copyright 2011 Jean Helou.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import com.gitblit.GitBlit
import com.gitblit.Keys
import com.gitblit.models.RepositoryModel
import com.gitblit.models.UserModel
import com.gitblit.utils.JGitUtils
import org.eclipse.jgit.lib.Repository
import org.eclipse.jgit.revwalk.RevCommit
import org.eclipse.jgit.transport.ReceiveCommand
import org.eclipse.jgit.transport.ReceiveCommand.Result
import org.slf4j.Logger
import java.io.File
/**
* Sample Gitblit Post-Receive Hook: jenkins
*
* The Post-Receive hook is executed AFTER the pushed commits have been applied
* to the Git repository. This is the appropriate point to trigger an
* integration build or to send a notification.
*
* This script is only executed when pushing to *Gitblit*, not to other Git
* tooling you may be using.
*
* If this script is specified in *groovy.postReceiveScripts* of gitblit.properties
* or web.xml then it will be executed by any repository when it receives a
* push. If you choose to share your script then you may have to consider
* tailoring control-flow based on repository access restrictions.
*
* Scripts may also be specified per-repository in the repository settings page.
* Shared scripts will be excluded from this list of available scripts.
*
* This script is dynamically reloaded and it is executed within it's own
* exception handler so it will not crash another script nor crash Gitblit.
*
* Bound Variables:
* gitblit Gitblit Server com.gitblit.GitBlit
* repository Gitblit Repository com.gitblit.models.RepositoryModel
* user Gitblit User com.gitblit.models.UserModel
* commands JGit commands Collection<org.eclipse.jgit.transport.ReceiveCommand>
* url Base url for Gitblit String
* logger Logger instance org.slf4j.Logger
*
*/
logger.info("subgit pre-receive hook triggered by ${user.username} for ${repository.name} repository")
/* Bind env */
def env = System.getenv()
/* Extract repository path from jgit repo */
Repository repo = gitblit.getRepository(repository.name)
String repoPath=repo.directory.canonicalPath
logger.info("Subgit hook triggered on ${repo.directory.canonicalPath}")
/* Prepare subgit command */
String java_home= env['JAVA_HOME']
String java=java_home+File.separator+'bin'+File.separator+'java'
String classpath=${SUBGIT_CLASSPATH}
String main_class='org.tmatesoft.translator.SubGitHook'
def command = ["${java}","-noverify","-Xint","-cp","${classpath}","${main_class}","pre-receive","\"${repoPath}\""] // Create the String
logger.info("executing :"+"${java} -noverify -Xint -cp ${classpath} ${main_class} pre-receive \"${repoPath}\"")
def proc = command.execute() // Call *execute* on the string
proc.waitFor() // Wait for the command to finish
logger.info("subgit stderr: ${proc.err.text}")
logger.info("subgit stdout: ${proc.in.text}")
logger.info("subgit return code : ${ proc.exitValue()}")
if(proc.exitValue()!=0) return false
return true
@jalbersdorfer
Copy link

Hi Jean, have these hooks ever worked for you?
I have several issues with it.

  • ${SUBGIT_CLASSPATH} seems to be undefined -> Script crashes. (I defined statically as the subgit scripts do)
  • then the hook executes, but gets stuck in the command.execute() in the pre-commit hook.
    It seems to wait for some input from the stdin there.
    Any ideas? - the original subgit pre-commit script echos some HOOK_INPUT into that java call.

HOOK_INPUT=$(cat)
echo "$HOOK_INPUT" | "$JAVA_EXE" $JAVA_OPTIONS -cp "$TS_CLASSPATH" $MAIN_CLASS pre-receive "$GIT_REPOS" $ARGUMENTS

Can you help me with this? thanks.

@vs
Copy link

vs commented Mar 27, 2013

Hi @jalbersdorfer,

AFAIK, Jean has implemented these hooks for some of early non-stable SubGit builds. A lot of changes were introduced into SubGit and its hooks since then.

Please submit an issue to our tracker with the full description of your problem: what version of SubGit you're trying to use with GitBlit, what's the sync mode (remote or local mirror), etc:

http://issues.tmatesoft.com/issues/SGT

Thanks!

@vs
Copy link

vs commented Feb 12, 2014

GitBlit pre-receive hook for SubGit 2.0.x: https://gist.github.com/vs/8960974

@jpbaudoin
Copy link

Hello, Can we use GitBlit with the pre-receive hook from the previos post or is the post hook still necesary?

Regards

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