PageRenderTime 48ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/jgit-flow-core/src/main/java/com/atlassian/jgitflow/core/util/RequirementHelper.java

https://bitbucket.org/lukejackson/jgit-flow
Java | 244 lines | 148 code | 21 blank | 75 comment | 13 complexity | c381f202432c461b32b09b2589288390 MD5 | raw file
Possible License(s): Apache-2.0
  1. package com.atlassian.jgitflow.core.util;
  2. import java.util.List;
  3. import com.atlassian.jgitflow.core.GitFlowConfiguration;
  4. import com.atlassian.jgitflow.core.JGitFlowConstants;
  5. import com.atlassian.jgitflow.core.JGitFlowReporter;
  6. import com.atlassian.jgitflow.core.exception.*;
  7. import org.eclipse.jgit.api.CreateBranchCommand;
  8. import org.eclipse.jgit.api.Git;
  9. import org.eclipse.jgit.api.errors.GitAPIException;
  10. import org.eclipse.jgit.lib.Constants;
  11. import org.eclipse.jgit.lib.Ref;
  12. import org.eclipse.jgit.revwalk.RevCommit;
  13. public class RequirementHelper
  14. {
  15. protected final Git git;
  16. protected final GitFlowConfiguration gfConfig;
  17. protected final JGitFlowReporter reporter;
  18. protected final String commandName;
  19. public RequirementHelper(Git git, GitFlowConfiguration gfConfig, JGitFlowReporter reporter, String commandName)
  20. {
  21. this.git = git;
  22. this.gfConfig = gfConfig;
  23. this.reporter = reporter;
  24. this.commandName = commandName;
  25. }
  26. /**
  27. * Requires that git flow has been initialized for the project represented by the internal {Git} instance
  28. *
  29. * @throws com.atlassian.jgitflow.core.exception.NotInitializedException
  30. * @throws com.atlassian.jgitflow.core.exception.JGitFlowGitAPIException
  31. */
  32. public void requireGitFlowInitialized() throws NotInitializedException, JGitFlowGitAPIException
  33. {
  34. if (!gfConfig.gitFlowIsInitialized())
  35. {
  36. reporter.errorText(commandName, "requireGitFlowInitialized() failed");
  37. reporter.flush();
  38. throw new NotInitializedException("Git flow is not initialized in " + git.getRepository().getWorkTree().getPath());
  39. }
  40. }
  41. /**
  42. * Requires that a local branch with the given name does not yet exist
  43. *
  44. * @param branch the name of the branch to test
  45. * @throws com.atlassian.jgitflow.core.exception.LocalBranchExistsException
  46. * @throws com.atlassian.jgitflow.core.exception.JGitFlowGitAPIException
  47. */
  48. public void requireLocalBranchAbsent(String branch) throws LocalBranchExistsException, JGitFlowGitAPIException
  49. {
  50. if (GitHelper.localBranchExists(git, branch))
  51. {
  52. reporter.errorText(commandName, "requireLocalBranchAbsent() failed: '" + branch + "' already exists");
  53. reporter.flush();
  54. throw new LocalBranchExistsException("local branch '" + branch + "' already exists");
  55. }
  56. }
  57. /**
  58. * Requires that a local branch with the given name exists
  59. *
  60. * @param branch The name of the branch to test
  61. * @throws com.atlassian.jgitflow.core.exception.LocalBranchMissingException
  62. * @throws com.atlassian.jgitflow.core.exception.JGitFlowGitAPIException
  63. */
  64. public void requireLocalBranchExists(String branch) throws LocalBranchMissingException, JGitFlowGitAPIException
  65. {
  66. if (!GitHelper.localBranchExists(git, branch) && GitHelper.remoteBranchExists(git, branch, reporter))
  67. {
  68. try
  69. {
  70. git.checkout()
  71. .setName(branch)
  72. .setCreateBranch(true)
  73. .setUpstreamMode(CreateBranchCommand.SetupUpstreamMode.TRACK)
  74. .setStartPoint(Constants.DEFAULT_REMOTE_NAME + "/" + branch)
  75. .call();
  76. }
  77. catch (GitAPIException e)
  78. {
  79. throw new JGitFlowGitAPIException("error checking out remote branch.", e);
  80. }
  81. }
  82. if (!GitHelper.localBranchExists(git, branch))
  83. {
  84. reporter.errorText(commandName, "localBranchExists() failed: '" + branch + "' does not exist");
  85. reporter.flush();
  86. throw new LocalBranchMissingException("local branch " + branch + " does not exist");
  87. }
  88. }
  89. /**
  90. * Requires that a remote branch with the given name does not yet exist
  91. *
  92. * @param branch The name of the branch to test
  93. * @throws com.atlassian.jgitflow.core.exception.RemoteBranchExistsException
  94. * @throws com.atlassian.jgitflow.core.exception.JGitFlowGitAPIException
  95. */
  96. public void requireRemoteBranchAbsent(String branch) throws RemoteBranchExistsException, JGitFlowGitAPIException
  97. {
  98. if (GitHelper.remoteBranchExists(git, branch, reporter))
  99. {
  100. reporter.errorText(commandName, "requireRemoteBranchAbsent() failed: '" + branch + "' already exists");
  101. reporter.flush();
  102. throw new RemoteBranchExistsException("remote branch '" + branch + "' already exists");
  103. }
  104. }
  105. /**
  106. * Requires that a remote branch with the given name exists
  107. *
  108. * @param branch The name of the branch to test
  109. * @throws com.atlassian.jgitflow.core.exception.RemoteBranchMissingException
  110. * @throws com.atlassian.jgitflow.core.exception.JGitFlowGitAPIException
  111. */
  112. public void requireRemoteBranchExists(String branch) throws RemoteBranchMissingException, JGitFlowGitAPIException
  113. {
  114. if (!GitHelper.remoteBranchExists(git, branch, reporter))
  115. {
  116. reporter.errorText(commandName, "requireRemoteBranchExists() failed: '" + branch + "' does not exist");
  117. reporter.flush();
  118. throw new RemoteBranchMissingException("remote branch " + branch + " does not exist");
  119. }
  120. }
  121. /**
  122. * Requires that a tag with the given name does not yet exist
  123. *
  124. * @param name The name of the tag to test
  125. * @throws com.atlassian.jgitflow.core.exception.TagExistsException
  126. * @throws com.atlassian.jgitflow.core.exception.JGitFlowGitAPIException
  127. */
  128. public void requireTagAbsent(String name) throws TagExistsException, JGitFlowGitAPIException
  129. {
  130. if (GitHelper.tagExists(git, name))
  131. {
  132. reporter.errorText(commandName, "requireTagAbsent() failed: '" + name + "' already exists");
  133. reporter.flush();
  134. throw new TagExistsException("tag '" + name + "' already exists");
  135. }
  136. }
  137. /**
  138. * Requires that the local branch with the given name is not behind a remote brach with the same name
  139. *
  140. * @param branch The name of the branch to test
  141. * @throws com.atlassian.jgitflow.core.exception.BranchOutOfDateException
  142. * @throws com.atlassian.jgitflow.core.exception.JGitFlowIOException
  143. */
  144. public void requireLocalBranchNotBehindRemote(String branch) throws BranchOutOfDateException, JGitFlowIOException
  145. {
  146. reporter.debugMethod(commandName, "requireLocalBranchNotBehindRemote");
  147. boolean behind = GitHelper.localBranchBehindRemote(git, branch, reporter);
  148. if (behind)
  149. {
  150. reporter.errorText(commandName, "local branch '" + branch + "' is behind the remote branch");
  151. reporter.endMethod();
  152. reporter.flush();
  153. throw new BranchOutOfDateException("local branch '" + branch + "' is behind the remote branch");
  154. }
  155. reporter.endMethod();
  156. }
  157. /**
  158. * Requires that the local working tree has no un-committed changes
  159. *
  160. * @throws com.atlassian.jgitflow.core.exception.DirtyWorkingTreeException
  161. * @throws com.atlassian.jgitflow.core.exception.JGitFlowIOException
  162. * @throws com.atlassian.jgitflow.core.exception.JGitFlowGitAPIException
  163. */
  164. public void requireCleanWorkingTree(boolean allowUntracked) throws DirtyWorkingTreeException, JGitFlowIOException, JGitFlowGitAPIException
  165. {
  166. CleanStatus cs = GitHelper.workingTreeIsClean(git, allowUntracked, reporter);
  167. if (cs.isNotClean())
  168. {
  169. reporter.errorText(commandName, cs.getMessage());
  170. reporter.flush();
  171. throw new DirtyWorkingTreeException(cs.getMessage());
  172. }
  173. }
  174. /**
  175. * Requires that no release branches already exist
  176. *
  177. * @throws com.atlassian.jgitflow.core.exception.ReleaseBranchExistsException
  178. * @throws com.atlassian.jgitflow.core.exception.JGitFlowGitAPIException
  179. */
  180. public void requireNoExistingReleaseBranches() throws ReleaseBranchExistsException, JGitFlowGitAPIException
  181. {
  182. List<Ref> branches = GitHelper.listBranchesWithPrefix(git, gfConfig.getPrefixValue(JGitFlowConstants.PREFIXES.RELEASE.configKey()), reporter);
  183. if (!branches.isEmpty())
  184. {
  185. reporter.errorText(commandName, "a release branch [" + branches.get(0).getName() + "] already exists. Finish that first!");
  186. reporter.flush();
  187. throw new ReleaseBranchExistsException("a release branch [" + branches.get(0).getName() + "] already exists. Finish that first!");
  188. }
  189. }
  190. /**
  191. * Requires that no hotfix branches already exist
  192. *
  193. * @throws com.atlassian.jgitflow.core.exception.HotfixBranchExistsException
  194. * @throws com.atlassian.jgitflow.core.exception.JGitFlowGitAPIException
  195. */
  196. public void requireNoExistingHotfixBranches() throws HotfixBranchExistsException, JGitFlowGitAPIException
  197. {
  198. List<Ref> branches = GitHelper.listBranchesWithPrefix(git, gfConfig.getPrefixValue(JGitFlowConstants.PREFIXES.HOTFIX.configKey()), reporter);
  199. if (!branches.isEmpty())
  200. {
  201. reporter.errorText(commandName, "a hotfix branch [" + branches.get(0).getName() + "] already exists. Finish that first!");
  202. reporter.flush();
  203. throw new HotfixBranchExistsException("a hotfix branch [" + branches.get(0).getName() + "] already exists. Finish that first!");
  204. }
  205. }
  206. /**
  207. * Requires that a local branch contains the given commit
  208. *
  209. * @param commit the commit to test
  210. * @param branch the name of the branch to check
  211. * @throws com.atlassian.jgitflow.core.exception.LocalBranchMissingException
  212. * @throws com.atlassian.jgitflow.core.exception.JGitFlowGitAPIException
  213. */
  214. public void requireCommitOnBranch(RevCommit commit, String branch) throws LocalBranchExistsException, JGitFlowGitAPIException, JGitFlowIOException
  215. {
  216. if (!GitHelper.isMergedInto(git, commit, branch))
  217. {
  218. reporter.errorText(commandName, "requireCommitOnBranch() failed: '" + commit.getName() + "' is not on " + branch);
  219. reporter.flush();
  220. throw new LocalBranchExistsException("commit '" + commit.getName() + "' does not exist on " + branch);
  221. }
  222. }
  223. }