Skip to content

Instantly share code, notes, and snippets.

@jeantil
Created March 8, 2012 11:16
Show Gist options
  • 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
@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