Created
March 8, 2012 11:16
-
-
Save jeantil/2000521 to your computer and use it in GitHub Desktop.
gitblit hooks to sync with subgit
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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 |
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!
GitBlit pre-receive hook for SubGit 2.0.x: https://gist.github.com/vs/8960974
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
Hi Jean, have these hooks ever worked for you?
I have several issues with it.
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.