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.

AddCommandTest.java 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  1. /*
  2. * Copyright (C) 2010, Stefan Lay <stefan.lay@sap.com>
  3. * Copyright (C) 2010, Christian Halstrick <christian.halstrick@sap.com>
  4. * and other copyright owners as documented in the project's IP log.
  5. *
  6. * This program and the accompanying materials are made available
  7. * under the terms of the Eclipse Distribution License v1.0 which
  8. * accompanies this distribution, is reproduced below, and is
  9. * available at http://www.eclipse.org/org/documents/edl-v10.php
  10. *
  11. * All rights reserved.
  12. *
  13. * Redistribution and use in source and binary forms, with or
  14. * without modification, are permitted provided that the following
  15. * conditions are met:
  16. *
  17. * - Redistributions of source code must retain the above copyright
  18. * notice, this list of conditions and the following disclaimer.
  19. *
  20. * - Redistributions in binary form must reproduce the above
  21. * copyright notice, this list of conditions and the following
  22. * disclaimer in the documentation and/or other materials provided
  23. * with the distribution.
  24. *
  25. * - Neither the name of the Eclipse Foundation, Inc. nor the
  26. * names of its contributors may be used to endorse or promote
  27. * products derived from this software without specific prior
  28. * written permission.
  29. *
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  31. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  32. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  33. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  34. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  35. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  36. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  37. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  38. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  39. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  40. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  41. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  42. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  43. */
  44. package org.eclipse.jgit.api;
  45. import static org.junit.Assert.assertEquals;
  46. import static org.junit.Assert.fail;
  47. import java.io.File;
  48. import java.io.FileInputStream;
  49. import java.io.IOException;
  50. import java.io.PrintWriter;
  51. import org.eclipse.jgit.api.errors.NoFilepatternException;
  52. import org.eclipse.jgit.dircache.DirCache;
  53. import org.eclipse.jgit.dircache.DirCacheBuilder;
  54. import org.eclipse.jgit.dircache.DirCacheEntry;
  55. import org.eclipse.jgit.lib.Constants;
  56. import org.eclipse.jgit.lib.FileMode;
  57. import org.eclipse.jgit.lib.ObjectId;
  58. import org.eclipse.jgit.lib.ObjectInserter;
  59. import org.eclipse.jgit.lib.RepositoryTestCase;
  60. import org.eclipse.jgit.util.FileUtils;
  61. import org.junit.Test;
  62. public class AddCommandTest extends RepositoryTestCase {
  63. @Test
  64. public void testAddNothing() {
  65. Git git = new Git(db);
  66. try {
  67. git.add().call();
  68. fail("Expected IllegalArgumentException");
  69. } catch (NoFilepatternException e) {
  70. // expected
  71. }
  72. }
  73. @Test
  74. public void testAddNonExistingSingleFile() throws NoFilepatternException {
  75. Git git = new Git(db);
  76. DirCache dc = git.add().addFilepattern("a.txt").call();
  77. assertEquals(0, dc.getEntryCount());
  78. }
  79. @Test
  80. public void testAddExistingSingleFile() throws IOException, NoFilepatternException {
  81. File file = new File(db.getWorkTree(), "a.txt");
  82. file.createNewFile();
  83. PrintWriter writer = new PrintWriter(file);
  84. writer.print("content");
  85. writer.close();
  86. Git git = new Git(db);
  87. git.add().addFilepattern("a.txt").call();
  88. assertEquals(
  89. "[a.txt, mode:100644, content:content]",
  90. indexState(CONTENT));
  91. }
  92. @Test
  93. public void testAddExistingSingleFileInSubDir() throws IOException, NoFilepatternException {
  94. new File(db.getWorkTree(), "sub").mkdir();
  95. File file = new File(db.getWorkTree(), "sub/a.txt");
  96. file.createNewFile();
  97. PrintWriter writer = new PrintWriter(file);
  98. writer.print("content");
  99. writer.close();
  100. Git git = new Git(db);
  101. git.add().addFilepattern("sub/a.txt").call();
  102. assertEquals(
  103. "[sub/a.txt, mode:100644, content:content]",
  104. indexState(CONTENT));
  105. }
  106. @Test
  107. public void testAddExistingSingleFileTwice() throws IOException, NoFilepatternException {
  108. File file = new File(db.getWorkTree(), "a.txt");
  109. file.createNewFile();
  110. PrintWriter writer = new PrintWriter(file);
  111. writer.print("content");
  112. writer.close();
  113. Git git = new Git(db);
  114. DirCache dc = git.add().addFilepattern("a.txt").call();
  115. dc.getEntry(0).getObjectId();
  116. writer = new PrintWriter(file);
  117. writer.print("other content");
  118. writer.close();
  119. dc = git.add().addFilepattern("a.txt").call();
  120. assertEquals(
  121. "[a.txt, mode:100644, content:other content]",
  122. indexState(CONTENT));
  123. }
  124. @Test
  125. public void testAddExistingSingleFileTwiceWithCommit() throws Exception {
  126. File file = new File(db.getWorkTree(), "a.txt");
  127. file.createNewFile();
  128. PrintWriter writer = new PrintWriter(file);
  129. writer.print("content");
  130. writer.close();
  131. Git git = new Git(db);
  132. DirCache dc = git.add().addFilepattern("a.txt").call();
  133. dc.getEntry(0).getObjectId();
  134. git.commit().setMessage("commit a.txt").call();
  135. writer = new PrintWriter(file);
  136. writer.print("other content");
  137. writer.close();
  138. dc = git.add().addFilepattern("a.txt").call();
  139. assertEquals(
  140. "[a.txt, mode:100644, content:other content]",
  141. indexState(CONTENT));
  142. }
  143. @Test
  144. public void testAddRemovedFile() throws Exception {
  145. File file = new File(db.getWorkTree(), "a.txt");
  146. file.createNewFile();
  147. PrintWriter writer = new PrintWriter(file);
  148. writer.print("content");
  149. writer.close();
  150. Git git = new Git(db);
  151. DirCache dc = git.add().addFilepattern("a.txt").call();
  152. dc.getEntry(0).getObjectId();
  153. FileUtils.delete(file);
  154. // is supposed to do nothing
  155. dc = git.add().addFilepattern("a.txt").call();
  156. assertEquals(
  157. "[a.txt, mode:100644, content:content]",
  158. indexState(CONTENT));
  159. }
  160. @Test
  161. public void testAddRemovedCommittedFile() throws Exception {
  162. File file = new File(db.getWorkTree(), "a.txt");
  163. file.createNewFile();
  164. PrintWriter writer = new PrintWriter(file);
  165. writer.print("content");
  166. writer.close();
  167. Git git = new Git(db);
  168. DirCache dc = git.add().addFilepattern("a.txt").call();
  169. git.commit().setMessage("commit a.txt").call();
  170. dc.getEntry(0).getObjectId();
  171. FileUtils.delete(file);
  172. // is supposed to do nothing
  173. dc = git.add().addFilepattern("a.txt").call();
  174. assertEquals(
  175. "[a.txt, mode:100644, content:content]",
  176. indexState(CONTENT));
  177. }
  178. @Test
  179. public void testAddWithConflicts() throws Exception {
  180. // prepare conflict
  181. File file = new File(db.getWorkTree(), "a.txt");
  182. file.createNewFile();
  183. PrintWriter writer = new PrintWriter(file);
  184. writer.print("content");
  185. writer.close();
  186. File file2 = new File(db.getWorkTree(), "b.txt");
  187. file2.createNewFile();
  188. writer = new PrintWriter(file2);
  189. writer.print("content b");
  190. writer.close();
  191. ObjectInserter newObjectInserter = db.newObjectInserter();
  192. DirCache dc = db.lockDirCache();
  193. DirCacheBuilder builder = dc.builder();
  194. addEntryToBuilder("b.txt", file2, newObjectInserter, builder, 0);
  195. addEntryToBuilder("a.txt", file, newObjectInserter, builder, 1);
  196. writer = new PrintWriter(file);
  197. writer.print("other content");
  198. writer.close();
  199. addEntryToBuilder("a.txt", file, newObjectInserter, builder, 3);
  200. writer = new PrintWriter(file);
  201. writer.print("our content");
  202. writer.close();
  203. addEntryToBuilder("a.txt", file, newObjectInserter, builder, 2)
  204. .getObjectId();
  205. builder.commit();
  206. assertEquals(
  207. "[a.txt, mode:100644, stage:1, content:content]" +
  208. "[a.txt, mode:100644, stage:2, content:our content]" +
  209. "[a.txt, mode:100644, stage:3, content:other content]" +
  210. "[b.txt, mode:100644, content:content b]",
  211. indexState(CONTENT));
  212. // now the test begins
  213. Git git = new Git(db);
  214. dc = git.add().addFilepattern("a.txt").call();
  215. assertEquals(
  216. "[a.txt, mode:100644, content:our content]" +
  217. "[b.txt, mode:100644, content:content b]",
  218. indexState(CONTENT));
  219. }
  220. @Test
  221. public void testAddTwoFiles() throws Exception {
  222. File file = new File(db.getWorkTree(), "a.txt");
  223. file.createNewFile();
  224. PrintWriter writer = new PrintWriter(file);
  225. writer.print("content");
  226. writer.close();
  227. File file2 = new File(db.getWorkTree(), "b.txt");
  228. file2.createNewFile();
  229. writer = new PrintWriter(file2);
  230. writer.print("content b");
  231. writer.close();
  232. Git git = new Git(db);
  233. git.add().addFilepattern("a.txt").addFilepattern("b.txt").call();
  234. assertEquals(
  235. "[a.txt, mode:100644, content:content]" +
  236. "[b.txt, mode:100644, content:content b]",
  237. indexState(CONTENT));
  238. }
  239. @Test
  240. public void testAddFolder() throws Exception {
  241. new File(db.getWorkTree(), "sub").mkdir();
  242. File file = new File(db.getWorkTree(), "sub/a.txt");
  243. file.createNewFile();
  244. PrintWriter writer = new PrintWriter(file);
  245. writer.print("content");
  246. writer.close();
  247. File file2 = new File(db.getWorkTree(), "sub/b.txt");
  248. file2.createNewFile();
  249. writer = new PrintWriter(file2);
  250. writer.print("content b");
  251. writer.close();
  252. Git git = new Git(db);
  253. git.add().addFilepattern("sub").call();
  254. assertEquals(
  255. "[sub/a.txt, mode:100644, content:content]" +
  256. "[sub/b.txt, mode:100644, content:content b]",
  257. indexState(CONTENT));
  258. }
  259. @Test
  260. public void testAddIgnoredFile() throws Exception {
  261. new File(db.getWorkTree(), "sub").mkdir();
  262. File file = new File(db.getWorkTree(), "sub/a.txt");
  263. file.createNewFile();
  264. PrintWriter writer = new PrintWriter(file);
  265. writer.print("content");
  266. writer.close();
  267. File ignoreFile = new File(db.getWorkTree(), ".gitignore");
  268. ignoreFile.createNewFile();
  269. writer = new PrintWriter(ignoreFile);
  270. writer.print("sub/b.txt");
  271. writer.close();
  272. File file2 = new File(db.getWorkTree(), "sub/b.txt");
  273. file2.createNewFile();
  274. writer = new PrintWriter(file2);
  275. writer.print("content b");
  276. writer.close();
  277. Git git = new Git(db);
  278. git.add().addFilepattern("sub").call();
  279. assertEquals(
  280. "[sub/a.txt, mode:100644, content:content]",
  281. indexState(CONTENT));
  282. }
  283. @Test
  284. public void testAddWholeRepo() throws Exception {
  285. new File(db.getWorkTree(), "sub").mkdir();
  286. File file = new File(db.getWorkTree(), "sub/a.txt");
  287. file.createNewFile();
  288. PrintWriter writer = new PrintWriter(file);
  289. writer.print("content");
  290. writer.close();
  291. File file2 = new File(db.getWorkTree(), "sub/b.txt");
  292. file2.createNewFile();
  293. writer = new PrintWriter(file2);
  294. writer.print("content b");
  295. writer.close();
  296. Git git = new Git(db);
  297. git.add().addFilepattern(".").call();
  298. assertEquals(
  299. "[sub/a.txt, mode:100644, content:content]" +
  300. "[sub/b.txt, mode:100644, content:content b]",
  301. indexState(CONTENT));
  302. }
  303. // the same three cases as in testAddWithParameterUpdate
  304. // file a exists in workdir and in index -> added
  305. // file b exists not in workdir but in index -> unchanged
  306. // file c exists in workdir but not in index -> added
  307. @Test
  308. public void testAddWithoutParameterUpdate() throws Exception {
  309. new File(db.getWorkTree(), "sub").mkdir();
  310. File file = new File(db.getWorkTree(), "sub/a.txt");
  311. file.createNewFile();
  312. PrintWriter writer = new PrintWriter(file);
  313. writer.print("content");
  314. writer.close();
  315. File file2 = new File(db.getWorkTree(), "sub/b.txt");
  316. file2.createNewFile();
  317. writer = new PrintWriter(file2);
  318. writer.print("content b");
  319. writer.close();
  320. Git git = new Git(db);
  321. git.add().addFilepattern("sub").call();
  322. assertEquals(
  323. "[sub/a.txt, mode:100644, content:content]" +
  324. "[sub/b.txt, mode:100644, content:content b]",
  325. indexState(CONTENT));
  326. git.commit().setMessage("commit").call();
  327. // new unstaged file sub/c.txt
  328. File file3 = new File(db.getWorkTree(), "sub/c.txt");
  329. file3.createNewFile();
  330. writer = new PrintWriter(file3);
  331. writer.print("content c");
  332. writer.close();
  333. // file sub/a.txt is modified
  334. writer = new PrintWriter(file);
  335. writer.print("modified content");
  336. writer.close();
  337. // file sub/b.txt is deleted
  338. FileUtils.delete(file2);
  339. git.add().addFilepattern("sub").call();
  340. // change in sub/a.txt is staged
  341. // deletion of sub/b.txt is not staged
  342. // sub/c.txt is staged
  343. assertEquals(
  344. "[sub/a.txt, mode:100644, content:modified content]" +
  345. "[sub/b.txt, mode:100644, content:content b]" +
  346. "[sub/c.txt, mode:100644, content:content c]",
  347. indexState(CONTENT));
  348. }
  349. // file a exists in workdir and in index -> added
  350. // file b exists not in workdir but in index -> deleted
  351. // file c exists in workdir but not in index -> unchanged
  352. @Test
  353. public void testAddWithParameterUpdate() throws Exception {
  354. new File(db.getWorkTree(), "sub").mkdir();
  355. File file = new File(db.getWorkTree(), "sub/a.txt");
  356. file.createNewFile();
  357. PrintWriter writer = new PrintWriter(file);
  358. writer.print("content");
  359. writer.close();
  360. File file2 = new File(db.getWorkTree(), "sub/b.txt");
  361. file2.createNewFile();
  362. writer = new PrintWriter(file2);
  363. writer.print("content b");
  364. writer.close();
  365. Git git = new Git(db);
  366. git.add().addFilepattern("sub").call();
  367. assertEquals(
  368. "[sub/a.txt, mode:100644, content:content]" +
  369. "[sub/b.txt, mode:100644, content:content b]",
  370. indexState(CONTENT));
  371. git.commit().setMessage("commit").call();
  372. // new unstaged file sub/c.txt
  373. File file3 = new File(db.getWorkTree(), "sub/c.txt");
  374. file3.createNewFile();
  375. writer = new PrintWriter(file3);
  376. writer.print("content c");
  377. writer.close();
  378. // file sub/a.txt is modified
  379. writer = new PrintWriter(file);
  380. writer.print("modified content");
  381. writer.close();
  382. FileUtils.delete(file2);
  383. // change in sub/a.txt is staged
  384. // deletion of sub/b.txt is staged
  385. // sub/c.txt is not staged
  386. git.add().addFilepattern("sub").setUpdate(true).call();
  387. // change in sub/a.txt is staged
  388. assertEquals(
  389. "[sub/a.txt, mode:100644, content:modified content]",
  390. indexState(CONTENT));
  391. }
  392. @Test
  393. public void testAssumeUnchanged() throws Exception {
  394. Git git = new Git(db);
  395. String path = "a.txt";
  396. writeTrashFile(path, "content");
  397. git.add().addFilepattern(path).call();
  398. String path2 = "b.txt";
  399. writeTrashFile(path2, "content");
  400. git.add().addFilepattern(path2).call();
  401. git.commit().setMessage("commit").call();
  402. assertEquals("[a.txt, mode:100644, content:"
  403. + "content, assume-unchanged:false]"
  404. + "[b.txt, mode:100644, content:content, "
  405. + "assume-unchanged:false]", indexState(CONTENT
  406. | ASSUME_UNCHANGED));
  407. assumeUnchanged(path2);
  408. assertEquals("[a.txt, mode:100644, content:content, "
  409. + "assume-unchanged:false][b.txt, mode:100644, "
  410. + "content:content, assume-unchanged:true]", indexState(CONTENT
  411. | ASSUME_UNCHANGED));
  412. writeTrashFile(path, "more content");
  413. writeTrashFile(path2, "more content");
  414. git.add().addFilepattern(".").call();
  415. assertEquals("[a.txt, mode:100644, content:more content,"
  416. + " assume-unchanged:false][b.txt, mode:100644,"
  417. + "" + ""
  418. + " content:content, assume-unchanged:true]",
  419. indexState(CONTENT
  420. | ASSUME_UNCHANGED));
  421. }
  422. private DirCacheEntry addEntryToBuilder(String path, File file,
  423. ObjectInserter newObjectInserter, DirCacheBuilder builder, int stage)
  424. throws IOException {
  425. FileInputStream inputStream = new FileInputStream(file);
  426. ObjectId id = newObjectInserter.insert(
  427. Constants.OBJ_BLOB, file.length(), inputStream);
  428. inputStream.close();
  429. DirCacheEntry entry = new DirCacheEntry(path, stage);
  430. entry.setObjectId(id);
  431. entry.setFileMode(FileMode.REGULAR_FILE);
  432. entry.setLastModified(file.lastModified());
  433. entry.setLength((int) file.length());
  434. builder.add(entry);
  435. return entry;
  436. }
  437. private void assumeUnchanged(String path) throws IOException {
  438. final DirCache dirc = db.lockDirCache();
  439. final DirCacheEntry ent = dirc.getEntry(path);
  440. if (ent != null)
  441. ent.setAssumeValid(true);
  442. dirc.write();
  443. if (!dirc.commit())
  444. throw new IOException("could not commit");
  445. }
  446. }