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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503
  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.assertNotNull;
  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.Repository;
  65. import org.eclipse.jgit.lib.RepositoryTestCase;
  66. import org.eclipse.jgit.revwalk.RevCommit;
  67. import org.eclipse.jgit.revwalk.RevWalk;
  68. import org.eclipse.jgit.treewalk.TreeWalk;
  69. import org.eclipse.jgit.util.FileUtils;
  70. import org.junit.Test;
  71. /**
  72. * Testing the git commit and log commands
  73. *
  74. * Testing the 'commit only' option:
  75. *
  76. * I. A single file (f1.txt) specified as part of the --only/ -o option can have
  77. * one of the following (14) states:
  78. *
  79. * <pre>
  80. * | | expected result
  81. * ---------------------------------------------------------------------
  82. * | HEAD DirCache Worktree | HEAD DirCache
  83. * ---------------------------------------------------------------------
  84. * f1_1 | - - c | => e: path unknown
  85. * f1_2 | - c - | => no changes
  86. * f1_3 | c - - | - -
  87. * f1_4 | - c c | c c
  88. * f1_5 | c c - | - -
  89. * f1_6 | c - c | => no changes
  90. * f1_7 | c c c | => no changes
  91. * ---------------------------------------------------------------------
  92. * f1_8 | - c c' | c' c'
  93. * f1_9 | c - c' | c' c'
  94. * f1_10 | c c' - | - -
  95. * f1_11 | c c c' | c' c'
  96. * f1_12 | c c' c | => no changes
  97. * f1_13 | c c' c' | c' c'
  98. * ---------------------------------------------------------------------
  99. * f1_14 | c c' c'' | c'' c''
  100. * </pre>
  101. *
  102. * II. Scenarios that do not end with a successful commit (1, 2, 6, 7, 12) have
  103. * to be tested with a second file (f2.txt) specified that would lead to a
  104. * successful commit, if it were executed separately (e.g. scenario 14).
  105. *
  106. * <pre>
  107. * | | expected result
  108. * ---------------------------------------------------------------------------
  109. * | HEAD DirCache Worktree | HEAD DirCache
  110. * ---------------------------------------------------------------------------
  111. * f1_1_f2_14 | - - c | => e: path unknown
  112. * f1_2_f2_14 | - c - | - -
  113. * f1_6_f2_14 | c - c | c c
  114. * f1_7_f2_14 | c c c | c c
  115. * ---------------------------------------------------------------------------
  116. * f1_12_f2_14 | c c' c | c c
  117. * </pre>
  118. *
  119. * III. All scenarios (1-14, I-II) have to be tested with different repository
  120. * states, to check that the --only/ -o option does not change existing content
  121. * (HEAD and DirCache). The following states for a file (f3.txt) not specified
  122. * shall be tested:
  123. *
  124. * <pre>
  125. * | HEAD DirCache
  126. * --------------------
  127. * *_a | - -
  128. * *_b | - c
  129. * *_c | c c
  130. * *_d | c -
  131. * --------------------
  132. * *_e | c c'
  133. * </pre>
  134. **/
  135. public class CommitAndLogCommandTests extends RepositoryTestCase {
  136. private static final String F1 = "f1.txt";
  137. private static final String F2 = "f2.txt";
  138. private static final String F3 = "f3.txt";
  139. private static final String MSG = "commit";
  140. private static int A = 0;
  141. private static int B = 1;
  142. private static int C = 2;
  143. private static int D = 3;
  144. private static int E = 4;
  145. @Test
  146. public void testSomeCommits() throws NoHeadException, NoMessageException,
  147. UnmergedPathException, ConcurrentRefUpdateException,
  148. JGitInternalException, WrongRepositoryStateException {
  149. // do 4 commits
  150. Git git = new Git(db);
  151. git.commit().setMessage("initial commit").call();
  152. git.commit().setMessage("second commit").setCommitter(committer).call();
  153. git.commit().setMessage("third commit").setAuthor(author).call();
  154. git.commit().setMessage("fourth commit").setAuthor(author)
  155. .setCommitter(committer).call();
  156. Iterable<RevCommit> commits = git.log().call();
  157. // check that all commits came in correctly
  158. PersonIdent defaultCommitter = new PersonIdent(db);
  159. PersonIdent expectedAuthors[] = new PersonIdent[] { defaultCommitter,
  160. committer, author, author };
  161. PersonIdent expectedCommitters[] = new PersonIdent[] {
  162. defaultCommitter, committer, defaultCommitter, committer };
  163. String expectedMessages[] = new String[] { "initial commit",
  164. "second commit", "third commit", "fourth commit" };
  165. int l = expectedAuthors.length - 1;
  166. for (RevCommit c : commits) {
  167. assertEquals(expectedAuthors[l].getName(), c.getAuthorIdent()
  168. .getName());
  169. assertEquals(expectedCommitters[l].getName(), c.getCommitterIdent()
  170. .getName());
  171. assertEquals(c.getFullMessage(), expectedMessages[l]);
  172. l--;
  173. }
  174. assertEquals(l, -1);
  175. }
  176. // try to do a commit without specifying a message. Should fail!
  177. @Test
  178. public void testWrongParams() throws UnmergedPathException,
  179. NoHeadException, ConcurrentRefUpdateException,
  180. JGitInternalException, WrongRepositoryStateException {
  181. Git git = new Git(db);
  182. try {
  183. git.commit().setAuthor(author).call();
  184. fail("Didn't get the expected exception");
  185. } catch (NoMessageException e) {
  186. // expected
  187. }
  188. }
  189. // try to work with Commands after command has been invoked. Should throw
  190. // exceptions
  191. @Test
  192. public void testMultipleInvocations() throws NoHeadException,
  193. ConcurrentRefUpdateException, NoMessageException,
  194. UnmergedPathException, JGitInternalException,
  195. WrongRepositoryStateException {
  196. Git git = new Git(db);
  197. CommitCommand commitCmd = git.commit();
  198. commitCmd.setMessage("initial commit").call();
  199. try {
  200. // check that setters can't be called after invocation
  201. commitCmd.setAuthor(author);
  202. fail("didn't catch the expected exception");
  203. } catch (IllegalStateException e) {
  204. // expected
  205. }
  206. LogCommand logCmd = git.log();
  207. logCmd.call();
  208. try {
  209. // check that call can't be called twice
  210. logCmd.call();
  211. fail("didn't catch the expected exception");
  212. } catch (IllegalStateException e) {
  213. // expected
  214. }
  215. }
  216. @Test
  217. public void testMergeEmptyBranches() throws IOException, NoHeadException,
  218. NoMessageException, ConcurrentRefUpdateException,
  219. JGitInternalException, WrongRepositoryStateException {
  220. Git git = new Git(db);
  221. git.commit().setMessage("initial commit").call();
  222. RefUpdate r = db.updateRef("refs/heads/side");
  223. r.setNewObjectId(db.resolve(Constants.HEAD));
  224. assertEquals(r.forceUpdate(), RefUpdate.Result.NEW);
  225. RevCommit second = git.commit().setMessage("second commit").setCommitter(committer).call();
  226. db.updateRef(Constants.HEAD).link("refs/heads/side");
  227. RevCommit firstSide = git.commit().setMessage("first side commit").setAuthor(author).call();
  228. write(new File(db.getDirectory(), Constants.MERGE_HEAD), ObjectId
  229. .toString(db.resolve("refs/heads/master")));
  230. write(new File(db.getDirectory(), Constants.MERGE_MSG), "merging");
  231. RevCommit commit = git.commit().call();
  232. RevCommit[] parents = commit.getParents();
  233. assertEquals(parents[0], firstSide);
  234. assertEquals(parents[1], second);
  235. assertTrue(parents.length==2);
  236. }
  237. @Test
  238. public void testAddUnstagedChanges() throws IOException, NoHeadException,
  239. NoMessageException, ConcurrentRefUpdateException,
  240. JGitInternalException, WrongRepositoryStateException,
  241. NoFilepatternException {
  242. File file = new File(db.getWorkTree(), "a.txt");
  243. FileUtils.createNewFile(file);
  244. PrintWriter writer = new PrintWriter(file);
  245. writer.print("content");
  246. writer.close();
  247. Git git = new Git(db);
  248. git.add().addFilepattern("a.txt").call();
  249. RevCommit commit = git.commit().setMessage("initial commit").call();
  250. TreeWalk tw = TreeWalk.forPath(db, "a.txt", commit.getTree());
  251. assertEquals("6b584e8ece562ebffc15d38808cd6b98fc3d97ea",
  252. tw.getObjectId(0).getName());
  253. writer = new PrintWriter(file);
  254. writer.print("content2");
  255. writer.close();
  256. commit = git.commit().setMessage("second commit").call();
  257. tw = TreeWalk.forPath(db, "a.txt", commit.getTree());
  258. assertEquals("6b584e8ece562ebffc15d38808cd6b98fc3d97ea",
  259. tw.getObjectId(0).getName());
  260. commit = git.commit().setAll(true).setMessage("third commit")
  261. .setAll(true).call();
  262. tw = TreeWalk.forPath(db, "a.txt", commit.getTree());
  263. assertEquals("db00fd65b218578127ea51f3dffac701f12f486a",
  264. tw.getObjectId(0).getName());
  265. }
  266. @Test
  267. public void testCommitRange() throws NoHeadException, NoMessageException,
  268. UnmergedPathException, ConcurrentRefUpdateException,
  269. JGitInternalException, WrongRepositoryStateException,
  270. IncorrectObjectTypeException, MissingObjectException {
  271. // do 4 commits and set the range to the second and fourth one
  272. Git git = new Git(db);
  273. git.commit().setMessage("first commit").call();
  274. RevCommit second = git.commit().setMessage("second commit")
  275. .setCommitter(committer).call();
  276. git.commit().setMessage("third commit").setAuthor(author).call();
  277. RevCommit last = git.commit().setMessage("fourth commit").setAuthor(
  278. author)
  279. .setCommitter(committer).call();
  280. Iterable<RevCommit> commits = git.log().addRange(second.getId(),
  281. last.getId()).call();
  282. // check that we have the third and fourth commit
  283. PersonIdent defaultCommitter = new PersonIdent(db);
  284. PersonIdent expectedAuthors[] = new PersonIdent[] { author, author };
  285. PersonIdent expectedCommitters[] = new PersonIdent[] {
  286. defaultCommitter, committer };
  287. String expectedMessages[] = new String[] { "third commit",
  288. "fourth commit" };
  289. int l = expectedAuthors.length - 1;
  290. for (RevCommit c : commits) {
  291. assertEquals(expectedAuthors[l].getName(), c.getAuthorIdent()
  292. .getName());
  293. assertEquals(expectedCommitters[l].getName(), c.getCommitterIdent()
  294. .getName());
  295. assertEquals(c.getFullMessage(), expectedMessages[l]);
  296. l--;
  297. }
  298. assertEquals(l, -1);
  299. }
  300. @Test
  301. public void testCommitAmend() throws NoHeadException, NoMessageException,
  302. UnmergedPathException, ConcurrentRefUpdateException,
  303. JGitInternalException, WrongRepositoryStateException {
  304. Git git = new Git(db);
  305. git.commit().setMessage("first comit").call(); // typo
  306. git.commit().setAmend(true).setMessage("first commit").call();
  307. Iterable<RevCommit> commits = git.log().call();
  308. int c = 0;
  309. for (RevCommit commit : commits) {
  310. assertEquals("first commit", commit.getFullMessage());
  311. c++;
  312. }
  313. assertEquals(1, c);
  314. }
  315. @Test
  316. public void testOnlyOption_f1_1_a() throws Exception {
  317. final Git git = new Git(db);
  318. prepare_f3(git, A);
  319. prepare_f1_1(git);
  320. executeAndCheck_f1_1(git, A);
  321. }
  322. @Test
  323. public void testOnlyOption_f1_1_b() throws Exception {
  324. final Git git = new Git(db);
  325. prepare_f3(git, B);
  326. prepare_f1_1(git);
  327. executeAndCheck_f1_1(git, B);
  328. }
  329. @Test
  330. public void testOnlyOption_f1_1_c() throws Exception {
  331. final Git git = new Git(db);
  332. prepare_f3(git, C);
  333. prepare_f1_1(git);
  334. executeAndCheck_f1_1(git, C);
  335. }
  336. @Test
  337. public void testOnlyOption_f1_1_d() throws Exception {
  338. final Git git = new Git(db);
  339. prepare_f3(git, D);
  340. prepare_f1_1(git);
  341. executeAndCheck_f1_1(git, D);
  342. }
  343. @Test
  344. public void testOnlyOption_f1_1_e() throws Exception {
  345. final Git git = new Git(db);
  346. prepare_f3(git, E);
  347. prepare_f1_1(git);
  348. executeAndCheck_f1_1(git, E);
  349. }
  350. @Test
  351. public void testOnlyOption_f1_1_f2_14_a() throws Exception {
  352. final Git git = new Git(db);
  353. prepare_f3_f2_14(git, A);
  354. prepare_f1_1(git);
  355. executeAndCheck_f1_1_f2_f14(git, A);
  356. }
  357. @Test
  358. public void testOnlyOption_f1_1_f2_14_b() throws Exception {
  359. final Git git = new Git(db);
  360. prepare_f3_f2_14(git, B);
  361. prepare_f1_1(git);
  362. executeAndCheck_f1_1_f2_f14(git, B);
  363. }
  364. @Test
  365. public void testOnlyOption_f1_1_f2_14_c() throws Exception {
  366. final Git git = new Git(db);
  367. prepare_f3_f2_14(git, C);
  368. prepare_f1_1(git);
  369. executeAndCheck_f1_1_f2_f14(git, C);
  370. }
  371. @Test
  372. public void testOnlyOption_f1_1_f2_14_d() throws Exception {
  373. final Git git = new Git(db);
  374. prepare_f3_f2_14(git, D);
  375. prepare_f1_1(git);
  376. executeAndCheck_f1_1_f2_f14(git, D);
  377. }
  378. @Test
  379. public void testOnlyOption_f1_1_f2_14_e() throws Exception {
  380. final Git git = new Git(db);
  381. prepare_f3_f2_14(git, E);
  382. prepare_f1_1(git);
  383. executeAndCheck_f1_1_f2_f14(git, E);
  384. }
  385. @Test
  386. public void testOnlyOption_f1_2_a() throws Exception {
  387. final Git git = new Git(db);
  388. prepare_f3(git, A);
  389. prepare_f1_2(git);
  390. executeAndCheck_f1_2(git, A);
  391. }
  392. @Test
  393. public void testOnlyOption_f1_2_b() throws Exception {
  394. final Git git = new Git(db);
  395. prepare_f3(git, B);
  396. prepare_f1_2(git);
  397. executeAndCheck_f1_2(git, B);
  398. }
  399. @Test
  400. public void testOnlyOption_f1_2_c() throws Exception {
  401. final Git git = new Git(db);
  402. prepare_f3(git, C);
  403. prepare_f1_2(git);
  404. executeAndCheck_f1_2(git, C);
  405. }
  406. @Test
  407. public void testOnlyOption_f1_2_d() throws Exception {
  408. final Git git = new Git(db);
  409. prepare_f3(git, D);
  410. prepare_f1_2(git);
  411. executeAndCheck_f1_2(git, D);
  412. }
  413. @Test
  414. public void testOnlyOption_f1_2_e() throws Exception {
  415. final Git git = new Git(db);
  416. prepare_f3(git, E);
  417. prepare_f1_2(git);
  418. executeAndCheck_f1_2(git, E);
  419. }
  420. @Test
  421. public void testOnlyOption_f1_2_f2_14_a() throws Exception {
  422. final Git git = new Git(db);
  423. prepare_f3_f2_14(git, A);
  424. prepare_f1_2(git);
  425. executeAndCheck_f1_2_f2_f14(git, A);
  426. }
  427. @Test
  428. public void testOnlyOption_f1_2_f2_14_b() throws Exception {
  429. final Git git = new Git(db);
  430. prepare_f3_f2_14(git, B);
  431. prepare_f1_2(git);
  432. executeAndCheck_f1_2_f2_f14(git, B);
  433. }
  434. @Test
  435. public void testOnlyOption_f1_2_f2_14_c() throws Exception {
  436. final Git git = new Git(db);
  437. prepare_f3_f2_14(git, C);
  438. prepare_f1_2(git);
  439. executeAndCheck_f1_2_f2_f14(git, C);
  440. }
  441. @Test
  442. public void testOnlyOption_f1_2_f2_14_d() throws Exception {
  443. final Git git = new Git(db);
  444. prepare_f3_f2_14(git, D);
  445. prepare_f1_2(git);
  446. executeAndCheck_f1_2_f2_f14(git, D);
  447. }
  448. @Test
  449. public void testOnlyOption_f1_2_f2_14_e() throws Exception {
  450. final Git git = new Git(db);
  451. prepare_f3_f2_14(git, E);
  452. prepare_f1_2(git);
  453. executeAndCheck_f1_2_f2_f14(git, E);
  454. }
  455. @Test
  456. public void testOnlyOption_f1_3_a() throws Exception {
  457. final Git git = new Git(db);
  458. prepare_f3(git, A);
  459. prepare_f1_3(git);
  460. executeAndCheck_f1_3(git, A);
  461. }
  462. @Test
  463. public void testOnlyOption_f1_3_b() throws Exception {
  464. final Git git = new Git(db);
  465. prepare_f3(git, B);
  466. prepare_f1_3(git);
  467. executeAndCheck_f1_3(git, B);
  468. }
  469. @Test
  470. public void testOnlyOption_f1_3_c() throws Exception {
  471. final Git git = new Git(db);
  472. prepare_f3(git, C);
  473. prepare_f1_3(git);
  474. executeAndCheck_f1_3(git, C);
  475. }
  476. @Test
  477. public void testOnlyOption_f1_3_d() throws Exception {
  478. final Git git = new Git(db);
  479. prepare_f3(git, D);
  480. prepare_f1_3(git);
  481. executeAndCheck_f1_3(git, D);
  482. }
  483. @Test
  484. public void testOnlyOption_f1_3_e() throws Exception {
  485. final Git git = new Git(db);
  486. prepare_f3(git, E);
  487. prepare_f1_3(git);
  488. executeAndCheck_f1_3(git, E);
  489. }
  490. @Test
  491. public void testOnlyOption_f1_4_a() throws Exception {
  492. final Git git = new Git(db);
  493. prepare_f3(git, A);
  494. prepare_f1_4(git);
  495. executeAndCheck_f1_4(git, A);
  496. }
  497. @Test
  498. public void testOnlyOption_f1_4_b() throws Exception {
  499. final Git git = new Git(db);
  500. prepare_f3(git, B);
  501. prepare_f1_4(git);
  502. executeAndCheck_f1_4(git, B);
  503. }
  504. @Test
  505. public void testOnlyOption_f1_4_c() throws Exception {
  506. final Git git = new Git(db);
  507. prepare_f3(git, C);
  508. prepare_f1_4(git);
  509. executeAndCheck_f1_4(git, C);
  510. }
  511. @Test
  512. public void testOnlyOption_f1_4_d() throws Exception {
  513. final Git git = new Git(db);
  514. prepare_f3(git, D);
  515. prepare_f1_4(git);
  516. executeAndCheck_f1_4(git, D);
  517. }
  518. @Test
  519. public void testOnlyOption_f1_4_e() throws Exception {
  520. final Git git = new Git(db);
  521. prepare_f3(git, E);
  522. prepare_f1_4(git);
  523. executeAndCheck_f1_4(git, E);
  524. }
  525. @Test
  526. public void testOnlyOption_f1_5_a() throws Exception {
  527. final Git git = new Git(db);
  528. prepare_f3(git, A);
  529. prepare_f1_5(git);
  530. executeAndCheck_f1_5(git, A);
  531. }
  532. @Test
  533. public void testOnlyOption_f1_5_b() throws Exception {
  534. final Git git = new Git(db);
  535. prepare_f3(git, B);
  536. prepare_f1_5(git);
  537. executeAndCheck_f1_5(git, B);
  538. }
  539. @Test
  540. public void testOnlyOption_f1_5_c() throws Exception {
  541. final Git git = new Git(db);
  542. prepare_f3(git, C);
  543. prepare_f1_5(git);
  544. executeAndCheck_f1_5(git, C);
  545. }
  546. @Test
  547. public void testOnlyOption_f1_5_d() throws Exception {
  548. final Git git = new Git(db);
  549. prepare_f3(git, D);
  550. prepare_f1_5(git);
  551. executeAndCheck_f1_5(git, D);
  552. }
  553. @Test
  554. public void testOnlyOption_f1_5_e() throws Exception {
  555. final Git git = new Git(db);
  556. prepare_f3(git, E);
  557. prepare_f1_5(git);
  558. executeAndCheck_f1_5(git, E);
  559. }
  560. @Test
  561. public void testOnlyOption_f1_6_a() throws Exception {
  562. final Git git = new Git(db);
  563. prepare_f3(git, A);
  564. prepare_f1_6(git);
  565. executeAndCheck_f1_6(git, A);
  566. }
  567. @Test
  568. public void testOnlyOption_f1_6_b() throws Exception {
  569. final Git git = new Git(db);
  570. prepare_f3(git, B);
  571. prepare_f1_6(git);
  572. executeAndCheck_f1_6(git, B);
  573. }
  574. @Test
  575. public void testOnlyOption_f1_6_c() throws Exception {
  576. final Git git = new Git(db);
  577. prepare_f3(git, C);
  578. prepare_f1_6(git);
  579. executeAndCheck_f1_6(git, C);
  580. }
  581. @Test
  582. public void testOnlyOption_f1_6_d() throws Exception {
  583. final Git git = new Git(db);
  584. prepare_f3(git, D);
  585. prepare_f1_6(git);
  586. executeAndCheck_f1_6(git, D);
  587. }
  588. @Test
  589. public void testOnlyOption_f1_6_e() throws Exception {
  590. final Git git = new Git(db);
  591. prepare_f3(git, E);
  592. prepare_f1_6(git);
  593. executeAndCheck_f1_6(git, E);
  594. }
  595. @Test
  596. public void testOnlyOption_f1_6_f2_14_a() throws Exception {
  597. final Git git = new Git(db);
  598. prepare_f3_f2_14(git, A);
  599. prepare_f1_6(git);
  600. executeAndCheck_f1_6_f2_14(git, A);
  601. }
  602. @Test
  603. public void testOnlyOption_f1_6_f2_14_b() throws Exception {
  604. final Git git = new Git(db);
  605. prepare_f3_f2_14(git, B);
  606. prepare_f1_6(git);
  607. executeAndCheck_f1_6_f2_14(git, B);
  608. }
  609. @Test
  610. public void testOnlyOption_f1_6_f2_14_c() throws Exception {
  611. final Git git = new Git(db);
  612. prepare_f3_f2_14(git, C);
  613. prepare_f1_6(git);
  614. executeAndCheck_f1_6_f2_14(git, C);
  615. }
  616. @Test
  617. public void testOnlyOption_f1_6_f2_14_d() throws Exception {
  618. final Git git = new Git(db);
  619. prepare_f3_f2_14(git, D);
  620. prepare_f1_6(git);
  621. executeAndCheck_f1_6_f2_14(git, D);
  622. }
  623. @Test
  624. public void testOnlyOption_f1_6_f2_14_e() throws Exception {
  625. final Git git = new Git(db);
  626. prepare_f3_f2_14(git, E);
  627. prepare_f1_6(git);
  628. executeAndCheck_f1_6_f2_14(git, E);
  629. }
  630. @Test
  631. public void testOnlyOption_f1_7_a() throws Exception {
  632. final Git git = new Git(db);
  633. prepare_f3(git, A);
  634. prepare_f1_7(git);
  635. executeAndCheck_f1_7(git, A);
  636. }
  637. @Test
  638. public void testOnlyOption_f1_7_b() throws Exception {
  639. final Git git = new Git(db);
  640. prepare_f3(git, B);
  641. prepare_f1_7(git);
  642. executeAndCheck_f1_7(git, B);
  643. }
  644. @Test
  645. public void testOnlyOption_f1_7_c() throws Exception {
  646. final Git git = new Git(db);
  647. prepare_f3(git, C);
  648. prepare_f1_7(git);
  649. executeAndCheck_f1_7(git, C);
  650. }
  651. @Test
  652. public void testOnlyOption_f1_7_d() throws Exception {
  653. final Git git = new Git(db);
  654. prepare_f3(git, D);
  655. prepare_f1_7(git);
  656. executeAndCheck_f1_7(git, D);
  657. }
  658. @Test
  659. public void testOnlyOption_f1_7_e() throws Exception {
  660. final Git git = new Git(db);
  661. prepare_f3(git, E);
  662. prepare_f1_7(git);
  663. executeAndCheck_f1_7(git, E);
  664. }
  665. @Test
  666. public void testOnlyOption_f1_7_f2_14_a() throws Exception {
  667. final Git git = new Git(db);
  668. prepare_f3_f2_14(git, A);
  669. prepare_f1_7(git);
  670. executeAndCheck_f1_7_f2_14(git, A);
  671. }
  672. @Test
  673. public void testOnlyOption_f1_7_f2_14_b() throws Exception {
  674. final Git git = new Git(db);
  675. prepare_f3_f2_14(git, B);
  676. prepare_f1_7(git);
  677. executeAndCheck_f1_7_f2_14(git, B);
  678. }
  679. @Test
  680. public void testOnlyOption_f1_7_f2_14_c() throws Exception {
  681. final Git git = new Git(db);
  682. prepare_f3_f2_14(git, C);
  683. prepare_f1_7(git);
  684. executeAndCheck_f1_7_f2_14(git, C);
  685. }
  686. @Test
  687. public void testOnlyOption_f1_7_f2_14_d() throws Exception {
  688. final Git git = new Git(db);
  689. prepare_f3_f2_14(git, D);
  690. prepare_f1_7(git);
  691. executeAndCheck_f1_7_f2_14(git, D);
  692. }
  693. @Test
  694. public void testOnlyOption_f1_7_f2_14_e() throws Exception {
  695. final Git git = new Git(db);
  696. prepare_f3_f2_14(git, E);
  697. prepare_f1_7(git);
  698. executeAndCheck_f1_7_f2_14(git, E);
  699. }
  700. @Test
  701. public void testOnlyOption_f1_8_a() throws Exception {
  702. final Git git = new Git(db);
  703. prepare_f3(git, A);
  704. prepare_f1_8(git);
  705. executeAndCheck_f1_8(git, A);
  706. }
  707. @Test
  708. public void testOnlyOption_f1_8_b() throws Exception {
  709. final Git git = new Git(db);
  710. prepare_f3(git, B);
  711. prepare_f1_8(git);
  712. executeAndCheck_f1_8(git, B);
  713. }
  714. @Test
  715. public void testOnlyOption_f1_8_c() throws Exception {
  716. final Git git = new Git(db);
  717. prepare_f3(git, C);
  718. prepare_f1_8(git);
  719. executeAndCheck_f1_8(git, C);
  720. }
  721. @Test
  722. public void testOnlyOption_f1_8_d() throws Exception {
  723. final Git git = new Git(db);
  724. prepare_f3(git, D);
  725. prepare_f1_8(git);
  726. executeAndCheck_f1_8(git, D);
  727. }
  728. @Test
  729. public void testOnlyOption_f1_8_e() throws Exception {
  730. final Git git = new Git(db);
  731. prepare_f3(git, E);
  732. prepare_f1_8(git);
  733. executeAndCheck_f1_8(git, E);
  734. }
  735. @Test
  736. public void testOnlyOption_f1_9_a() throws Exception {
  737. final Git git = new Git(db);
  738. prepare_f3(git, A);
  739. prepare_f1_9(git);
  740. executeAndCheck_f1_9(git, A);
  741. }
  742. @Test
  743. public void testOnlyOption_f1_9_b() throws Exception {
  744. final Git git = new Git(db);
  745. prepare_f3(git, B);
  746. prepare_f1_9(git);
  747. executeAndCheck_f1_9(git, B);
  748. }
  749. @Test
  750. public void testOnlyOption_f1_9_c() throws Exception {
  751. final Git git = new Git(db);
  752. prepare_f3(git, C);
  753. prepare_f1_9(git);
  754. executeAndCheck_f1_9(git, C);
  755. }
  756. @Test
  757. public void testOnlyOption_f1_9_d() throws Exception {
  758. final Git git = new Git(db);
  759. prepare_f3(git, D);
  760. prepare_f1_9(git);
  761. executeAndCheck_f1_9(git, D);
  762. }
  763. @Test
  764. public void testOnlyOption_f1_9_e() throws Exception {
  765. final Git git = new Git(db);
  766. prepare_f3(git, E);
  767. prepare_f1_9(git);
  768. executeAndCheck_f1_9(git, E);
  769. }
  770. @Test
  771. public void testOnlyOption_f1_10_a() throws Exception {
  772. final Git git = new Git(db);
  773. prepare_f3(git, A);
  774. prepare_f1_10(git);
  775. executeAndCheck_f1_10(git, A);
  776. }
  777. @Test
  778. public void testOnlyOption_f1_10_b() throws Exception {
  779. final Git git = new Git(db);
  780. prepare_f3(git, B);
  781. prepare_f1_10(git);
  782. executeAndCheck_f1_10(git, B);
  783. }
  784. @Test
  785. public void testOnlyOption_f1_10_c() throws Exception {
  786. final Git git = new Git(db);
  787. prepare_f3(git, C);
  788. prepare_f1_10(git);
  789. executeAndCheck_f1_10(git, C);
  790. }
  791. @Test
  792. public void testOnlyOption_f1_10_d() throws Exception {
  793. final Git git = new Git(db);
  794. prepare_f3(git, D);
  795. prepare_f1_10(git);
  796. executeAndCheck_f1_10(git, D);
  797. }
  798. @Test
  799. public void testOnlyOption_f1_10_e() throws Exception {
  800. final Git git = new Git(db);
  801. prepare_f3(git, E);
  802. prepare_f1_10(git);
  803. executeAndCheck_f1_10(git, E);
  804. }
  805. @Test
  806. public void testOnlyOption_f1_11_a() throws Exception {
  807. final Git git = new Git(db);
  808. prepare_f3(git, A);
  809. prepare_f1_11(git);
  810. executeAndCheck_f1_11(git, A);
  811. }
  812. @Test
  813. public void testOnlyOption_f1_11_b() throws Exception {
  814. final Git git = new Git(db);
  815. prepare_f3(git, B);
  816. prepare_f1_11(git);
  817. executeAndCheck_f1_11(git, B);
  818. }
  819. @Test
  820. public void testOnlyOption_f1_11_c() throws Exception {
  821. final Git git = new Git(db);
  822. prepare_f3(git, C);
  823. prepare_f1_11(git);
  824. executeAndCheck_f1_11(git, C);
  825. }
  826. @Test
  827. public void testOnlyOption_f1_11_d() throws Exception {
  828. final Git git = new Git(db);
  829. prepare_f3(git, D);
  830. prepare_f1_11(git);
  831. executeAndCheck_f1_11(git, D);
  832. }
  833. @Test
  834. public void testOnlyOption_f1_11_e() throws Exception {
  835. final Git git = new Git(db);
  836. prepare_f3(git, E);
  837. prepare_f1_11(git);
  838. executeAndCheck_f1_11(git, E);
  839. }
  840. @Test
  841. public void testOnlyOption_f1_12_a() throws Exception {
  842. final Git git = new Git(db);
  843. prepare_f3(git, A);
  844. prepare_f1_12(git);
  845. executeAndCheck_f1_12(git, A);
  846. }
  847. @Test
  848. public void testOnlyOption_f1_12_b() throws Exception {
  849. final Git git = new Git(db);
  850. prepare_f3(git, B);
  851. prepare_f1_12(git);
  852. executeAndCheck_f1_12(git, B);
  853. }
  854. @Test
  855. public void testOnlyOption_f1_12_c() throws Exception {
  856. final Git git = new Git(db);
  857. prepare_f3(git, C);
  858. prepare_f1_12(git);
  859. executeAndCheck_f1_12(git, C);
  860. }
  861. @Test
  862. public void testOnlyOption_f1_12_d() throws Exception {
  863. final Git git = new Git(db);
  864. prepare_f3(git, D);
  865. prepare_f1_12(git);
  866. executeAndCheck_f1_12(git, D);
  867. }
  868. @Test
  869. public void testOnlyOption_f1_12_e() throws Exception {
  870. final Git git = new Git(db);
  871. prepare_f3(git, E);
  872. prepare_f1_12(git);
  873. executeAndCheck_f1_12(git, E);
  874. }
  875. @Test
  876. public void testOnlyOption_f1_12_f2_14_a() throws Exception {
  877. final Git git = new Git(db);
  878. prepare_f3_f2_14(git, A);
  879. prepare_f1_12(git);
  880. executeAndCheck_f1_12_f2_14(git, A);
  881. }
  882. @Test
  883. public void testOnlyOption_f1_12_f2_14_b() throws Exception {
  884. final Git git = new Git(db);
  885. prepare_f3_f2_14(git, B);
  886. prepare_f1_12(git);
  887. executeAndCheck_f1_12_f2_14(git, B);
  888. }
  889. @Test
  890. public void testOnlyOption_f1_12_f2_14_c() throws Exception {
  891. final Git git = new Git(db);
  892. prepare_f3_f2_14(git, C);
  893. prepare_f1_12(git);
  894. executeAndCheck_f1_12_f2_14(git, C);
  895. }
  896. @Test
  897. public void testOnlyOption_f1_12_f2_14_d() throws Exception {
  898. final Git git = new Git(db);
  899. prepare_f3_f2_14(git, D);
  900. prepare_f1_12(git);
  901. executeAndCheck_f1_12_f2_14(git, D);
  902. }
  903. @Test
  904. public void testOnlyOption_f1_12_f2_14_e() throws Exception {
  905. final Git git = new Git(db);
  906. prepare_f3_f2_14(git, E);
  907. prepare_f1_12(git);
  908. executeAndCheck_f1_12_f2_14(git, E);
  909. }
  910. @Test
  911. public void testOnlyOption_f1_13_a() throws Exception {
  912. final Git git = new Git(db);
  913. prepare_f3(git, A);
  914. prepare_f1_13(git);
  915. executeAndCheck_f1_13(git, A);
  916. }
  917. @Test
  918. public void testOnlyOption_f1_13_b() throws Exception {
  919. final Git git = new Git(db);
  920. prepare_f3(git, B);
  921. prepare_f1_13(git);
  922. executeAndCheck_f1_13(git, B);
  923. }
  924. @Test
  925. public void testOnlyOption_f1_13_c() throws Exception {
  926. final Git git = new Git(db);
  927. prepare_f3(git, C);
  928. prepare_f1_13(git);
  929. executeAndCheck_f1_13(git, C);
  930. }
  931. @Test
  932. public void testOnlyOption_f1_13_d() throws Exception {
  933. final Git git = new Git(db);
  934. prepare_f3(git, D);
  935. prepare_f1_13(git);
  936. executeAndCheck_f1_13(git, D);
  937. }
  938. @Test
  939. public void testOnlyOption_f1_13_e() throws Exception {
  940. final Git git = new Git(db);
  941. prepare_f3(git, E);
  942. prepare_f1_13(git);
  943. executeAndCheck_f1_13(git, E);
  944. }
  945. @Test
  946. public void testOnlyOption_f1_14_a() throws Exception {
  947. final Git git = new Git(db);
  948. prepare_f3(git, A);
  949. prepare_f1_14(git);
  950. executeAndCheck_f1_14(git, A);
  951. }
  952. @Test
  953. public void testOnlyOption_f1_14_b() throws Exception {
  954. final Git git = new Git(db);
  955. prepare_f3(git, B);
  956. prepare_f1_14(git);
  957. executeAndCheck_f1_14(git, B);
  958. }
  959. @Test
  960. public void testOnlyOption_f1_14_c() throws Exception {
  961. final Git git = new Git(db);
  962. prepare_f3(git, C);
  963. prepare_f1_14(git);
  964. executeAndCheck_f1_14(git, C);
  965. }
  966. @Test
  967. public void testOnlyOption_f1_14_d() throws Exception {
  968. final Git git = new Git(db);
  969. prepare_f3(git, D);
  970. prepare_f1_14(git);
  971. executeAndCheck_f1_14(git, D);
  972. }
  973. @Test
  974. public void testOnlyOption_f1_14_e() throws Exception {
  975. final Git git = new Git(db);
  976. prepare_f3(git, E);
  977. prepare_f1_14(git);
  978. executeAndCheck_f1_14(git, E);
  979. }
  980. @Test
  981. public void testOnlyOptionWithDirectory() throws Exception {
  982. final Git git = new Git(db);
  983. // write files
  984. final File f1 = writeTrashFile("d1/d2/f1.txt", "c1");
  985. writeTrashFile("d1/d2/f2.txt", "c2");
  986. final File f3 = writeTrashFile("d1/f3.txt", "c3");
  987. writeTrashFile("d1/f4.txt", "c4");
  988. final File f5 = writeTrashFile("d3/d4/f5.txt", "c5");
  989. writeTrashFile("d3/d4/f6.txt", "c6");
  990. final File f7 = writeTrashFile("d3/f7.txt", "c7");
  991. writeTrashFile("d3/f8.txt", "c8");
  992. final File f9 = writeTrashFile("d5/f9.txt", "c9");
  993. writeTrashFile("d5/f10.txt", "c10");
  994. final File f11 = writeTrashFile("d6/f11.txt", "c11");
  995. writeTrashFile("d6/f12.txt", "c12");
  996. // add files
  997. git.add().addFilepattern(".").call();
  998. // modify files, but do not stage changes
  999. write(f1, "c1'");
  1000. write(f3, "c3'");
  1001. write(f5, "c5'");
  1002. write(f7, "c7'");
  1003. write(f9, "c9'");
  1004. write(f11, "c11'");
  1005. // commit selected files only
  1006. git.commit().setOnly("d1").setOnly("d3/d4/").setOnly("d5")
  1007. .setOnly("d6/f11.txt").setMessage(MSG).call();
  1008. assertEquals("c1'", getHead(git, "d1/d2/f1.txt"));
  1009. assertEquals("c2", getHead(git, "d1/d2/f2.txt"));
  1010. assertEquals("c3'", getHead(git, "d1/f3.txt"));
  1011. assertEquals("c4", getHead(git, "d1/f4.txt"));
  1012. assertEquals("c5'", getHead(git, "d3/d4/f5.txt"));
  1013. assertEquals("c6", getHead(git, "d3/d4/f6.txt"));
  1014. assertEquals("", getHead(git, "d3/f7.txt"));
  1015. assertEquals("", getHead(git, "d3/f8.txt"));
  1016. assertEquals("c9'", getHead(git, "d5/f9.txt"));
  1017. assertEquals("c10", getHead(git, "d5/f10.txt"));
  1018. assertEquals("c11'", getHead(git, "d6/f11.txt"));
  1019. assertEquals("", getHead(git, "d6/f12.txt"));
  1020. assertEquals("[d1/d2/f1.txt, mode:100644, content:c1']"
  1021. + "[d1/d2/f2.txt, mode:100644, content:c2]"
  1022. + "[d1/f3.txt, mode:100644, content:c3']"
  1023. + "[d1/f4.txt, mode:100644, content:c4]"
  1024. + "[d3/d4/f5.txt, mode:100644, content:c5']"
  1025. + "[d3/d4/f6.txt, mode:100644, content:c6]"
  1026. + "[d3/f7.txt, mode:100644, content:c7]"
  1027. + "[d3/f8.txt, mode:100644, content:c8]"
  1028. + "[d5/f10.txt, mode:100644, content:c10]"
  1029. + "[d5/f9.txt, mode:100644, content:c9']"
  1030. + "[d6/f11.txt, mode:100644, content:c11']"
  1031. + "[d6/f12.txt, mode:100644, content:c12]", indexState(CONTENT));
  1032. }
  1033. @SuppressWarnings("unused")
  1034. private File prepare_f1_1(final Git git) throws IOException {
  1035. return writeTrashFile(F1, "c1");
  1036. }
  1037. private File prepare_f1_2(final Git git) throws Exception {
  1038. final File f1 = prepare_f1_4(git);
  1039. f1.delete();
  1040. return f1;
  1041. }
  1042. private File prepare_f1_3(final Git git) throws Exception {
  1043. final File f1 = prepare_f1_7(git);
  1044. git.rm().addFilepattern(F1).call();
  1045. return f1;
  1046. }
  1047. private File prepare_f1_4(final Git git) throws Exception {
  1048. final File f1 = prepare_f1_1(git);
  1049. git.add().addFilepattern(F1).call();
  1050. return f1;
  1051. }
  1052. private File prepare_f1_5(final Git git) throws Exception {
  1053. final File f1 = prepare_f1_7(git);
  1054. f1.delete();
  1055. return f1;
  1056. }
  1057. private File prepare_f1_6(final Git git) throws Exception {
  1058. final File f1 = prepare_f1_3(git);
  1059. write(f1, "c1");
  1060. return f1;
  1061. }
  1062. private File prepare_f1_7(final Git git) throws Exception {
  1063. final File f1 = prepare_f1_4(git);
  1064. git.commit().setOnly(F1).setMessage(MSG).call();
  1065. return f1;
  1066. }
  1067. private File prepare_f1_8(final Git git) throws Exception {
  1068. final File f1 = prepare_f1_4(git);
  1069. write(f1, "c1'");
  1070. return f1;
  1071. }
  1072. private File prepare_f1_9(final Git git) throws Exception {
  1073. final File f1 = prepare_f1_3(git);
  1074. write(f1, "c1'");
  1075. return f1;
  1076. }
  1077. private File prepare_f1_10(final Git git) throws Exception {
  1078. final File f1 = prepare_f1_9(git);
  1079. git.add().addFilepattern(F1).call();
  1080. f1.delete();
  1081. return f1;
  1082. }
  1083. private File prepare_f1_11(final Git git) throws Exception {
  1084. final File f1 = prepare_f1_7(git);
  1085. write(f1, "c1'");
  1086. return f1;
  1087. }
  1088. private File prepare_f1_12(final Git git) throws Exception {
  1089. final File f1 = prepare_f1_13(git);
  1090. write(f1, "c1");
  1091. return f1;
  1092. }
  1093. private File prepare_f1_13(final Git git) throws Exception {
  1094. final File f1 = prepare_f1_11(git);
  1095. git.add().addFilepattern(F1).call();
  1096. return f1;
  1097. }
  1098. private File prepare_f1_14(final Git git) throws Exception {
  1099. final File f1 = prepare_f1_13(git);
  1100. write(f1, "c1''");
  1101. return f1;
  1102. }
  1103. @SuppressWarnings("null")
  1104. private void executeAndCheck_f1_1(final Git git, final int state)
  1105. throws Exception {
  1106. JGitInternalException exception = null;
  1107. try {
  1108. git.commit().setOnly(F1).setMessage(MSG).call();
  1109. } catch (JGitInternalException e) {
  1110. exception = e;
  1111. }
  1112. assertNotNull(exception);
  1113. assertTrue(exception.getMessage().contains(F1));
  1114. assertEquals(expected_f3_head(state), getHead(git, F3));
  1115. assertEquals(expected_f3_idx(state), indexState(CONTENT));
  1116. }
  1117. @SuppressWarnings("null")
  1118. private void executeAndCheck_f1_1_f2_f14(final Git git, final int state)
  1119. throws Exception {
  1120. JGitInternalException exception = null;
  1121. try {
  1122. git.commit().setOnly(F1).setOnly(F2).setMessage(MSG).call();
  1123. } catch (JGitInternalException e) {
  1124. exception = e;
  1125. }
  1126. assertNotNull(exception);
  1127. assertTrue(exception.getMessage().contains(F1));
  1128. assertEquals("c2", getHead(git, F2));
  1129. assertEquals(expected_f3_head(state), getHead(git, F3));
  1130. assertEquals("[f2.txt, mode:100644, content:c2']"
  1131. + expected_f3_idx(state), indexState(CONTENT));
  1132. }
  1133. @SuppressWarnings("null")
  1134. private void executeAndCheck_f1_2(final Git git, final int state)
  1135. throws Exception {
  1136. JGitInternalException exception = null;
  1137. try {
  1138. git.commit().setOnly(F1).setMessage(MSG).call();
  1139. } catch (JGitInternalException e) {
  1140. exception = e;
  1141. }
  1142. assertNotNull(exception);
  1143. assertTrue(exception.getMessage().contains("No changes"));
  1144. assertEquals(expected_f3_head(state), getHead(git, F3));
  1145. assertEquals("[f1.txt, mode:100644, content:c1]"
  1146. + expected_f3_idx(state), indexState(CONTENT));
  1147. }
  1148. private void executeAndCheck_f1_2_f2_f14(final Git git, final int state)
  1149. throws Exception {
  1150. git.commit().setOnly(F1).setOnly(F2).setMessage(MSG).call();
  1151. assertEquals("", getHead(git, F1));
  1152. assertEquals("c2''", getHead(git, F2));
  1153. assertEquals(expected_f3_head(state), getHead(git, F3));
  1154. assertEquals("[f2.txt, mode:100644, content:c2'']"
  1155. + expected_f3_idx(state), indexState(CONTENT));
  1156. }
  1157. private void executeAndCheck_f1_3(final Git git, final int state)
  1158. throws Exception {
  1159. git.commit().setOnly(F1).setMessage(MSG).call();
  1160. assertEquals("", getHead(git, F1));
  1161. assertEquals(expected_f3_head(state), getHead(git, F3));
  1162. assertEquals(expected_f3_idx(state), indexState(CONTENT));
  1163. }
  1164. private void executeAndCheck_f1_4(final Git git, final int state)
  1165. throws Exception {
  1166. git.commit().setOnly(F1).setMessage(MSG).call();
  1167. assertEquals("c1", getHead(git, F1));
  1168. assertEquals(expected_f3_head(state), getHead(git, F3));
  1169. assertEquals("[f1.txt, mode:100644, content:c1]"
  1170. + expected_f3_idx(state), indexState(CONTENT));
  1171. }
  1172. private void executeAndCheck_f1_5(final Git git, final int state)
  1173. throws Exception {
  1174. executeAndCheck_f1_3(git, state);
  1175. }
  1176. @SuppressWarnings("null")
  1177. private void executeAndCheck_f1_6(final Git git, final int state)
  1178. throws Exception {
  1179. JGitInternalException exception = null;
  1180. try {
  1181. git.commit().setOnly(F1).setMessage(MSG).call();
  1182. } catch (JGitInternalException e) {
  1183. exception = e;
  1184. }
  1185. assertNotNull(exception);
  1186. assertTrue(exception.getMessage().contains("No changes"));
  1187. assertEquals(expected_f3_head(state), getHead(git, F3));
  1188. assertEquals(expected_f3_idx(state), indexState(CONTENT));
  1189. }
  1190. private void executeAndCheck_f1_6_f2_14(final Git git, final int state)
  1191. throws Exception {
  1192. git.commit().setOnly(F1).setOnly(F2).setMessage(MSG).call();
  1193. assertEquals("c1", getHead(git, F1));
  1194. assertEquals("c2''", getHead(git, F2));
  1195. assertEquals(expected_f3_head(state), getHead(git, F3));
  1196. assertEquals("[f1.txt, mode:100644, content:c1]"
  1197. + "[f2.txt, mode:100644, content:c2'']"
  1198. + expected_f3_idx(state), indexState(CONTENT));
  1199. }
  1200. private void executeAndCheck_f1_7(final Git git, final int state)
  1201. throws Exception {
  1202. executeAndCheck_f1_2(git, state);
  1203. }
  1204. private void executeAndCheck_f1_7_f2_14(final Git git, final int state)
  1205. throws Exception {
  1206. executeAndCheck_f1_6_f2_14(git, state);
  1207. }
  1208. private void executeAndCheck_f1_8(final Git git, final int state)
  1209. throws Exception {
  1210. git.commit().setOnly(F1).setMessage(MSG).call();
  1211. assertEquals("c1'", getHead(git, F1));
  1212. assertEquals(expected_f3_head(state), getHead(git, F3));
  1213. assertEquals("[f1.txt, mode:100644, content:c1']"
  1214. + expected_f3_idx(state), indexState(CONTENT));
  1215. }
  1216. private void executeAndCheck_f1_9(final Git git, final int state)
  1217. throws Exception {
  1218. executeAndCheck_f1_8(git, state);
  1219. }
  1220. private void executeAndCheck_f1_10(final Git git, final int state)
  1221. throws Exception {
  1222. executeAndCheck_f1_3(git, state);
  1223. }
  1224. private void executeAndCheck_f1_11(final Git git, final int state)
  1225. throws Exception {
  1226. executeAndCheck_f1_8(git, state);
  1227. }
  1228. @SuppressWarnings("null")
  1229. private void executeAndCheck_f1_12(final Git git, final int state)
  1230. throws Exception {
  1231. JGitInternalException exception = null;
  1232. try {
  1233. git.commit().setOnly(F1).setMessage(MSG).call();
  1234. } catch (JGitInternalException e) {
  1235. exception = e;
  1236. }
  1237. assertNotNull(exception);
  1238. assertTrue(exception.getMessage().contains("No changes"));
  1239. assertEquals(expected_f3_head(state), getHead(git, F3));
  1240. assertEquals("[f1.txt, mode:100644, content:c1']"
  1241. + expected_f3_idx(state), indexState(CONTENT));
  1242. }
  1243. private void executeAndCheck_f1_12_f2_14(final Git git, final int state)
  1244. throws Exception {
  1245. executeAndCheck_f1_6_f2_14(git, state);
  1246. }
  1247. private void executeAndCheck_f1_13(final Git git, final int state)
  1248. throws Exception {
  1249. executeAndCheck_f1_8(git, state);
  1250. }
  1251. private void executeAndCheck_f1_14(final Git git, final int state)
  1252. throws Exception {
  1253. git.commit().setOnly(F1).setMessage(MSG).call();
  1254. assertEquals("c1''", getHead(git, F1));
  1255. assertEquals(expected_f3_head(state), getHead(git, F3));
  1256. assertEquals("[f1.txt, mode:100644, content:c1'']"
  1257. + expected_f3_idx(state), indexState(CONTENT));
  1258. }
  1259. private void prepare_f3(final Git git, final int state) throws Exception {
  1260. prepare_f3_f2_14(git, state, false);
  1261. }
  1262. private void prepare_f3_f2_14(final Git git, final int state)
  1263. throws Exception {
  1264. prepare_f3_f2_14(git, state, true);
  1265. }
  1266. private void prepare_f3_f2_14(final Git git, final int state,
  1267. final boolean include_f2) throws Exception {
  1268. File f2 = null;
  1269. if (include_f2) {
  1270. f2 = writeTrashFile(F2, "c2");
  1271. git.add().addFilepattern(F2).call();
  1272. git.commit().setMessage(MSG).call();
  1273. }
  1274. if (state >= 1) {
  1275. writeTrashFile(F3, "c3");
  1276. git.add().addFilepattern(F3).call();
  1277. }
  1278. if (state >= 2)
  1279. git.commit().setMessage(MSG).call();
  1280. if (state >= 3)
  1281. git.rm().addFilepattern(F3).call();
  1282. if (state == 4) {
  1283. writeTrashFile(F3, "c3'");
  1284. git.add().addFilepattern(F3).call();
  1285. }
  1286. if (include_f2) {
  1287. write(f2, "c2'");
  1288. git.add().addFilepattern(F2).call();
  1289. write(f2, "c2''");
  1290. }
  1291. }
  1292. private String expected_f3_head(final int state) {
  1293. switch (state) {
  1294. case 0:
  1295. case 1:
  1296. return "";
  1297. case 2:
  1298. case 3:
  1299. case 4:
  1300. return "c3";
  1301. }
  1302. return null;
  1303. }
  1304. private String expected_f3_idx(final int state) {
  1305. switch (state) {
  1306. case 0:
  1307. case 3:
  1308. return "";
  1309. case 1:
  1310. case 2:
  1311. return "[f3.txt, mode:100644, content:c3]";
  1312. case 4:
  1313. return "[f3.txt, mode:100644, content:c3']";
  1314. }
  1315. return null;
  1316. }
  1317. private String getHead(final Git git, final String path) throws Exception {
  1318. try {
  1319. final Repository repo = git.getRepository();
  1320. final ObjectId headId = repo.resolve(Constants.HEAD + "^{commit}");
  1321. final TreeWalk tw = TreeWalk.forPath(repo, path,
  1322. new RevWalk(repo).parseTree(headId));
  1323. return new String(tw.getObjectReader().open(tw.getObjectId(0))
  1324. .getBytes());
  1325. } catch (Exception e) {
  1326. return "";
  1327. }
  1328. }
  1329. }