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

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