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 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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.assertFalse;
  46. import static org.junit.Assert.assertTrue;
  47. import static org.junit.Assert.fail;
  48. import java.io.File;
  49. import java.io.IOException;
  50. import java.io.PrintWriter;
  51. import org.eclipse.jgit.api.errors.GitAPIException;
  52. import org.eclipse.jgit.api.errors.JGitInternalException;
  53. import org.eclipse.jgit.api.errors.NoMessageException;
  54. import org.eclipse.jgit.errors.IncorrectObjectTypeException;
  55. import org.eclipse.jgit.errors.MissingObjectException;
  56. import org.eclipse.jgit.junit.RepositoryTestCase;
  57. import org.eclipse.jgit.lib.Constants;
  58. import org.eclipse.jgit.lib.ObjectId;
  59. import org.eclipse.jgit.lib.PersonIdent;
  60. import org.eclipse.jgit.lib.RefUpdate;
  61. import org.eclipse.jgit.lib.ReflogReader;
  62. import org.eclipse.jgit.revwalk.RevCommit;
  63. import org.eclipse.jgit.treewalk.TreeWalk;
  64. import org.eclipse.jgit.util.FS;
  65. import org.eclipse.jgit.util.FileUtils;
  66. import org.eclipse.jgit.util.RawParseUtils;
  67. import org.junit.Test;
  68. /**
  69. * Testing the git commit and log commands
  70. */
  71. public class CommitAndLogCommandTests extends RepositoryTestCase {
  72. @Test
  73. public void testSomeCommits() throws JGitInternalException, IOException,
  74. GitAPIException {
  75. // do 4 commits
  76. Git git = new Git(db);
  77. git.commit().setMessage("initial commit").call();
  78. git.commit().setMessage("second commit").setCommitter(committer).call();
  79. git.commit().setMessage("third commit").setAuthor(author).call();
  80. git.commit().setMessage("fourth commit").setAuthor(author)
  81. .setCommitter(committer).call();
  82. Iterable<RevCommit> commits = git.log().call();
  83. // check that all commits came in correctly
  84. PersonIdent defaultCommitter = new PersonIdent(db);
  85. PersonIdent expectedAuthors[] = new PersonIdent[] { defaultCommitter,
  86. committer, author, author };
  87. PersonIdent expectedCommitters[] = new PersonIdent[] {
  88. defaultCommitter, committer, defaultCommitter, committer };
  89. String expectedMessages[] = new String[] { "initial commit",
  90. "second commit", "third commit", "fourth commit" };
  91. int l = expectedAuthors.length - 1;
  92. for (RevCommit c : commits) {
  93. assertEquals(expectedAuthors[l].getName(), c.getAuthorIdent()
  94. .getName());
  95. assertEquals(expectedCommitters[l].getName(), c.getCommitterIdent()
  96. .getName());
  97. assertEquals(c.getFullMessage(), expectedMessages[l]);
  98. l--;
  99. }
  100. assertEquals(l, -1);
  101. ReflogReader reader = db.getReflogReader(Constants.HEAD);
  102. assertTrue(reader.getLastEntry().getComment().startsWith("commit:"));
  103. reader = db.getReflogReader(db.getBranch());
  104. assertTrue(reader.getLastEntry().getComment().startsWith("commit:"));
  105. }
  106. @Test
  107. public void testLogWithFilter() throws IOException, JGitInternalException,
  108. GitAPIException {
  109. Git git = new Git(db);
  110. // create first file
  111. File file = new File(db.getWorkTree(), "a.txt");
  112. FileUtils.createNewFile(file);
  113. PrintWriter writer = new PrintWriter(file);
  114. writer.print("content1");
  115. writer.close();
  116. // First commit - a.txt file
  117. git.add().addFilepattern("a.txt").call();
  118. git.commit().setMessage("commit1").setCommitter(committer).call();
  119. // create second file
  120. file = new File(db.getWorkTree(), "b.txt");
  121. FileUtils.createNewFile(file);
  122. writer = new PrintWriter(file);
  123. writer.print("content2");
  124. writer.close();
  125. // Second commit - b.txt file
  126. git.add().addFilepattern("b.txt").call();
  127. git.commit().setMessage("commit2").setCommitter(committer).call();
  128. // First log - a.txt filter
  129. int count = 0;
  130. for (RevCommit c : git.log().addPath("a.txt").call()) {
  131. assertEquals("commit1", c.getFullMessage());
  132. count++;
  133. }
  134. assertEquals(1, count);
  135. // Second log - b.txt filter
  136. count = 0;
  137. for (RevCommit c : git.log().addPath("b.txt").call()) {
  138. assertEquals("commit2", c.getFullMessage());
  139. count++;
  140. }
  141. assertEquals(1, count);
  142. // Third log - without filter
  143. count = 0;
  144. for (RevCommit c : git.log().call()) {
  145. assertEquals(committer, c.getCommitterIdent());
  146. count++;
  147. }
  148. assertEquals(2, count);
  149. }
  150. // try to do a commit without specifying a message. Should fail!
  151. @Test
  152. public void testWrongParams() throws GitAPIException {
  153. Git git = new Git(db);
  154. try {
  155. git.commit().setAuthor(author).call();
  156. fail("Didn't get the expected exception");
  157. } catch (NoMessageException e) {
  158. // expected
  159. }
  160. }
  161. // try to work with Commands after command has been invoked. Should throw
  162. // exceptions
  163. @Test
  164. public void testMultipleInvocations() throws GitAPIException {
  165. Git git = new Git(db);
  166. CommitCommand commitCmd = git.commit();
  167. commitCmd.setMessage("initial commit").call();
  168. try {
  169. // check that setters can't be called after invocation
  170. commitCmd.setAuthor(author);
  171. fail("didn't catch the expected exception");
  172. } catch (IllegalStateException e) {
  173. // expected
  174. }
  175. LogCommand logCmd = git.log();
  176. logCmd.call();
  177. try {
  178. // check that call can't be called twice
  179. logCmd.call();
  180. fail("didn't catch the expected exception");
  181. } catch (IllegalStateException e) {
  182. // expected
  183. }
  184. }
  185. @Test
  186. public void testMergeEmptyBranches() throws IOException,
  187. JGitInternalException, GitAPIException {
  188. Git git = new Git(db);
  189. git.commit().setMessage("initial commit").call();
  190. RefUpdate r = db.updateRef("refs/heads/side");
  191. r.setNewObjectId(db.resolve(Constants.HEAD));
  192. assertEquals(r.forceUpdate(), RefUpdate.Result.NEW);
  193. RevCommit second = git.commit().setMessage("second commit").setCommitter(committer).call();
  194. db.updateRef(Constants.HEAD).link("refs/heads/side");
  195. RevCommit firstSide = git.commit().setMessage("first side commit").setAuthor(author).call();
  196. write(new File(db.getDirectory(), Constants.MERGE_HEAD), ObjectId
  197. .toString(db.resolve("refs/heads/master")));
  198. write(new File(db.getDirectory(), Constants.MERGE_MSG), "merging");
  199. RevCommit commit = git.commit().call();
  200. RevCommit[] parents = commit.getParents();
  201. assertEquals(parents[0], firstSide);
  202. assertEquals(parents[1], second);
  203. assertEquals(2, parents.length);
  204. }
  205. @Test
  206. public void testAddUnstagedChanges() throws IOException,
  207. JGitInternalException, GitAPIException {
  208. File file = new File(db.getWorkTree(), "a.txt");
  209. FileUtils.createNewFile(file);
  210. PrintWriter writer = new PrintWriter(file);
  211. writer.print("content");
  212. writer.close();
  213. Git git = new Git(db);
  214. git.add().addFilepattern("a.txt").call();
  215. RevCommit commit = git.commit().setMessage("initial commit").call();
  216. TreeWalk tw = TreeWalk.forPath(db, "a.txt", commit.getTree());
  217. assertEquals("6b584e8ece562ebffc15d38808cd6b98fc3d97ea",
  218. tw.getObjectId(0).getName());
  219. writer = new PrintWriter(file);
  220. writer.print("content2");
  221. writer.close();
  222. commit = git.commit().setMessage("second commit").call();
  223. tw = TreeWalk.forPath(db, "a.txt", commit.getTree());
  224. assertEquals("6b584e8ece562ebffc15d38808cd6b98fc3d97ea",
  225. tw.getObjectId(0).getName());
  226. commit = git.commit().setAll(true).setMessage("third commit")
  227. .setAll(true).call();
  228. tw = TreeWalk.forPath(db, "a.txt", commit.getTree());
  229. assertEquals("db00fd65b218578127ea51f3dffac701f12f486a",
  230. tw.getObjectId(0).getName());
  231. }
  232. @Test
  233. public void testModeChange() throws IOException, GitAPIException {
  234. if (System.getProperty("os.name").startsWith("Windows"))
  235. return; // SKIP
  236. Git git = new Git(db);
  237. // create file
  238. File file = new File(db.getWorkTree(), "a.txt");
  239. FileUtils.createNewFile(file);
  240. PrintWriter writer = new PrintWriter(file);
  241. writer.print("content1");
  242. writer.close();
  243. // First commit - a.txt file
  244. git.add().addFilepattern("a.txt").call();
  245. git.commit().setMessage("commit1").setCommitter(committer).call();
  246. // pure mode change should be committable
  247. FS fs = db.getFS();
  248. fs.setExecute(file, true);
  249. git.add().addFilepattern("a.txt").call();
  250. git.commit().setMessage("mode change").setCommitter(committer).call();
  251. // pure mode change should be committable with -o option
  252. fs.setExecute(file, false);
  253. git.add().addFilepattern("a.txt").call();
  254. git.commit().setMessage("mode change").setCommitter(committer)
  255. .setOnly("a.txt").call();
  256. }
  257. @Test
  258. public void testCommitRange() throws GitAPIException,
  259. JGitInternalException, MissingObjectException,
  260. IncorrectObjectTypeException {
  261. // do 4 commits and set the range to the second and fourth one
  262. Git git = new Git(db);
  263. git.commit().setMessage("first commit").call();
  264. RevCommit second = git.commit().setMessage("second commit")
  265. .setCommitter(committer).call();
  266. git.commit().setMessage("third commit").setAuthor(author).call();
  267. RevCommit last = git.commit().setMessage("fourth commit").setAuthor(
  268. author)
  269. .setCommitter(committer).call();
  270. Iterable<RevCommit> commits = git.log().addRange(second.getId(),
  271. last.getId()).call();
  272. // check that we have the third and fourth commit
  273. PersonIdent defaultCommitter = new PersonIdent(db);
  274. PersonIdent expectedAuthors[] = new PersonIdent[] { author, author };
  275. PersonIdent expectedCommitters[] = new PersonIdent[] {
  276. defaultCommitter, committer };
  277. String expectedMessages[] = new String[] { "third commit",
  278. "fourth commit" };
  279. int l = expectedAuthors.length - 1;
  280. for (RevCommit c : commits) {
  281. assertEquals(expectedAuthors[l].getName(), c.getAuthorIdent()
  282. .getName());
  283. assertEquals(expectedCommitters[l].getName(), c.getCommitterIdent()
  284. .getName());
  285. assertEquals(c.getFullMessage(), expectedMessages[l]);
  286. l--;
  287. }
  288. assertEquals(l, -1);
  289. }
  290. @Test
  291. public void testCommitAmend() throws JGitInternalException, IOException,
  292. GitAPIException {
  293. Git git = new Git(db);
  294. git.commit().setMessage("first comit").call(); // typo
  295. git.commit().setAmend(true).setMessage("first commit").call();
  296. Iterable<RevCommit> commits = git.log().call();
  297. int c = 0;
  298. for (RevCommit commit : commits) {
  299. assertEquals("first commit", commit.getFullMessage());
  300. c++;
  301. }
  302. assertEquals(1, c);
  303. ReflogReader reader = db.getReflogReader(Constants.HEAD);
  304. assertTrue(reader.getLastEntry().getComment()
  305. .startsWith("commit (amend):"));
  306. reader = db.getReflogReader(db.getBranch());
  307. assertTrue(reader.getLastEntry().getComment()
  308. .startsWith("commit (amend):"));
  309. }
  310. @Test
  311. public void testInsertChangeId() throws JGitInternalException,
  312. GitAPIException {
  313. Git git = new Git(db);
  314. String messageHeader = "Some header line\n\nSome detail explanation\n";
  315. String changeIdTemplate = "\nChange-Id: I"
  316. + ObjectId.zeroId().getName() + "\n";
  317. String messageFooter = "Some foooter lines\nAnother footer line\n";
  318. RevCommit commit = git.commit().setMessage(
  319. messageHeader + messageFooter)
  320. .setInsertChangeId(true).call();
  321. // we should find a real change id (at the end of the file)
  322. byte[] chars = commit.getFullMessage().getBytes();
  323. int lastLineBegin = RawParseUtils.prevLF(chars, chars.length - 2);
  324. String lastLine = RawParseUtils.decode(chars, lastLineBegin + 1,
  325. chars.length);
  326. assertTrue(lastLine.contains("Change-Id:"));
  327. assertFalse(lastLine.contains(
  328. "Change-Id: I" + ObjectId.zeroId().getName()));
  329. commit = git.commit().setMessage(
  330. messageHeader + changeIdTemplate + messageFooter)
  331. .setInsertChangeId(true).call();
  332. // we should find a real change id (in the line as dictated by the
  333. // template)
  334. chars = commit.getFullMessage().getBytes();
  335. int lineStart = 0;
  336. int lineEnd = 0;
  337. for (int i = 0; i < 4; i++) {
  338. lineStart = RawParseUtils.nextLF(chars, lineStart);
  339. }
  340. lineEnd = RawParseUtils.nextLF(chars, lineStart);
  341. String line = RawParseUtils.decode(chars, lineStart, lineEnd);
  342. assertTrue(line.contains("Change-Id:"));
  343. assertFalse(line.contains(
  344. "Change-Id: I" + ObjectId.zeroId().getName()));
  345. commit = git.commit().setMessage(
  346. messageHeader + changeIdTemplate + messageFooter)
  347. .setInsertChangeId(false).call();
  348. // we should find the untouched template
  349. chars = commit.getFullMessage().getBytes();
  350. lineStart = 0;
  351. lineEnd = 0;
  352. for (int i = 0; i < 4; i++) {
  353. lineStart = RawParseUtils.nextLF(chars, lineStart);
  354. }
  355. lineEnd = RawParseUtils.nextLF(chars, lineStart);
  356. line = RawParseUtils.decode(chars, lineStart, lineEnd);
  357. assertTrue(commit.getFullMessage().contains(
  358. "Change-Id: I" + ObjectId.zeroId().getName()));
  359. }
  360. }