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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  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(mapTree("c696abc3ab8e091c665f49d00eb8919690b3aec3"));
  207. Entry[] members = index2.getMembers();
  208. assertEquals(3, members.length);
  209. assertEquals("a.b", members[0].getName());
  210. assertEquals("a/b", members[1].getName());
  211. assertEquals("a:b", members[2].getName());
  212. assertEquals(3, members.length);
  213. GitIndex indexr = new GitIndex(db);
  214. indexr.read();
  215. Entry[] membersr = indexr.getMembers();
  216. assertEquals(3, membersr.length);
  217. assertEquals("a.b", membersr[0].getName());
  218. assertEquals("a/b", membersr[1].getName());
  219. assertEquals("a:b", membersr[2].getName());
  220. assertEquals(3, membersr.length);
  221. if (canrungitstatus)
  222. assertEquals(0, system(trash, "git status"));
  223. }
  224. @Test
  225. public void testReadTree2() throws Exception {
  226. // Prepare a larger tree to test some odd cases in tree writing
  227. GitIndex index = new GitIndex(db);
  228. File f1 = writeTrashFile("a/a/a/a", "data:a/a/a/a");
  229. File f2 = writeTrashFile("a/c/c", "data:a/c/c");
  230. File f3 = writeTrashFile("a/b", "data:a/b");
  231. File f4 = writeTrashFile("a:b", "data:a:b");
  232. File f5 = writeTrashFile("a/d", "data:a/d");
  233. File f6 = writeTrashFile("a.b", "data:a.b");
  234. index.add(trash, f1);
  235. index.add(trash, f2);
  236. index.add(trash, f3);
  237. index.add(trash, f4);
  238. index.add(trash, f5);
  239. index.add(trash, f6);
  240. index.write();
  241. ObjectId id = index.writeTree();
  242. System.out.println("wrote id " + id);
  243. assertEquals("ba78e065e2c261d4f7b8f42107588051e87e18e9", id.name());
  244. GitIndex index2 = new GitIndex(db);
  245. index2.readTree(mapTree("ba78e065e2c261d4f7b8f42107588051e87e18e9"));
  246. Entry[] members = index2.getMembers();
  247. assertEquals(6, members.length);
  248. assertEquals("a.b", members[0].getName());
  249. assertEquals("a/a/a/a", members[1].getName());
  250. assertEquals("a/b", members[2].getName());
  251. assertEquals("a/c/c", members[3].getName());
  252. assertEquals("a/d", members[4].getName());
  253. assertEquals("a:b", members[5].getName());
  254. // reread and test
  255. GitIndex indexr = new GitIndex(db);
  256. indexr.read();
  257. Entry[] membersr = indexr.getMembers();
  258. assertEquals(6, membersr.length);
  259. assertEquals("a.b", membersr[0].getName());
  260. assertEquals("a/a/a/a", membersr[1].getName());
  261. assertEquals("a/b", membersr[2].getName());
  262. assertEquals("a/c/c", membersr[3].getName());
  263. assertEquals("a/d", membersr[4].getName());
  264. assertEquals("a:b", membersr[5].getName());
  265. }
  266. @Test
  267. public void testDelete() throws Exception {
  268. GitIndex index = new GitIndex(db);
  269. writeTrashFile("a/b", "data:a/b");
  270. writeTrashFile("a:b", "data:a:b");
  271. writeTrashFile("a.b", "data:a.b");
  272. index.add(trash, new File(trash, "a/b"));
  273. index.add(trash, new File(trash, "a:b"));
  274. index.add(trash, new File(trash, "a.b"));
  275. index.write();
  276. index.writeTree();
  277. index.remove(trash, new File(trash, "a:b"));
  278. index.write();
  279. assertEquals("a.b", index.getMembers()[0].getName());
  280. assertEquals("a/b", index.getMembers()[1].getName());
  281. GitIndex indexr = new GitIndex(db);
  282. indexr.read();
  283. assertEquals("a.b", indexr.getMembers()[0].getName());
  284. assertEquals("a/b", indexr.getMembers()[1].getName());
  285. if (canrungitstatus)
  286. assertEquals(0, system(trash, "git status"));
  287. }
  288. @Test
  289. public void testCheckout() throws Exception {
  290. // Prepare tree, remote it and checkout
  291. GitIndex index = new GitIndex(db);
  292. File aslashb = writeTrashFile("a/b", "data:a/b");
  293. File acolonb = writeTrashFile("a:b", "data:a:b");
  294. File adotb = writeTrashFile("a.b", "data:a.b");
  295. index.add(trash, aslashb);
  296. index.add(trash, acolonb);
  297. index.add(trash, adotb);
  298. index.write();
  299. index.writeTree();
  300. delete(aslashb);
  301. delete(acolonb);
  302. delete(adotb);
  303. delete(aslashb.getParentFile());
  304. GitIndex index2 = new GitIndex(db);
  305. assertEquals(0, index2.getMembers().length);
  306. index2.readTree(mapTree("c696abc3ab8e091c665f49d00eb8919690b3aec3"));
  307. index2.checkout(trash);
  308. assertEquals("data:a/b", read(aslashb));
  309. assertEquals("data:a:b", read(acolonb));
  310. assertEquals("data:a.b", read(adotb));
  311. if (canrungitstatus)
  312. assertEquals(0, system(trash, "git status"));
  313. }
  314. @Test
  315. public void test030_executeBit_coreModeTrue() throws IllegalArgumentException, IllegalAccessException, InvocationTargetException, Error, Exception {
  316. if (!FS.DETECTED.supportsExecute()) {
  317. System.err.println("Test ignored since platform FS does not support the execute permission");
  318. return;
  319. }
  320. try {
  321. // coremode true is the default, typically set to false
  322. // by git init (but not jgit!)
  323. Method canExecute = File.class.getMethod("canExecute", (Class[])null);
  324. Method setExecute = File.class.getMethod("setExecutable", new Class[] { Boolean.TYPE });
  325. File execFile = writeTrashFile("exec","exec");
  326. if (!((Boolean)setExecute.invoke(execFile, new Object[] { Boolean.TRUE })).booleanValue())
  327. throw new Error("could not set execute bit on "+execFile.getAbsolutePath()+"for test");
  328. File nonexecFile = writeTrashFile("nonexec","nonexec");
  329. if (!((Boolean)setExecute.invoke(nonexecFile, new Object[] { Boolean.FALSE })).booleanValue())
  330. throw new Error("could not clear execute bit on "+nonexecFile.getAbsolutePath()+"for test");
  331. GitIndex index = new GitIndex(db);
  332. index.filemode = Boolean.TRUE; // TODO: we need a way to set this using config
  333. index.add(trash, execFile);
  334. index.add(trash, nonexecFile);
  335. Tree tree = mapTree(index.writeTree().name());
  336. assertEquals(FileMode.EXECUTABLE_FILE, tree.findBlobMember(execFile.getName()).getMode());
  337. assertEquals(FileMode.REGULAR_FILE, tree.findBlobMember(nonexecFile.getName()).getMode());
  338. index.write();
  339. if (!execFile.delete())
  340. throw new Error("Problem in test, cannot delete test file "+execFile.getAbsolutePath());
  341. if (!nonexecFile.delete())
  342. throw new Error("Problem in test, cannot delete test file "+nonexecFile.getAbsolutePath());
  343. GitIndex index2 = new GitIndex(db);
  344. index2.filemode = Boolean.TRUE; // TODO: we need a way to set this using config
  345. index2.read();
  346. index2.checkout(trash);
  347. assertTrue(((Boolean)canExecute.invoke(execFile,(Object[])null)).booleanValue());
  348. assertFalse(((Boolean)canExecute.invoke(nonexecFile,(Object[])null)).booleanValue());
  349. assertFalse(index2.getEntry(execFile.getName()).isModified(trash));
  350. assertFalse(index2.getEntry(nonexecFile.getName()).isModified(trash));
  351. if (!((Boolean)setExecute.invoke(execFile, new Object[] { Boolean.FALSE })).booleanValue())
  352. throw new Error("could not clear set execute bit on "+execFile.getAbsolutePath()+"for test");
  353. if (!((Boolean)setExecute.invoke(nonexecFile, new Object[] { Boolean.TRUE })).booleanValue())
  354. throw new Error("could set execute bit on "+nonexecFile.getAbsolutePath()+"for test");
  355. assertTrue(index2.getEntry(execFile.getName()).isModified(trash));
  356. assertTrue(index2.getEntry(nonexecFile.getName()).isModified(trash));
  357. } catch (NoSuchMethodException e) {
  358. System.err.println("Test ignored when running under JDK < 1.6");
  359. return;
  360. }
  361. }
  362. @Test
  363. public void test031_executeBit_coreModeFalse() throws IllegalArgumentException, IllegalAccessException, InvocationTargetException, Error, Exception {
  364. if (!FS.DETECTED.supportsExecute()) {
  365. System.err.println("Test ignored since platform FS does not support the execute permission");
  366. return;
  367. }
  368. try {
  369. // coremode true is the default, typically set to false
  370. // by git init (but not jgit!)
  371. Method canExecute = File.class.getMethod("canExecute", (Class[])null);
  372. Method setExecute = File.class.getMethod("setExecutable", new Class[] { Boolean.TYPE });
  373. File execFile = writeTrashFile("exec","exec");
  374. if (!((Boolean)setExecute.invoke(execFile, new Object[] { Boolean.TRUE })).booleanValue())
  375. throw new Error("could not set execute bit on "+execFile.getAbsolutePath()+"for test");
  376. File nonexecFile = writeTrashFile("nonexec","nonexec");
  377. if (!((Boolean)setExecute.invoke(nonexecFile, new Object[] { Boolean.FALSE })).booleanValue())
  378. throw new Error("could not clear execute bit on "+nonexecFile.getAbsolutePath()+"for test");
  379. GitIndex index = new GitIndex(db);
  380. index.filemode = Boolean.FALSE; // TODO: we need a way to set this using config
  381. index.add(trash, execFile);
  382. index.add(trash, nonexecFile);
  383. Tree tree = mapTree(index.writeTree().name());
  384. assertEquals(FileMode.REGULAR_FILE, tree.findBlobMember(execFile.getName()).getMode());
  385. assertEquals(FileMode.REGULAR_FILE, tree.findBlobMember(nonexecFile.getName()).getMode());
  386. index.write();
  387. if (!execFile.delete())
  388. throw new Error("Problem in test, cannot delete test file "+execFile.getAbsolutePath());
  389. if (!nonexecFile.delete())
  390. throw new Error("Problem in test, cannot delete test file "+nonexecFile.getAbsolutePath());
  391. GitIndex index2 = new GitIndex(db);
  392. index2.filemode = Boolean.FALSE; // TODO: we need a way to set this using config
  393. index2.read();
  394. index2.checkout(trash);
  395. assertFalse(((Boolean)canExecute.invoke(execFile,(Object[])null)).booleanValue());
  396. assertFalse(((Boolean)canExecute.invoke(nonexecFile,(Object[])null)).booleanValue());
  397. assertFalse(index2.getEntry(execFile.getName()).isModified(trash));
  398. assertFalse(index2.getEntry(nonexecFile.getName()).isModified(trash));
  399. if (!((Boolean)setExecute.invoke(execFile, new Object[] { Boolean.FALSE })).booleanValue())
  400. throw new Error("could not clear set execute bit on "+execFile.getAbsolutePath()+"for test");
  401. if (!((Boolean)setExecute.invoke(nonexecFile, new Object[] { Boolean.TRUE })).booleanValue())
  402. throw new Error("could set execute bit on "+nonexecFile.getAbsolutePath()+"for test");
  403. // no change since we ignore the execute bit
  404. assertFalse(index2.getEntry(execFile.getName()).isModified(trash));
  405. assertFalse(index2.getEntry(nonexecFile.getName()).isModified(trash));
  406. } catch (NoSuchMethodException e) {
  407. System.err.println("Test ignored when running under JDK < 1.6");
  408. return;
  409. }
  410. }
  411. private void delete(File f) throws IOException {
  412. if (!f.delete())
  413. throw new IOException("Failed to delete f");
  414. }
  415. private File writeTrashFile(String name, String body) throws IOException {
  416. final File path = new File(trash, name);
  417. write(path, body);
  418. return path;
  419. }
  420. private Tree mapTree(String name) throws IOException {
  421. ObjectId id = db.resolve(name + "^{tree}");
  422. return new Tree(db, id, db.open(id).getCachedBytes());
  423. }
  424. }