Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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