You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

localclone.groovy 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  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. * Sample Gitblit Post-Receive Hook: localclone
  35. *
  36. * The Post-Receive hook is executed AFTER the pushed commits have been applied
  37. * to the Git repository. This is the appropriate point to trigger an
  38. * integration build or to send a notification.
  39. *
  40. * This script is only executed when pushing to *Gitblit*, not to other Git
  41. * tooling you may be using.
  42. *
  43. * If this script is specified in *groovy.postReceiveScripts* of gitblit.properties
  44. * or web.xml then it will be executed by any repository when it receives a
  45. * push. If you choose to share your script then you may have to consider
  46. * tailoring control-flow based on repository access restrictions.
  47. *
  48. * Scripts may also be specified per-repository in the repository settings page.
  49. * Shared scripts will be excluded from this list of available scripts.
  50. *
  51. * This script is dynamically reloaded and it is executed within it's own
  52. * exception handler so it will not crash another script nor crash Gitblit.
  53. *
  54. * If you want this hook script to fail and abort all subsequent scripts in the
  55. * chain, "return false" at the appropriate failure points.
  56. *
  57. * Bound Variables:
  58. * gitblit Gitblit Server com.gitblit.GitBlit
  59. * repository Gitblit Repository com.gitblit.models.RepositoryModel
  60. * receivePack JGit Receive Pack org.eclipse.jgit.transport.ReceivePack
  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
  66. *
  67. * Accessing Gitblit Custom Fields:
  68. * def myCustomField = repository.customFields.myCustomField
  69. *
  70. */
  71. // Indicate we have started the script
  72. logger.info("localclone hook triggered by ${user.username} for ${repository.name}")
  73. def rootFolder = 'c:/test'
  74. def bare = false
  75. def cloneAllBranches = true
  76. def cloneBranch = 'refs/heads/master'
  77. def includeSubmodules = true
  78. def repoName = repository.name
  79. def destinationFolder = new File(rootFolder, StringUtils.stripDotGit(repoName))
  80. def srcUrl = 'file://' + new File(GitBlit.getRepositoriesFolder(), repoName).absolutePath
  81. // delete any previous clone
  82. if (destinationFolder.exists()) {
  83. FileUtils.delete(destinationFolder, FileUtils.RECURSIVE)
  84. }
  85. // clone the repository
  86. logger.info("cloning ${srcUrl} to ${destinationFolder}")
  87. CloneCommand cmd = Git.cloneRepository();
  88. cmd.setBare(bare)
  89. if (cloneAllBranches)
  90. cmd.setCloneAllBranches(true)
  91. else
  92. cmd.setBranch(cloneBranch)
  93. cmd.setCloneSubmodules(includeSubmodules)
  94. cmd.setURI(srcUrl)
  95. cmd.setDirectory(destinationFolder)
  96. Git git = cmd.call();
  97. git.repository.close()
  98. // report clone operation success back to pushing Git client
  99. clientLogger.info("${repoName} cloned to ${destinationFolder}")