View Javadoc
1   /*
2    * Copyright (C) 2012-2013, Robin Rosenberg <robin.rosenberg@dewire.com>
3    * and other copyright owners as documented in the project's IP log.
4    *
5    * This program and the accompanying materials are made available
6    * under the terms of the Eclipse Distribution License v1.0 which
7    * accompanies this distribution, is reproduced below, and is
8    * available at http://www.eclipse.org/org/documents/edl-v10.php
9    *
10   * All rights reserved.
11   *
12   * Redistribution and use in source and binary forms, with or
13   * without modification, are permitted provided that the following
14   * conditions are met:
15   *
16   * - Redistributions of source code must retain the above copyright
17   *   notice, this list of conditions and the following disclaimer.
18   *
19   * - Redistributions in binary form must reproduce the above
20   *   copyright notice, this list of conditions and the following
21   *   disclaimer in the documentation and/or other materials provided
22   *   with the distribution.
23   *
24   * - Neither the name of the Eclipse Foundation, Inc. nor the
25   *   names of its contributors may be used to endorse or promote
26   *   products derived from this software without specific prior
27   *   written permission.
28   *
29   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
30   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
31   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
34   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42   */
43  package org.eclipse.jgit.treewalk;
44  
45  import static org.junit.Assert.assertEquals;
46  import static org.junit.Assert.assertFalse;
47  import static org.junit.Assert.assertTrue;
48  
49  import java.io.File;
50  import java.io.IOException;
51  
52  import org.eclipse.jgit.api.Git;
53  import org.eclipse.jgit.api.ResetCommand.ResetType;
54  import org.eclipse.jgit.dircache.DirCache;
55  import org.eclipse.jgit.dircache.DirCacheEditor;
56  import org.eclipse.jgit.dircache.DirCacheEntry;
57  import org.eclipse.jgit.dircache.DirCacheIterator;
58  import org.eclipse.jgit.junit.RepositoryTestCase;
59  import org.eclipse.jgit.lib.Constants;
60  import org.eclipse.jgit.lib.FileMode;
61  import org.eclipse.jgit.lib.ObjectId;
62  import org.eclipse.jgit.lib.ObjectInserter;
63  import org.eclipse.jgit.util.FS;
64  import org.eclipse.jgit.util.FileUtils;
65  import org.junit.Test;
66  
67  public class FileTreeIteratorJava7Test extends RepositoryTestCase {
68  	@Test
69  	public void testFileModeSymLinkIsNotATree() throws IOException {
70  		FS fs = db.getFS();
71  		// mål = target in swedish, just to get som unicode in here
72  		writeTrashFile("mål/data", "targetdata");
73  		fs.createSymLink(new File(trash, "länk"), "mål");
74  		FileTreeIterator fti = new FileTreeIterator(db);
75  		assertFalse(fti.eof());
76  		assertEquals("länk", fti.getEntryPathString());
77  		assertEquals(FileMode.SYMLINK, fti.getEntryFileMode());
78  		fti.next(1);
79  		assertFalse(fti.eof());
80  		assertEquals("mål", fti.getEntryPathString());
81  		assertEquals(FileMode.TREE, fti.getEntryFileMode());
82  		fti.next(1);
83  		assertTrue(fti.eof());
84  	}
85  
86  	@Test
87  	public void testSymlinkNotModifiedThoughNormalized() throws Exception {
88  		DirCache dc = db.lockDirCache();
89  		DirCacheEditor dce = dc.editor();
90  		final String UNNORMALIZED = "target/";
91  		final byte[] UNNORMALIZED_BYTES = Constants.encode(UNNORMALIZED);
92  		try (ObjectInserter oi = db.newObjectInserter()) {
93  			final ObjectId linkid = oi.insert(Constants.OBJ_BLOB,
94  					UNNORMALIZED_BYTES, 0, UNNORMALIZED_BYTES.length);
95  			dce.add(new DirCacheEditor.PathEdit("link") {
96  				@Override
97  				public void apply(DirCacheEntry ent) {
98  					ent.setFileMode(FileMode.SYMLINK);
99  					ent.setObjectId(linkid);
100 					ent.setLength(UNNORMALIZED_BYTES.length);
101 				}
102 			});
103 			assertTrue(dce.commit());
104 		}
105 		new Git(db).commit().setMessage("Adding link").call();
106 		new Git(db).reset().setMode(ResetType.HARD).call();
107 		DirCacheIterator dci = new DirCacheIterator(db.readDirCache());
108 		FileTreeIterator fti = new FileTreeIterator(db);
109 
110 		// self-check
111 		assertEquals("link", fti.getEntryPathString());
112 		assertEquals("link", dci.getEntryPathString());
113 
114 		// test
115 		assertFalse(fti.isModified(dci.getDirCacheEntry(), true,
116 				db.newObjectReader()));
117 	}
118 
119 	/**
120 	 * Like #testSymlinkNotModifiedThoughNormalized but there is no
121 	 * normalization being done.
122 	 *
123 	 * @throws Exception
124 	 */
125 	@Test
126 	public void testSymlinkModifiedNotNormalized() throws Exception {
127 		DirCache dc = db.lockDirCache();
128 		DirCacheEditor dce = dc.editor();
129 		final String NORMALIZED = "target";
130 		final byte[] NORMALIZED_BYTES = Constants.encode(NORMALIZED);
131 		try (ObjectInserter oi = db.newObjectInserter()) {
132 			final ObjectId linkid = oi.insert(Constants.OBJ_BLOB,
133 					NORMALIZED_BYTES, 0, NORMALIZED_BYTES.length);
134 			dce.add(new DirCacheEditor.PathEdit("link") {
135 				@Override
136 				public void apply(DirCacheEntry ent) {
137 					ent.setFileMode(FileMode.SYMLINK);
138 					ent.setObjectId(linkid);
139 					ent.setLength(NORMALIZED_BYTES.length);
140 				}
141 			});
142 			assertTrue(dce.commit());
143 		}
144 		new Git(db).commit().setMessage("Adding link").call();
145 		new Git(db).reset().setMode(ResetType.HARD).call();
146 		DirCacheIterator dci = new DirCacheIterator(db.readDirCache());
147 		FileTreeIterator fti = new FileTreeIterator(db);
148 
149 		// self-check
150 		assertEquals("link", fti.getEntryPathString());
151 		assertEquals("link", dci.getEntryPathString());
152 
153 		// test
154 		assertFalse(fti.isModified(dci.getDirCacheEntry(), true,
155 				db.newObjectReader()));
156 	}
157 
158 	/**
159 	 * Like #testSymlinkNotModifiedThoughNormalized but here the link is
160 	 * modified.
161 	 *
162 	 * @throws Exception
163 	 */
164 	@Test
165 	public void testSymlinkActuallyModified() throws Exception {
166 		final String NORMALIZED = "target";
167 		final byte[] NORMALIZED_BYTES = Constants.encode(NORMALIZED);
168 		try (ObjectInserter oi = db.newObjectInserter()) {
169 			final ObjectId linkid = oi.insert(Constants.OBJ_BLOB,
170 					NORMALIZED_BYTES, 0, NORMALIZED_BYTES.length);
171 			DirCache dc = db.lockDirCache();
172 			DirCacheEditor dce = dc.editor();
173 			dce.add(new DirCacheEditor.PathEdit("link") {
174 				@Override
175 				public void apply(DirCacheEntry ent) {
176 					ent.setFileMode(FileMode.SYMLINK);
177 					ent.setObjectId(linkid);
178 					ent.setLength(NORMALIZED_BYTES.length);
179 				}
180 			});
181 			assertTrue(dce.commit());
182 		}
183 		new Git(db).commit().setMessage("Adding link").call();
184 		new Git(db).reset().setMode(ResetType.HARD).call();
185 
186 		FileUtils.delete(new File(trash, "link"), FileUtils.NONE);
187 		FS.DETECTED.createSymLink(new File(trash, "link"), "newtarget");
188 		DirCacheIterator dci = new DirCacheIterator(db.readDirCache());
189 		FileTreeIterator fti = new FileTreeIterator(db);
190 
191 		// self-check
192 		assertEquals("link", fti.getEntryPathString());
193 		assertEquals("link", dci.getEntryPathString());
194 
195 		// test
196 		assertTrue(fti.isModified(dci.getDirCacheEntry(), true,
197 				db.newObjectReader()));
198 	}
199 }