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.

T0007_GitIndexTest.java 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. /*
  2. * Copyright (C) 2007, Robin Rosenberg <robin.rosenberg@dewire.com>
  3. * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
  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.lib;
  45. import static org.junit.Assert.assertEquals;
  46. import static org.junit.Assert.assertFalse;
  47. import static org.junit.Assert.assertNull;
  48. import static org.junit.Assert.assertTrue;
  49. import java.io.File;
  50. import java.io.IOException;
  51. import java.io.InputStream;
  52. import java.lang.reflect.InvocationTargetException;
  53. import java.lang.reflect.Method;
  54. import org.eclipse.jgit.junit.LocalDiskRepositoryTestCase;
  55. import org.eclipse.jgit.lib.GitIndex.Entry;
  56. import org.eclipse.jgit.util.FS;
  57. import org.junit.Before;
  58. import org.junit.Test;
  59. public class T0007_GitIndexTest extends LocalDiskRepositoryTestCase {
  60. static boolean canrungitstatus;
  61. static {
  62. try {
  63. canrungitstatus = system(new File("."),"git --version") == 0;
  64. } catch (IOException e) {
  65. System.out.println("Warning: cannot invoke native git to validate index");
  66. } catch (InterruptedException e) {
  67. e.printStackTrace();
  68. }
  69. }
  70. private static int system(File dir, String cmd) throws IOException,
  71. InterruptedException {
  72. final Process process = Runtime.getRuntime().exec(cmd, null, dir);
  73. new Thread() {
  74. public void run() {
  75. try {
  76. InputStream s = process.getErrorStream();
  77. for (int c = s.read(); c != -1; c = s.read()) {
  78. System.err.print((char) c);
  79. }
  80. s.close();
  81. } catch (IOException e1) {
  82. // TODO Auto-generated catch block
  83. e1.printStackTrace();
  84. }
  85. }
  86. }.start();
  87. final Thread t2 = new Thread() {
  88. public void run() {
  89. synchronized (this) {
  90. try {
  91. InputStream e = process.getInputStream();
  92. for (int c = e.read(); c != -1; c = e.read()) {
  93. System.out.print((char) c);
  94. }
  95. e.close();
  96. } catch (IOException e1) {
  97. // TODO Auto-generated catch block
  98. e1.printStackTrace();
  99. }
  100. }
  101. }
  102. };
  103. t2.start();
  104. process.getOutputStream().close();
  105. int ret = process.waitFor();
  106. synchronized (t2) {
  107. return ret;
  108. }
  109. }
  110. private Repository db;
  111. private File trash;
  112. @Override
  113. @Before
  114. public void setUp() throws Exception {
  115. super.setUp();
  116. db = createWorkRepository();
  117. trash = db.getWorkTree();
  118. }
  119. @Test
  120. public void testCreateEmptyIndex() throws Exception {
  121. GitIndex index = new GitIndex(db);
  122. index.write();
  123. // native git doesn't like an empty index
  124. // assertEquals(0,system(trash,"git status"));
  125. GitIndex indexr = new GitIndex(db);
  126. indexr.read();
  127. assertEquals(0, indexr.getMembers().length);
  128. }
  129. @Test
  130. public void testReadWithNoIndex() throws Exception {
  131. GitIndex index = new GitIndex(db);
  132. index.read();
  133. assertEquals(0, index.getMembers().length);
  134. }
  135. @Test
  136. public void testCreateSimpleSortTestIndex() throws Exception {
  137. GitIndex index = new GitIndex(db);
  138. writeTrashFile("a/b", "data:a/b");
  139. writeTrashFile("a:b", "data:a:b");
  140. writeTrashFile("a.b", "data:a.b");
  141. index.add(trash, new File(trash, "a/b"));
  142. index.add(trash, new File(trash, "a:b"));
  143. index.add(trash, new File(trash, "a.b"));
  144. index.write();
  145. assertEquals("a/b", index.getEntry("a/b").getName());
  146. assertEquals("a:b", index.getEntry("a:b").getName());
  147. assertEquals("a.b", index.getEntry("a.b").getName());
  148. assertNull(index.getEntry("a*b"));
  149. // Repeat test for re-read index
  150. GitIndex indexr = new GitIndex(db);
  151. indexr.read();
  152. assertEquals("a/b", indexr.getEntry("a/b").getName());
  153. assertEquals("a:b", indexr.getEntry("a:b").getName());
  154. assertEquals("a.b", indexr.getEntry("a.b").getName());
  155. assertNull(indexr.getEntry("a*b"));
  156. if (canrungitstatus)
  157. assertEquals(0, system(trash, "git status"));
  158. }
  159. @Test
  160. public void testUpdateSimpleSortTestIndex() throws Exception {
  161. GitIndex index = new GitIndex(db);
  162. writeTrashFile("a/b", "data:a/b");
  163. writeTrashFile("a:b", "data:a:b");
  164. writeTrashFile("a.b", "data:a.b");
  165. index.add(trash, new File(trash, "a/b"));
  166. index.add(trash, new File(trash, "a:b"));
  167. index.add(trash, new File(trash, "a.b"));
  168. writeTrashFile("a/b", "data:a/b modified");
  169. index.add(trash, new File(trash, "a/b"));
  170. index.write();
  171. if (canrungitstatus)
  172. assertEquals(0, system(trash, "git status"));
  173. }
  174. @Test
  175. public void testWriteTree() throws Exception {
  176. GitIndex index = new GitIndex(db);
  177. writeTrashFile("a/b", "data:a/b");
  178. writeTrashFile("a:b", "data:a:b");
  179. writeTrashFile("a.b", "data:a.b");
  180. index.add(trash, new File(trash, "a/b"));
  181. index.add(trash, new File(trash, "a:b"));
  182. index.add(trash, new File(trash, "a.b"));
  183. index.write();
  184. ObjectId id = index.writeTree();
  185. assertEquals("c696abc3ab8e091c665f49d00eb8919690b3aec3", id.name());
  186. writeTrashFile("a/b", "data:a/b");
  187. index.add(trash, new File(trash, "a/b"));
  188. if (canrungitstatus)
  189. assertEquals(0, system(trash, "git status"));
  190. }
  191. @Test
  192. public void testReadTree() throws Exception {
  193. // Prepare tree
  194. GitIndex index = new GitIndex(db);
  195. writeTrashFile("a/b", "data:a/b");
  196. writeTrashFile("a:b", "data:a:b");
  197. writeTrashFile("a.b", "data:a.b");
  198. index.add(trash, new File(trash, "a/b"));
  199. index.add(trash, new File(trash, "a:b"));
  200. index.add(trash, new File(trash, "a.b"));
  201. index.write();
  202. ObjectId id = index.writeTree();
  203. System.out.println("wrote id " + id);
  204. assertEquals("c696abc3ab8e091c665f49d00eb8919690b3aec3", id.name());
  205. GitIndex index2 = new GitIndex(db);
  206. index2.readTree(db.mapTree(ObjectId.fromString(
  207. "c696abc3ab8e091c665f49d00eb8919690b3aec3")));
  208. Entry[] members = index2.getMembers();
  209. assertEquals(3, members.length);
  210. assertEquals("a.b", members[0].getName());
  211. assertEquals("a/b", members[1].getName());
  212. assertEquals("a:b", members[2].getName());
  213. assertEquals(3, members.length);
  214. GitIndex indexr = new GitIndex(db);
  215. indexr.read();
  216. Entry[] membersr = indexr.getMembers();
  217. assertEquals(3, membersr.length);
  218. assertEquals("a.b", membersr[0].getName());
  219. assertEquals("a/b", membersr[1].getName());
  220. assertEquals("a:b", membersr[2].getName());
  221. assertEquals(3, membersr.length);
  222. if (canrungitstatus)
  223. assertEquals(0, system(trash, "git status"));
  224. }
  225. @Test
  226. public void testReadTree2() throws Exception {
  227. // Prepare a larger tree to test some odd cases in tree writing
  228. GitIndex index = new GitIndex(db);
  229. File f1 = writeTrashFile("a/a/a/a", "data:a/a/a/a");
  230. File f2 = writeTrashFile("a/c/c", "data:a/c/c");
  231. File f3 = writeTrashFile("a/b", "data:a/b");
  232. File f4 = writeTrashFile("a:b", "data:a:b");
  233. File f5 = writeTrashFile("a/d", "data:a/d");
  234. File f6 = writeTrashFile("a.b", "data:a.b");
  235. index.add(trash, f1);
  236. index.add(trash, f2);
  237. index.add(trash, f3);
  238. index.add(trash, f4);
  239. index.add(trash, f5);
  240. index.add(trash, f6);
  241. index.write();
  242. ObjectId id = index.writeTree();
  243. System.out.println("wrote id " + id);
  244. assertEquals("ba78e065e2c261d4f7b8f42107588051e87e18e9", id.name());
  245. GitIndex index2 = new GitIndex(db);
  246. index2.readTree(db.mapTree(ObjectId.fromString(
  247. "ba78e065e2c261d4f7b8f42107588051e87e18e9")));
  248. Entry[] members = index2.getMembers();
  249. assertEquals(6, members.length);
  250. assertEquals("a.b", members[0].getName());
  251. assertEquals("a/a/a/a", members[1].getName());
  252. assertEquals("a/b", members[2].getName());
  253. assertEquals("a/c/c", members[3].getName());
  254. assertEquals("a/d", members[4].getName());
  255. assertEquals("a:b", members[5].getName());
  256. // reread and test
  257. GitIndex indexr = new GitIndex(db);
  258. indexr.read();
  259. Entry[] membersr = indexr.getMembers();
  260. assertEquals(6, membersr.length);
  261. assertEquals("a.b", membersr[0].getName());
  262. assertEquals("a/a/a/a", membersr[1].getName());
  263. assertEquals("a/b", membersr[2].getName());
  264. assertEquals("a/c/c", membersr[3].getName());
  265. assertEquals("a/d", membersr[4].getName());
  266. assertEquals("a:b", membersr[5].getName());
  267. }
  268. @Test
  269. public void testDelete() throws Exception {
  270. GitIndex index = new GitIndex(db);
  271. writeTrashFile("a/b", "data:a/b");
  272. writeTrashFile("a:b", "data:a:b");
  273. writeTrashFile("a.b", "data:a.b");
  274. index.add(trash, new File(trash, "a/b"));
  275. index.add(trash, new File(trash, "a:b"));
  276. index.add(trash, new File(trash, "a.b"));
  277. index.write();
  278. index.writeTree();
  279. index.remove(trash, new File(trash, "a:b"));
  280. index.write();
  281. assertEquals("a.b", index.getMembers()[0].getName());
  282. assertEquals("a/b", index.getMembers()[1].getName());
  283. GitIndex indexr = new GitIndex(db);
  284. indexr.read();
  285. assertEquals("a.b", indexr.getMembers()[0].getName());
  286. assertEquals("a/b", indexr.getMembers()[1].getName());
  287. if (canrungitstatus)
  288. assertEquals(0, system(trash, "git status"));
  289. }
  290. @Test
  291. public void testCheckout() throws Exception {
  292. // Prepare tree, remote it and checkout
  293. GitIndex index = new GitIndex(db);
  294. File aslashb = writeTrashFile("a/b", "data:a/b");
  295. File acolonb = writeTrashFile("a:b", "data:a:b");
  296. File adotb = writeTrashFile("a.b", "data:a.b");
  297. index.add(trash, aslashb);
  298. index.add(trash, acolonb);
  299. index.add(trash, adotb);
  300. index.write();
  301. index.writeTree();
  302. delete(aslashb);
  303. delete(acolonb);
  304. delete(adotb);
  305. delete(aslashb.getParentFile());
  306. GitIndex index2 = new GitIndex(db);
  307. assertEquals(0, index2.getMembers().length);
  308. index2.readTree(db.mapTree(ObjectId.fromString(
  309. "c696abc3ab8e091c665f49d00eb8919690b3aec3")));
  310. index2.checkout(trash);
  311. assertEquals("data:a/b", read(aslashb));
  312. assertEquals("data:a:b", read(acolonb));
  313. assertEquals("data:a.b", read(adotb));
  314. if (canrungitstatus)
  315. assertEquals(0, system(trash, "git status"));
  316. }
  317. @Test
  318. public void test030_executeBit_coreModeTrue() throws IllegalArgumentException, IllegalAccessException, InvocationTargetException, Error, Exception {
  319. if (!FS.DETECTED.supportsExecute()) {
  320. System.err.println("Test ignored since platform FS does not support the execute permission");
  321. return;
  322. }
  323. try {
  324. // coremode true is the default, typically set to false
  325. // by git init (but not jgit!)
  326. Method canExecute = File.class.getMethod("canExecute", (Class[])null);
  327. Method setExecute = File.class.getMethod("setExecutable", new Class[] { Boolean.TYPE });
  328. File execFile = writeTrashFile("exec","exec");
  329. if (!((Boolean)setExecute.invoke(execFile, new Object[] { Boolean.TRUE })).booleanValue())
  330. throw new Error("could not set execute bit on "+execFile.getAbsolutePath()+"for test");
  331. File nonexecFile = writeTrashFile("nonexec","nonexec");
  332. if (!((Boolean)setExecute.invoke(nonexecFile, new Object[] { Boolean.FALSE })).booleanValue())
  333. throw new Error("could not clear execute bit on "+nonexecFile.getAbsolutePath()+"for test");
  334. GitIndex index = new GitIndex(db);
  335. index.filemode = Boolean.TRUE; // TODO: we need a way to set this using config
  336. index.add(trash, execFile);
  337. index.add(trash, nonexecFile);
  338. Tree tree = db.mapTree(index.writeTree());
  339. assertEquals(FileMode.EXECUTABLE_FILE, tree.findBlobMember(execFile.getName()).getMode());
  340. assertEquals(FileMode.REGULAR_FILE, tree.findBlobMember(nonexecFile.getName()).getMode());
  341. index.write();
  342. if (!execFile.delete())
  343. throw new Error("Problem in test, cannot delete test file "+execFile.getAbsolutePath());
  344. if (!nonexecFile.delete())
  345. throw new Error("Problem in test, cannot delete test file "+nonexecFile.getAbsolutePath());
  346. GitIndex index2 = new GitIndex(db);
  347. index2.filemode = Boolean.TRUE; // TODO: we need a way to set this using config
  348. index2.read();
  349. index2.checkout(trash);
  350. assertTrue(((Boolean)canExecute.invoke(execFile,(Object[])null)).booleanValue());
  351. assertFalse(((Boolean)canExecute.invoke(nonexecFile,(Object[])null)).booleanValue());
  352. assertFalse(index2.getEntry(execFile.getName()).isModified(trash));
  353. assertFalse(index2.getEntry(nonexecFile.getName()).isModified(trash));
  354. if (!((Boolean)setExecute.invoke(execFile, new Object[] { Boolean.FALSE })).booleanValue())
  355. throw new Error("could not clear set execute bit on "+execFile.getAbsolutePath()+"for test");
  356. if (!((Boolean)setExecute.invoke(nonexecFile, new Object[] { Boolean.TRUE })).booleanValue())
  357. throw new Error("could set execute bit on "+nonexecFile.getAbsolutePath()+"for test");
  358. assertTrue(index2.getEntry(execFile.getName()).isModified(trash));
  359. assertTrue(index2.getEntry(nonexecFile.getName()).isModified(trash));
  360. } catch (NoSuchMethodException e) {
  361. System.err.println("Test ignored when running under JDK < 1.6");
  362. return;
  363. }
  364. }
  365. @Test
  366. public void test031_executeBit_coreModeFalse() throws IllegalArgumentException, IllegalAccessException, InvocationTargetException, Error, Exception {
  367. if (!FS.DETECTED.supportsExecute()) {
  368. System.err.println("Test ignored since platform FS does not support the execute permission");
  369. return;
  370. }
  371. try {
  372. // coremode true is the default, typically set to false
  373. // by git init (but not jgit!)
  374. Method canExecute = File.class.getMethod("canExecute", (Class[])null);
  375. Method setExecute = File.class.getMethod("setExecutable", new Class[] { Boolean.TYPE });
  376. File execFile = writeTrashFile("exec","exec");
  377. if (!((Boolean)setExecute.invoke(execFile, new Object[] { Boolean.TRUE })).booleanValue())
  378. throw new Error("could not set execute bit on "+execFile.getAbsolutePath()+"for test");
  379. File nonexecFile = writeTrashFile("nonexec","nonexec");
  380. if (!((Boolean)setExecute.invoke(nonexecFile, new Object[] { Boolean.FALSE })).booleanValue())
  381. throw new Error("could not clear execute bit on "+nonexecFile.getAbsolutePath()+"for test");
  382. GitIndex index = new GitIndex(db);
  383. index.filemode = Boolean.FALSE; // TODO: we need a way to set this using config
  384. index.add(trash, execFile);
  385. index.add(trash, nonexecFile);
  386. Tree tree = db.mapTree(index.writeTree());
  387. assertEquals(FileMode.REGULAR_FILE, tree.findBlobMember(execFile.getName()).getMode());
  388. assertEquals(FileMode.REGULAR_FILE, tree.findBlobMember(nonexecFile.getName()).getMode());
  389. index.write();
  390. if (!execFile.delete())
  391. throw new Error("Problem in test, cannot delete test file "+execFile.getAbsolutePath());
  392. if (!nonexecFile.delete())
  393. throw new Error("Problem in test, cannot delete test file "+nonexecFile.getAbsolutePath());
  394. GitIndex index2 = new GitIndex(db);
  395. index2.filemode = Boolean.FALSE; // TODO: we need a way to set this using config
  396. index2.read();
  397. index2.checkout(trash);
  398. assertFalse(((Boolean)canExecute.invoke(execFile,(Object[])null)).booleanValue());
  399. assertFalse(((Boolean)canExecute.invoke(nonexecFile,(Object[])null)).booleanValue());
  400. assertFalse(index2.getEntry(execFile.getName()).isModified(trash));
  401. assertFalse(index2.getEntry(nonexecFile.getName()).isModified(trash));
  402. if (!((Boolean)setExecute.invoke(execFile, new Object[] { Boolean.FALSE })).booleanValue())
  403. throw new Error("could not clear set execute bit on "+execFile.getAbsolutePath()+"for test");
  404. if (!((Boolean)setExecute.invoke(nonexecFile, new Object[] { Boolean.TRUE })).booleanValue())
  405. throw new Error("could set execute bit on "+nonexecFile.getAbsolutePath()+"for test");
  406. // no change since we ignore the execute bit
  407. assertFalse(index2.getEntry(execFile.getName()).isModified(trash));
  408. assertFalse(index2.getEntry(nonexecFile.getName()).isModified(trash));
  409. } catch (NoSuchMethodException e) {
  410. System.err.println("Test ignored when running under JDK < 1.6");
  411. return;
  412. }
  413. }
  414. private void delete(File f) throws IOException {
  415. if (!f.delete())
  416. throw new IOException("Failed to delete f");
  417. }
  418. private File writeTrashFile(String name, String body) throws IOException {
  419. final File path = new File(trash, name);
  420. write(path, body);
  421. return path;
  422. }
  423. }