PageRenderTime 51ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/lukejackson/jgit-flow
Java | 571 lines | 248 code | 50 blank | 273 comment | 6 complexity | 95e949773e1a052997d93c929b72d000 MD5 | raw file
Possible License(s): Apache-2.0
  1. package com.atlassian.jgitflow.core;
  2. import java.io.File;
  3. import java.io.IOException;
  4. import com.atlassian.jgitflow.core.command.*;
  5. import com.atlassian.jgitflow.core.exception.AlreadyInitializedException;
  6. import com.atlassian.jgitflow.core.exception.JGitFlowGitAPIException;
  7. import com.atlassian.jgitflow.core.exception.JGitFlowIOException;
  8. import com.atlassian.jgitflow.core.exception.SameBranchException;
  9. import org.eclipse.jgit.api.Git;
  10. import org.eclipse.jgit.lib.RepositoryBuilder;
  11. /**
  12. * Offers a Git Flow API to interact with a git repository.
  13. * <p/>
  14. * This class only offers methods to construct so-called command classes. Each
  15. * command is represented by one command class.<br>
  16. * Example: this class offers a {@code featureStart} method returning an instance of
  17. * the {@code FeatureStartCommand} class. The {@code FeatureStartCommand} class has setters
  18. * for all the arguments and options. The {@code FeatureStartCommand} class also has a
  19. * {@code call} method to actually execute the command.
  20. * <p/>
  21. * All mandatory parameters for commands have to be specified in the methods of
  22. * this class, the optional parameters have to be specified by the
  23. * setter-methods of the Command class.
  24. */
  25. public class JGitFlow
  26. {
  27. private Git git;
  28. private GitFlowConfiguration gfConfig;
  29. private JGitFlowReporter reporter;
  30. private JGitFlow()
  31. {
  32. }
  33. JGitFlow(Git git, GitFlowConfiguration gfConfig, JGitFlowReporter reporter)
  34. {
  35. this.git = git;
  36. this.gfConfig = gfConfig;
  37. this.reporter = reporter;
  38. this.reporter.setGitFlowConfiguration(git, gfConfig);
  39. }
  40. /**
  41. * Initializes a project for use with git flow and returns a JGitFlow instance.
  42. * <p>
  43. * This method will throw exceptions if the project has already been initialized
  44. * </p>
  45. *
  46. * @param projectDir
  47. * @return
  48. * @throws com.atlassian.jgitflow.core.exception.JGitFlowIOException
  49. * @throws com.atlassian.jgitflow.core.exception.AlreadyInitializedException
  50. * @throws com.atlassian.jgitflow.core.exception.SameBranchException
  51. * @throws com.atlassian.jgitflow.core.exception.JGitFlowGitAPIException
  52. */
  53. public static JGitFlow init(File projectDir) throws JGitFlowIOException, AlreadyInitializedException, SameBranchException, JGitFlowGitAPIException
  54. {
  55. JGitFlowInitCommand initCommand = new JGitFlowInitCommand();
  56. return initCommand.setDirectory(projectDir).call();
  57. }
  58. /**
  59. * Initializes a project for use with git flow and returns a JGitFlow instance.
  60. * <p>
  61. * This method will throw exceptions if the project has already been initialized
  62. * </p>
  63. *
  64. * @param projectDir
  65. * @return
  66. * @throws com.atlassian.jgitflow.core.exception.JGitFlowIOException
  67. * @throws com.atlassian.jgitflow.core.exception.AlreadyInitializedException
  68. * @throws com.atlassian.jgitflow.core.exception.SameBranchException
  69. * @throws com.atlassian.jgitflow.core.exception.JGitFlowGitAPIException
  70. */
  71. public static JGitFlow init(File projectDir, String defaultOriginUrl) throws JGitFlowIOException, AlreadyInitializedException, SameBranchException, JGitFlowGitAPIException
  72. {
  73. JGitFlowInitCommand initCommand = new JGitFlowInitCommand();
  74. return initCommand.setDirectory(projectDir).setDefaultOriginUrl(defaultOriginUrl).call();
  75. }
  76. /**
  77. * Initializes a project for use with git flow using a custom context and returns a JGitFlow instance.
  78. * <p>
  79. * This method will throw exceptions if the project has already been initialized
  80. * </p>
  81. *
  82. * @param projectDir
  83. * @param context
  84. * @return
  85. * @throws com.atlassian.jgitflow.core.exception.JGitFlowIOException
  86. * @throws com.atlassian.jgitflow.core.exception.AlreadyInitializedException
  87. * @throws com.atlassian.jgitflow.core.exception.SameBranchException
  88. * @throws com.atlassian.jgitflow.core.exception.JGitFlowGitAPIException
  89. */
  90. public static JGitFlow init(File projectDir, InitContext context) throws JGitFlowIOException, AlreadyInitializedException, SameBranchException, JGitFlowGitAPIException
  91. {
  92. JGitFlowInitCommand initCommand = new JGitFlowInitCommand();
  93. return initCommand.setDirectory(projectDir).setInitContext(context).call();
  94. }
  95. /**
  96. * Initializes a project for use with git flow using a custom context and returns a JGitFlow instance.
  97. * <p>
  98. * This method will throw exceptions if the project has already been initialized
  99. * </p>
  100. *
  101. * @param projectDir
  102. * @param context
  103. * @return
  104. * @throws com.atlassian.jgitflow.core.exception.JGitFlowIOException
  105. * @throws com.atlassian.jgitflow.core.exception.AlreadyInitializedException
  106. * @throws com.atlassian.jgitflow.core.exception.SameBranchException
  107. * @throws com.atlassian.jgitflow.core.exception.JGitFlowGitAPIException
  108. */
  109. public static JGitFlow init(File projectDir, InitContext context, String defaultOriginUrl) throws JGitFlowIOException, AlreadyInitializedException, SameBranchException, JGitFlowGitAPIException
  110. {
  111. JGitFlowInitCommand initCommand = new JGitFlowInitCommand();
  112. return initCommand.setDirectory(projectDir).setDefaultOriginUrl(defaultOriginUrl).setInitContext(context).call();
  113. }
  114. /**
  115. * Initializes a project for use with git flow using a default context overriding any existing configuration.
  116. *
  117. * @param projectDir
  118. * @return
  119. * @throws com.atlassian.jgitflow.core.exception.JGitFlowIOException
  120. * @throws com.atlassian.jgitflow.core.exception.AlreadyInitializedException
  121. * @throws com.atlassian.jgitflow.core.exception.SameBranchException
  122. * @throws com.atlassian.jgitflow.core.exception.JGitFlowGitAPIException
  123. */
  124. public static JGitFlow forceInit(File projectDir) throws JGitFlowIOException, AlreadyInitializedException, SameBranchException, JGitFlowGitAPIException
  125. {
  126. JGitFlowInitCommand initCommand = new JGitFlowInitCommand();
  127. return initCommand.setDirectory(projectDir).setForce(true).call();
  128. }
  129. /**
  130. * Initializes a project for use with git flow using a default context overriding any existing configuration.
  131. *
  132. * @param projectDir
  133. * @return
  134. * @throws com.atlassian.jgitflow.core.exception.JGitFlowIOException
  135. * @throws com.atlassian.jgitflow.core.exception.AlreadyInitializedException
  136. * @throws com.atlassian.jgitflow.core.exception.SameBranchException
  137. * @throws com.atlassian.jgitflow.core.exception.JGitFlowGitAPIException
  138. */
  139. public static JGitFlow forceInit(File projectDir, String defaultOriginUrl) throws JGitFlowIOException, AlreadyInitializedException, SameBranchException, JGitFlowGitAPIException
  140. {
  141. JGitFlowInitCommand initCommand = new JGitFlowInitCommand();
  142. return initCommand.setDirectory(projectDir).setDefaultOriginUrl(defaultOriginUrl).setForce(true).call();
  143. }
  144. /**
  145. * Initializes a project for use with git flow using a custom context overriding any existing configuration.
  146. *
  147. * @param projectDir
  148. * @param context
  149. * @return
  150. * @throws com.atlassian.jgitflow.core.exception.JGitFlowIOException
  151. * @throws com.atlassian.jgitflow.core.exception.AlreadyInitializedException
  152. * @throws com.atlassian.jgitflow.core.exception.SameBranchException
  153. * @throws com.atlassian.jgitflow.core.exception.JGitFlowGitAPIException
  154. */
  155. public static JGitFlow forceInit(File projectDir, InitContext context) throws JGitFlowIOException, AlreadyInitializedException, SameBranchException, JGitFlowGitAPIException
  156. {
  157. JGitFlowInitCommand initCommand = new JGitFlowInitCommand();
  158. return initCommand.setDirectory(projectDir).setForce(true).setInitContext(context).call();
  159. }
  160. /**
  161. * Initializes a project for use with git flow using a custom context overriding any existing configuration.
  162. *
  163. * @param projectDir
  164. * @param context
  165. * @return
  166. * @throws com.atlassian.jgitflow.core.exception.JGitFlowIOException
  167. * @throws com.atlassian.jgitflow.core.exception.AlreadyInitializedException
  168. * @throws com.atlassian.jgitflow.core.exception.SameBranchException
  169. * @throws com.atlassian.jgitflow.core.exception.JGitFlowGitAPIException
  170. */
  171. public static JGitFlow forceInit(File projectDir, InitContext context, String defaultOriginUrl) throws JGitFlowIOException, AlreadyInitializedException, SameBranchException, JGitFlowGitAPIException
  172. {
  173. JGitFlowInitCommand initCommand = new JGitFlowInitCommand();
  174. return initCommand.setDirectory(projectDir).setDefaultOriginUrl(defaultOriginUrl).setForce(true).setInitContext(context).call();
  175. }
  176. /**
  177. * Initializes a project for use with git flow using a custom context overriding any existing configuration.
  178. *
  179. * @param projectDir
  180. * @param context
  181. * @return
  182. * @throws com.atlassian.jgitflow.core.exception.JGitFlowIOException
  183. * @throws com.atlassian.jgitflow.core.exception.AlreadyInitializedException
  184. * @throws com.atlassian.jgitflow.core.exception.SameBranchException
  185. * @throws com.atlassian.jgitflow.core.exception.JGitFlowGitAPIException
  186. */
  187. public static JGitFlowInitCommand forceInitCommand(File projectDir, InitContext context) throws JGitFlowIOException, AlreadyInitializedException, SameBranchException, JGitFlowGitAPIException
  188. {
  189. JGitFlowInitCommand initCommand = new JGitFlowInitCommand();
  190. return initCommand.setDirectory(projectDir).setForce(true).setInitContext(context);
  191. }
  192. /**
  193. * Gets an existing git flow project and returns a JGitFlow instance
  194. *
  195. * @param projectDir
  196. * @return
  197. * @throws com.atlassian.jgitflow.core.exception.JGitFlowIOException
  198. */
  199. public static JGitFlow get(File projectDir) throws JGitFlowIOException
  200. {
  201. try
  202. {
  203. RepositoryBuilder rb = new RepositoryBuilder()
  204. .readEnvironment()
  205. .findGitDir(projectDir);
  206. File gitDir = rb.getGitDir();
  207. Git gitRepo = Git.open(gitDir);
  208. GitFlowConfiguration gfConfig = new GitFlowConfiguration(gitRepo);
  209. return new JGitFlow(gitRepo, gfConfig, new JGitFlowReporter());
  210. }
  211. catch (IOException e)
  212. {
  213. throw new JGitFlowIOException(e);
  214. }
  215. }
  216. /**
  217. * Initializes a project for use with git flow or gets an existing project and returns a JGitFlow instance.
  218. *
  219. * @param projectDir
  220. * @return
  221. * @throws com.atlassian.jgitflow.core.exception.JGitFlowIOException
  222. * @throws com.atlassian.jgitflow.core.exception.AlreadyInitializedException
  223. * @throws com.atlassian.jgitflow.core.exception.SameBranchException
  224. * @throws com.atlassian.jgitflow.core.exception.JGitFlowGitAPIException
  225. */
  226. public static JGitFlow getOrInit(File projectDir) throws JGitFlowIOException, AlreadyInitializedException, SameBranchException, JGitFlowGitAPIException
  227. {
  228. if (isInitialized(projectDir))
  229. {
  230. return get(projectDir);
  231. }
  232. else
  233. {
  234. return init(projectDir);
  235. }
  236. }
  237. /**
  238. * Initializes a project for use with git flow or gets an existing project and returns a JGitFlow instance.
  239. *
  240. * @param projectDir
  241. * @return
  242. * @throws com.atlassian.jgitflow.core.exception.JGitFlowIOException
  243. * @throws com.atlassian.jgitflow.core.exception.AlreadyInitializedException
  244. * @throws com.atlassian.jgitflow.core.exception.SameBranchException
  245. * @throws com.atlassian.jgitflow.core.exception.JGitFlowGitAPIException
  246. */
  247. public static JGitFlow getOrInit(File projectDir, String defaultOriginUrl) throws JGitFlowIOException, AlreadyInitializedException, SameBranchException, JGitFlowGitAPIException
  248. {
  249. if (isInitialized(projectDir))
  250. {
  251. return get(projectDir);
  252. }
  253. else
  254. {
  255. return init(projectDir, defaultOriginUrl);
  256. }
  257. }
  258. /**
  259. * Initializes a project for use with git flow using a custom context or gets an existing project and returns a JGitFlow instance.
  260. *
  261. * @param projectDir
  262. * @param ctx
  263. * @return
  264. * @throws com.atlassian.jgitflow.core.exception.JGitFlowIOException
  265. * @throws com.atlassian.jgitflow.core.exception.AlreadyInitializedException
  266. * @throws com.atlassian.jgitflow.core.exception.SameBranchException
  267. * @throws com.atlassian.jgitflow.core.exception.JGitFlowGitAPIException
  268. */
  269. public static JGitFlow getOrInit(File projectDir, InitContext ctx) throws JGitFlowIOException, AlreadyInitializedException, SameBranchException, JGitFlowGitAPIException
  270. {
  271. if (isInitialized(projectDir))
  272. {
  273. return get(projectDir);
  274. }
  275. else
  276. {
  277. return init(projectDir, ctx);
  278. }
  279. }
  280. /**
  281. * Initializes a project for use with git flow using a custom context or gets an existing project and returns a JGitFlow instance.
  282. *
  283. * @param projectDir
  284. * @param ctx
  285. * @return
  286. * @throws com.atlassian.jgitflow.core.exception.JGitFlowIOException
  287. * @throws com.atlassian.jgitflow.core.exception.AlreadyInitializedException
  288. * @throws com.atlassian.jgitflow.core.exception.SameBranchException
  289. * @throws com.atlassian.jgitflow.core.exception.JGitFlowGitAPIException
  290. */
  291. public static JGitFlow getOrInit(File projectDir, InitContext ctx, String defaultOriginUrl) throws JGitFlowIOException, AlreadyInitializedException, SameBranchException, JGitFlowGitAPIException
  292. {
  293. if (isInitialized(projectDir))
  294. {
  295. return get(projectDir);
  296. }
  297. else
  298. {
  299. return init(projectDir, ctx, defaultOriginUrl);
  300. }
  301. }
  302. /**
  303. * Tests whether a project folder is git flow enabled
  304. *
  305. * @param dir
  306. * @return
  307. */
  308. public static boolean isInitialized(File dir)
  309. {
  310. boolean inited = false;
  311. try
  312. {
  313. RepositoryBuilder rb = new RepositoryBuilder()
  314. .readEnvironment()
  315. .findGitDir(dir);
  316. File gitDir = rb.getGitDir();
  317. if (gitDir != null)
  318. {
  319. Git gitRepo = Git.open(gitDir);
  320. GitFlowConfiguration gfConfig = new GitFlowConfiguration(gitRepo);
  321. inited = gfConfig.gitFlowIsInitialized();
  322. }
  323. }
  324. catch (IOException e)
  325. {
  326. //ignore
  327. }
  328. catch (JGitFlowGitAPIException e)
  329. {
  330. //ignore
  331. }
  332. return inited;
  333. }
  334. /**
  335. * Tests whether the current project is git flow enabled
  336. *
  337. * @return
  338. */
  339. public boolean isInitialized()
  340. {
  341. boolean inited = false;
  342. try
  343. {
  344. inited = gfConfig.gitFlowIsInitialized();
  345. }
  346. catch (JGitFlowGitAPIException e)
  347. {
  348. //ignore
  349. }
  350. return inited;
  351. }
  352. /**
  353. * Returns a command object to start a feature
  354. *
  355. * @param name
  356. * @return a {@link com.atlassian.jgitflow.core.command.FeatureStartCommand}
  357. */
  358. public FeatureStartCommand featureStart(String name)
  359. {
  360. return new FeatureStartCommand(name, git, gfConfig, reporter);
  361. }
  362. /**
  363. * Returns a command object to finish a feature
  364. *
  365. * @param name
  366. * @return a {@link com.atlassian.jgitflow.core.command.FeatureFinishCommand}
  367. */
  368. public FeatureFinishCommand featureFinish(String name)
  369. {
  370. return new FeatureFinishCommand(name, git, gfConfig, reporter);
  371. }
  372. /**
  373. * Returns a command object to publish a feature
  374. *
  375. * @param name
  376. * @return a {@link com.atlassian.jgitflow.core.command.FeaturePublishCommand}
  377. */
  378. public FeaturePublishCommand featurePublish(String name)
  379. {
  380. return new FeaturePublishCommand(name, git, gfConfig, reporter);
  381. }
  382. /**
  383. * Returns a command object to start a release
  384. *
  385. * @param name
  386. * @return a {@link com.atlassian.jgitflow.core.command.ReleaseStartCommand}
  387. */
  388. public ReleaseStartCommand releaseStart(String name)
  389. {
  390. return new ReleaseStartCommand(name, git, gfConfig, reporter);
  391. }
  392. /**
  393. * Returns a command object to finish a release
  394. *
  395. * @param name
  396. * @return a {@link com.atlassian.jgitflow.core.command.ReleaseFinishCommand}
  397. */
  398. public ReleaseFinishCommand releaseFinish(String name)
  399. {
  400. return new ReleaseFinishCommand(name, git, gfConfig, reporter);
  401. }
  402. /**
  403. * Returns a command object to publish a release
  404. *
  405. * @param name
  406. * @return a {@link com.atlassian.jgitflow.core.command.ReleasePublishCommand}
  407. */
  408. public ReleasePublishCommand releasePublish(String name)
  409. {
  410. return new ReleasePublishCommand(name, git, gfConfig, reporter);
  411. }
  412. /**
  413. * Returns a command object to start a hotfix
  414. *
  415. * @param name
  416. * @return a {@link com.atlassian.jgitflow.core.command.HotfixStartCommand}
  417. */
  418. public HotfixStartCommand hotfixStart(String name)
  419. {
  420. return new HotfixStartCommand(name, git, gfConfig, reporter);
  421. }
  422. /**
  423. * Returns a command object to finish a hotfix
  424. *
  425. * @param name
  426. * @return a {@link com.atlassian.jgitflow.core.command.HotfixFinishCommand}
  427. */
  428. public HotfixFinishCommand hotfixFinish(String name)
  429. {
  430. return new HotfixFinishCommand(name, git, gfConfig, reporter);
  431. }
  432. /**
  433. * Returns a command object to publish a hotfix
  434. *
  435. * @param name
  436. * @return a {@link com.atlassian.jgitflow.core.command.HotfixPublishCommand}
  437. */
  438. public HotfixPublishCommand hotfixPublish(String name)
  439. {
  440. return new HotfixPublishCommand(name, git, gfConfig, reporter);
  441. }
  442. /**
  443. * Returns the {@link org.eclipse.jgit.api.Git} instance used by this JGitFlow instance
  444. *
  445. * @return
  446. */
  447. public Git git()
  448. {
  449. return git;
  450. }
  451. /**
  452. * Returns the master branch name configured for this instance's git flow project
  453. *
  454. * @return
  455. */
  456. public String getMasterBranchName()
  457. {
  458. return gfConfig.getMaster();
  459. }
  460. /**
  461. * Returns the develop branch name configured for this instance's git flow project
  462. *
  463. * @return
  464. */
  465. public String getDevelopBranchName()
  466. {
  467. return gfConfig.getDevelop();
  468. }
  469. /**
  470. * Returns the feature branch prefix configured for this instance's git flow project
  471. *
  472. * @return
  473. */
  474. public String getFeatureBranchPrefix()
  475. {
  476. return gfConfig.getPrefixValue(JGitFlowConstants.PREFIXES.FEATURE.configKey());
  477. }
  478. /**
  479. * Returns the release branch prefix configured for this instance's git flow project
  480. *
  481. * @return
  482. */
  483. public String getReleaseBranchPrefix()
  484. {
  485. return gfConfig.getPrefixValue(JGitFlowConstants.PREFIXES.RELEASE.configKey());
  486. }
  487. /**
  488. * Returns the hotfix branch prefix configured for this instance's git flow project
  489. *
  490. * @return
  491. */
  492. public String getHotfixBranchPrefix()
  493. {
  494. return gfConfig.getPrefixValue(JGitFlowConstants.PREFIXES.HOTFIX.configKey());
  495. }
  496. public String getSupportBranchPrefix()
  497. {
  498. return gfConfig.getPrefixValue(JGitFlowConstants.PREFIXES.SUPPORT.configKey());
  499. }
  500. /**
  501. * Returns the versiontag prefix configured for this instance's git flow project
  502. *
  503. * @return
  504. */
  505. public String getVersionTagPrefix()
  506. {
  507. return gfConfig.getPrefixValue(JGitFlowConstants.PREFIXES.VERSIONTAG.configKey());
  508. }
  509. public String getPrefixForBranch(String branchName)
  510. {
  511. return gfConfig.getPrefixForBranch(branchName);
  512. }
  513. public BranchType getTypeForBranch(String branchName)
  514. {
  515. return gfConfig.getTypeForBranch(branchName);
  516. }
  517. public JGitFlowReporter getReporter()
  518. {
  519. return reporter;
  520. }
  521. }