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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  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.assertNotNull;
  47. import static org.junit.Assert.fail;
  48. import java.io.File;
  49. import java.io.FileInputStream;
  50. import java.io.IOException;
  51. import java.io.PrintWriter;
  52. import org.eclipse.jgit.api.errors.NoFilepatternException;
  53. import org.eclipse.jgit.dircache.DirCache;
  54. import org.eclipse.jgit.dircache.DirCacheBuilder;
  55. import org.eclipse.jgit.dircache.DirCacheEntry;
  56. import org.eclipse.jgit.lib.ConfigConstants;
  57. import org.eclipse.jgit.lib.Constants;
  58. import org.eclipse.jgit.lib.FileMode;
  59. import org.eclipse.jgit.lib.ObjectId;
  60. import org.eclipse.jgit.lib.ObjectInserter;
  61. import org.eclipse.jgit.lib.RepositoryTestCase;
  62. import org.eclipse.jgit.lib.StoredConfig;
  63. import org.eclipse.jgit.revwalk.RevCommit;
  64. import org.eclipse.jgit.treewalk.TreeWalk;
  65. import org.eclipse.jgit.util.FS;
  66. import org.eclipse.jgit.util.FileUtils;
  67. import org.junit.Test;
  68. public class AddCommandTest extends RepositoryTestCase {
  69. @Test
  70. public void testAddNothing() {
  71. Git git = new Git(db);
  72. try {
  73. git.add().call();
  74. fail("Expected IllegalArgumentException");
  75. } catch (NoFilepatternException e) {
  76. // expected
  77. }
  78. }
  79. @Test
  80. public void testAddNonExistingSingleFile() throws NoFilepatternException {
  81. Git git = new Git(db);
  82. DirCache dc = git.add().addFilepattern("a.txt").call();
  83. assertEquals(0, dc.getEntryCount());
  84. }
  85. @Test
  86. public void testAddExistingSingleFile() throws IOException, NoFilepatternException {
  87. File file = new File(db.getWorkTree(), "a.txt");
  88. FileUtils.createNewFile(file);
  89. PrintWriter writer = new PrintWriter(file);
  90. writer.print("content");
  91. writer.close();
  92. Git git = new Git(db);
  93. git.add().addFilepattern("a.txt").call();
  94. assertEquals(
  95. "[a.txt, mode:100644, content:content]",
  96. indexState(CONTENT));
  97. }
  98. @Test
  99. public void testAddExistingSingleFileInSubDir() throws IOException, NoFilepatternException {
  100. FileUtils.mkdir(new File(db.getWorkTree(), "sub"));
  101. File file = new File(db.getWorkTree(), "sub/a.txt");
  102. FileUtils.createNewFile(file);
  103. PrintWriter writer = new PrintWriter(file);
  104. writer.print("content");
  105. writer.close();
  106. Git git = new Git(db);
  107. git.add().addFilepattern("sub/a.txt").call();
  108. assertEquals(
  109. "[sub/a.txt, mode:100644, content:content]",
  110. indexState(CONTENT));
  111. }
  112. @Test
  113. public void testAddExistingSingleFileTwice() throws IOException, NoFilepatternException {
  114. File file = new File(db.getWorkTree(), "a.txt");
  115. FileUtils.createNewFile(file);
  116. PrintWriter writer = new PrintWriter(file);
  117. writer.print("content");
  118. writer.close();
  119. Git git = new Git(db);
  120. DirCache dc = git.add().addFilepattern("a.txt").call();
  121. dc.getEntry(0).getObjectId();
  122. writer = new PrintWriter(file);
  123. writer.print("other content");
  124. writer.close();
  125. dc = git.add().addFilepattern("a.txt").call();
  126. assertEquals(
  127. "[a.txt, mode:100644, content:other content]",
  128. indexState(CONTENT));
  129. }
  130. @Test
  131. public void testAddExistingSingleFileTwiceWithCommit() throws Exception {
  132. File file = new File(db.getWorkTree(), "a.txt");
  133. FileUtils.createNewFile(file);
  134. PrintWriter writer = new PrintWriter(file);
  135. writer.print("content");
  136. writer.close();
  137. Git git = new Git(db);
  138. DirCache dc = git.add().addFilepattern("a.txt").call();
  139. dc.getEntry(0).getObjectId();
  140. git.commit().setMessage("commit a.txt").call();
  141. writer = new PrintWriter(file);
  142. writer.print("other content");
  143. writer.close();
  144. dc = git.add().addFilepattern("a.txt").call();
  145. assertEquals(
  146. "[a.txt, mode:100644, content:other content]",
  147. indexState(CONTENT));
  148. }
  149. @Test
  150. public void testAddRemovedFile() throws Exception {
  151. File file = new File(db.getWorkTree(), "a.txt");
  152. FileUtils.createNewFile(file);
  153. PrintWriter writer = new PrintWriter(file);
  154. writer.print("content");
  155. writer.close();
  156. Git git = new Git(db);
  157. DirCache dc = git.add().addFilepattern("a.txt").call();
  158. dc.getEntry(0).getObjectId();
  159. FileUtils.delete(file);
  160. // is supposed to do nothing
  161. dc = git.add().addFilepattern("a.txt").call();
  162. assertEquals(
  163. "[a.txt, mode:100644, content:content]",
  164. indexState(CONTENT));
  165. }
  166. @Test
  167. public void testAddRemovedCommittedFile() throws Exception {
  168. File file = new File(db.getWorkTree(), "a.txt");
  169. FileUtils.createNewFile(file);
  170. PrintWriter writer = new PrintWriter(file);
  171. writer.print("content");
  172. writer.close();
  173. Git git = new Git(db);
  174. DirCache dc = git.add().addFilepattern("a.txt").call();
  175. git.commit().setMessage("commit a.txt").call();
  176. dc.getEntry(0).getObjectId();
  177. FileUtils.delete(file);
  178. // is supposed to do nothing
  179. dc = git.add().addFilepattern("a.txt").call();
  180. assertEquals(
  181. "[a.txt, mode:100644, content:content]",
  182. indexState(CONTENT));
  183. }
  184. @Test
  185. public void testAddWithConflicts() throws Exception {
  186. // prepare conflict
  187. File file = new File(db.getWorkTree(), "a.txt");
  188. FileUtils.createNewFile(file);
  189. PrintWriter writer = new PrintWriter(file);
  190. writer.print("content");
  191. writer.close();
  192. File file2 = new File(db.getWorkTree(), "b.txt");
  193. FileUtils.createNewFile(file2);
  194. writer = new PrintWriter(file2);
  195. writer.print("content b");
  196. writer.close();
  197. ObjectInserter newObjectInserter = db.newObjectInserter();
  198. DirCache dc = db.lockDirCache();
  199. DirCacheBuilder builder = dc.builder();
  200. addEntryToBuilder("b.txt", file2, newObjectInserter, builder, 0);
  201. addEntryToBuilder("a.txt", file, newObjectInserter, builder, 1);
  202. writer = new PrintWriter(file);
  203. writer.print("other content");
  204. writer.close();
  205. addEntryToBuilder("a.txt", file, newObjectInserter, builder, 3);
  206. writer = new PrintWriter(file);
  207. writer.print("our content");
  208. writer.close();
  209. addEntryToBuilder("a.txt", file, newObjectInserter, builder, 2)
  210. .getObjectId();
  211. builder.commit();
  212. assertEquals(
  213. "[a.txt, mode:100644, stage:1, content:content]" +
  214. "[a.txt, mode:100644, stage:2, content:our content]" +
  215. "[a.txt, mode:100644, stage:3, content:other content]" +
  216. "[b.txt, mode:100644, content:content b]",
  217. indexState(CONTENT));
  218. // now the test begins
  219. Git git = new Git(db);
  220. dc = git.add().addFilepattern("a.txt").call();
  221. assertEquals(
  222. "[a.txt, mode:100644, content:our content]" +
  223. "[b.txt, mode:100644, content:content b]",
  224. indexState(CONTENT));
  225. }
  226. @Test
  227. public void testAddTwoFiles() throws Exception {
  228. File file = new File(db.getWorkTree(), "a.txt");
  229. FileUtils.createNewFile(file);
  230. PrintWriter writer = new PrintWriter(file);
  231. writer.print("content");
  232. writer.close();
  233. File file2 = new File(db.getWorkTree(), "b.txt");
  234. FileUtils.createNewFile(file2);
  235. writer = new PrintWriter(file2);
  236. writer.print("content b");
  237. writer.close();
  238. Git git = new Git(db);
  239. git.add().addFilepattern("a.txt").addFilepattern("b.txt").call();
  240. assertEquals(
  241. "[a.txt, mode:100644, content:content]" +
  242. "[b.txt, mode:100644, content:content b]",
  243. indexState(CONTENT));
  244. }
  245. @Test
  246. public void testAddFolder() throws Exception {
  247. FileUtils.mkdir(new File(db.getWorkTree(), "sub"));
  248. File file = new File(db.getWorkTree(), "sub/a.txt");
  249. FileUtils.createNewFile(file);
  250. PrintWriter writer = new PrintWriter(file);
  251. writer.print("content");
  252. writer.close();
  253. File file2 = new File(db.getWorkTree(), "sub/b.txt");
  254. FileUtils.createNewFile(file2);
  255. writer = new PrintWriter(file2);
  256. writer.print("content b");
  257. writer.close();
  258. Git git = new Git(db);
  259. git.add().addFilepattern("sub").call();
  260. assertEquals(
  261. "[sub/a.txt, mode:100644, content:content]" +
  262. "[sub/b.txt, mode:100644, content:content b]",
  263. indexState(CONTENT));
  264. }
  265. @Test
  266. public void testAddIgnoredFile() throws Exception {
  267. FileUtils.mkdir(new File(db.getWorkTree(), "sub"));
  268. File file = new File(db.getWorkTree(), "sub/a.txt");
  269. FileUtils.createNewFile(file);
  270. PrintWriter writer = new PrintWriter(file);
  271. writer.print("content");
  272. writer.close();
  273. File ignoreFile = new File(db.getWorkTree(), ".gitignore");
  274. FileUtils.createNewFile(ignoreFile);
  275. writer = new PrintWriter(ignoreFile);
  276. writer.print("sub/b.txt");
  277. writer.close();
  278. File file2 = new File(db.getWorkTree(), "sub/b.txt");
  279. FileUtils.createNewFile(file2);
  280. writer = new PrintWriter(file2);
  281. writer.print("content b");
  282. writer.close();
  283. Git git = new Git(db);
  284. git.add().addFilepattern("sub").call();
  285. assertEquals(
  286. "[sub/a.txt, mode:100644, content:content]",
  287. indexState(CONTENT));
  288. }
  289. @Test
  290. public void testAddWholeRepo() throws Exception {
  291. FileUtils.mkdir(new File(db.getWorkTree(), "sub"));
  292. File file = new File(db.getWorkTree(), "sub/a.txt");
  293. FileUtils.createNewFile(file);
  294. PrintWriter writer = new PrintWriter(file);
  295. writer.print("content");
  296. writer.close();
  297. File file2 = new File(db.getWorkTree(), "sub/b.txt");
  298. FileUtils.createNewFile(file2);
  299. writer = new PrintWriter(file2);
  300. writer.print("content b");
  301. writer.close();
  302. Git git = new Git(db);
  303. git.add().addFilepattern(".").call();
  304. assertEquals(
  305. "[sub/a.txt, mode:100644, content:content]" +
  306. "[sub/b.txt, mode:100644, content:content b]",
  307. indexState(CONTENT));
  308. }
  309. // the same three cases as in testAddWithParameterUpdate
  310. // file a exists in workdir and in index -> added
  311. // file b exists not in workdir but in index -> unchanged
  312. // file c exists in workdir but not in index -> added
  313. @Test
  314. public void testAddWithoutParameterUpdate() throws Exception {
  315. FileUtils.mkdir(new File(db.getWorkTree(), "sub"));
  316. File file = new File(db.getWorkTree(), "sub/a.txt");
  317. FileUtils.createNewFile(file);
  318. PrintWriter writer = new PrintWriter(file);
  319. writer.print("content");
  320. writer.close();
  321. File file2 = new File(db.getWorkTree(), "sub/b.txt");
  322. FileUtils.createNewFile(file2);
  323. writer = new PrintWriter(file2);
  324. writer.print("content b");
  325. writer.close();
  326. Git git = new Git(db);
  327. git.add().addFilepattern("sub").call();
  328. assertEquals(
  329. "[sub/a.txt, mode:100644, content:content]" +
  330. "[sub/b.txt, mode:100644, content:content b]",
  331. indexState(CONTENT));
  332. git.commit().setMessage("commit").call();
  333. // new unstaged file sub/c.txt
  334. File file3 = new File(db.getWorkTree(), "sub/c.txt");
  335. FileUtils.createNewFile(file3);
  336. writer = new PrintWriter(file3);
  337. writer.print("content c");
  338. writer.close();
  339. // file sub/a.txt is modified
  340. writer = new PrintWriter(file);
  341. writer.print("modified content");
  342. writer.close();
  343. // file sub/b.txt is deleted
  344. FileUtils.delete(file2);
  345. git.add().addFilepattern("sub").call();
  346. // change in sub/a.txt is staged
  347. // deletion of sub/b.txt is not staged
  348. // sub/c.txt is staged
  349. assertEquals(
  350. "[sub/a.txt, mode:100644, content:modified content]" +
  351. "[sub/b.txt, mode:100644, content:content b]" +
  352. "[sub/c.txt, mode:100644, content:content c]",
  353. indexState(CONTENT));
  354. }
  355. // file a exists in workdir and in index -> added
  356. // file b exists not in workdir but in index -> deleted
  357. // file c exists in workdir but not in index -> unchanged
  358. @Test
  359. public void testAddWithParameterUpdate() throws Exception {
  360. FileUtils.mkdir(new File(db.getWorkTree(), "sub"));
  361. File file = new File(db.getWorkTree(), "sub/a.txt");
  362. FileUtils.createNewFile(file);
  363. PrintWriter writer = new PrintWriter(file);
  364. writer.print("content");
  365. writer.close();
  366. File file2 = new File(db.getWorkTree(), "sub/b.txt");
  367. FileUtils.createNewFile(file2);
  368. writer = new PrintWriter(file2);
  369. writer.print("content b");
  370. writer.close();
  371. Git git = new Git(db);
  372. git.add().addFilepattern("sub").call();
  373. assertEquals(
  374. "[sub/a.txt, mode:100644, content:content]" +
  375. "[sub/b.txt, mode:100644, content:content b]",
  376. indexState(CONTENT));
  377. git.commit().setMessage("commit").call();
  378. // new unstaged file sub/c.txt
  379. File file3 = new File(db.getWorkTree(), "sub/c.txt");
  380. FileUtils.createNewFile(file3);
  381. writer = new PrintWriter(file3);
  382. writer.print("content c");
  383. writer.close();
  384. // file sub/a.txt is modified
  385. writer = new PrintWriter(file);
  386. writer.print("modified content");
  387. writer.close();
  388. FileUtils.delete(file2);
  389. // change in sub/a.txt is staged
  390. // deletion of sub/b.txt is staged
  391. // sub/c.txt is not staged
  392. git.add().addFilepattern("sub").setUpdate(true).call();
  393. // change in sub/a.txt is staged
  394. assertEquals(
  395. "[sub/a.txt, mode:100644, content:modified content]",
  396. indexState(CONTENT));
  397. }
  398. @Test
  399. public void testAssumeUnchanged() throws Exception {
  400. Git git = new Git(db);
  401. String path = "a.txt";
  402. writeTrashFile(path, "content");
  403. git.add().addFilepattern(path).call();
  404. String path2 = "b.txt";
  405. writeTrashFile(path2, "content");
  406. git.add().addFilepattern(path2).call();
  407. git.commit().setMessage("commit").call();
  408. assertEquals("[a.txt, mode:100644, content:"
  409. + "content, assume-unchanged:false]"
  410. + "[b.txt, mode:100644, content:content, "
  411. + "assume-unchanged:false]", indexState(CONTENT
  412. | ASSUME_UNCHANGED));
  413. assumeUnchanged(path2);
  414. assertEquals("[a.txt, mode:100644, content:content, "
  415. + "assume-unchanged:false][b.txt, mode:100644, "
  416. + "content:content, assume-unchanged:true]", indexState(CONTENT
  417. | ASSUME_UNCHANGED));
  418. writeTrashFile(path, "more content");
  419. writeTrashFile(path2, "more content");
  420. git.add().addFilepattern(".").call();
  421. assertEquals("[a.txt, mode:100644, content:more content,"
  422. + " assume-unchanged:false][b.txt, mode:100644,"
  423. + "" + ""
  424. + " content:content, assume-unchanged:true]",
  425. indexState(CONTENT
  426. | ASSUME_UNCHANGED));
  427. }
  428. @Test
  429. public void testExecutableRetention() throws Exception {
  430. StoredConfig config = db.getConfig();
  431. config.setBoolean(ConfigConstants.CONFIG_CORE_SECTION, null,
  432. ConfigConstants.CONFIG_KEY_FILEMODE, true);
  433. config.save();
  434. FS executableFs = new FS() {
  435. public boolean supportsExecute() {
  436. return true;
  437. }
  438. public boolean setExecute(File f, boolean canExec) {
  439. return true;
  440. }
  441. public ProcessBuilder runInShell(String cmd, String[] args) {
  442. return null;
  443. }
  444. public boolean retryFailedLockFileCommit() {
  445. return false;
  446. }
  447. public FS newInstance() {
  448. return this;
  449. }
  450. protected File discoverGitPrefix() {
  451. return null;
  452. }
  453. public boolean canExecute(File f) {
  454. return true;
  455. }
  456. };
  457. Git git = Git.open(db.getDirectory(), executableFs);
  458. String path = "a.txt";
  459. writeTrashFile(path, "content");
  460. git.add().addFilepattern(path).call();
  461. RevCommit commit1 = git.commit().setMessage("commit").call();
  462. TreeWalk walk = TreeWalk.forPath(db, path, commit1.getTree());
  463. assertNotNull(walk);
  464. assertEquals(FileMode.EXECUTABLE_FILE, walk.getFileMode(0));
  465. FS nonExecutableFs = new FS() {
  466. public boolean supportsExecute() {
  467. return false;
  468. }
  469. public boolean setExecute(File f, boolean canExec) {
  470. return false;
  471. }
  472. public ProcessBuilder runInShell(String cmd, String[] args) {
  473. return null;
  474. }
  475. public boolean retryFailedLockFileCommit() {
  476. return false;
  477. }
  478. public FS newInstance() {
  479. return this;
  480. }
  481. protected File discoverGitPrefix() {
  482. return null;
  483. }
  484. public boolean canExecute(File f) {
  485. return false;
  486. }
  487. };
  488. config = db.getConfig();
  489. config.setBoolean(ConfigConstants.CONFIG_CORE_SECTION, null,
  490. ConfigConstants.CONFIG_KEY_FILEMODE, false);
  491. config.save();
  492. Git git2 = Git.open(db.getDirectory(), nonExecutableFs);
  493. writeTrashFile(path, "content2");
  494. git2.add().addFilepattern(path).call();
  495. RevCommit commit2 = git2.commit().setMessage("commit2").call();
  496. walk = TreeWalk.forPath(db, path, commit2.getTree());
  497. assertNotNull(walk);
  498. assertEquals(FileMode.EXECUTABLE_FILE, walk.getFileMode(0));
  499. }
  500. private DirCacheEntry addEntryToBuilder(String path, File file,
  501. ObjectInserter newObjectInserter, DirCacheBuilder builder, int stage)
  502. throws IOException {
  503. FileInputStream inputStream = new FileInputStream(file);
  504. ObjectId id = newObjectInserter.insert(
  505. Constants.OBJ_BLOB, file.length(), inputStream);
  506. inputStream.close();
  507. DirCacheEntry entry = new DirCacheEntry(path, stage);
  508. entry.setObjectId(id);
  509. entry.setFileMode(FileMode.REGULAR_FILE);
  510. entry.setLastModified(file.lastModified());
  511. entry.setLength((int) file.length());
  512. builder.add(entry);
  513. return entry;
  514. }
  515. private void assumeUnchanged(String path) throws IOException {
  516. final DirCache dirc = db.lockDirCache();
  517. final DirCacheEntry ent = dirc.getEntry(path);
  518. if (ent != null)
  519. ent.setAssumeValid(true);
  520. dirc.write();
  521. if (!dirc.commit())
  522. throw new IOException("could not commit");
  523. }
  524. }