]> source.dussan.org Git - gitblit.git/commitdiff
Example post-receive hook script for creating a local clone on push
authorJames Moger <james.moger@gitblit.com>
Wed, 11 Apr 2012 12:29:14 +0000 (08:29 -0400)
committerJames Moger <james.moger@gitblit.com>
Wed, 11 Apr 2012 12:29:14 +0000 (08:29 -0400)
build.xml
groovy/localclone.groovy [new file with mode: 0644]

index b57a0336e770770818cffa5db5a6b6fc0b81de2c..0dd19c5e36a47de9142121d23f959e9e24ac8b0b 100644 (file)
--- a/build.xml
+++ b/build.xml
                                <include name="sendmail.groovy" />\r
                                <include name="jenkins.groovy" />\r
                                <include name="protect-refs.groovy" />\r
+                               <include name="localclone.groovy" />\r
                        </fileset>\r
                </copy>\r
        \r
                                <include name="sendmail.groovy" />\r
                                <include name="jenkins.groovy" />\r
                                <include name="protect-refs.groovy" />\r
+                               <include name="localclone.groovy" />\r
                        </fileset>\r
                </copy>\r
 \r
diff --git a/groovy/localclone.groovy b/groovy/localclone.groovy
new file mode 100644 (file)
index 0000000..e1d4493
--- /dev/null
@@ -0,0 +1,102 @@
+/*\r
+ * Copyright 2012 gitblit.com.\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ *     http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
+import com.gitblit.GitBlit\r
+import com.gitblit.Keys\r
+import com.gitblit.models.RepositoryModel\r
+import com.gitblit.models.TeamModel\r
+import com.gitblit.models.UserModel\r
+import com.gitblit.utils.JGitUtils\r
+import com.gitblit.utils.StringUtils\r
+import java.text.SimpleDateFormat\r
+import org.eclipse.jgit.api.CloneCommand\r
+import org.eclipse.jgit.api.Git\r
+import org.eclipse.jgit.lib.Repository\r
+import org.eclipse.jgit.lib.Config\r
+import org.eclipse.jgit.revwalk.RevCommit\r
+import org.eclipse.jgit.transport.ReceiveCommand\r
+import org.eclipse.jgit.transport.ReceiveCommand.Result\r
+import org.eclipse.jgit.util.FileUtils\r
+import org.slf4j.Logger\r
+\r
+/**\r
+ * Sample Gitblit Post-Receive Hook: localclone\r
+ *\r
+ * The Post-Receive hook is executed AFTER the pushed commits have been applied\r
+ * to the Git repository.  This is the appropriate point to trigger an\r
+ * integration build or to send a notification.\r
+ * \r
+ * This script is only executed when pushing to *Gitblit*, not to other Git\r
+ * tooling you may be using.\r
+ * \r
+ * If this script is specified in *groovy.postReceiveScripts* of gitblit.properties\r
+ * or web.xml then it will be executed by any repository when it receives a\r
+ * push.  If you choose to share your script then you may have to consider\r
+ * tailoring control-flow based on repository access restrictions.\r
+ *\r
+ * Scripts may also be specified per-repository in the repository settings page.\r
+ * Shared scripts will be excluded from this list of available scripts.\r
+ * \r
+ * This script is dynamically reloaded and it is executed within it's own\r
+ * exception handler so it will not crash another script nor crash Gitblit.\r
+ *\r
+ * If you want this hook script to fail and abort all subsequent scripts in the\r
+ * chain, "return false" at the appropriate failure points.\r
+ * \r
+ * Bound Variables:\r
+ *  gitblit                    Gitblit Server                          com.gitblit.GitBlit\r
+ *  repository         Gitblit Repository                      com.gitblit.models.RepositoryModel\r
+ *  user                       Gitblit User                            com.gitblit.models.UserModel\r
+ *  commands           JGit commands                           Collection<org.eclipse.jgit.transport.ReceiveCommand>\r
+ *     url                             Base url for Gitblit            String\r
+ *  logger                     Logs messages to Gitblit        org.slf4j.Logger\r
+ *  clientLogger       Logs messages to Git client     com.gitblit.utils.ClientLogger\r
+ *  \r
+ */\r
+\r
+// Indicate we have started the script\r
+logger.info("localclone hook triggered by ${user.username} for ${repository.name}")\r
+\r
+def rootFolder = 'c:/test'\r
+def bare = false\r
+def cloneAllBranches = true\r
+def cloneBranch = 'refs/heads/master'\r
+def includeSubmodules = true\r
+\r
+def repoName = repository.name\r
+def destinationFolder = new File(rootFolder, StringUtils.stripDotGit(repoName))\r
+def srcUrl = 'file://' + new File(GitBlit.getRepositoriesFolder(), repoName).absolutePath\r
+\r
+// delete any previous clone\r
+if (destinationFolder.exists()) {\r
+       FileUtils.delete(destinationFolder, FileUtils.RECURSIVE)\r
+}\r
+\r
+// clone the repository\r
+logger.info("cloning ${srcUrl} to ${destinationFolder}")\r
+CloneCommand cmd = Git.cloneRepository();\r
+cmd.setBare(bare)\r
+if (cloneAllBranches)\r
+       cmd.setCloneAllBranches(true)\r
+else\r
+       cmd.setBranch(cloneBranch)\r
+cmd.setCloneSubmodules(includeSubmodules)\r
+cmd.setURI(srcUrl)\r
+cmd.setDirectory(destinationFolder)\r
+Git git = cmd.call();\r
+git.repository.close()\r
+\r
+// report clone operation success back to pushing Git client\r
+clientLogger.info("${repoName} cloned to ${destinationFolder}")
\ No newline at end of file