Git Porcelain API: Add Command
[jgit.git] / org.eclipse.jgit.test / tst / org / eclipse / jgit / api / AddCommandTest.java
blob5eec1eb48d255f6e250990cf2630675f8d7d5b8d
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.
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
11 * All rights reserved.
13 * Redistribution and use in source and binary forms, with or
14 * without modification, are permitted provided that the following
15 * conditions are met:
17 * - Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
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.
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.
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.
44 package org.eclipse.jgit.api;
46 import java.io.File;
47 import java.io.IOException;
48 import java.io.PrintWriter;
50 import org.eclipse.jgit.dircache.DirCache;
51 import org.eclipse.jgit.dircache.DirCacheBuilder;
52 import org.eclipse.jgit.dircache.DirCacheEntry;
53 import org.eclipse.jgit.lib.FileMode;
54 import org.eclipse.jgit.lib.ObjectId;
55 import org.eclipse.jgit.lib.ObjectWriter;
56 import org.eclipse.jgit.lib.RepositoryTestCase;
58 public class AddCommandTest extends RepositoryTestCase {
60 public void testAddNothing() {
61 Git git = new Git(db);
63 try {
64 git.add().call();
65 fail("Expected IllegalArgumentException");
66 } catch (NoFilepatternException e) {
67 // expected
72 public void testAddNonExistingSingleFile() throws NoFilepatternException {
73 Git git = new Git(db);
75 DirCache dc = git.add().addFilepattern("a.txt").call();
76 assertEquals(0, dc.getEntryCount());
80 public void testAddExistingSingleFile() throws IOException, NoFilepatternException {
81 File file = new File(db.getWorkDir(), "a.txt");
82 file.createNewFile();
83 PrintWriter writer = new PrintWriter(file);
84 writer.print("content");
85 writer.close();
87 Git git = new Git(db);
89 DirCache dc = git.add().addFilepattern("a.txt").call();
91 assertEquals(1, dc.getEntryCount());
92 assertEquals("a.txt", dc.getEntry(0).getPathString());
93 assertNotNull(dc.getEntry(0).getObjectId());
94 assertEquals(file.lastModified(), dc.getEntry(0).getLastModified());
95 assertEquals(file.length(), dc.getEntry(0).getLength());
96 assertEquals(FileMode.REGULAR_FILE, dc.getEntry(0).getFileMode());
97 assertEquals(0, dc.getEntry(0).getStage());
100 public void testAddExistingSingleFileInSubDir() throws IOException, NoFilepatternException {
101 new File(db.getWorkDir(), "sub").mkdir();
102 File file = new File(db.getWorkDir(), "sub/a.txt");
103 file.createNewFile();
104 PrintWriter writer = new PrintWriter(file);
105 writer.print("content");
106 writer.close();
108 Git git = new Git(db);
110 DirCache dc = git.add().addFilepattern("sub/a.txt").call();
112 assertEquals(1, dc.getEntryCount());
113 assertEquals("sub/a.txt", dc.getEntry(0).getPathString());
114 assertNotNull(dc.getEntry(0).getObjectId());
115 assertEquals(file.lastModified(), dc.getEntry(0).getLastModified());
116 assertEquals(file.length(), dc.getEntry(0).getLength());
117 assertEquals(FileMode.REGULAR_FILE, dc.getEntry(0).getFileMode());
118 assertEquals(0, dc.getEntry(0).getStage());
121 public void testAddExistingSingleFileTwice() throws IOException, NoFilepatternException {
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();
128 Git git = new Git(db);
129 DirCache dc = git.add().addFilepattern("a.txt").call();
131 ObjectId id1 = dc.getEntry(0).getObjectId();
133 writer = new PrintWriter(file);
134 writer.print("other content");
135 writer.close();
137 dc = git.add().addFilepattern("a.txt").call();
139 assertEquals(1, dc.getEntryCount());
140 assertEquals("a.txt", dc.getEntry(0).getPathString());
141 assertNotSame(id1, dc.getEntry(0).getObjectId());
142 assertEquals(0, dc.getEntry(0).getStage());
145 public void testAddExistingSingleFileTwiceWithCommit() throws Exception {
146 File file = new File(db.getWorkDir(), "a.txt");
147 file.createNewFile();
148 PrintWriter writer = new PrintWriter(file);
149 writer.print("content");
150 writer.close();
152 Git git = new Git(db);
153 DirCache dc = git.add().addFilepattern("a.txt").call();
155 ObjectId id1 = dc.getEntry(0).getObjectId();
157 git.commit().setMessage("commit a.txt").call();
159 writer = new PrintWriter(file);
160 writer.print("other content");
161 writer.close();
163 dc = git.add().addFilepattern("a.txt").call();
165 assertEquals(1, dc.getEntryCount());
166 assertEquals("a.txt", dc.getEntry(0).getPathString());
167 assertNotSame(id1, dc.getEntry(0).getObjectId());
168 assertEquals(0, dc.getEntry(0).getStage());
171 public void testAddRemovedFile() throws Exception {
172 File file = new File(db.getWorkDir(), "a.txt");
173 file.createNewFile();
174 PrintWriter writer = new PrintWriter(file);
175 writer.print("content");
176 writer.close();
178 Git git = new Git(db);
179 DirCache dc = git.add().addFilepattern("a.txt").call();
181 ObjectId id1 = dc.getEntry(0).getObjectId();
182 file.delete();
184 // is supposed to do nothing
185 dc = git.add().addFilepattern("a.txt").call();
187 assertEquals(1, dc.getEntryCount());
188 assertEquals("a.txt", dc.getEntry(0).getPathString());
189 assertEquals(id1, dc.getEntry(0).getObjectId());
190 assertEquals(0, dc.getEntry(0).getStage());
193 public void testAddRemovedCommittedFile() throws Exception {
194 File file = new File(db.getWorkDir(), "a.txt");
195 file.createNewFile();
196 PrintWriter writer = new PrintWriter(file);
197 writer.print("content");
198 writer.close();
200 Git git = new Git(db);
201 DirCache dc = git.add().addFilepattern("a.txt").call();
203 git.commit().setMessage("commit a.txt").call();
205 ObjectId id1 = dc.getEntry(0).getObjectId();
206 file.delete();
208 // is supposed to do nothing
209 dc = git.add().addFilepattern("a.txt").call();
211 assertEquals(1, dc.getEntryCount());
212 assertEquals("a.txt", dc.getEntry(0).getPathString());
213 assertEquals(id1, dc.getEntry(0).getObjectId());
214 assertEquals(0, dc.getEntry(0).getStage());
217 public void testAddWithConflicts() throws Exception {
218 // prepare conflict
220 File file = new File(db.getWorkDir(), "a.txt");
221 file.createNewFile();
222 PrintWriter writer = new PrintWriter(file);
223 writer.print("content");
224 writer.close();
226 File file2 = new File(db.getWorkDir(), "b.txt");
227 file2.createNewFile();
228 writer = new PrintWriter(file2);
229 writer.print("content b");
230 writer.close();
232 ObjectWriter ow = new ObjectWriter(db);
233 DirCache dc = DirCache.lock(db);
234 DirCacheBuilder builder = dc.builder();
236 addEntryToBuilder("b.txt", file2, ow, builder, 0);
237 addEntryToBuilder("a.txt", file, ow, builder, 1);
239 writer = new PrintWriter(file);
240 writer.print("other content");
241 writer.close();
242 addEntryToBuilder("a.txt", file, ow, builder, 3);
244 writer = new PrintWriter(file);
245 writer.print("our content");
246 writer.close();
247 ObjectId id1 = addEntryToBuilder("a.txt", file, ow, builder, 2)
248 .getObjectId();
250 builder.commit();
252 assertEquals(4, dc.getEntryCount());
254 // now the test begins
256 Git git = new Git(db);
257 dc = git.add().addFilepattern("a.txt").call();
259 assertEquals(2, dc.getEntryCount());
260 assertEquals("a.txt", dc.getEntry("a.txt").getPathString());
261 assertEquals(id1, dc.getEntry("a.txt").getObjectId());
262 assertEquals(0, dc.getEntry("a.txt").getStage());
263 assertEquals(0, dc.getEntry("b.txt").getStage());
266 public void testAddTwoFiles() throws Exception {
267 File file = new File(db.getWorkDir(), "a.txt");
268 file.createNewFile();
269 PrintWriter writer = new PrintWriter(file);
270 writer.print("content");
271 writer.close();
273 File file2 = new File(db.getWorkDir(), "b.txt");
274 file2.createNewFile();
275 writer = new PrintWriter(file2);
276 writer.print("content b");
277 writer.close();
279 Git git = new Git(db);
280 DirCache dc = git.add().addFilepattern("a.txt").addFilepattern("b.txt").call();
281 assertEquals("a.txt", dc.getEntry("a.txt").getPathString());
282 assertEquals("b.txt", dc.getEntry("b.txt").getPathString());
283 assertNotNull(dc.getEntry("a.txt").getObjectId());
284 assertNotNull(dc.getEntry("b.txt").getObjectId());
285 assertEquals(0, dc.getEntry("a.txt").getStage());
286 assertEquals(0, dc.getEntry("b.txt").getStage());
289 public void testAddFolder() throws Exception {
290 new File(db.getWorkDir(), "sub").mkdir();
291 File file = new File(db.getWorkDir(), "sub/a.txt");
292 file.createNewFile();
293 PrintWriter writer = new PrintWriter(file);
294 writer.print("content");
295 writer.close();
297 File file2 = new File(db.getWorkDir(), "sub/b.txt");
298 file2.createNewFile();
299 writer = new PrintWriter(file2);
300 writer.print("content b");
301 writer.close();
303 Git git = new Git(db);
304 DirCache dc = git.add().addFilepattern("sub").call();
305 assertEquals("sub/a.txt", dc.getEntry("sub/a.txt").getPathString());
306 assertEquals("sub/b.txt", dc.getEntry("sub/b.txt").getPathString());
307 assertNotNull(dc.getEntry("sub/a.txt").getObjectId());
308 assertNotNull(dc.getEntry("sub/b.txt").getObjectId());
309 assertEquals(0, dc.getEntry("sub/a.txt").getStage());
310 assertEquals(0, dc.getEntry("sub/b.txt").getStage());
313 private DirCacheEntry addEntryToBuilder(String path, File file,
314 ObjectWriter ow, DirCacheBuilder builder, int stage)
315 throws IOException {
316 ObjectId id = ow.writeBlob(file);
317 DirCacheEntry entry = new DirCacheEntry(path, stage);
318 entry.setObjectId(id);
319 entry.setFileMode(FileMode.REGULAR_FILE);
320 entry.setLastModified(file.lastModified());
321 entry.setLength((int) file.length());
323 builder.add(entry);
324 return entry;