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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /*
  2. * Copyright (C) 2010, Christian Halstrick <christian.halstrick@sap.com>
  3. * Copyright (C) 2010, Chris Aniszczyk <caniszczyk@gmail.com>
  4. * and other copyright owners as documented in the project's IP log.
  5. *
  6. * This program and the accompanying materials are made available
  7. * under the terms of the Eclipse Distribution License v1.0 which
  8. * accompanies this distribution, is reproduced below, and is
  9. * available at http://www.eclipse.org/org/documents/edl-v10.php
  10. *
  11. * All rights reserved.
  12. *
  13. * Redistribution and use in source and binary forms, with or
  14. * without modification, are permitted provided that the following
  15. * conditions are met:
  16. *
  17. * - Redistributions of source code must retain the above copyright
  18. * notice, this list of conditions and the following disclaimer.
  19. *
  20. * - Redistributions in binary form must reproduce the above
  21. * copyright notice, this list of conditions and the following
  22. * disclaimer in the documentation and/or other materials provided
  23. * with the distribution.
  24. *
  25. * - Neither the name of the Eclipse Foundation, Inc. nor the
  26. * names of its contributors may be used to endorse or promote
  27. * products derived from this software without specific prior
  28. * written permission.
  29. *
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  31. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  32. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  33. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  34. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  35. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  36. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  37. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  38. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  39. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  40. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  41. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  42. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  43. */
  44. package org.eclipse.jgit.api;
  45. import org.eclipse.jgit.lib.Repository;
  46. /**
  47. * Offers a "GitPorcelain"-like API to interact with a git repository.
  48. * <p>
  49. * The GitPorcelain commands are described in the <a href="http://www.kernel.org/pub/software/scm/git/docs/git.html#_high_level_commands_porcelain"
  50. * >Git Documentation</a>.
  51. * <p>
  52. * This class only offers methods to construct so-called command classes. Each
  53. * GitPorcelain command is represented by one command class.<br>
  54. * Example: this class offers a {@code commit()} method returning an instance of
  55. * the {@code CommitCommand} class. The {@code CommitCommand} class has setters
  56. * for all the arguments and options. The {@code CommitCommand} class also has a
  57. * {@code call} method to actually execute the commit. The following code show's
  58. * how to do a simple commit:
  59. *
  60. * <pre>
  61. * Git git = new Git(myRepo);
  62. * git.commit().setMessage(&quot;Fix393&quot;).setAuthor(developerIdent).call();
  63. * </pre>
  64. *
  65. * All mandatory parameters for commands have to be specified in the methods of
  66. * this class, the optional parameters have to be specified by the
  67. * setter-methods of the Command class.
  68. * <p>
  69. * This class is intended to be used internally (e.g. by JGit tests) or by
  70. * external components (EGit, third-party tools) when they need exactly the
  71. * functionality of a GitPorcelain command. There are use-cases where this class
  72. * is not optimal and where you should use the more low-level JGit classes. The
  73. * methods in this class may for example offer too much functionality or they
  74. * offer the functionality with the wrong arguments.
  75. */
  76. public class Git {
  77. /** The git repository this class is interacting with */
  78. private final Repository repo;
  79. /**
  80. * Constructs a new {@link Git} object which can interact with the specified
  81. * git repository. All command classes returned by methods of this class
  82. * will always interact with this git repository.
  83. *
  84. * @param repo
  85. * the git repository this class is interacting with.
  86. * {@code null} is not allowed
  87. */
  88. public Git(Repository repo) {
  89. if (repo == null)
  90. throw new NullPointerException();
  91. this.repo = repo;
  92. }
  93. /**
  94. * Returns a command object to execute a {@code Commit} command
  95. *
  96. * @see <a
  97. * href="http://www.kernel.org/pub/software/scm/git/docs/git-commit.html"
  98. * >Git documentation about Commit</a>
  99. * @return a {@link CommitCommand} used to collect all optional parameters
  100. * and to finally execute the {@code Commit} command
  101. */
  102. public CommitCommand commit() {
  103. return new CommitCommand(repo);
  104. }
  105. /**
  106. * Returns a command object to execute a {@code Log} command
  107. *
  108. * @see <a
  109. * href="http://www.kernel.org/pub/software/scm/git/docs/git-log.html"
  110. * >Git documentation about Log</a>
  111. * @return a {@link LogCommand} used to collect all optional parameters and
  112. * to finally execute the {@code Log} command
  113. */
  114. public LogCommand log() {
  115. return new LogCommand(repo);
  116. }
  117. /**
  118. * Returns a command object to execute a {@code Merge} command
  119. *
  120. * @see <a
  121. * href="http://www.kernel.org/pub/software/scm/git/docs/git-merge.html"
  122. * >Git documentation about Merge</a>
  123. * @return a {@link MergeCommand} used to collect all optional parameters
  124. * and to finally execute the {@code Merge} command
  125. */
  126. public MergeCommand merge() {
  127. return new MergeCommand(repo);
  128. }
  129. /**
  130. * Returns a command object to execute a {@code Pull} command
  131. *
  132. * @return a {@link PullCommand}
  133. */
  134. public PullCommand pull() {
  135. return new PullCommand(repo);
  136. }
  137. /**
  138. * Returns a command object used to create branches
  139. *
  140. * @return a {@link CreateBranchCommand}
  141. */
  142. public CreateBranchCommand branchCreate() {
  143. return new CreateBranchCommand(repo);
  144. }
  145. /**
  146. * Returns a command object used to delete branches
  147. *
  148. * @return a {@link DeleteBranchCommand}
  149. */
  150. public DeleteBranchCommand branchDelete() {
  151. return new DeleteBranchCommand(repo);
  152. }
  153. /**
  154. * Returns a command object used to list branches
  155. *
  156. * @return a {@link ListBranchCommand}
  157. */
  158. public ListBranchCommand branchList() {
  159. return new ListBranchCommand(repo);
  160. }
  161. /**
  162. * Returns a command object used to rename branches
  163. *
  164. * @return a {@link RenameBranchCommand}
  165. */
  166. public RenameBranchCommand branchRename() {
  167. return new RenameBranchCommand(repo);
  168. }
  169. /**
  170. * Returns a command object to execute a {@code Add} command
  171. *
  172. * @see <a
  173. * href="http://www.kernel.org/pub/software/scm/git/docs/git-add.html"
  174. * >Git documentation about Add</a>
  175. * @return a {@link AddCommand} used to collect all optional parameters
  176. * and to finally execute the {@code Add} command
  177. */
  178. public AddCommand add() {
  179. return new AddCommand(repo);
  180. }
  181. /**
  182. * Returns a command object to execute a {@code Tag} command
  183. *
  184. * @see <a
  185. * href="http://www.kernel.org/pub/software/scm/git/docs/git-tag.html"
  186. * >Git documentation about Tag</a>
  187. * @return a {@link TagCommand} used to collect all optional parameters
  188. * and to finally execute the {@code Tag} command
  189. */
  190. public TagCommand tag() {
  191. return new TagCommand(repo);
  192. }
  193. /**
  194. * Returns a command object to execute a {@code Fetch} command
  195. *
  196. * @see <a
  197. * href="http://www.kernel.org/pub/software/scm/git/docs/git-fetch.html"
  198. * >Git documentation about Fetch</a>
  199. * @return a {@link FetchCommand} used to collect all optional parameters
  200. * and to finally execute the {@code Fetch} command
  201. */
  202. public FetchCommand fetch() {
  203. return new FetchCommand(repo);
  204. }
  205. /**
  206. * Returns a command object to execute a {@code Push} command
  207. *
  208. * @see <a
  209. * href="http://www.kernel.org/pub/software/scm/git/docs/git-push.html"
  210. * >Git documentation about Push</a>
  211. * @return a {@link PushCommand} used to collect all optional parameters and
  212. * to finally execute the {@code Push} command
  213. */
  214. public PushCommand push() {
  215. return new PushCommand(repo);
  216. }
  217. /**
  218. * Returns a command object to execute a {@code cherry-pick} command
  219. *
  220. * @see <a
  221. * href="http://www.kernel.org/pub/software/scm/git/docs/git-cherry-pick.html"
  222. * >Git documentation about cherry-pick</a>
  223. * @return a {@link CherryPickCommand} used to collect all optional
  224. * parameters and to finally execute the {@code cherry-pick} command
  225. */
  226. public CherryPickCommand cherryPick() {
  227. return new CherryPickCommand(repo);
  228. }
  229. /**
  230. * Returns a command object to execute a {@code Rebase} command
  231. *
  232. * @see <a
  233. * href="http://www.kernel.org/pub/software/scm/git/docs/git-rebase.html"
  234. * >Git documentation about rebase</a>
  235. * @return a {@link RebaseCommand} used to collect all optional parameters
  236. * and to finally execute the {@code rebase} command
  237. */
  238. public RebaseCommand rebase() {
  239. return new RebaseCommand(repo);
  240. }
  241. /**
  242. * Returns a command object to execute a {@code rm} command
  243. *
  244. * @see <a
  245. * href="http://www.kernel.org/pub/software/scm/git/docs/git-rm.html"
  246. * >Git documentation about rm</a>
  247. * @return a {@link RmCommand} used to collect all optional parameters and
  248. * to finally execute the {@code rm} command
  249. */
  250. public RmCommand rm() {
  251. return new RmCommand(repo);
  252. }
  253. /**
  254. * Returns a command object to execute a {@code checkout} command
  255. *
  256. * @see <a
  257. * href="http://www.kernel.org/pub/software/scm/git/docs/git-checkout.html"
  258. * >Git documentation about checkout</a>
  259. * @return a {@link CheckoutCommand} used to collect all optional parameters
  260. * and to finally execute the {@code checkout} command
  261. */
  262. public CheckoutCommand checkout() {
  263. return new CheckoutCommand(repo);
  264. }
  265. /**
  266. * Returns a command object to execute a {@code init} command
  267. *
  268. * @see <a
  269. * href="http://www.kernel.org/pub/software/scm/git/docs/git-init.html"
  270. * >Git documentation about init</a>
  271. * @return a {@link InitCommand} used to collect all optional parameters and
  272. * to finally execute the {@code init} command
  273. */
  274. static public InitCommand init() {
  275. return new InitCommand();
  276. }
  277. /**
  278. * @return the git repository this class is interacting with
  279. */
  280. public Repository getRepository() {
  281. return repo;
  282. }
  283. }