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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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 java.io.File;
  46. import java.io.IOException;
  47. import java.io.PrintWriter;
  48. import org.eclipse.jgit.dircache.DirCache;
  49. import org.eclipse.jgit.dircache.DirCacheBuilder;
  50. import org.eclipse.jgit.dircache.DirCacheEntry;
  51. import org.eclipse.jgit.lib.FileMode;
  52. import org.eclipse.jgit.lib.ObjectId;
  53. import org.eclipse.jgit.lib.ObjectWriter;
  54. import org.eclipse.jgit.lib.RepositoryTestCase;
  55. public class AddCommandTest extends RepositoryTestCase {
  56. public void testAddNothing() {
  57. Git git = new Git(db);
  58. try {
  59. git.add().call();
  60. fail("Expected IllegalArgumentException");
  61. } catch (NoFilepatternException e) {
  62. // expected
  63. }
  64. }
  65. public void testAddNonExistingSingleFile() throws NoFilepatternException {
  66. Git git = new Git(db);
  67. DirCache dc = git.add().addFilepattern("a.txt").call();
  68. assertEquals(0, dc.getEntryCount());
  69. }
  70. public void testAddExistingSingleFile() throws IOException, NoFilepatternException {
  71. File file = new File(db.getWorkDir(), "a.txt");
  72. file.createNewFile();
  73. PrintWriter writer = new PrintWriter(file);
  74. writer.print("content");
  75. writer.close();
  76. Git git = new Git(db);
  77. DirCache dc = git.add().addFilepattern("a.txt").call();
  78. assertEquals(1, dc.getEntryCount());
  79. assertEquals("a.txt", dc.getEntry(0).getPathString());
  80. assertNotNull(dc.getEntry(0).getObjectId());
  81. assertEquals(file.lastModified(), dc.getEntry(0).getLastModified());
  82. assertEquals(file.length(), dc.getEntry(0).getLength());
  83. assertEquals(FileMode.REGULAR_FILE, dc.getEntry(0).getFileMode());
  84. assertEquals(0, dc.getEntry(0).getStage());
  85. }
  86. public void testAddExistingSingleFileInSubDir() throws IOException, NoFilepatternException {
  87. new File(db.getWorkDir(), "sub").mkdir();
  88. File file = new File(db.getWorkDir(), "sub/a.txt");
  89. file.createNewFile();
  90. PrintWriter writer = new PrintWriter(file);
  91. writer.print("content");
  92. writer.close();
  93. Git git = new Git(db);
  94. DirCache dc = git.add().addFilepattern("sub/a.txt").call();
  95. assertEquals(1, dc.getEntryCount());
  96. assertEquals("sub/a.txt", dc.getEntry(0).getPathString());
  97. assertNotNull(dc.getEntry(0).getObjectId());
  98. assertEquals(file.lastModified(), dc.getEntry(0).getLastModified());
  99. assertEquals(file.length(), dc.getEntry(0).getLength());
  100. assertEquals(FileMode.REGULAR_FILE, dc.getEntry(0).getFileMode());
  101. assertEquals(0, dc.getEntry(0).getStage());
  102. }
  103. public void testAddExistingSingleFileTwice() throws IOException, NoFilepatternException {
  104. File file = new File(db.getWorkDir(), "a.txt");
  105. file.createNewFile();
  106. PrintWriter writer = new PrintWriter(file);
  107. writer.print("content");
  108. writer.close();
  109. Git git = new Git(db);
  110. DirCache dc = git.add().addFilepattern("a.txt").call();
  111. ObjectId id1 = dc.getEntry(0).getObjectId();
  112. writer = new PrintWriter(file);
  113. writer.print("other content");
  114. writer.close();
  115. dc = git.add().addFilepattern("a.txt").call();
  116. assertEquals(1, dc.getEntryCount());
  117. assertEquals("a.txt", dc.getEntry(0).getPathString());
  118. assertNotSame(id1, dc.getEntry(0).getObjectId());
  119. assertEquals(0, dc.getEntry(0).getStage());
  120. }
  121. public void testAddExistingSingleFileTwiceWithCommit() throws Exception {
  122. File file = new File(db.getWorkDir(), "a.txt");
  123. file.createNewFile();
  124. PrintWriter writer = new PrintWriter(file);
  125. writer.print("content");
  126. writer.close();
  127. Git git = new Git(db);
  128. DirCache dc = git.add().addFilepattern("a.txt").call();
  129. ObjectId id1 = dc.getEntry(0).getObjectId();
  130. git.commit().setMessage("commit a.txt").call();
  131. writer = new PrintWriter(file);
  132. writer.print("other content");
  133. writer.close();
  134. dc = git.add().addFilepattern("a.txt").call();
  135. assertEquals(1, dc.getEntryCount());
  136. assertEquals("a.txt", dc.getEntry(0).getPathString());
  137. assertNotSame(id1, dc.getEntry(0).getObjectId());
  138. assertEquals(0, dc.getEntry(0).getStage());
  139. }
  140. public void testAddRemovedFile() throws Exception {
  141. File file = new File(db.getWorkDir(), "a.txt");
  142. file.createNewFile();
  143. PrintWriter writer = new PrintWriter(file);
  144. writer.print("content");
  145. writer.close();
  146. Git git = new Git(db);
  147. DirCache dc = git.add().addFilepattern("a.txt").call();
  148. ObjectId id1 = dc.getEntry(0).getObjectId();
  149. file.delete();
  150. // is supposed to do nothing
  151. dc = git.add().addFilepattern("a.txt").call();
  152. assertEquals(1, dc.getEntryCount());
  153. assertEquals("a.txt", dc.getEntry(0).getPathString());
  154. assertEquals(id1, dc.getEntry(0).getObjectId());
  155. assertEquals(0, dc.getEntry(0).getStage());
  156. }
  157. public void testAddRemovedCommittedFile() throws Exception {
  158. File file = new File(db.getWorkDir(), "a.txt");
  159. file.createNewFile();
  160. PrintWriter writer = new PrintWriter(file);
  161. writer.print("content");
  162. writer.close();
  163. Git git = new Git(db);
  164. DirCache dc = git.add().addFilepattern("a.txt").call();
  165. git.commit().setMessage("commit a.txt").call();
  166. ObjectId id1 = dc.getEntry(0).getObjectId();
  167. file.delete();
  168. // is supposed to do nothing
  169. dc = git.add().addFilepattern("a.txt").call();
  170. assertEquals(1, dc.getEntryCount());
  171. assertEquals("a.txt", dc.getEntry(0).getPathString());
  172. assertEquals(id1, dc.getEntry(0).getObjectId());
  173. assertEquals(0, dc.getEntry(0).getStage());
  174. }
  175. public void testAddWithConflicts() throws Exception {
  176. // prepare conflict
  177. File file = new File(db.getWorkDir(), "a.txt");
  178. file.createNewFile();
  179. PrintWriter writer = new PrintWriter(file);
  180. writer.print("content");
  181. writer.close();
  182. File file2 = new File(db.getWorkDir(), "b.txt");
  183. file2.createNewFile();
  184. writer = new PrintWriter(file2);
  185. writer.print("content b");
  186. writer.close();
  187. ObjectWriter ow = new ObjectWriter(db);
  188. DirCache dc = DirCache.lock(db);
  189. DirCacheBuilder builder = dc.builder();
  190. addEntryToBuilder("b.txt", file2, ow, builder, 0);
  191. addEntryToBuilder("a.txt", file, ow, builder, 1);
  192. writer = new PrintWriter(file);
  193. writer.print("other content");
  194. writer.close();
  195. addEntryToBuilder("a.txt", file, ow, builder, 3);
  196. writer = new PrintWriter(file);
  197. writer.print("our content");
  198. writer.close();
  199. ObjectId id1 = addEntryToBuilder("a.txt", file, ow, builder, 2)
  200. .getObjectId();
  201. builder.commit();
  202. assertEquals(4, dc.getEntryCount());
  203. // now the test begins
  204. Git git = new Git(db);
  205. dc = git.add().addFilepattern("a.txt").call();
  206. assertEquals(2, dc.getEntryCount());
  207. assertEquals("a.txt", dc.getEntry("a.txt").getPathString());
  208. assertEquals(id1, dc.getEntry("a.txt").getObjectId());
  209. assertEquals(0, dc.getEntry("a.txt").getStage());
  210. assertEquals(0, dc.getEntry("b.txt").getStage());
  211. }
  212. public void testAddTwoFiles() throws Exception {
  213. File file = new File(db.getWorkDir(), "a.txt");
  214. file.createNewFile();
  215. PrintWriter writer = new PrintWriter(file);
  216. writer.print("content");
  217. writer.close();
  218. File file2 = new File(db.getWorkDir(), "b.txt");
  219. file2.createNewFile();
  220. writer = new PrintWriter(file2);
  221. writer.print("content b");
  222. writer.close();
  223. Git git = new Git(db);
  224. DirCache dc = git.add().addFilepattern("a.txt").addFilepattern("b.txt").call();
  225. assertEquals("a.txt", dc.getEntry("a.txt").getPathString());
  226. assertEquals("b.txt", dc.getEntry("b.txt").getPathString());
  227. assertNotNull(dc.getEntry("a.txt").getObjectId());
  228. assertNotNull(dc.getEntry("b.txt").getObjectId());
  229. assertEquals(0, dc.getEntry("a.txt").getStage());
  230. assertEquals(0, dc.getEntry("b.txt").getStage());
  231. }
  232. public void testAddFolder() throws Exception {
  233. new File(db.getWorkDir(), "sub").mkdir();
  234. File file = new File(db.getWorkDir(), "sub/a.txt");
  235. file.createNewFile();
  236. PrintWriter writer = new PrintWriter(file);
  237. writer.print("content");
  238. writer.close();
  239. File file2 = new File(db.getWorkDir(), "sub/b.txt");
  240. file2.createNewFile();
  241. writer = new PrintWriter(file2);
  242. writer.print("content b");
  243. writer.close();
  244. Git git = new Git(db);
  245. DirCache dc = git.add().addFilepattern("sub").call();
  246. assertEquals("sub/a.txt", dc.getEntry("sub/a.txt").getPathString());
  247. assertEquals("sub/b.txt", dc.getEntry("sub/b.txt").getPathString());
  248. assertNotNull(dc.getEntry("sub/a.txt").getObjectId());
  249. assertNotNull(dc.getEntry("sub/b.txt").getObjectId());
  250. assertEquals(0, dc.getEntry("sub/a.txt").getStage());
  251. assertEquals(0, dc.getEntry("sub/b.txt").getStage());
  252. }
  253. public void testAddIgnoredFile() throws Exception {
  254. new File(db.getWorkDir(), "sub").mkdir();
  255. File file = new File(db.getWorkDir(), "sub/a.txt");
  256. file.createNewFile();
  257. PrintWriter writer = new PrintWriter(file);
  258. writer.print("content");
  259. writer.close();
  260. File ignoreFile = new File(db.getWorkDir(), ".gitignore");
  261. ignoreFile.createNewFile();
  262. writer = new PrintWriter(ignoreFile);
  263. writer.print("sub/b.txt");
  264. writer.close();
  265. File file2 = new File(db.getWorkDir(), "sub/b.txt");
  266. file2.createNewFile();
  267. writer = new PrintWriter(file2);
  268. writer.print("content b");
  269. writer.close();
  270. Git git = new Git(db);
  271. DirCache dc = git.add().addFilepattern("sub").call();
  272. assertEquals("sub/a.txt", dc.getEntry("sub/a.txt").getPathString());
  273. assertNull(dc.getEntry("sub/b.txt"));
  274. assertNotNull(dc.getEntry("sub/a.txt").getObjectId());
  275. assertEquals(0, dc.getEntry("sub/a.txt").getStage());
  276. }
  277. private DirCacheEntry addEntryToBuilder(String path, File file,
  278. ObjectWriter ow, DirCacheBuilder builder, int stage)
  279. throws IOException {
  280. ObjectId id = ow.writeBlob(file);
  281. DirCacheEntry entry = new DirCacheEntry(path, stage);
  282. entry.setObjectId(id);
  283. entry.setFileMode(FileMode.REGULAR_FILE);
  284. entry.setLastModified(file.lastModified());
  285. entry.setLength((int) file.length());
  286. builder.add(entry);
  287. return entry;
  288. }
  289. }