View Javadoc
1   /*
2    * Copyright (C) 2018, Thomas Wolf <thomas.wolf@paranor.ch>
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.api;
44  
45  import static org.junit.Assert.assertEquals;
46  import static org.junit.Assert.assertTrue;
47  
48  import java.io.File;
49  import java.util.HashMap;
50  import java.util.Map;
51  
52  import org.eclipse.jgit.api.ResetCommand.ResetType;
53  import org.eclipse.jgit.junit.MockSystemReader;
54  import org.eclipse.jgit.junit.RepositoryTestCase;
55  import org.eclipse.jgit.lib.CoreConfig.EolStreamType;
56  import org.eclipse.jgit.storage.file.FileBasedConfig;
57  import org.eclipse.jgit.treewalk.FileTreeIterator;
58  import org.eclipse.jgit.treewalk.TreeWalk;
59  import org.eclipse.jgit.treewalk.TreeWalk.OperationType;
60  import org.eclipse.jgit.util.SystemReader;
61  import org.junit.Test;
62  
63  public class CrLfNativeTest extends RepositoryTestCase {
64  
65  	@Test
66  	public void checkoutWithCrLfNativeUnix() throws Exception {
67  		verifyNativeCheckout(new MockSystemReader() {
68  			{
69  				setUnix();
70  			}
71  		});
72  	}
73  
74  	@Test
75  	public void checkoutWithCrLfNativeWindows() throws Exception {
76  		verifyNativeCheckout(new MockSystemReader() {
77  			{
78  				setWindows();
79  			}
80  		});
81  	}
82  
83  	private void verifyNativeCheckout(SystemReader systemReader)
84  			throws Exception {
85  		SystemReader.setInstance(systemReader);
86  		Git git = Git.wrap(db);
87  		FileBasedConfig config = db.getConfig();
88  		config.setString("core", null, "autocrlf", "false");
89  		config.setString("core", null, "eol", "native");
90  		config.save();
91  		// core.eol is active only if text is set, or if text=auto
92  		writeTrashFile(".gitattributes", "*.txt text\n");
93  		File file = writeTrashFile("file.txt", "line 1\nline 2\n");
94  		git.add().addFilepattern("file.txt").addFilepattern(".gitattributes")
95  				.call();
96  		git.commit().setMessage("Initial").call();
97  		// Check-in with core.eol=native normalization
98  		assertEquals(
99  				"[.gitattributes, mode:100644, content:*.txt text\n]"
100 						+ "[file.txt, mode:100644, content:line 1\nline 2\n]",
101 				indexState(CONTENT));
102 		writeTrashFile("file.txt", "something else");
103 		git.add().addFilepattern("file.txt").call();
104 		git.commit().setMessage("New commit").call();
105 		git.reset().setMode(ResetType.HARD).setRef("HEAD~").call();
106 		// Check-out should convert to the native line separator
107 		checkFile(file, systemReader.isWindows() ? "line 1\r\nline 2\r\n"
108 				: "line 1\nline 2\n");
109 		Status status = git.status().call();
110 		assertTrue("git status should be clean", status.isClean());
111 	}
112 
113 	/**
114 	 * Verifies the handling of the crlf attribute: crlf == text, -crlf ==
115 	 * -text, crlf=input == eol=lf
116 	 *
117 	 * @throws Exception
118 	 */
119 	@Test
120 	public void testCrLfAttribute() throws Exception {
121 		FileBasedConfig config = db.getConfig();
122 		config.setString("core", null, "autocrlf", "false");
123 		config.setString("core", null, "eol", "crlf");
124 		config.save();
125 		writeTrashFile(".gitattributes",
126 				"*.txt text\n*.crlf crlf\n*.bin -text\n*.nocrlf -crlf\n*.input crlf=input\n*.eol eol=lf");
127 		writeTrashFile("foo.txt", "");
128 		writeTrashFile("foo.crlf", "");
129 		writeTrashFile("foo.bin", "");
130 		writeTrashFile("foo.nocrlf", "");
131 		writeTrashFile("foo.input", "");
132 		writeTrashFile("foo.eol", "");
133 		Map<String, EolStreamType> inTypes = new HashMap<>();
134 		Map<String, EolStreamType> outTypes = new HashMap<>();
135 		try (TreeWalk walk = new TreeWalk(db)) {
136 			walk.addTree(new FileTreeIterator(db));
137 			while (walk.next()) {
138 				String path = walk.getPathString();
139 				if (".gitattributes".equals(path)) {
140 					continue;
141 				}
142 				EolStreamType in = walk
143 						.getEolStreamType(OperationType.CHECKIN_OP);
144 				EolStreamType out = walk
145 						.getEolStreamType(OperationType.CHECKOUT_OP);
146 				inTypes.put(path, in);
147 				outTypes.put(path, out);
148 			}
149 		}
150 		assertEquals("", checkTypes("check-in", inTypes));
151 		assertEquals("", checkTypes("check-out", outTypes));
152 	}
153 
154 	private String checkTypes(String prefix, Map<String, EolStreamType> types) {
155 		StringBuilder result = new StringBuilder();
156 		EolStreamType a = types.get("foo.crlf");
157 		EolStreamType b = types.get("foo.txt");
158 		report(result, prefix, "crlf != text", a, b);
159 		a = types.get("foo.nocrlf");
160 		b = types.get("foo.bin");
161 		report(result, prefix, "-crlf != -text", a, b);
162 		a = types.get("foo.input");
163 		b = types.get("foo.eol");
164 		report(result, prefix, "crlf=input != eol=lf", a, b);
165 		return result.toString();
166 	}
167 
168 	private void report(StringBuilder result, String prefix, String label,
169 			EolStreamType a,
170 			EolStreamType b) {
171 		if (a == null || b == null || !a.equals(b)) {
172 			result.append(prefix).append(' ').append(label).append(": ")
173 					.append(toString(a)).append(" != ").append(toString(b))
174 					.append('\n');
175 		}
176 	}
177 
178 	private String toString(EolStreamType type) {
179 		return type == null ? "null" : type.name();
180 	}
181 }