1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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 org.junit.Assume.assumeTrue(FS.DETECTED.supportsSymlinks());
71 FS fs = db.getFS();
72
73 writeTrashFile("mål/data", "targetdata");
74 fs.createSymLink(new File(trash, "länk"), "mål");
75 FileTreeIterator fti = new FileTreeIterator(db);
76 assertFalse(fti.eof());
77 assertEquals("länk", fti.getEntryPathString());
78 assertEquals(FileMode.SYMLINK, fti.getEntryFileMode());
79 fti.next(1);
80 assertFalse(fti.eof());
81 assertEquals("mål", fti.getEntryPathString());
82 assertEquals(FileMode.TREE, fti.getEntryFileMode());
83 fti.next(1);
84 assertTrue(fti.eof());
85 }
86
87 @Test
88 public void testSymlinkNotModifiedThoughNormalized() throws Exception {
89 DirCache dc = db.lockDirCache();
90 DirCacheEditor dce = dc.editor();
91 final String UNNORMALIZED = "target/";
92 final byte[] UNNORMALIZED_BYTES = Constants.encode(UNNORMALIZED);
93 try (ObjectInserter oi = db.newObjectInserter()) {
94 final ObjectId linkid = oi.insert(Constants.OBJ_BLOB,
95 UNNORMALIZED_BYTES, 0, UNNORMALIZED_BYTES.length);
96 dce.add(new DirCacheEditor.PathEdit("link") {
97 @Override
98 public void apply(DirCacheEntry ent) {
99 ent.setFileMode(FileMode.SYMLINK);
100 ent.setObjectId(linkid);
101 ent.setLength(UNNORMALIZED_BYTES.length);
102 }
103 });
104 assertTrue(dce.commit());
105 }
106 try (Git git = new Git(db)) {
107 git.commit().setMessage("Adding link").call();
108 git.reset().setMode(ResetType.HARD).call();
109 DirCacheIterator dci = new DirCacheIterator(db.readDirCache());
110 FileTreeIterator fti = new FileTreeIterator(db);
111
112
113 assertEquals("link", fti.getEntryPathString());
114 assertEquals("link", dci.getEntryPathString());
115
116
117 assertFalse(fti.isModified(dci.getDirCacheEntry(), true,
118 db.newObjectReader()));
119 }
120 }
121
122
123
124
125
126
127
128 @Test
129 public void testSymlinkModifiedNotNormalized() throws Exception {
130 DirCache dc = db.lockDirCache();
131 DirCacheEditor dce = dc.editor();
132 final String NORMALIZED = "target";
133 final byte[] NORMALIZED_BYTES = Constants.encode(NORMALIZED);
134 try (ObjectInserter oi = db.newObjectInserter()) {
135 final ObjectId linkid = oi.insert(Constants.OBJ_BLOB,
136 NORMALIZED_BYTES, 0, NORMALIZED_BYTES.length);
137 dce.add(new DirCacheEditor.PathEdit("link") {
138 @Override
139 public void apply(DirCacheEntry ent) {
140 ent.setFileMode(FileMode.SYMLINK);
141 ent.setObjectId(linkid);
142 ent.setLength(NORMALIZED_BYTES.length);
143 }
144 });
145 assertTrue(dce.commit());
146 }
147 try (Git git = new Git(db)) {
148 git.commit().setMessage("Adding link").call();
149 git.reset().setMode(ResetType.HARD).call();
150 DirCacheIterator dci = new DirCacheIterator(db.readDirCache());
151 FileTreeIterator fti = new FileTreeIterator(db);
152
153
154 assertEquals("link", fti.getEntryPathString());
155 assertEquals("link", dci.getEntryPathString());
156
157
158 assertFalse(fti.isModified(dci.getDirCacheEntry(), true,
159 db.newObjectReader()));
160 }
161 }
162
163
164
165
166
167
168
169 @Test
170 public void testSymlinkActuallyModified() throws Exception {
171 org.junit.Assume.assumeTrue(FS.DETECTED.supportsSymlinks());
172 final String NORMALIZED = "target";
173 final byte[] NORMALIZED_BYTES = Constants.encode(NORMALIZED);
174 try (ObjectInserter oi = db.newObjectInserter()) {
175 final ObjectId linkid = oi.insert(Constants.OBJ_BLOB,
176 NORMALIZED_BYTES, 0, NORMALIZED_BYTES.length);
177 DirCache dc = db.lockDirCache();
178 DirCacheEditor dce = dc.editor();
179 dce.add(new DirCacheEditor.PathEdit("link") {
180 @Override
181 public void apply(DirCacheEntry ent) {
182 ent.setFileMode(FileMode.SYMLINK);
183 ent.setObjectId(linkid);
184 ent.setLength(NORMALIZED_BYTES.length);
185 }
186 });
187 assertTrue(dce.commit());
188 }
189 try (Git git = new Git(db)) {
190 git.commit().setMessage("Adding link").call();
191 git.reset().setMode(ResetType.HARD).call();
192
193 FileUtils.delete(new File(trash, "link"), FileUtils.NONE);
194 FS.DETECTED.createSymLink(new File(trash, "link"), "newtarget");
195 DirCacheIterator dci = new DirCacheIterator(db.readDirCache());
196 FileTreeIterator fti = new FileTreeIterator(db);
197
198
199 assertEquals("link", fti.getEntryPathString());
200 assertEquals("link", dci.getEntryPathString());
201
202
203 assertTrue(fti.isModified(dci.getDirCacheEntry(), true,
204 db.newObjectReader()));
205 }
206 }
207 }