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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  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 java.io.File;
  46. import java.io.IOException;
  47. import org.eclipse.jgit.lib.Repository;
  48. import org.eclipse.jgit.lib.RepositoryBuilder;
  49. import org.eclipse.jgit.lib.RepositoryCache;
  50. import org.eclipse.jgit.util.FS;
  51. /**
  52. * Offers a "GitPorcelain"-like API to interact with a git repository.
  53. * <p>
  54. * The GitPorcelain commands are described in the <a href="http://www.kernel.org/pub/software/scm/git/docs/git.html#_high_level_commands_porcelain"
  55. * >Git Documentation</a>.
  56. * <p>
  57. * This class only offers methods to construct so-called command classes. Each
  58. * GitPorcelain command is represented by one command class.<br>
  59. * Example: this class offers a {@code commit()} method returning an instance of
  60. * the {@code CommitCommand} class. The {@code CommitCommand} class has setters
  61. * for all the arguments and options. The {@code CommitCommand} class also has a
  62. * {@code call} method to actually execute the commit. The following code show's
  63. * how to do a simple commit:
  64. *
  65. * <pre>
  66. * Git git = new Git(myRepo);
  67. * git.commit().setMessage(&quot;Fix393&quot;).setAuthor(developerIdent).call();
  68. * </pre>
  69. *
  70. * All mandatory parameters for commands have to be specified in the methods of
  71. * this class, the optional parameters have to be specified by the
  72. * setter-methods of the Command class.
  73. * <p>
  74. * This class is intended to be used internally (e.g. by JGit tests) or by
  75. * external components (EGit, third-party tools) when they need exactly the
  76. * functionality of a GitPorcelain command. There are use-cases where this class
  77. * is not optimal and where you should use the more low-level JGit classes. The
  78. * methods in this class may for example offer too much functionality or they
  79. * offer the functionality with the wrong arguments.
  80. */
  81. public class Git {
  82. /** The git repository this class is interacting with */
  83. private final Repository repo;
  84. /**
  85. * @param dir
  86. * the repository to open. May be either the GIT_DIR, or the
  87. * working tree directory that contains {@code .git}.
  88. * @return a {@link Git} object for the existing git repository
  89. * @throws IOException
  90. */
  91. public static Git open(File dir) throws IOException {
  92. return open(dir, FS.DETECTED);
  93. }
  94. /**
  95. * @param dir
  96. * the repository to open. May be either the GIT_DIR, or the
  97. * working tree directory that contains {@code .git}.
  98. * @param fs
  99. * filesystem abstraction to use when accessing the repository.
  100. * @return a {@link Git} object for the existing git repository
  101. * @throws IOException
  102. */
  103. public static Git open(File dir, FS fs) throws IOException {
  104. RepositoryCache.FileKey key;
  105. key = RepositoryCache.FileKey.lenient(dir, fs);
  106. return wrap(new RepositoryBuilder()
  107. .setFS(fs)
  108. .setGitDir(key.getFile())
  109. .setMustExist(true).build());
  110. }
  111. /**
  112. * @param repo
  113. * the git repository this class is interacting with.
  114. * {@code null} is not allowed
  115. * @return a {@link Git} object for the existing git repository
  116. */
  117. public static Git wrap(Repository repo) {
  118. return new Git(repo);
  119. }
  120. /**
  121. * Returns a command object to execute a {@code clone} command
  122. *
  123. * @see <a
  124. * href="http://www.kernel.org/pub/software/scm/git/docs/git-clone.html"
  125. * >Git documentation about clone</a>
  126. * @return a {@link CloneCommand} used to collect all optional parameters
  127. * and to finally execute the {@code clone} command
  128. */
  129. public static CloneCommand cloneRepository() {
  130. return new CloneCommand();
  131. }
  132. /**
  133. * Returns a command object to execute a {@code init} command
  134. *
  135. * @see <a
  136. * href="http://www.kernel.org/pub/software/scm/git/docs/git-init.html"
  137. * >Git documentation about init</a>
  138. * @return a {@link InitCommand} used to collect all optional parameters and
  139. * to finally execute the {@code init} command
  140. */
  141. public static InitCommand init() {
  142. return new InitCommand();
  143. }
  144. /**
  145. * Constructs a new {@link Git} object which can interact with the specified
  146. * git repository. All command classes returned by methods of this class
  147. * will always interact with this git repository.
  148. *
  149. * @param repo
  150. * the git repository this class is interacting with.
  151. * {@code null} is not allowed
  152. */
  153. public Git(Repository repo) {
  154. if (repo == null)
  155. throw new NullPointerException();
  156. this.repo = repo;
  157. }
  158. /**
  159. * Returns a command object to execute a {@code Commit} command
  160. *
  161. * @see <a
  162. * href="http://www.kernel.org/pub/software/scm/git/docs/git-commit.html"
  163. * >Git documentation about Commit</a>
  164. * @return a {@link CommitCommand} used to collect all optional parameters
  165. * and to finally execute the {@code Commit} command
  166. */
  167. public CommitCommand commit() {
  168. return new CommitCommand(repo);
  169. }
  170. /**
  171. * Returns a command object to execute a {@code Log} command
  172. *
  173. * @see <a
  174. * href="http://www.kernel.org/pub/software/scm/git/docs/git-log.html"
  175. * >Git documentation about Log</a>
  176. * @return a {@link LogCommand} used to collect all optional parameters and
  177. * to finally execute the {@code Log} command
  178. */
  179. public LogCommand log() {
  180. return new LogCommand(repo);
  181. }
  182. /**
  183. * Returns a command object to execute a {@code Merge} command
  184. *
  185. * @see <a
  186. * href="http://www.kernel.org/pub/software/scm/git/docs/git-merge.html"
  187. * >Git documentation about Merge</a>
  188. * @return a {@link MergeCommand} used to collect all optional parameters
  189. * and to finally execute the {@code Merge} command
  190. */
  191. public MergeCommand merge() {
  192. return new MergeCommand(repo);
  193. }
  194. /**
  195. * Returns a command object to execute a {@code Pull} command
  196. *
  197. * @return a {@link PullCommand}
  198. */
  199. public PullCommand pull() {
  200. return new PullCommand(repo);
  201. }
  202. /**
  203. * Returns a command object used to create branches
  204. *
  205. * @return a {@link CreateBranchCommand}
  206. */
  207. public CreateBranchCommand branchCreate() {
  208. return new CreateBranchCommand(repo);
  209. }
  210. /**
  211. * Returns a command object used to delete branches
  212. *
  213. * @return a {@link DeleteBranchCommand}
  214. */
  215. public DeleteBranchCommand branchDelete() {
  216. return new DeleteBranchCommand(repo);
  217. }
  218. /**
  219. * Returns a command object used to list branches
  220. *
  221. * @return a {@link ListBranchCommand}
  222. */
  223. public ListBranchCommand branchList() {
  224. return new ListBranchCommand(repo);
  225. }
  226. /**
  227. * Returns a command object used to rename branches
  228. *
  229. * @return a {@link RenameBranchCommand}
  230. */
  231. public RenameBranchCommand branchRename() {
  232. return new RenameBranchCommand(repo);
  233. }
  234. /**
  235. * Returns a command object to execute a {@code Add} command
  236. *
  237. * @see <a
  238. * href="http://www.kernel.org/pub/software/scm/git/docs/git-add.html"
  239. * >Git documentation about Add</a>
  240. * @return a {@link AddCommand} used to collect all optional parameters
  241. * and to finally execute the {@code Add} command
  242. */
  243. public AddCommand add() {
  244. return new AddCommand(repo);
  245. }
  246. /**
  247. * Returns a command object to execute a {@code Tag} command
  248. *
  249. * @see <a
  250. * href="http://www.kernel.org/pub/software/scm/git/docs/git-tag.html"
  251. * >Git documentation about Tag</a>
  252. * @return a {@link TagCommand} used to collect all optional parameters
  253. * and to finally execute the {@code Tag} command
  254. */
  255. public TagCommand tag() {
  256. return new TagCommand(repo);
  257. }
  258. /**
  259. * Returns a command object to execute a {@code Fetch} command
  260. *
  261. * @see <a
  262. * href="http://www.kernel.org/pub/software/scm/git/docs/git-fetch.html"
  263. * >Git documentation about Fetch</a>
  264. * @return a {@link FetchCommand} used to collect all optional parameters
  265. * and to finally execute the {@code Fetch} command
  266. */
  267. public FetchCommand fetch() {
  268. return new FetchCommand(repo);
  269. }
  270. /**
  271. * Returns a command object to execute a {@code Push} command
  272. *
  273. * @see <a
  274. * href="http://www.kernel.org/pub/software/scm/git/docs/git-push.html"
  275. * >Git documentation about Push</a>
  276. * @return a {@link PushCommand} used to collect all optional parameters and
  277. * to finally execute the {@code Push} command
  278. */
  279. public PushCommand push() {
  280. return new PushCommand(repo);
  281. }
  282. /**
  283. * Returns a command object to execute a {@code cherry-pick} command
  284. *
  285. * @see <a
  286. * href="http://www.kernel.org/pub/software/scm/git/docs/git-cherry-pick.html"
  287. * >Git documentation about cherry-pick</a>
  288. * @return a {@link CherryPickCommand} used to collect all optional
  289. * parameters and to finally execute the {@code cherry-pick} command
  290. */
  291. public CherryPickCommand cherryPick() {
  292. return new CherryPickCommand(repo);
  293. }
  294. /**
  295. * Returns a command object to execute a {@code revert} command
  296. *
  297. * @see <a
  298. * href="http://www.kernel.org/pub/software/scm/git/docs/git-revert.html"
  299. * >Git documentation about reverting changes</a>
  300. * @return a {@link RevertCommand} used to collect all optional
  301. * parameters and to finally execute the {@code cherry-pick} command
  302. */
  303. public RevertCommand revert() {
  304. return new RevertCommand(repo);
  305. }
  306. /**
  307. * Returns a command object to execute a {@code Rebase} command
  308. *
  309. * @see <a
  310. * href="http://www.kernel.org/pub/software/scm/git/docs/git-rebase.html"
  311. * >Git documentation about rebase</a>
  312. * @return a {@link RebaseCommand} used to collect all optional parameters
  313. * and to finally execute the {@code rebase} command
  314. */
  315. public RebaseCommand rebase() {
  316. return new RebaseCommand(repo);
  317. }
  318. /**
  319. * Returns a command object to execute a {@code rm} command
  320. *
  321. * @see <a
  322. * href="http://www.kernel.org/pub/software/scm/git/docs/git-rm.html"
  323. * >Git documentation about rm</a>
  324. * @return a {@link RmCommand} used to collect all optional parameters and
  325. * to finally execute the {@code rm} command
  326. */
  327. public RmCommand rm() {
  328. return new RmCommand(repo);
  329. }
  330. /**
  331. * Returns a command object to execute a {@code checkout} command
  332. *
  333. * @see <a
  334. * href="http://www.kernel.org/pub/software/scm/git/docs/git-checkout.html"
  335. * >Git documentation about checkout</a>
  336. * @return a {@link CheckoutCommand} used to collect all optional parameters
  337. * and to finally execute the {@code checkout} command
  338. */
  339. public CheckoutCommand checkout() {
  340. return new CheckoutCommand(repo);
  341. }
  342. /**
  343. * Returns a command object to execute a {@code reset} command
  344. *
  345. * @see <a
  346. * href="http://www.kernel.org/pub/software/scm/git/docs/git-reset.html"
  347. * >Git documentation about reset</a>
  348. * @return a {@link ResetCommand} used to collect all optional parameters
  349. * and to finally execute the {@code reset} command
  350. */
  351. public ResetCommand reset() {
  352. return new ResetCommand(repo);
  353. }
  354. /**
  355. * Returns a command object to execute a {@code status} command
  356. *
  357. * @see <a
  358. * href="http://www.kernel.org/pub/software/scm/git/docs/git-status.html"
  359. * >Git documentation about status</a>
  360. * @return a {@link StatusCommand} used to collect all optional parameters
  361. * and to finally execute the {@code status} command
  362. */
  363. public StatusCommand status() {
  364. return new StatusCommand(repo);
  365. }
  366. /**
  367. * Returns a command to add notes to an object
  368. *
  369. * @return a {@link AddNoteCommand}
  370. */
  371. public AddNoteCommand notesAdd() {
  372. return new AddNoteCommand(repo);
  373. }
  374. /**
  375. * Returns a command to remove notes on an object
  376. *
  377. * @return a {@link RemoveNoteCommand}
  378. */
  379. public RemoveNoteCommand notesRemove() {
  380. return new RemoveNoteCommand(repo);
  381. }
  382. /**
  383. * Returns a command to list all notes
  384. *
  385. * @return a {@link ListNotesCommand}
  386. */
  387. public ListNotesCommand notesList() {
  388. return new ListNotesCommand(repo);
  389. }
  390. /**
  391. * Returns a command to show notes on an object
  392. *
  393. * @return a {@link ShowNoteCommand}
  394. */
  395. public ShowNoteCommand notesShow() {
  396. return new ShowNoteCommand(repo);
  397. }
  398. /**
  399. * Returns a command object to execute a {@code ls-remote} command
  400. *
  401. * @see <a
  402. * href="http://www.kernel.org/pub/software/scm/git/docs/git-ls-remote.html"
  403. * >Git documentation about ls-remote</a>
  404. * @return a {@link LsRemoteCommand} used to collect all optional parameters
  405. * and to finally execute the {@code status} command
  406. */
  407. public LsRemoteCommand lsRemote() {
  408. return new LsRemoteCommand(repo);
  409. }
  410. /**
  411. * Returns a command object to execute a {@code clean} command
  412. *
  413. * @see <a
  414. * href="http://www.kernel.org/pub/software/scm/git/docs/git-clean.html"
  415. * >Git documentation about Clean</a>
  416. * @return a {@link CleanCommand} used to collect all optional parameters
  417. * and to finally execute the {@code clean} command
  418. */
  419. public CleanCommand clean() {
  420. return new CleanCommand(repo);
  421. }
  422. /**
  423. * Returns a command object to execute a {@code blame} command
  424. *
  425. * @see <a
  426. * href="http://www.kernel.org/pub/software/scm/git/docs/git-blame.html"
  427. * >Git documentation about Blame</a>
  428. * @return a {@link BlameCommand} used to collect all optional parameters
  429. * and to finally execute the {@code blame} command
  430. */
  431. public BlameCommand blame() {
  432. return new BlameCommand(repo);
  433. }
  434. /**
  435. * Returns a command object to execute a {@code reflog} command
  436. *
  437. * @see <a
  438. * href="http://www.kernel.org/pub/software/scm/git/docs/git-reflog.html"
  439. * >Git documentation about reflog</a>
  440. * @return a {@link ReflogCommand} used to collect all optional parameters
  441. * and to finally execute the {@code reflog} command
  442. */
  443. public ReflogCommand reflog() {
  444. return new ReflogCommand(repo);
  445. }
  446. /**
  447. * Returns a command object to execute a {@code diff} command
  448. *
  449. * @see <a
  450. * href="http://www.kernel.org/pub/software/scm/git/docs/git-diff.html"
  451. * >Git documentation about diff</a>
  452. * @return a {@link DiffCommand} used to collect all optional parameters and
  453. * to finally execute the {@code diff} command
  454. */
  455. public DiffCommand diff() {
  456. return new DiffCommand(repo);
  457. }
  458. /**
  459. * @return the git repository this class is interacting with
  460. */
  461. public Repository getRepository() {
  462. return repo;
  463. }
  464. }