您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

AddCommandTest.java 21KB

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