View Javadoc
1   /*
2    * Copyright (C) 2010, Red Hat Inc.
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.attributes;
44  
45  import static java.util.Arrays.asList;
46  import static org.hamcrest.CoreMatchers.hasItem;
47  import static org.hamcrest.MatcherAssert.assertThat;
48  import static org.junit.Assert.assertEquals;
49  import static org.junit.Assert.assertFalse;
50  import static org.junit.Assert.assertNotNull;
51  import static org.junit.Assert.assertTrue;
52  
53  import java.io.IOException;
54  import java.util.Collections;
55  import java.util.List;
56  
57  import org.eclipse.jgit.api.Git;
58  import org.eclipse.jgit.attributes.Attribute.State;
59  import org.eclipse.jgit.dircache.DirCacheIterator;
60  import org.eclipse.jgit.junit.RepositoryTestCase;
61  import org.eclipse.jgit.lib.FileMode;
62  import org.eclipse.jgit.treewalk.TreeWalk;
63  import org.junit.Before;
64  import org.junit.Test;
65  
66  /**
67   * Tests attributes node behavior on the index.
68   */
69  public class AttributesNodeDirCacheIteratorTest extends RepositoryTestCase {
70  
71  	private static final FileMode D = FileMode.TREE;
72  
73  	private static final FileMode F = FileMode.REGULAR_FILE;
74  
75  	private static Attribute EOL_LF = new Attribute("eol", "lf");
76  
77  	private static Attribute DELTA_UNSET = new Attribute("delta", State.UNSET);
78  
79  	private Git git;
80  
81  	private TreeWalk walk;
82  
83  	@Override
84  	@Before
85  	public void setUp() throws Exception {
86  		super.setUp();
87  		git = new Git(db);
88  
89  	}
90  
91  	@Test
92  	public void testRules() throws Exception {
93  		writeAttributesFile(".git/info/attributes", "windows* eol=crlf");
94  
95  		writeAttributesFile(".gitattributes", "*.txt eol=lf");
96  		writeTrashFile("windows.file", "");
97  		writeTrashFile("windows.txt", "");
98  		writeTrashFile("readme.txt", "");
99  
100 		writeAttributesFile("src/config/.gitattributes", "*.txt -delta");
101 		writeTrashFile("src/config/readme.txt", "");
102 		writeTrashFile("src/config/windows.file", "");
103 		writeTrashFile("src/config/windows.txt", "");
104 
105 		// Adds file to index
106 		git.add().addFilepattern(".").call();
107 
108 		walk = beginWalk();
109 
110 		assertIteration(F, ".gitattributes");
111 		assertIteration(F, "readme.txt", asList(EOL_LF));
112 
113 		assertIteration(D, "src");
114 
115 		assertIteration(D, "src/config");
116 		assertIteration(F, "src/config/.gitattributes");
117 		assertIteration(F, "src/config/readme.txt", asList(DELTA_UNSET));
118 		assertIteration(F, "src/config/windows.file", null);
119 		assertIteration(F, "src/config/windows.txt", asList(DELTA_UNSET));
120 
121 		assertIteration(F, "windows.file", null);
122 		assertIteration(F, "windows.txt", asList(EOL_LF));
123 
124 		endWalk();
125 	}
126 
127 	/**
128 	 * Checks that if there is no .gitattributes file in the repository
129 	 * everything still work fine.
130 	 *
131 	 * @throws Exception
132 	 */
133 	@Test
134 	public void testNoAttributes() throws Exception {
135 		writeTrashFile("l0.txt", "");
136 		writeTrashFile("level1/l1.txt", "");
137 		writeTrashFile("level1/level2/l2.txt", "");
138 
139 		// Adds file to index
140 		git.add().addFilepattern(".").call();
141 		walk = beginWalk();
142 
143 		assertIteration(F, "l0.txt");
144 
145 		assertIteration(D, "level1");
146 		assertIteration(F, "level1/l1.txt");
147 
148 		assertIteration(D, "level1/level2");
149 		assertIteration(F, "level1/level2/l2.txt");
150 
151 		endWalk();
152 	}
153 
154 	/**
155 	 * Checks that empty .gitattribute files do not return incorrect value.
156 	 *
157 	 * @throws Exception
158 	 */
159 	@Test
160 	public void testEmptyGitAttributeFile() throws Exception {
161 		writeAttributesFile(".git/info/attributes", "");
162 		writeTrashFile("l0.txt", "");
163 		writeAttributesFile(".gitattributes", "");
164 		writeTrashFile("level1/l1.txt", "");
165 		writeTrashFile("level1/level2/l2.txt", "");
166 
167 		// Adds file to index
168 		git.add().addFilepattern(".").call();
169 		walk = beginWalk();
170 
171 		assertIteration(F, ".gitattributes");
172 		assertIteration(F, "l0.txt");
173 
174 		assertIteration(D, "level1");
175 		assertIteration(F, "level1/l1.txt");
176 
177 		assertIteration(D, "level1/level2");
178 		assertIteration(F, "level1/level2/l2.txt");
179 
180 		endWalk();
181 	}
182 
183 	@Test
184 	public void testNoMatchingAttributes() throws Exception {
185 		writeAttributesFile(".git/info/attributes", "*.java delta");
186 		writeAttributesFile(".gitattributes", "*.java -delta");
187 		writeAttributesFile("levelA/.gitattributes", "*.java eol=lf");
188 		writeAttributesFile("levelB/.gitattributes", "*.txt eol=lf");
189 
190 		writeTrashFile("levelA/lA.txt", "");
191 
192 		// Adds file to index
193 		git.add().addFilepattern(".").call();
194 		walk = beginWalk();
195 
196 		assertIteration(F, ".gitattributes");
197 
198 		assertIteration(D, "levelA");
199 		assertIteration(F, "levelA/.gitattributes");
200 		assertIteration(F, "levelA/lA.txt");
201 
202 		assertIteration(D, "levelB");
203 		assertIteration(F, "levelB/.gitattributes");
204 
205 		endWalk();
206 	}
207 
208 	@Test
209 	public void testIncorrectAttributeFileName() throws Exception {
210 		writeAttributesFile("levelA/file.gitattributes", "*.txt -delta");
211 		writeAttributesFile("gitattributes", "*.txt eol=lf");
212 
213 		writeTrashFile("l0.txt", "");
214 		writeTrashFile("levelA/lA.txt", "");
215 
216 		// Adds file to index
217 		git.add().addFilepattern(".").call();
218 		walk = beginWalk();
219 
220 		assertIteration(F, "gitattributes");
221 
222 		assertIteration(F, "l0.txt");
223 
224 		assertIteration(D, "levelA");
225 		assertIteration(F, "levelA/file.gitattributes");
226 		assertIteration(F, "levelA/lA.txt");
227 
228 		endWalk();
229 	}
230 
231 	private void assertIteration(FileMode type, String pathName)
232 			throws IOException {
233 		assertIteration(type, pathName, Collections.<Attribute> emptyList());
234 	}
235 
236 	private void assertIteration(FileMode type, String pathName,
237 			List<Attribute> nodeAttrs) throws IOException {
238 		assertTrue("walk has entry", walk.next());
239 		assertEquals(pathName, walk.getPathString());
240 		assertEquals(type, walk.getFileMode(0));
241 		DirCacheIterator itr = walk.getTree(0, DirCacheIterator.class);
242 		assertNotNull("has tree", itr);
243 
244 		AttributesNode attributesNode = itr.getEntryAttributesNode(db
245 				.newObjectReader());
246 		assertAttributesNode(pathName, attributesNode, nodeAttrs);
247 
248 		if (D.equals(type))
249 			walk.enterSubtree();
250 
251 	}
252 
253 	private void assertAttributesNode(String pathName,
254 			AttributesNode attributesNode, List<Attribute> nodeAttrs)
255 					throws IOException {
256 		if (attributesNode == null)
257 			assertTrue(nodeAttrs == null || nodeAttrs.isEmpty());
258 		else {
259 
260 			Attributes entryAttributes = new Attributes();
261 			new AttributesHandler(walk).mergeAttributes(attributesNode,
262 					pathName,
263 					false,
264 					entryAttributes);
265 
266 			if (nodeAttrs != null && !nodeAttrs.isEmpty()) {
267 				for (Attribute attribute : nodeAttrs) {
268 					assertThat(entryAttributes.getAll(),
269 							hasItem(attribute));
270 				}
271 			} else {
272 				assertTrue(
273 						"The entry "
274 								+ pathName
275 								+ " should not have any attributes. Instead, the following attributes are applied to this file "
276 								+ entryAttributes.toString(),
277 						entryAttributes.isEmpty());
278 			}
279 		}
280 	}
281 
282 	private void writeAttributesFile(String name, String... rules)
283 			throws IOException {
284 		StringBuilder data = new StringBuilder();
285 		for (String line : rules)
286 			data.append(line + "\n");
287 		writeTrashFile(name, data.toString());
288 	}
289 
290 	private TreeWalk beginWalk() throws Exception {
291 		TreeWalk newWalk = new TreeWalk(db);
292 		newWalk.addTree(new DirCacheIterator(db.readDirCache()));
293 		return newWalk;
294 	}
295 
296 	private void endWalk() throws IOException {
297 		assertFalse("Not all files tested", walk.next());
298 	}
299 }