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 FS fs = db.getFS();
71
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
111 assertEquals("link", fti.getEntryPathString());
112 assertEquals("link", dci.getEntryPathString());
113
114
115 assertFalse(fti.isModified(dci.getDirCacheEntry(), true,
116 db.newObjectReader()));
117 }
118
119
120
121
122
123
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
150 assertEquals("link", fti.getEntryPathString());
151 assertEquals("link", dci.getEntryPathString());
152
153
154 assertFalse(fti.isModified(dci.getDirCacheEntry(), true,
155 db.newObjectReader()));
156 }
157
158
159
160
161
162
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
192 assertEquals("link", fti.getEntryPathString());
193 assertEquals("link", dci.getEntryPathString());
194
195
196 assertTrue(fti.isModified(dci.getDirCacheEntry(), true,
197 db.newObjectReader()));
198 }
199 }