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

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