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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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.lib.Constants;
  57. import org.eclipse.jgit.lib.ObjectId;
  58. import org.eclipse.jgit.lib.PersonIdent;
  59. import org.eclipse.jgit.lib.RefUpdate;
  60. import org.eclipse.jgit.lib.RepositoryTestCase;
  61. import org.eclipse.jgit.revwalk.RevCommit;
  62. import org.eclipse.jgit.storage.file.ReflogReader;
  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. assertTrue(parents.length==2);
  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. Git git = new Git(db);
  235. // create file
  236. File file = new File(db.getWorkTree(), "a.txt");
  237. FileUtils.createNewFile(file);
  238. PrintWriter writer = new PrintWriter(file);
  239. writer.print("content1");
  240. writer.close();
  241. // First commit - a.txt file
  242. git.add().addFilepattern("a.txt").call();
  243. git.commit().setMessage("commit1").setCommitter(committer).call();
  244. // pure mode change should be committable
  245. FS fs = db.getFS();
  246. fs.setExecute(file, true);
  247. git.add().addFilepattern("a.txt").call();
  248. git.commit().setMessage("mode change").setCommitter(committer).call();
  249. // pure mode change should be committable with -o option
  250. fs.setExecute(file, false);
  251. git.add().addFilepattern("a.txt").call();
  252. git.commit().setMessage("mode change").setCommitter(committer)
  253. .setOnly("a.txt").call();
  254. }
  255. @Test
  256. public void testCommitRange() throws GitAPIException,
  257. JGitInternalException, MissingObjectException,
  258. IncorrectObjectTypeException {
  259. // do 4 commits and set the range to the second and fourth one
  260. Git git = new Git(db);
  261. git.commit().setMessage("first commit").call();
  262. RevCommit second = git.commit().setMessage("second commit")
  263. .setCommitter(committer).call();
  264. git.commit().setMessage("third commit").setAuthor(author).call();
  265. RevCommit last = git.commit().setMessage("fourth commit").setAuthor(
  266. author)
  267. .setCommitter(committer).call();
  268. Iterable<RevCommit> commits = git.log().addRange(second.getId(),
  269. last.getId()).call();
  270. // check that we have the third and fourth commit
  271. PersonIdent defaultCommitter = new PersonIdent(db);
  272. PersonIdent expectedAuthors[] = new PersonIdent[] { author, author };
  273. PersonIdent expectedCommitters[] = new PersonIdent[] {
  274. defaultCommitter, committer };
  275. String expectedMessages[] = new String[] { "third commit",
  276. "fourth commit" };
  277. int l = expectedAuthors.length - 1;
  278. for (RevCommit c : commits) {
  279. assertEquals(expectedAuthors[l].getName(), c.getAuthorIdent()
  280. .getName());
  281. assertEquals(expectedCommitters[l].getName(), c.getCommitterIdent()
  282. .getName());
  283. assertEquals(c.getFullMessage(), expectedMessages[l]);
  284. l--;
  285. }
  286. assertEquals(l, -1);
  287. }
  288. @Test
  289. public void testCommitAmend() throws JGitInternalException, IOException,
  290. GitAPIException {
  291. Git git = new Git(db);
  292. git.commit().setMessage("first comit").call(); // typo
  293. git.commit().setAmend(true).setMessage("first commit").call();
  294. Iterable<RevCommit> commits = git.log().call();
  295. int c = 0;
  296. for (RevCommit commit : commits) {
  297. assertEquals("first commit", commit.getFullMessage());
  298. c++;
  299. }
  300. assertEquals(1, c);
  301. ReflogReader reader = db.getReflogReader(Constants.HEAD);
  302. assertTrue(reader.getLastEntry().getComment()
  303. .startsWith("commit (amend):"));
  304. reader = db.getReflogReader(db.getBranch());
  305. assertTrue(reader.getLastEntry().getComment()
  306. .startsWith("commit (amend):"));
  307. }
  308. @Test
  309. public void testInsertChangeId() throws JGitInternalException,
  310. GitAPIException {
  311. Git git = new Git(db);
  312. String messageHeader = "Some header line\n\nSome detail explanation\n";
  313. String changeIdTemplate = "\nChange-Id: I"
  314. + ObjectId.zeroId().getName() + "\n";
  315. String messageFooter = "Some foooter lines\nAnother footer line\n";
  316. RevCommit commit = git.commit().setMessage(
  317. messageHeader + messageFooter)
  318. .setInsertChangeId(true).call();
  319. // we should find a real change id (at the end of the file)
  320. byte[] chars = commit.getFullMessage().getBytes();
  321. int lastLineBegin = RawParseUtils.prevLF(chars, chars.length - 2);
  322. String lastLine = RawParseUtils.decode(chars, lastLineBegin + 1,
  323. chars.length);
  324. assertTrue(lastLine.contains("Change-Id:"));
  325. assertFalse(lastLine.contains(
  326. "Change-Id: I" + ObjectId.zeroId().getName()));
  327. commit = git.commit().setMessage(
  328. messageHeader + changeIdTemplate + messageFooter)
  329. .setInsertChangeId(true).call();
  330. // we should find a real change id (in the line as dictated by the
  331. // template)
  332. chars = commit.getFullMessage().getBytes();
  333. int lineStart = 0;
  334. int lineEnd = 0;
  335. for (int i = 0; i < 4; i++) {
  336. lineStart = RawParseUtils.nextLF(chars, lineStart);
  337. }
  338. lineEnd = RawParseUtils.nextLF(chars, lineStart);
  339. String line = RawParseUtils.decode(chars, lineStart, lineEnd);
  340. assertTrue(line.contains("Change-Id:"));
  341. assertFalse(line.contains(
  342. "Change-Id: I" + ObjectId.zeroId().getName()));
  343. commit = git.commit().setMessage(
  344. messageHeader + changeIdTemplate + messageFooter)
  345. .setInsertChangeId(false).call();
  346. // we should find the untouched template
  347. chars = commit.getFullMessage().getBytes();
  348. lineStart = 0;
  349. lineEnd = 0;
  350. for (int i = 0; i < 4; i++) {
  351. lineStart = RawParseUtils.nextLF(chars, lineStart);
  352. }
  353. lineEnd = RawParseUtils.nextLF(chars, lineStart);
  354. line = RawParseUtils.decode(chars, lineStart, lineEnd);
  355. assertTrue(commit.getFullMessage().contains(
  356. "Change-Id: I" + ObjectId.zeroId().getName()));
  357. }
  358. }