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

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