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.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. import org.eclipse.jgit.util.FileUtils;
  67. public class CommitAndLogCommandTests extends RepositoryTestCase {
  68. public void testSomeCommits() throws NoHeadException, NoMessageException,
  69. UnmergedPathException, ConcurrentRefUpdateException,
  70. JGitInternalException, WrongRepositoryStateException {
  71. // do 4 commits
  72. Git git = new Git(db);
  73. git.commit().setMessage("initial commit").call();
  74. git.commit().setMessage("second commit").setCommitter(committer).call();
  75. git.commit().setMessage("third commit").setAuthor(author).call();
  76. git.commit().setMessage("fourth commit").setAuthor(author)
  77. .setCommitter(committer).call();
  78. Iterable<RevCommit> commits = git.log().call();
  79. // check that all commits came in correctly
  80. PersonIdent defaultCommitter = new PersonIdent(db);
  81. PersonIdent expectedAuthors[] = new PersonIdent[] { defaultCommitter,
  82. committer, author, author };
  83. PersonIdent expectedCommitters[] = new PersonIdent[] {
  84. defaultCommitter, committer, defaultCommitter, committer };
  85. String expectedMessages[] = new String[] { "initial commit",
  86. "second commit", "third commit", "fourth commit" };
  87. int l = expectedAuthors.length - 1;
  88. for (RevCommit c : commits) {
  89. assertEquals(expectedAuthors[l].getName(), c.getAuthorIdent()
  90. .getName());
  91. assertEquals(expectedCommitters[l].getName(), c.getCommitterIdent()
  92. .getName());
  93. assertEquals(c.getFullMessage(), expectedMessages[l]);
  94. l--;
  95. }
  96. assertEquals(l, -1);
  97. }
  98. // try to do a commit without specifying a message. Should fail!
  99. public void testWrongParams() throws UnmergedPathException,
  100. NoHeadException, ConcurrentRefUpdateException,
  101. JGitInternalException, WrongRepositoryStateException {
  102. Git git = new Git(db);
  103. try {
  104. git.commit().setAuthor(author).call();
  105. fail("Didn't get the expected exception");
  106. } catch (NoMessageException e) {
  107. // expected
  108. }
  109. }
  110. // try to work with Commands after command has been invoked. Should throw
  111. // exceptions
  112. public void testMultipleInvocations() throws NoHeadException,
  113. ConcurrentRefUpdateException, NoMessageException,
  114. UnmergedPathException, JGitInternalException,
  115. WrongRepositoryStateException {
  116. Git git = new Git(db);
  117. CommitCommand commitCmd = git.commit();
  118. commitCmd.setMessage("initial commit").call();
  119. try {
  120. // check that setters can't be called after invocation
  121. commitCmd.setAuthor(author);
  122. fail("didn't catch the expected exception");
  123. } catch (IllegalStateException e) {
  124. // expected
  125. }
  126. LogCommand logCmd = git.log();
  127. logCmd.call();
  128. try {
  129. // check that call can't be called twice
  130. logCmd.call();
  131. fail("didn't catch the expected exception");
  132. } catch (IllegalStateException e) {
  133. // expected
  134. }
  135. }
  136. public void testMergeEmptyBranches() throws IOException, NoHeadException,
  137. NoMessageException, ConcurrentRefUpdateException,
  138. JGitInternalException, WrongRepositoryStateException {
  139. Git git = new Git(db);
  140. git.commit().setMessage("initial commit").call();
  141. RefUpdate r = db.updateRef("refs/heads/side");
  142. r.setNewObjectId(db.resolve(Constants.HEAD));
  143. assertEquals(r.forceUpdate(), RefUpdate.Result.NEW);
  144. RevCommit second = git.commit().setMessage("second commit").setCommitter(committer).call();
  145. db.updateRef(Constants.HEAD).link("refs/heads/side");
  146. RevCommit firstSide = git.commit().setMessage("first side commit").setAuthor(author).call();
  147. write(new File(db.getDirectory(), Constants.MERGE_HEAD), ObjectId
  148. .toString(db.resolve("refs/heads/master")));
  149. write(new File(db.getDirectory(), Constants.MERGE_MSG), "merging");
  150. RevCommit commit = git.commit().call();
  151. RevCommit[] parents = commit.getParents();
  152. assertEquals(parents[0], firstSide);
  153. assertEquals(parents[1], second);
  154. assertTrue(parents.length==2);
  155. }
  156. public void testAddUnstagedChanges() throws IOException, NoHeadException,
  157. NoMessageException, ConcurrentRefUpdateException,
  158. JGitInternalException, WrongRepositoryStateException,
  159. NoFilepatternException {
  160. File file = new File(db.getWorkTree(), "a.txt");
  161. FileUtils.createNewFile(file);
  162. PrintWriter writer = new PrintWriter(file);
  163. writer.print("content");
  164. writer.close();
  165. Git git = new Git(db);
  166. git.add().addFilepattern("a.txt").call();
  167. RevCommit commit = git.commit().setMessage("initial commit").call();
  168. TreeWalk tw = TreeWalk.forPath(db, "a.txt", commit.getTree());
  169. assertEquals("6b584e8ece562ebffc15d38808cd6b98fc3d97ea",
  170. tw.getObjectId(0).getName());
  171. writer = new PrintWriter(file);
  172. writer.print("content2");
  173. writer.close();
  174. commit = git.commit().setMessage("second commit").call();
  175. tw = TreeWalk.forPath(db, "a.txt", commit.getTree());
  176. assertEquals("6b584e8ece562ebffc15d38808cd6b98fc3d97ea",
  177. tw.getObjectId(0).getName());
  178. commit = git.commit().setAll(true).setMessage("third commit")
  179. .setAll(true).call();
  180. tw = TreeWalk.forPath(db, "a.txt", commit.getTree());
  181. assertEquals("db00fd65b218578127ea51f3dffac701f12f486a",
  182. tw.getObjectId(0).getName());
  183. }
  184. public void testCommitRange() throws NoHeadException, NoMessageException,
  185. UnmergedPathException, ConcurrentRefUpdateException,
  186. JGitInternalException, WrongRepositoryStateException,
  187. IncorrectObjectTypeException, MissingObjectException {
  188. // do 4 commits and set the range to the second and fourth one
  189. Git git = new Git(db);
  190. git.commit().setMessage("first commit").call();
  191. RevCommit second = git.commit().setMessage("second commit")
  192. .setCommitter(committer).call();
  193. git.commit().setMessage("third commit").setAuthor(author).call();
  194. RevCommit last = git.commit().setMessage("fourth commit").setAuthor(
  195. author)
  196. .setCommitter(committer).call();
  197. Iterable<RevCommit> commits = git.log().addRange(second.getId(),
  198. last.getId()).call();
  199. // check that we have the third and fourth commit
  200. PersonIdent defaultCommitter = new PersonIdent(db);
  201. PersonIdent expectedAuthors[] = new PersonIdent[] { author, author };
  202. PersonIdent expectedCommitters[] = new PersonIdent[] {
  203. defaultCommitter, committer };
  204. String expectedMessages[] = new String[] { "third commit",
  205. "fourth commit" };
  206. int l = expectedAuthors.length - 1;
  207. for (RevCommit c : commits) {
  208. assertEquals(expectedAuthors[l].getName(), c.getAuthorIdent()
  209. .getName());
  210. assertEquals(expectedCommitters[l].getName(), c.getCommitterIdent()
  211. .getName());
  212. assertEquals(c.getFullMessage(), expectedMessages[l]);
  213. l--;
  214. }
  215. assertEquals(l, -1);
  216. }
  217. }