James Moger
2012-11-01 b701ed7c4e138c4aaa3acb029f6e35fdf01388e4
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
6bb3b2 61  *  receivePack        JGit Receive Pack            org.eclipse.jgit.transport.ReceivePack
a411c8 62  *  user            Gitblit User                com.gitblit.models.UserModel
JM 63  *  commands        JGit commands                 Collection<org.eclipse.jgit.transport.ReceiveCommand>
64  *    url                Base url for Gitblit        String
65  *  logger            Logs messages to Gitblit     org.slf4j.Logger
66  *  clientLogger    Logs messages to Git client    com.gitblit.utils.ClientLogger
7c1cdc 67  *
JM 68  * Accessing Gitblit Custom Fields:
69  *   def myCustomField = repository.customFields.myCustomField
a411c8 70  *  
JM 71  */
72
73 // Indicate we have started the script
74 logger.info("localclone hook triggered by ${user.username} for ${repository.name}")
75
76 def rootFolder = 'c:/test'
77 def bare = false
78 def cloneAllBranches = true
79 def cloneBranch = 'refs/heads/master'
80 def includeSubmodules = true
81
82 def repoName = repository.name
83 def destinationFolder = new File(rootFolder, StringUtils.stripDotGit(repoName))
84 def srcUrl = 'file://' + new File(GitBlit.getRepositoriesFolder(), repoName).absolutePath
85
86 // delete any previous clone
87 if (destinationFolder.exists()) {
88     FileUtils.delete(destinationFolder, FileUtils.RECURSIVE)
89 }
90
91 // clone the repository
92 logger.info("cloning ${srcUrl} to ${destinationFolder}")
93 CloneCommand cmd = Git.cloneRepository();
94 cmd.setBare(bare)
95 if (cloneAllBranches)
96     cmd.setCloneAllBranches(true)
97 else
98     cmd.setBranch(cloneBranch)
99 cmd.setCloneSubmodules(includeSubmodules)
100 cmd.setURI(srcUrl)
101 cmd.setDirectory(destinationFolder)
102 Git git = cmd.call();
103 git.repository.close()
104
105 // report clone operation success back to pushing Git client
106 clientLogger.info("${repoName} cloned to ${destinationFolder}")