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.

CommitOnlyTest.java 32KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281
  1. /*
  2. * Copyright (C) 2011, Christian Halstrick <christian.halstrick@sap.com>
  3. * Copyright (C) 2011, Philipp Thun <philipp.thun@sap.com> and others
  4. *
  5. * This program and the accompanying materials are made available under the
  6. * terms of the Eclipse Distribution License v. 1.0 which is available at
  7. * https://www.eclipse.org/org/documents/edl-v10.php.
  8. *
  9. * SPDX-License-Identifier: BSD-3-Clause
  10. */
  11. package org.eclipse.jgit.api;
  12. import static java.nio.charset.StandardCharsets.UTF_8;
  13. import static org.junit.Assert.assertEquals;
  14. import static org.junit.Assert.assertNotNull;
  15. import static org.junit.Assert.assertTrue;
  16. import java.io.File;
  17. import java.io.IOException;
  18. import org.eclipse.jgit.api.errors.JGitInternalException;
  19. import org.eclipse.jgit.junit.RepositoryTestCase;
  20. import org.eclipse.jgit.lib.Constants;
  21. import org.eclipse.jgit.lib.ObjectId;
  22. import org.eclipse.jgit.lib.Repository;
  23. import org.eclipse.jgit.revwalk.RevWalk;
  24. import org.eclipse.jgit.treewalk.TreeWalk;
  25. import org.junit.Test;
  26. /**
  27. * Testing the 'commit only' option:
  28. *
  29. * I. A single file (f1.txt) specified as part of the --only/ -o option can have
  30. * one of the following (14) states:
  31. *
  32. * <pre>
  33. * | | expected result
  34. * ---------------------------------------------------------------------
  35. * | HEAD DirCache Worktree | HEAD DirCache
  36. * ---------------------------------------------------------------------
  37. * f1_1 | - - c | =&gt; e: path unknown
  38. * f1_2 | - c - | =&gt; no changes
  39. * f1_3 | c - - | - -
  40. * f1_4 | - c c | c c
  41. * f1_5 | c c - | - -
  42. * f1_6 | c - c | =&gt; no changes
  43. * f1_7 | c c c | =&gt; no changes
  44. * ---------------------------------------------------------------------
  45. * f1_8 | - c c' | c' c'
  46. * f1_9 | c - c' | c' c'
  47. * f1_10 | c c' - | - -
  48. * f1_11 | c c c' | c' c'
  49. * f1_12 | c c' c | =&gt; no changes
  50. * f1_13 | c c' c' | c' c'
  51. * ---------------------------------------------------------------------
  52. * f1_14 | c c' c'' | c'' c''
  53. * </pre>
  54. *
  55. * II. Scenarios that do not end with a successful commit (1, 2, 6, 7, 12) have
  56. * to be tested with a second file (f2.txt) specified that would lead to a
  57. * successful commit, if it were executed separately (e.g. scenario 14).
  58. *
  59. * <pre>
  60. * | | expected result
  61. * ---------------------------------------------------------------------------
  62. * | HEAD DirCache Worktree | HEAD DirCache
  63. * ---------------------------------------------------------------------------
  64. * f1_1_f2_14 | - - c | =&gt; e: path unknown
  65. * f1_2_f2_14 | - c - | - -
  66. * f1_6_f2_14 | c - c | c c
  67. * f1_7_f2_14 | c c c | c c
  68. * ---------------------------------------------------------------------------
  69. * f1_12_f2_14 | c c' c | c c
  70. * </pre>
  71. *
  72. * III. All scenarios (1-14, I-II) have to be tested with different repository
  73. * states, to check that the --only/ -o option does not change existing content
  74. * (HEAD and DirCache). The following states for a file (f3.txt) not specified
  75. * shall be tested:
  76. *
  77. * <pre>
  78. * | HEAD DirCache
  79. * --------------------
  80. * *_a | - -
  81. * *_b | - c
  82. * *_c | c c
  83. * *_d | c -
  84. * --------------------
  85. * *_e | c c'
  86. * </pre>
  87. **/
  88. public class CommitOnlyTest extends RepositoryTestCase {
  89. private static final String F1 = "f1.txt";
  90. private static final String F2 = "f2.txt";
  91. private static final String F3 = "f3.txt";
  92. private static final String MSG = "commit";
  93. private static int A = 0;
  94. private static int B = 1;
  95. private static int C = 2;
  96. private static int D = 3;
  97. private static int E = 4;
  98. @Test
  99. public void testOnlyOption_f1_1_a() throws Exception {
  100. final Git git = new Git(db);
  101. prepare_f3(git, A);
  102. prepare_f1_1(git);
  103. executeAndCheck_f1_1(git, A);
  104. }
  105. @Test
  106. public void testOnlyOption_f1_1_b() throws Exception {
  107. final Git git = new Git(db);
  108. prepare_f3(git, B);
  109. prepare_f1_1(git);
  110. executeAndCheck_f1_1(git, B);
  111. }
  112. @Test
  113. public void testOnlyOption_f1_1_c() throws Exception {
  114. final Git git = new Git(db);
  115. prepare_f3(git, C);
  116. prepare_f1_1(git);
  117. executeAndCheck_f1_1(git, C);
  118. }
  119. @Test
  120. public void testOnlyOption_f1_1_d() throws Exception {
  121. final Git git = new Git(db);
  122. prepare_f3(git, D);
  123. prepare_f1_1(git);
  124. executeAndCheck_f1_1(git, D);
  125. }
  126. @Test
  127. public void testOnlyOption_f1_1_e() throws Exception {
  128. final Git git = new Git(db);
  129. prepare_f3(git, E);
  130. prepare_f1_1(git);
  131. executeAndCheck_f1_1(git, E);
  132. }
  133. @Test
  134. public void testOnlyOption_f1_1_f2_14_a() throws Exception {
  135. final Git git = new Git(db);
  136. prepare_f3_f2_14(git, A);
  137. prepare_f1_1(git);
  138. executeAndCheck_f1_1_f2_f14(git, A);
  139. }
  140. @Test
  141. public void testOnlyOption_f1_1_f2_14_b() throws Exception {
  142. final Git git = new Git(db);
  143. prepare_f3_f2_14(git, B);
  144. prepare_f1_1(git);
  145. executeAndCheck_f1_1_f2_f14(git, B);
  146. }
  147. @Test
  148. public void testOnlyOption_f1_1_f2_14_c() throws Exception {
  149. final Git git = new Git(db);
  150. prepare_f3_f2_14(git, C);
  151. prepare_f1_1(git);
  152. executeAndCheck_f1_1_f2_f14(git, C);
  153. }
  154. @Test
  155. public void testOnlyOption_f1_1_f2_14_d() throws Exception {
  156. final Git git = new Git(db);
  157. prepare_f3_f2_14(git, D);
  158. prepare_f1_1(git);
  159. executeAndCheck_f1_1_f2_f14(git, D);
  160. }
  161. @Test
  162. public void testOnlyOption_f1_1_f2_14_e() throws Exception {
  163. final Git git = new Git(db);
  164. prepare_f3_f2_14(git, E);
  165. prepare_f1_1(git);
  166. executeAndCheck_f1_1_f2_f14(git, E);
  167. }
  168. @Test
  169. public void testOnlyOption_f1_2_a() throws Exception {
  170. final Git git = new Git(db);
  171. prepare_f3(git, A);
  172. prepare_f1_2(git);
  173. executeAndCheck_f1_2(git, A);
  174. }
  175. @Test
  176. public void testOnlyOption_f1_2_b() throws Exception {
  177. final Git git = new Git(db);
  178. prepare_f3(git, B);
  179. prepare_f1_2(git);
  180. executeAndCheck_f1_2(git, B);
  181. }
  182. @Test
  183. public void testOnlyOption_f1_2_c() throws Exception {
  184. final Git git = new Git(db);
  185. prepare_f3(git, C);
  186. prepare_f1_2(git);
  187. executeAndCheck_f1_2(git, C);
  188. }
  189. @Test
  190. public void testOnlyOption_f1_2_d() throws Exception {
  191. final Git git = new Git(db);
  192. prepare_f3(git, D);
  193. prepare_f1_2(git);
  194. executeAndCheck_f1_2(git, D);
  195. }
  196. @Test
  197. public void testOnlyOption_f1_2_e() throws Exception {
  198. final Git git = new Git(db);
  199. prepare_f3(git, E);
  200. prepare_f1_2(git);
  201. executeAndCheck_f1_2(git, E);
  202. }
  203. @Test
  204. public void testOnlyOption_f1_2_f2_14_a() throws Exception {
  205. final Git git = new Git(db);
  206. prepare_f3_f2_14(git, A);
  207. prepare_f1_2(git);
  208. executeAndCheck_f1_2_f2_f14(git, A);
  209. }
  210. @Test
  211. public void testOnlyOption_f1_2_f2_14_b() throws Exception {
  212. final Git git = new Git(db);
  213. prepare_f3_f2_14(git, B);
  214. prepare_f1_2(git);
  215. executeAndCheck_f1_2_f2_f14(git, B);
  216. }
  217. @Test
  218. public void testOnlyOption_f1_2_f2_14_c() throws Exception {
  219. final Git git = new Git(db);
  220. prepare_f3_f2_14(git, C);
  221. prepare_f1_2(git);
  222. executeAndCheck_f1_2_f2_f14(git, C);
  223. }
  224. @Test
  225. public void testOnlyOption_f1_2_f2_14_d() throws Exception {
  226. final Git git = new Git(db);
  227. prepare_f3_f2_14(git, D);
  228. prepare_f1_2(git);
  229. executeAndCheck_f1_2_f2_f14(git, D);
  230. }
  231. @Test
  232. public void testOnlyOption_f1_2_f2_14_e() throws Exception {
  233. final Git git = new Git(db);
  234. prepare_f3_f2_14(git, E);
  235. prepare_f1_2(git);
  236. executeAndCheck_f1_2_f2_f14(git, E);
  237. }
  238. @Test
  239. public void testOnlyOption_f1_3_a() throws Exception {
  240. final Git git = new Git(db);
  241. prepare_f3(git, A);
  242. prepare_f1_3(git);
  243. executeAndCheck_f1_3(git, A);
  244. }
  245. @Test
  246. public void testOnlyOption_f1_3_b() throws Exception {
  247. final Git git = new Git(db);
  248. prepare_f3(git, B);
  249. prepare_f1_3(git);
  250. executeAndCheck_f1_3(git, B);
  251. }
  252. @Test
  253. public void testOnlyOption_f1_3_c() throws Exception {
  254. final Git git = new Git(db);
  255. prepare_f3(git, C);
  256. prepare_f1_3(git);
  257. executeAndCheck_f1_3(git, C);
  258. }
  259. @Test
  260. public void testOnlyOption_f1_3_d() throws Exception {
  261. final Git git = new Git(db);
  262. prepare_f3(git, D);
  263. prepare_f1_3(git);
  264. executeAndCheck_f1_3(git, D);
  265. }
  266. @Test
  267. public void testOnlyOption_f1_3_e() throws Exception {
  268. final Git git = new Git(db);
  269. prepare_f3(git, E);
  270. prepare_f1_3(git);
  271. executeAndCheck_f1_3(git, E);
  272. }
  273. @Test
  274. public void testOnlyOption_f1_4_a() throws Exception {
  275. final Git git = new Git(db);
  276. prepare_f3(git, A);
  277. prepare_f1_4(git);
  278. executeAndCheck_f1_4(git, A);
  279. }
  280. @Test
  281. public void testOnlyOption_f1_4_b() throws Exception {
  282. final Git git = new Git(db);
  283. prepare_f3(git, B);
  284. prepare_f1_4(git);
  285. executeAndCheck_f1_4(git, B);
  286. }
  287. @Test
  288. public void testOnlyOption_f1_4_c() throws Exception {
  289. final Git git = new Git(db);
  290. prepare_f3(git, C);
  291. prepare_f1_4(git);
  292. executeAndCheck_f1_4(git, C);
  293. }
  294. @Test
  295. public void testOnlyOption_f1_4_d() throws Exception {
  296. final Git git = new Git(db);
  297. prepare_f3(git, D);
  298. prepare_f1_4(git);
  299. executeAndCheck_f1_4(git, D);
  300. }
  301. @Test
  302. public void testOnlyOption_f1_4_e() throws Exception {
  303. final Git git = new Git(db);
  304. prepare_f3(git, E);
  305. prepare_f1_4(git);
  306. executeAndCheck_f1_4(git, E);
  307. }
  308. @Test
  309. public void testOnlyOption_f1_5_a() throws Exception {
  310. final Git git = new Git(db);
  311. prepare_f3(git, A);
  312. prepare_f1_5(git);
  313. executeAndCheck_f1_5(git, A);
  314. }
  315. @Test
  316. public void testOnlyOption_f1_5_b() throws Exception {
  317. final Git git = new Git(db);
  318. prepare_f3(git, B);
  319. prepare_f1_5(git);
  320. executeAndCheck_f1_5(git, B);
  321. }
  322. @Test
  323. public void testOnlyOption_f1_5_c() throws Exception {
  324. final Git git = new Git(db);
  325. prepare_f3(git, C);
  326. prepare_f1_5(git);
  327. executeAndCheck_f1_5(git, C);
  328. }
  329. @Test
  330. public void testOnlyOption_f1_5_d() throws Exception {
  331. final Git git = new Git(db);
  332. prepare_f3(git, D);
  333. prepare_f1_5(git);
  334. executeAndCheck_f1_5(git, D);
  335. }
  336. @Test
  337. public void testOnlyOption_f1_5_e() throws Exception {
  338. final Git git = new Git(db);
  339. prepare_f3(git, E);
  340. prepare_f1_5(git);
  341. executeAndCheck_f1_5(git, E);
  342. }
  343. @Test
  344. public void testOnlyOption_f1_6_a() throws Exception {
  345. final Git git = new Git(db);
  346. prepare_f3(git, A);
  347. prepare_f1_6(git);
  348. executeAndCheck_f1_6(git, A);
  349. }
  350. @Test
  351. public void testOnlyOption_f1_6_b() throws Exception {
  352. final Git git = new Git(db);
  353. prepare_f3(git, B);
  354. prepare_f1_6(git);
  355. executeAndCheck_f1_6(git, B);
  356. }
  357. @Test
  358. public void testOnlyOption_f1_6_c() throws Exception {
  359. final Git git = new Git(db);
  360. prepare_f3(git, C);
  361. prepare_f1_6(git);
  362. executeAndCheck_f1_6(git, C);
  363. }
  364. @Test
  365. public void testOnlyOption_f1_6_d() throws Exception {
  366. final Git git = new Git(db);
  367. prepare_f3(git, D);
  368. prepare_f1_6(git);
  369. executeAndCheck_f1_6(git, D);
  370. }
  371. @Test
  372. public void testOnlyOption_f1_6_e() throws Exception {
  373. final Git git = new Git(db);
  374. prepare_f3(git, E);
  375. prepare_f1_6(git);
  376. executeAndCheck_f1_6(git, E);
  377. }
  378. @Test
  379. public void testOnlyOption_f1_6_f2_14_a() throws Exception {
  380. final Git git = new Git(db);
  381. prepare_f3_f2_14(git, A);
  382. prepare_f1_6(git);
  383. executeAndCheck_f1_6_f2_14(git, A);
  384. }
  385. @Test
  386. public void testOnlyOption_f1_6_f2_14_b() throws Exception {
  387. final Git git = new Git(db);
  388. prepare_f3_f2_14(git, B);
  389. prepare_f1_6(git);
  390. executeAndCheck_f1_6_f2_14(git, B);
  391. }
  392. @Test
  393. public void testOnlyOption_f1_6_f2_14_c() throws Exception {
  394. final Git git = new Git(db);
  395. prepare_f3_f2_14(git, C);
  396. prepare_f1_6(git);
  397. executeAndCheck_f1_6_f2_14(git, C);
  398. }
  399. @Test
  400. public void testOnlyOption_f1_6_f2_14_d() throws Exception {
  401. final Git git = new Git(db);
  402. prepare_f3_f2_14(git, D);
  403. prepare_f1_6(git);
  404. executeAndCheck_f1_6_f2_14(git, D);
  405. }
  406. @Test
  407. public void testOnlyOption_f1_6_f2_14_e() throws Exception {
  408. final Git git = new Git(db);
  409. prepare_f3_f2_14(git, E);
  410. prepare_f1_6(git);
  411. executeAndCheck_f1_6_f2_14(git, E);
  412. }
  413. @Test
  414. public void testOnlyOption_f1_7_a() throws Exception {
  415. final Git git = new Git(db);
  416. prepare_f3(git, A);
  417. prepare_f1_7(git);
  418. executeAndCheck_f1_7(git, A);
  419. }
  420. @Test
  421. public void testOnlyOption_f1_7_b() throws Exception {
  422. final Git git = new Git(db);
  423. prepare_f3(git, B);
  424. prepare_f1_7(git);
  425. executeAndCheck_f1_7(git, B);
  426. }
  427. @Test
  428. public void testOnlyOption_f1_7_c() throws Exception {
  429. final Git git = new Git(db);
  430. prepare_f3(git, C);
  431. prepare_f1_7(git);
  432. executeAndCheck_f1_7(git, C);
  433. }
  434. @Test
  435. public void testOnlyOption_f1_7_d() throws Exception {
  436. final Git git = new Git(db);
  437. prepare_f3(git, D);
  438. prepare_f1_7(git);
  439. executeAndCheck_f1_7(git, D);
  440. }
  441. @Test
  442. public void testOnlyOption_f1_7_e() throws Exception {
  443. final Git git = new Git(db);
  444. prepare_f3(git, E);
  445. prepare_f1_7(git);
  446. executeAndCheck_f1_7(git, E);
  447. }
  448. @Test
  449. public void testOnlyOption_f1_7_f2_14_a() throws Exception {
  450. final Git git = new Git(db);
  451. prepare_f3_f2_14(git, A);
  452. prepare_f1_7(git);
  453. executeAndCheck_f1_7_f2_14(git, A);
  454. }
  455. @Test
  456. public void testOnlyOption_f1_7_f2_14_b() throws Exception {
  457. final Git git = new Git(db);
  458. prepare_f3_f2_14(git, B);
  459. prepare_f1_7(git);
  460. executeAndCheck_f1_7_f2_14(git, B);
  461. }
  462. @Test
  463. public void testOnlyOption_f1_7_f2_14_c() throws Exception {
  464. final Git git = new Git(db);
  465. prepare_f3_f2_14(git, C);
  466. prepare_f1_7(git);
  467. executeAndCheck_f1_7_f2_14(git, C);
  468. }
  469. @Test
  470. public void testOnlyOption_f1_7_f2_14_d() throws Exception {
  471. final Git git = new Git(db);
  472. prepare_f3_f2_14(git, D);
  473. prepare_f1_7(git);
  474. executeAndCheck_f1_7_f2_14(git, D);
  475. }
  476. @Test
  477. public void testOnlyOption_f1_7_f2_14_e() throws Exception {
  478. final Git git = new Git(db);
  479. prepare_f3_f2_14(git, E);
  480. prepare_f1_7(git);
  481. executeAndCheck_f1_7_f2_14(git, E);
  482. }
  483. @Test
  484. public void testOnlyOption_f1_8_a() throws Exception {
  485. final Git git = new Git(db);
  486. prepare_f3(git, A);
  487. prepare_f1_8(git);
  488. executeAndCheck_f1_8(git, A);
  489. }
  490. @Test
  491. public void testOnlyOption_f1_8_b() throws Exception {
  492. final Git git = new Git(db);
  493. prepare_f3(git, B);
  494. prepare_f1_8(git);
  495. executeAndCheck_f1_8(git, B);
  496. }
  497. @Test
  498. public void testOnlyOption_f1_8_c() throws Exception {
  499. final Git git = new Git(db);
  500. prepare_f3(git, C);
  501. prepare_f1_8(git);
  502. executeAndCheck_f1_8(git, C);
  503. }
  504. @Test
  505. public void testOnlyOption_f1_8_d() throws Exception {
  506. final Git git = new Git(db);
  507. prepare_f3(git, D);
  508. prepare_f1_8(git);
  509. executeAndCheck_f1_8(git, D);
  510. }
  511. @Test
  512. public void testOnlyOption_f1_8_e() throws Exception {
  513. final Git git = new Git(db);
  514. prepare_f3(git, E);
  515. prepare_f1_8(git);
  516. executeAndCheck_f1_8(git, E);
  517. }
  518. @Test
  519. public void testOnlyOption_f1_9_a() throws Exception {
  520. final Git git = new Git(db);
  521. prepare_f3(git, A);
  522. prepare_f1_9(git);
  523. executeAndCheck_f1_9(git, A);
  524. }
  525. @Test
  526. public void testOnlyOption_f1_9_b() throws Exception {
  527. final Git git = new Git(db);
  528. prepare_f3(git, B);
  529. prepare_f1_9(git);
  530. executeAndCheck_f1_9(git, B);
  531. }
  532. @Test
  533. public void testOnlyOption_f1_9_c() throws Exception {
  534. final Git git = new Git(db);
  535. prepare_f3(git, C);
  536. prepare_f1_9(git);
  537. executeAndCheck_f1_9(git, C);
  538. }
  539. @Test
  540. public void testOnlyOption_f1_9_d() throws Exception {
  541. final Git git = new Git(db);
  542. prepare_f3(git, D);
  543. prepare_f1_9(git);
  544. executeAndCheck_f1_9(git, D);
  545. }
  546. @Test
  547. public void testOnlyOption_f1_9_e() throws Exception {
  548. final Git git = new Git(db);
  549. prepare_f3(git, E);
  550. prepare_f1_9(git);
  551. executeAndCheck_f1_9(git, E);
  552. }
  553. @Test
  554. public void testOnlyOption_f1_10_a() throws Exception {
  555. final Git git = new Git(db);
  556. prepare_f3(git, A);
  557. prepare_f1_10(git);
  558. executeAndCheck_f1_10(git, A);
  559. }
  560. @Test
  561. public void testOnlyOption_f1_10_b() throws Exception {
  562. final Git git = new Git(db);
  563. prepare_f3(git, B);
  564. prepare_f1_10(git);
  565. executeAndCheck_f1_10(git, B);
  566. }
  567. @Test
  568. public void testOnlyOption_f1_10_c() throws Exception {
  569. final Git git = new Git(db);
  570. prepare_f3(git, C);
  571. prepare_f1_10(git);
  572. executeAndCheck_f1_10(git, C);
  573. }
  574. @Test
  575. public void testOnlyOption_f1_10_d() throws Exception {
  576. final Git git = new Git(db);
  577. prepare_f3(git, D);
  578. prepare_f1_10(git);
  579. executeAndCheck_f1_10(git, D);
  580. }
  581. @Test
  582. public void testOnlyOption_f1_10_e() throws Exception {
  583. final Git git = new Git(db);
  584. prepare_f3(git, E);
  585. prepare_f1_10(git);
  586. executeAndCheck_f1_10(git, E);
  587. }
  588. @Test
  589. public void testOnlyOption_f1_11_a() throws Exception {
  590. final Git git = new Git(db);
  591. prepare_f3(git, A);
  592. prepare_f1_11(git);
  593. executeAndCheck_f1_11(git, A);
  594. }
  595. @Test
  596. public void testOnlyOption_f1_11_b() throws Exception {
  597. final Git git = new Git(db);
  598. prepare_f3(git, B);
  599. prepare_f1_11(git);
  600. executeAndCheck_f1_11(git, B);
  601. }
  602. @Test
  603. public void testOnlyOption_f1_11_c() throws Exception {
  604. final Git git = new Git(db);
  605. prepare_f3(git, C);
  606. prepare_f1_11(git);
  607. executeAndCheck_f1_11(git, C);
  608. }
  609. @Test
  610. public void testOnlyOption_f1_11_d() throws Exception {
  611. final Git git = new Git(db);
  612. prepare_f3(git, D);
  613. prepare_f1_11(git);
  614. executeAndCheck_f1_11(git, D);
  615. }
  616. @Test
  617. public void testOnlyOption_f1_11_e() throws Exception {
  618. final Git git = new Git(db);
  619. prepare_f3(git, E);
  620. prepare_f1_11(git);
  621. executeAndCheck_f1_11(git, E);
  622. }
  623. @Test
  624. public void testOnlyOption_f1_12_a() throws Exception {
  625. final Git git = new Git(db);
  626. prepare_f3(git, A);
  627. prepare_f1_12(git);
  628. executeAndCheck_f1_12(git, A);
  629. }
  630. @Test
  631. public void testOnlyOption_f1_12_b() throws Exception {
  632. final Git git = new Git(db);
  633. prepare_f3(git, B);
  634. prepare_f1_12(git);
  635. executeAndCheck_f1_12(git, B);
  636. }
  637. @Test
  638. public void testOnlyOption_f1_12_c() throws Exception {
  639. final Git git = new Git(db);
  640. prepare_f3(git, C);
  641. prepare_f1_12(git);
  642. executeAndCheck_f1_12(git, C);
  643. }
  644. @Test
  645. public void testOnlyOption_f1_12_d() throws Exception {
  646. final Git git = new Git(db);
  647. prepare_f3(git, D);
  648. prepare_f1_12(git);
  649. executeAndCheck_f1_12(git, D);
  650. }
  651. @Test
  652. public void testOnlyOption_f1_12_e() throws Exception {
  653. final Git git = new Git(db);
  654. prepare_f3(git, E);
  655. prepare_f1_12(git);
  656. executeAndCheck_f1_12(git, E);
  657. }
  658. @Test
  659. public void testOnlyOption_f1_12_f2_14_a() throws Exception {
  660. final Git git = new Git(db);
  661. prepare_f3_f2_14(git, A);
  662. prepare_f1_12(git);
  663. executeAndCheck_f1_12_f2_14(git, A);
  664. }
  665. @Test
  666. public void testOnlyOption_f1_12_f2_14_b() throws Exception {
  667. final Git git = new Git(db);
  668. prepare_f3_f2_14(git, B);
  669. prepare_f1_12(git);
  670. executeAndCheck_f1_12_f2_14(git, B);
  671. }
  672. @Test
  673. public void testOnlyOption_f1_12_f2_14_c() throws Exception {
  674. final Git git = new Git(db);
  675. prepare_f3_f2_14(git, C);
  676. prepare_f1_12(git);
  677. executeAndCheck_f1_12_f2_14(git, C);
  678. }
  679. @Test
  680. public void testOnlyOption_f1_12_f2_14_d() throws Exception {
  681. final Git git = new Git(db);
  682. prepare_f3_f2_14(git, D);
  683. prepare_f1_12(git);
  684. executeAndCheck_f1_12_f2_14(git, D);
  685. }
  686. @Test
  687. public void testOnlyOption_f1_12_f2_14_e() throws Exception {
  688. final Git git = new Git(db);
  689. prepare_f3_f2_14(git, E);
  690. prepare_f1_12(git);
  691. executeAndCheck_f1_12_f2_14(git, E);
  692. }
  693. @Test
  694. public void testOnlyOption_f1_13_a() throws Exception {
  695. final Git git = new Git(db);
  696. prepare_f3(git, A);
  697. prepare_f1_13(git);
  698. executeAndCheck_f1_13(git, A);
  699. }
  700. @Test
  701. public void testOnlyOption_f1_13_b() throws Exception {
  702. final Git git = new Git(db);
  703. prepare_f3(git, B);
  704. prepare_f1_13(git);
  705. executeAndCheck_f1_13(git, B);
  706. }
  707. @Test
  708. public void testOnlyOption_f1_13_c() throws Exception {
  709. final Git git = new Git(db);
  710. prepare_f3(git, C);
  711. prepare_f1_13(git);
  712. executeAndCheck_f1_13(git, C);
  713. }
  714. @Test
  715. public void testOnlyOption_f1_13_d() throws Exception {
  716. final Git git = new Git(db);
  717. prepare_f3(git, D);
  718. prepare_f1_13(git);
  719. executeAndCheck_f1_13(git, D);
  720. }
  721. @Test
  722. public void testOnlyOption_f1_13_e() throws Exception {
  723. final Git git = new Git(db);
  724. prepare_f3(git, E);
  725. prepare_f1_13(git);
  726. executeAndCheck_f1_13(git, E);
  727. }
  728. @Test
  729. public void testOnlyOption_f1_14_a() throws Exception {
  730. final Git git = new Git(db);
  731. prepare_f3(git, A);
  732. prepare_f1_14(git);
  733. executeAndCheck_f1_14(git, A);
  734. }
  735. @Test
  736. public void testOnlyOption_f1_14_b() throws Exception {
  737. final Git git = new Git(db);
  738. prepare_f3(git, B);
  739. prepare_f1_14(git);
  740. executeAndCheck_f1_14(git, B);
  741. }
  742. @Test
  743. public void testOnlyOption_f1_14_c() throws Exception {
  744. final Git git = new Git(db);
  745. prepare_f3(git, C);
  746. prepare_f1_14(git);
  747. executeAndCheck_f1_14(git, C);
  748. }
  749. @Test
  750. public void testOnlyOption_f1_14_d() throws Exception {
  751. final Git git = new Git(db);
  752. prepare_f3(git, D);
  753. prepare_f1_14(git);
  754. executeAndCheck_f1_14(git, D);
  755. }
  756. @Test
  757. public void testOnlyOption_f1_14_e() throws Exception {
  758. final Git git = new Git(db);
  759. prepare_f3(git, E);
  760. prepare_f1_14(git);
  761. executeAndCheck_f1_14(git, E);
  762. }
  763. @Test
  764. public void testOnlyOptionWithDirectory() throws Exception {
  765. final Git git = new Git(db);
  766. // write files
  767. final File f1 = writeTrashFile("d1/d2/f1.txt", "c1");
  768. writeTrashFile("d1/d2/f2.txt", "c2");
  769. final File f3 = writeTrashFile("d1/f3.txt", "c3");
  770. writeTrashFile("d1/f4.txt", "c4");
  771. final File f5 = writeTrashFile("d3/d4/f5.txt", "c5");
  772. writeTrashFile("d3/d4/f6.txt", "c6");
  773. final File f7 = writeTrashFile("d3/f7.txt", "c7");
  774. writeTrashFile("d3/f8.txt", "c8");
  775. final File f9 = writeTrashFile("d5/f9.txt", "c9");
  776. writeTrashFile("d5/f10.txt", "c10");
  777. final File f11 = writeTrashFile("d6/f11.txt", "c11");
  778. writeTrashFile("d6/f12.txt", "c12");
  779. // add files
  780. git.add().addFilepattern(".").call();
  781. // modify files, but do not stage changes
  782. write(f1, "c1'");
  783. write(f3, "c3'");
  784. write(f5, "c5'");
  785. write(f7, "c7'");
  786. write(f9, "c9'");
  787. write(f11, "c11'");
  788. // commit selected files only
  789. git.commit().setOnly("d1").setOnly("d3/d4/").setOnly("d5")
  790. .setOnly("d6/f11.txt").setMessage(MSG).call();
  791. assertEquals("c1'", getHead(git, "d1/d2/f1.txt"));
  792. assertEquals("c2", getHead(git, "d1/d2/f2.txt"));
  793. assertEquals("c3'", getHead(git, "d1/f3.txt"));
  794. assertEquals("c4", getHead(git, "d1/f4.txt"));
  795. assertEquals("c5'", getHead(git, "d3/d4/f5.txt"));
  796. assertEquals("c6", getHead(git, "d3/d4/f6.txt"));
  797. assertEquals("", getHead(git, "d3/f7.txt"));
  798. assertEquals("", getHead(git, "d3/f8.txt"));
  799. assertEquals("c9'", getHead(git, "d5/f9.txt"));
  800. assertEquals("c10", getHead(git, "d5/f10.txt"));
  801. assertEquals("c11'", getHead(git, "d6/f11.txt"));
  802. assertEquals("", getHead(git, "d6/f12.txt"));
  803. assertEquals("[d1/d2/f1.txt, mode:100644, content:c1']"
  804. + "[d1/d2/f2.txt, mode:100644, content:c2]"
  805. + "[d1/f3.txt, mode:100644, content:c3']"
  806. + "[d1/f4.txt, mode:100644, content:c4]"
  807. + "[d3/d4/f5.txt, mode:100644, content:c5']"
  808. + "[d3/d4/f6.txt, mode:100644, content:c6]"
  809. + "[d3/f7.txt, mode:100644, content:c7]"
  810. + "[d3/f8.txt, mode:100644, content:c8]"
  811. + "[d5/f10.txt, mode:100644, content:c10]"
  812. + "[d5/f9.txt, mode:100644, content:c9']"
  813. + "[d6/f11.txt, mode:100644, content:c11']"
  814. + "[d6/f12.txt, mode:100644, content:c12]", indexState(CONTENT));
  815. }
  816. @SuppressWarnings("unused")
  817. private File prepare_f1_1(final Git git) throws IOException {
  818. return writeTrashFile(F1, "c1");
  819. }
  820. private File prepare_f1_2(final Git git) throws Exception {
  821. final File f1 = prepare_f1_4(git);
  822. f1.delete();
  823. return f1;
  824. }
  825. private File prepare_f1_3(final Git git) throws Exception {
  826. final File f1 = prepare_f1_7(git);
  827. git.rm().addFilepattern(F1).call();
  828. return f1;
  829. }
  830. private File prepare_f1_4(final Git git) throws Exception {
  831. final File f1 = prepare_f1_1(git);
  832. git.add().addFilepattern(F1).call();
  833. return f1;
  834. }
  835. private File prepare_f1_5(final Git git) throws Exception {
  836. final File f1 = prepare_f1_7(git);
  837. f1.delete();
  838. return f1;
  839. }
  840. private File prepare_f1_6(final Git git) throws Exception {
  841. final File f1 = prepare_f1_3(git);
  842. write(f1, "c1");
  843. return f1;
  844. }
  845. private File prepare_f1_7(final Git git) throws Exception {
  846. final File f1 = prepare_f1_4(git);
  847. git.commit().setOnly(F1).setMessage(MSG).call();
  848. return f1;
  849. }
  850. private File prepare_f1_8(final Git git) throws Exception {
  851. final File f1 = prepare_f1_4(git);
  852. write(f1, "c1'");
  853. return f1;
  854. }
  855. private File prepare_f1_9(final Git git) throws Exception {
  856. final File f1 = prepare_f1_3(git);
  857. write(f1, "c1'");
  858. return f1;
  859. }
  860. private File prepare_f1_10(final Git git) throws Exception {
  861. final File f1 = prepare_f1_9(git);
  862. git.add().addFilepattern(F1).call();
  863. f1.delete();
  864. return f1;
  865. }
  866. private File prepare_f1_11(final Git git) throws Exception {
  867. final File f1 = prepare_f1_7(git);
  868. write(f1, "c1'");
  869. return f1;
  870. }
  871. private File prepare_f1_12(final Git git) throws Exception {
  872. final File f1 = prepare_f1_13(git);
  873. write(f1, "c1");
  874. return f1;
  875. }
  876. private File prepare_f1_13(final Git git) throws Exception {
  877. final File f1 = prepare_f1_11(git);
  878. git.add().addFilepattern(F1).call();
  879. return f1;
  880. }
  881. private File prepare_f1_14(final Git git) throws Exception {
  882. final File f1 = prepare_f1_13(git);
  883. write(f1, "c1''");
  884. return f1;
  885. }
  886. private void executeAndCheck_f1_1(final Git git, final int state)
  887. throws Exception {
  888. JGitInternalException exception = null;
  889. try {
  890. git.commit().setOnly(F1).setMessage(MSG).call();
  891. } catch (JGitInternalException e) {
  892. exception = e;
  893. }
  894. assertNotNull(exception);
  895. assertTrue(exception.getMessage().contains(F1));
  896. assertEquals(expected_f3_head(state), getHead(git, F3));
  897. assertEquals(expected_f3_idx(state), indexState(CONTENT));
  898. }
  899. private void executeAndCheck_f1_1_f2_f14(final Git git, final int state)
  900. throws Exception {
  901. JGitInternalException exception = null;
  902. try {
  903. git.commit().setOnly(F1).setOnly(F2).setMessage(MSG).call();
  904. } catch (JGitInternalException e) {
  905. exception = e;
  906. }
  907. assertNotNull(exception);
  908. assertTrue(exception.getMessage().contains(F1));
  909. assertEquals("c2", getHead(git, F2));
  910. assertEquals(expected_f3_head(state), getHead(git, F3));
  911. assertEquals("[f2.txt, mode:100644, content:c2']"
  912. + expected_f3_idx(state), indexState(CONTENT));
  913. }
  914. private void executeAndCheck_f1_2(final Git git, final int state)
  915. throws Exception {
  916. JGitInternalException exception = null;
  917. try {
  918. git.commit().setOnly(F1).setMessage(MSG).call();
  919. } catch (JGitInternalException e) {
  920. exception = e;
  921. }
  922. assertNotNull(exception);
  923. assertTrue(exception.getMessage().contains("No changes"));
  924. assertEquals(expected_f3_head(state), getHead(git, F3));
  925. assertEquals("[f1.txt, mode:100644, content:c1]"
  926. + expected_f3_idx(state), indexState(CONTENT));
  927. }
  928. private void executeAndCheck_f1_2_f2_f14(final Git git, final int state)
  929. throws Exception {
  930. git.commit().setOnly(F1).setOnly(F2).setMessage(MSG).call();
  931. assertEquals("", getHead(git, F1));
  932. assertEquals("c2''", getHead(git, F2));
  933. assertEquals(expected_f3_head(state), getHead(git, F3));
  934. assertEquals("[f2.txt, mode:100644, content:c2'']"
  935. + expected_f3_idx(state), indexState(CONTENT));
  936. }
  937. private void executeAndCheck_f1_3(final Git git, final int state)
  938. throws Exception {
  939. git.commit().setOnly(F1).setMessage(MSG).call();
  940. assertEquals("", getHead(git, F1));
  941. assertEquals(expected_f3_head(state), getHead(git, F3));
  942. assertEquals(expected_f3_idx(state), indexState(CONTENT));
  943. }
  944. private void executeAndCheck_f1_4(final Git git, final int state)
  945. throws Exception {
  946. git.commit().setOnly(F1).setMessage(MSG).call();
  947. assertEquals("c1", getHead(git, F1));
  948. assertEquals(expected_f3_head(state), getHead(git, F3));
  949. assertEquals("[f1.txt, mode:100644, content:c1]"
  950. + expected_f3_idx(state), indexState(CONTENT));
  951. }
  952. private void executeAndCheck_f1_5(final Git git, final int state)
  953. throws Exception {
  954. executeAndCheck_f1_3(git, state);
  955. }
  956. private void executeAndCheck_f1_6(final Git git, final int state)
  957. throws Exception {
  958. JGitInternalException exception = null;
  959. try {
  960. git.commit().setOnly(F1).setMessage(MSG).call();
  961. } catch (JGitInternalException e) {
  962. exception = e;
  963. }
  964. assertNotNull(exception);
  965. assertTrue(exception.getMessage().contains("No changes"));
  966. assertEquals(expected_f3_head(state), getHead(git, F3));
  967. assertEquals(expected_f3_idx(state), indexState(CONTENT));
  968. }
  969. private void executeAndCheck_f1_6_f2_14(final Git git, final int state)
  970. throws Exception {
  971. git.commit().setOnly(F1).setOnly(F2).setMessage(MSG).call();
  972. assertEquals("c1", getHead(git, F1));
  973. assertEquals("c2''", getHead(git, F2));
  974. assertEquals(expected_f3_head(state), getHead(git, F3));
  975. assertEquals("[f1.txt, mode:100644, content:c1]"
  976. + "[f2.txt, mode:100644, content:c2'']"
  977. + expected_f3_idx(state), indexState(CONTENT));
  978. }
  979. private void executeAndCheck_f1_7(final Git git, final int state)
  980. throws Exception {
  981. executeAndCheck_f1_2(git, state);
  982. }
  983. private void executeAndCheck_f1_7_f2_14(final Git git, final int state)
  984. throws Exception {
  985. executeAndCheck_f1_6_f2_14(git, state);
  986. }
  987. private void executeAndCheck_f1_8(final Git git, final int state)
  988. throws Exception {
  989. git.commit().setOnly(F1).setMessage(MSG).call();
  990. assertEquals("c1'", getHead(git, F1));
  991. assertEquals(expected_f3_head(state), getHead(git, F3));
  992. assertEquals("[f1.txt, mode:100644, content:c1']"
  993. + expected_f3_idx(state), indexState(CONTENT));
  994. }
  995. private void executeAndCheck_f1_9(final Git git, final int state)
  996. throws Exception {
  997. executeAndCheck_f1_8(git, state);
  998. }
  999. private void executeAndCheck_f1_10(final Git git, final int state)
  1000. throws Exception {
  1001. executeAndCheck_f1_3(git, state);
  1002. }
  1003. private void executeAndCheck_f1_11(final Git git, final int state)
  1004. throws Exception {
  1005. executeAndCheck_f1_8(git, state);
  1006. }
  1007. private void executeAndCheck_f1_12(final Git git, final int state)
  1008. throws Exception {
  1009. JGitInternalException exception = null;
  1010. try {
  1011. git.commit().setOnly(F1).setMessage(MSG).call();
  1012. } catch (JGitInternalException e) {
  1013. exception = e;
  1014. }
  1015. assertNotNull(exception);
  1016. assertTrue(exception.getMessage().contains("No changes"));
  1017. assertEquals(expected_f3_head(state), getHead(git, F3));
  1018. assertEquals("[f1.txt, mode:100644, content:c1']"
  1019. + expected_f3_idx(state), indexState(CONTENT));
  1020. }
  1021. private void executeAndCheck_f1_12_f2_14(final Git git, final int state)
  1022. throws Exception {
  1023. executeAndCheck_f1_6_f2_14(git, state);
  1024. }
  1025. private void executeAndCheck_f1_13(final Git git, final int state)
  1026. throws Exception {
  1027. executeAndCheck_f1_8(git, state);
  1028. }
  1029. private void executeAndCheck_f1_14(final Git git, final int state)
  1030. throws Exception {
  1031. git.commit().setOnly(F1).setMessage(MSG).call();
  1032. assertEquals("c1''", getHead(git, F1));
  1033. assertEquals(expected_f3_head(state), getHead(git, F3));
  1034. assertEquals("[f1.txt, mode:100644, content:c1'']"
  1035. + expected_f3_idx(state), indexState(CONTENT));
  1036. }
  1037. private void prepare_f3(final Git git, final int state) throws Exception {
  1038. prepare_f3_f2_14(git, state, false);
  1039. }
  1040. private void prepare_f3_f2_14(final Git git, final int state)
  1041. throws Exception {
  1042. prepare_f3_f2_14(git, state, true);
  1043. }
  1044. private void prepare_f3_f2_14(final Git git, final int state,
  1045. final boolean include_f2) throws Exception {
  1046. File f2 = null;
  1047. if (include_f2) {
  1048. f2 = writeTrashFile(F2, "c2");
  1049. git.add().addFilepattern(F2).call();
  1050. git.commit().setMessage(MSG).call();
  1051. }
  1052. if (state >= 1) {
  1053. writeTrashFile(F3, "c3");
  1054. git.add().addFilepattern(F3).call();
  1055. }
  1056. if (state >= 2)
  1057. git.commit().setMessage(MSG).call();
  1058. if (state >= 3)
  1059. git.rm().addFilepattern(F3).call();
  1060. if (state == 4) {
  1061. writeTrashFile(F3, "c3'");
  1062. git.add().addFilepattern(F3).call();
  1063. }
  1064. if (include_f2) {
  1065. write(f2, "c2'");
  1066. git.add().addFilepattern(F2).call();
  1067. write(f2, "c2''");
  1068. }
  1069. }
  1070. private static String expected_f3_head(final int state) {
  1071. switch (state) {
  1072. case 0:
  1073. case 1:
  1074. return "";
  1075. case 2:
  1076. case 3:
  1077. case 4:
  1078. return "c3";
  1079. }
  1080. return null;
  1081. }
  1082. private static String expected_f3_idx(final int state) {
  1083. switch (state) {
  1084. case 0:
  1085. case 3:
  1086. return "";
  1087. case 1:
  1088. case 2:
  1089. return "[f3.txt, mode:100644, content:c3]";
  1090. case 4:
  1091. return "[f3.txt, mode:100644, content:c3']";
  1092. }
  1093. return null;
  1094. }
  1095. static private String getHead(Git git, String path)
  1096. throws Exception {
  1097. try {
  1098. final Repository repo = git.getRepository();
  1099. final ObjectId headId = repo.resolve(Constants.HEAD + "^{commit}");
  1100. if (headId == null) {
  1101. return "";
  1102. }
  1103. try (RevWalk rw = new RevWalk(repo)) {
  1104. final TreeWalk tw = TreeWalk.forPath(repo, path,
  1105. rw.parseTree(headId));
  1106. if (tw == null) {
  1107. return "";
  1108. }
  1109. return new String(tw.getObjectReader().open(tw.getObjectId(0))
  1110. .getBytes(), UTF_8);
  1111. }
  1112. } catch (Exception e) {
  1113. return "";
  1114. }
  1115. }
  1116. }