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

https://bitbucket.org/ronsmits/jgit-flow · Java · 371 lines · 177 code · 36 blank · 158 comment · 4 complexity · cb3798f5b42466dcb96c6e6ca85a0aba MD5 · raw file

  1. package com.atlassian.jgitflow.core;
  2. import java.io.File;
  3. import java.io.IOException;
  4. import com.atlassian.jgitflow.core.exception.AlreadyInitializedException;
  5. import com.atlassian.jgitflow.core.exception.JGitFlowGitAPIException;
  6. import com.atlassian.jgitflow.core.exception.JGitFlowIOException;
  7. import com.atlassian.jgitflow.core.exception.SameBranchException;
  8. import org.eclipse.jgit.api.Git;
  9. import org.eclipse.jgit.lib.RepositoryBuilder;
  10. /**
  11. * Offers a Git Flow API to interact with a git repository.
  12. * <p>
  13. * This class only offers methods to construct so-called command classes. Each
  14. * command is represented by one command class.<br>
  15. * Example: this class offers a {@code featureStart} method returning an instance of
  16. * the {@code FeatureStartCommand} class. The {@code FeatureStartCommand} class has setters
  17. * for all the arguments and options. The {@code FeatureStartCommand} class also has a
  18. * {@code call} method to actually execute the command.
  19. * <p>
  20. * All mandatory parameters for commands have to be specified in the methods of
  21. * this class, the optional parameters have to be specified by the
  22. * setter-methods of the Command class.
  23. */
  24. public class JGitFlow
  25. {
  26. private Git git;
  27. private GitFlowConfiguration gfConfig;
  28. private JGitFlow()
  29. {
  30. }
  31. JGitFlow(Git git, GitFlowConfiguration gfConfig)
  32. {
  33. this.git = git;
  34. this.gfConfig = gfConfig;
  35. }
  36. /**
  37. * Initializes a project for use with git flow and returns a JGitFlow instance.
  38. * <p>
  39. * This method will throw exceptions if the project has already been initialized
  40. * </p>
  41. * @param projectDir
  42. * @return
  43. * @throws JGitFlowIOException
  44. * @throws AlreadyInitializedException
  45. * @throws SameBranchException
  46. * @throws JGitFlowGitAPIException
  47. */
  48. public static JGitFlow init(File projectDir) throws JGitFlowIOException, AlreadyInitializedException, SameBranchException, JGitFlowGitAPIException
  49. {
  50. JGitFlowInitCommand initCommand = new JGitFlowInitCommand();
  51. return initCommand.setDirectory(projectDir).call();
  52. }
  53. /**
  54. * Initializes a project for use with git flow using a custom context and returns a JGitFlow instance.
  55. * <p>
  56. * This method will throw exceptions if the project has already been initialized
  57. * </p>
  58. * @param projectDir
  59. * @param context
  60. * @return
  61. * @throws JGitFlowIOException
  62. * @throws AlreadyInitializedException
  63. * @throws SameBranchException
  64. * @throws JGitFlowGitAPIException
  65. */
  66. public static JGitFlow init(File projectDir, InitContext context) throws JGitFlowIOException, AlreadyInitializedException, SameBranchException, JGitFlowGitAPIException
  67. {
  68. JGitFlowInitCommand initCommand = new JGitFlowInitCommand();
  69. return initCommand.setDirectory(projectDir).setInitContext(context).call();
  70. }
  71. /**
  72. * Initializes a project for use with git flow using a default context overriding any existing configuration.
  73. * @param projectDir
  74. * @return
  75. * @throws JGitFlowIOException
  76. * @throws AlreadyInitializedException
  77. * @throws SameBranchException
  78. * @throws JGitFlowGitAPIException
  79. */
  80. public static JGitFlow forceInit(File projectDir) throws JGitFlowIOException, AlreadyInitializedException, SameBranchException, JGitFlowGitAPIException
  81. {
  82. JGitFlowInitCommand initCommand = new JGitFlowInitCommand();
  83. return initCommand.setDirectory(projectDir).setForce(true).call();
  84. }
  85. /**
  86. * Initializes a project for use with git flow using a custom context overriding any existing configuration.
  87. * @param projectDir
  88. * @param context
  89. * @return
  90. * @throws JGitFlowIOException
  91. * @throws AlreadyInitializedException
  92. * @throws SameBranchException
  93. * @throws JGitFlowGitAPIException
  94. */
  95. public static JGitFlow forceInit(File projectDir, InitContext context) throws JGitFlowIOException, AlreadyInitializedException, SameBranchException, JGitFlowGitAPIException
  96. {
  97. JGitFlowInitCommand initCommand = new JGitFlowInitCommand();
  98. return initCommand.setDirectory(projectDir).setForce(true).setInitContext(context).call();
  99. }
  100. /**
  101. * Gets an existing git flow project and returns a JGitFlow instance
  102. * @param projectDir
  103. * @return
  104. * @throws JGitFlowIOException
  105. */
  106. public static JGitFlow get(File projectDir) throws JGitFlowIOException
  107. {
  108. try
  109. {
  110. RepositoryBuilder rb = new RepositoryBuilder()
  111. .readEnvironment()
  112. .findGitDir(projectDir);
  113. File gitDir = rb.getGitDir();
  114. Git gitRepo = Git.open(gitDir);
  115. GitFlowConfiguration gfConfig = new GitFlowConfiguration(gitRepo);
  116. return new JGitFlow(gitRepo, gfConfig);
  117. }
  118. catch (IOException e)
  119. {
  120. throw new JGitFlowIOException(e);
  121. }
  122. }
  123. /**
  124. * Initializes a project for use with git flow or gets an existing project and returns a JGitFlow instance.
  125. * @param projectDir
  126. * @return
  127. * @throws JGitFlowIOException
  128. * @throws AlreadyInitializedException
  129. * @throws SameBranchException
  130. * @throws JGitFlowGitAPIException
  131. */
  132. public static JGitFlow getOrInit(File projectDir) throws JGitFlowIOException, AlreadyInitializedException, SameBranchException, JGitFlowGitAPIException
  133. {
  134. if (isInitialized(projectDir))
  135. {
  136. return get(projectDir);
  137. }
  138. else
  139. {
  140. return init(projectDir);
  141. }
  142. }
  143. /**
  144. * Initializes a project for use with git flow using a custom context or gets an existing project and returns a JGitFlow instance.
  145. * @param projectDir
  146. * @param ctx
  147. * @return
  148. * @throws JGitFlowIOException
  149. * @throws AlreadyInitializedException
  150. * @throws SameBranchException
  151. * @throws JGitFlowGitAPIException
  152. */
  153. public static JGitFlow getOrInit(File projectDir, InitContext ctx) throws JGitFlowIOException, AlreadyInitializedException, SameBranchException, JGitFlowGitAPIException
  154. {
  155. if (isInitialized(projectDir))
  156. {
  157. return get(projectDir);
  158. }
  159. else
  160. {
  161. return init(projectDir, ctx);
  162. }
  163. }
  164. /**
  165. * Tests whether a project folder is git flow enabled
  166. * @param dir
  167. * @return
  168. */
  169. public static boolean isInitialized(File dir)
  170. {
  171. boolean inited = false;
  172. try
  173. {
  174. RepositoryBuilder rb = new RepositoryBuilder()
  175. .readEnvironment()
  176. .findGitDir(dir);
  177. File gitDir = rb.getGitDir();
  178. if (gitDir != null)
  179. {
  180. Git gitRepo = Git.open(gitDir);
  181. GitFlowConfiguration gfConfig = new GitFlowConfiguration(gitRepo);
  182. inited = gfConfig.gitFlowIsInitialized();
  183. }
  184. }
  185. catch (IOException e)
  186. {
  187. //ignore
  188. }
  189. catch (JGitFlowGitAPIException e)
  190. {
  191. //ignore
  192. }
  193. return inited;
  194. }
  195. /**
  196. * Tests whether the current project is git flow enabled
  197. * @return
  198. */
  199. public boolean isInitialized()
  200. {
  201. boolean inited = false;
  202. try
  203. {
  204. inited = gfConfig.gitFlowIsInitialized();
  205. }
  206. catch (JGitFlowGitAPIException e)
  207. {
  208. //ignore
  209. }
  210. return inited;
  211. }
  212. /**
  213. * Returns a command object to start a feature
  214. * @param name
  215. * @return a {@link FeatureStartCommand}
  216. */
  217. public FeatureStartCommand featureStart(String name)
  218. {
  219. return new FeatureStartCommand(name, git, gfConfig);
  220. }
  221. /**
  222. * Returns a command object to finish a feature
  223. * @param name
  224. * @return a {@link FeatureFinishCommand}
  225. */
  226. public FeatureFinishCommand featureFinish(String name)
  227. {
  228. return new FeatureFinishCommand(name, git, gfConfig);
  229. }
  230. /**
  231. * Returns a command object to publish a feature
  232. * @param name
  233. * @return a {@link FeaturePublishCommand}
  234. */
  235. public FeaturePublishCommand featurePublish(String name)
  236. {
  237. return new FeaturePublishCommand(name, git, gfConfig);
  238. }
  239. /**
  240. * Returns a command object to start a release
  241. * @param name
  242. * @return a {@link ReleaseStartCommand}
  243. */
  244. public ReleaseStartCommand releaseStart(String name)
  245. {
  246. return new ReleaseStartCommand(name, git, gfConfig);
  247. }
  248. /**
  249. * Returns a command object to finish a release
  250. * @param name
  251. * @return a {@link ReleaseFinishCommand}
  252. */
  253. public ReleaseFinishCommand releaseFinish(String name)
  254. {
  255. return new ReleaseFinishCommand(name, git, gfConfig);
  256. }
  257. /**
  258. * Returns a command object to start a hotfix
  259. * @param name
  260. * @return a {@link HotfixStartCommand}
  261. */
  262. public HotfixStartCommand hotfixStart(String name)
  263. {
  264. return new HotfixStartCommand(name, git, gfConfig);
  265. }
  266. /**
  267. * Returns a command object to finish a hotfix
  268. * @param name
  269. * @return a {@link HotfixFinishCommand}
  270. */
  271. public HotfixFinishCommand hotfixFinish(String name)
  272. {
  273. return new HotfixFinishCommand(name, git, gfConfig);
  274. }
  275. /**
  276. * Returns the {@link Git} instance used by this JGitFlow instance
  277. * @return
  278. */
  279. public Git git()
  280. {
  281. return git;
  282. }
  283. /**
  284. * Returns the master branch name configured for this instance's git flow project
  285. * @return
  286. */
  287. public String getMasterBranchName()
  288. {
  289. return gfConfig.getMaster();
  290. }
  291. /**
  292. * Returns the develop branch name configured for this instance's git flow project
  293. * @return
  294. */
  295. public String getDevelopBranchName()
  296. {
  297. return gfConfig.getDevelop();
  298. }
  299. /**
  300. * Returns the feature branch prefix configured for this instance's git flow project
  301. * @return
  302. */
  303. public String getFeatureBranchPrefix()
  304. {
  305. return gfConfig.getPrefixValue(JGitFlowConstants.PREFIXES.FEATURE.configKey());
  306. }
  307. /**
  308. * Returns the release branch prefix configured for this instance's git flow project
  309. * @return
  310. */
  311. public String getReleaseBranchPrefix()
  312. {
  313. return gfConfig.getPrefixValue(JGitFlowConstants.PREFIXES.RELEASE.configKey());
  314. }
  315. /**
  316. * Returns the hotfix branch prefix configured for this instance's git flow project
  317. * @return
  318. */
  319. public String getHotfixBranchPrefix()
  320. {
  321. return gfConfig.getPrefixValue(JGitFlowConstants.PREFIXES.HOTFIX.configKey());
  322. }
  323. public String getSupportBranchPrefix()
  324. {
  325. return gfConfig.getPrefixValue(JGitFlowConstants.PREFIXES.SUPPORT.configKey());
  326. }
  327. /**
  328. * Returns the versiontag prefix configured for this instance's git flow project
  329. * @return
  330. */
  331. public String getVersionTagPrefix()
  332. {
  333. return gfConfig.getPrefixValue(JGitFlowConstants.PREFIXES.VERSIONTAG.configKey());
  334. }
  335. }