James Moger
2012-06-06 94dcbd617f3d06ca294d5d151390698e4bddd2cc
commit | author | age
a411c8 1 /*
JM 2  * Copyright 2012 gitblit.com.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 import com.gitblit.GitBlit
17 import com.gitblit.Keys
18 import com.gitblit.models.RepositoryModel
19 import com.gitblit.models.TeamModel
20 import com.gitblit.models.UserModel
21 import com.gitblit.utils.JGitUtils
22 import com.gitblit.utils.StringUtils
23 import java.text.SimpleDateFormat
24 import org.eclipse.jgit.api.CloneCommand
25 import org.eclipse.jgit.api.Git
26 import org.eclipse.jgit.lib.Repository
27 import org.eclipse.jgit.lib.Config
28 import org.eclipse.jgit.revwalk.RevCommit
29 import org.eclipse.jgit.transport.ReceiveCommand
30 import org.eclipse.jgit.transport.ReceiveCommand.Result
31 import org.eclipse.jgit.util.FileUtils
32 import org.slf4j.Logger
33
34 /**
35  * Sample Gitblit Post-Receive Hook: localclone
36  *
37  * The Post-Receive hook is executed AFTER the pushed commits have been applied
38  * to the Git repository.  This is the appropriate point to trigger an
39  * integration build or to send a notification.
40  * 
41  * This script is only executed when pushing to *Gitblit*, not to other Git
42  * tooling you may be using.
43  * 
44  * If this script is specified in *groovy.postReceiveScripts* of gitblit.properties
45  * or web.xml then it will be executed by any repository when it receives a
46  * push.  If you choose to share your script then you may have to consider
47  * tailoring control-flow based on repository access restrictions.
48  *
49  * Scripts may also be specified per-repository in the repository settings page.
50  * Shared scripts will be excluded from this list of available scripts.
51  * 
52  * This script is dynamically reloaded and it is executed within it's own
53  * exception handler so it will not crash another script nor crash Gitblit.
54  *
55  * If you want this hook script to fail and abort all subsequent scripts in the
56  * chain, "return false" at the appropriate failure points.
57  * 
58  * Bound Variables:
59  *  gitblit            Gitblit Server                 com.gitblit.GitBlit
60  *  repository        Gitblit Repository            com.gitblit.models.RepositoryModel
61  *  user            Gitblit User                com.gitblit.models.UserModel
62  *  commands        JGit commands                 Collection<org.eclipse.jgit.transport.ReceiveCommand>
63  *    url                Base url for Gitblit        String
64  *  logger            Logs messages to Gitblit     org.slf4j.Logger
65  *  clientLogger    Logs messages to Git client    com.gitblit.utils.ClientLogger
7c1cdc 66  *
JM 67  * Accessing Gitblit Custom Fields:
68  *   def myCustomField = repository.customFields.myCustomField
a411c8 69  *  
JM 70  */
71
72 // Indicate we have started the script
73 logger.info("localclone hook triggered by ${user.username} for ${repository.name}")
74
75 def rootFolder = 'c:/test'
76 def bare = false
77 def cloneAllBranches = true
78 def cloneBranch = 'refs/heads/master'
79 def includeSubmodules = true
80
81 def repoName = repository.name
82 def destinationFolder = new File(rootFolder, StringUtils.stripDotGit(repoName))
83 def srcUrl = 'file://' + new File(GitBlit.getRepositoriesFolder(), repoName).absolutePath
84
85 // delete any previous clone
86 if (destinationFolder.exists()) {
87     FileUtils.delete(destinationFolder, FileUtils.RECURSIVE)
88 }
89
90 // clone the repository
91 logger.info("cloning ${srcUrl} to ${destinationFolder}")
92 CloneCommand cmd = Git.cloneRepository();
93 cmd.setBare(bare)
94 if (cloneAllBranches)
95     cmd.setCloneAllBranches(true)
96 else
97     cmd.setBranch(cloneBranch)
98 cmd.setCloneSubmodules(includeSubmodules)
99 cmd.setURI(srcUrl)
100 cmd.setDirectory(destinationFolder)
101 Git git = cmd.call();
102 git.repository.close()
103
104 // report clone operation success back to pushing Git client
105 clientLogger.info("${repoName} cloned to ${destinationFolder}")