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.

CommitAndLogCommandTests.java 9.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. * Copyright (C) 2010, Christian Halstrick <christian.halstrick@sap.com>
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.api;
  44. import static org.junit.Assert.assertEquals;
  45. import static org.junit.Assert.assertTrue;
  46. import static org.junit.Assert.fail;
  47. import java.io.File;
  48. import java.io.IOException;
  49. import java.io.PrintWriter;
  50. import org.eclipse.jgit.api.errors.ConcurrentRefUpdateException;
  51. import org.eclipse.jgit.api.errors.JGitInternalException;
  52. import org.eclipse.jgit.api.errors.NoFilepatternException;
  53. import org.eclipse.jgit.api.errors.NoHeadException;
  54. import org.eclipse.jgit.api.errors.NoMessageException;
  55. import org.eclipse.jgit.api.errors.WrongRepositoryStateException;
  56. import org.eclipse.jgit.errors.IncorrectObjectTypeException;
  57. import org.eclipse.jgit.errors.MissingObjectException;
  58. import org.eclipse.jgit.errors.UnmergedPathException;
  59. import org.eclipse.jgit.lib.Constants;
  60. import org.eclipse.jgit.lib.ObjectId;
  61. import org.eclipse.jgit.lib.PersonIdent;
  62. import org.eclipse.jgit.lib.RefUpdate;
  63. import org.eclipse.jgit.lib.RepositoryTestCase;
  64. import org.eclipse.jgit.revwalk.RevCommit;
  65. import org.eclipse.jgit.treewalk.TreeWalk;
  66. public class CommitAndLogCommandTests extends RepositoryTestCase {
  67. public void testSomeCommits() throws NoHeadException, NoMessageException,
  68. UnmergedPathException, ConcurrentRefUpdateException,
  69. JGitInternalException, WrongRepositoryStateException {
  70. // do 4 commits
  71. Git git = new Git(db);
  72. git.commit().setMessage("initial commit").call();
  73. git.commit().setMessage("second commit").setCommitter(committer).call();
  74. git.commit().setMessage("third commit").setAuthor(author).call();
  75. git.commit().setMessage("fourth commit").setAuthor(author)
  76. .setCommitter(committer).call();
  77. Iterable<RevCommit> commits = git.log().call();
  78. // check that all commits came in correctly
  79. PersonIdent defaultCommitter = new PersonIdent(db);
  80. PersonIdent expectedAuthors[] = new PersonIdent[] { defaultCommitter,
  81. committer, author, author };
  82. PersonIdent expectedCommitters[] = new PersonIdent[] {
  83. defaultCommitter, committer, defaultCommitter, committer };
  84. String expectedMessages[] = new String[] { "initial commit",
  85. "second commit", "third commit", "fourth commit" };
  86. int l = expectedAuthors.length - 1;
  87. for (RevCommit c : commits) {
  88. assertEquals(expectedAuthors[l].getName(), c.getAuthorIdent()
  89. .getName());
  90. assertEquals(expectedCommitters[l].getName(), c.getCommitterIdent()
  91. .getName());
  92. assertEquals(c.getFullMessage(), expectedMessages[l]);
  93. l--;
  94. }
  95. assertEquals(l, -1);
  96. }
  97. // try to do a commit without specifying a message. Should fail!
  98. public void testWrongParams() throws UnmergedPathException,
  99. NoHeadException, ConcurrentRefUpdateException,
  100. JGitInternalException, WrongRepositoryStateException {
  101. Git git = new Git(db);
  102. try {
  103. git.commit().setAuthor(author).call();
  104. fail("Didn't get the expected exception");
  105. } catch (NoMessageException e) {
  106. // expected
  107. }
  108. }
  109. // try to work with Commands after command has been invoked. Should throw
  110. // exceptions
  111. public void testMultipleInvocations() throws NoHeadException,
  112. ConcurrentRefUpdateException, NoMessageException,
  113. UnmergedPathException, JGitInternalException,
  114. WrongRepositoryStateException {
  115. Git git = new Git(db);
  116. CommitCommand commitCmd = git.commit();
  117. commitCmd.setMessage("initial commit").call();
  118. try {
  119. // check that setters can't be called after invocation
  120. commitCmd.setAuthor(author);
  121. fail("didn't catch the expected exception");
  122. } catch (IllegalStateException e) {
  123. // expected
  124. }
  125. LogCommand logCmd = git.log();
  126. logCmd.call();
  127. try {
  128. // check that call can't be called twice
  129. logCmd.call();
  130. fail("didn't catch the expected exception");
  131. } catch (IllegalStateException e) {
  132. // expected
  133. }
  134. }
  135. public void testMergeEmptyBranches() throws IOException, NoHeadException,
  136. NoMessageException, ConcurrentRefUpdateException,
  137. JGitInternalException, WrongRepositoryStateException {
  138. Git git = new Git(db);
  139. git.commit().setMessage("initial commit").call();
  140. RefUpdate r = db.updateRef("refs/heads/side");
  141. r.setNewObjectId(db.resolve(Constants.HEAD));
  142. assertEquals(r.forceUpdate(), RefUpdate.Result.NEW);
  143. RevCommit second = git.commit().setMessage("second commit").setCommitter(committer).call();
  144. db.updateRef(Constants.HEAD).link("refs/heads/side");
  145. RevCommit firstSide = git.commit().setMessage("first side commit").setAuthor(author).call();
  146. write(new File(db.getDirectory(), Constants.MERGE_HEAD), ObjectId
  147. .toString(db.resolve("refs/heads/master")));
  148. write(new File(db.getDirectory(), Constants.MERGE_MSG), "merging");
  149. RevCommit commit = git.commit().call();
  150. RevCommit[] parents = commit.getParents();
  151. assertEquals(parents[0], firstSide);
  152. assertEquals(parents[1], second);
  153. assertTrue(parents.length==2);
  154. }
  155. public void testAddUnstagedChanges() throws IOException, NoHeadException,
  156. NoMessageException, ConcurrentRefUpdateException,
  157. JGitInternalException, WrongRepositoryStateException,
  158. NoFilepatternException {
  159. File file = new File(db.getWorkTree(), "a.txt");
  160. file.createNewFile();
  161. PrintWriter writer = new PrintWriter(file);
  162. writer.print("content");
  163. writer.close();
  164. Git git = new Git(db);
  165. git.add().addFilepattern("a.txt").call();
  166. RevCommit commit = git.commit().setMessage("initial commit").call();
  167. TreeWalk tw = TreeWalk.forPath(db, "a.txt", commit.getTree());
  168. assertEquals("6b584e8ece562ebffc15d38808cd6b98fc3d97ea",
  169. tw.getObjectId(0).getName());
  170. writer = new PrintWriter(file);
  171. writer.print("content2");
  172. writer.close();
  173. commit = git.commit().setMessage("second commit").call();
  174. tw = TreeWalk.forPath(db, "a.txt", commit.getTree());
  175. assertEquals("6b584e8ece562ebffc15d38808cd6b98fc3d97ea",
  176. tw.getObjectId(0).getName());
  177. commit = git.commit().setAll(true).setMessage("third commit")
  178. .setAll(true).call();
  179. tw = TreeWalk.forPath(db, "a.txt", commit.getTree());
  180. assertEquals("db00fd65b218578127ea51f3dffac701f12f486a",
  181. tw.getObjectId(0).getName());
  182. }
  183. public void testCommitRange() throws NoHeadException, NoMessageException,
  184. UnmergedPathException, ConcurrentRefUpdateException,
  185. JGitInternalException, WrongRepositoryStateException,
  186. IncorrectObjectTypeException, MissingObjectException {
  187. // do 4 commits and set the range to the second and fourth one
  188. Git git = new Git(db);
  189. git.commit().setMessage("first commit").call();
  190. RevCommit second = git.commit().setMessage("second commit")
  191. .setCommitter(committer).call();
  192. git.commit().setMessage("third commit").setAuthor(author).call();
  193. RevCommit last = git.commit().setMessage("fourth commit").setAuthor(
  194. author)
  195. .setCommitter(committer).call();
  196. Iterable<RevCommit> commits = git.log().addRange(second.getId(),
  197. last.getId()).call();
  198. // check that we have the third and fourth commit
  199. PersonIdent defaultCommitter = new PersonIdent(db);
  200. PersonIdent expectedAuthors[] = new PersonIdent[] { author, author };
  201. PersonIdent expectedCommitters[] = new PersonIdent[] {
  202. defaultCommitter, committer };
  203. String expectedMessages[] = new String[] { "third commit",
  204. "fourth commit" };
  205. int l = expectedAuthors.length - 1;
  206. for (RevCommit c : commits) {
  207. assertEquals(expectedAuthors[l].getName(), c.getAuthorIdent()
  208. .getName());
  209. assertEquals(expectedCommitters[l].getName(), c.getCommitterIdent()
  210. .getName());
  211. assertEquals(c.getFullMessage(), expectedMessages[l]);
  212. l--;
  213. }
  214. assertEquals(l, -1);
  215. }
  216. }