Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 145d3013cf20f4fd2cca95dd4c5256320e18ab94 (plain) (blame)
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/*******************************************************************************
 * Copyright (C) 2017, Thomas Wolf <thomas.wolf@paranor.ch>
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *******************************************************************************/
package org.eclipse.egit.core.internal.storage;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.egit.core.test.GitTestCase;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.ResetCommand.ResetType;
import org.eclipse.jgit.attributes.FilterCommand;
import org.eclipse.jgit.attributes.FilterCommandFactory;
import org.eclipse.jgit.attributes.FilterCommandRegistry;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.lib.StoredConfig;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
import org.eclipse.jgit.util.IO;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

/**
 * Tests for reading blobs from a commit with .gitattributes support.
 */
public class CommitFileRevisionTest extends GitTestCase {

	Repository repository;

	@Override
	@Before
	public void setUp() throws Exception {
		super.setUp();
		repository = FileRepositoryBuilder.create(gitDir);
		repository.create();
	}

	@Override
	@After
	public void tearDown() throws Exception {
		repository.close();
		super.tearDown();
	}

	private java.nio.file.Path createFile(IProject base, String name,
			String content) throws IOException {
		java.nio.file.Path path = base.getLocation().toFile().toPath()
				.resolve(name);
		Files.write(path, content.getBytes(StandardCharsets.UTF_8));
		return path;
	}

	private RevCommit setupFilter(Repository repo, IProject base,
			boolean commit) throws Exception {
		createFile(base, ".gitattributes", "*.txt filter=test");
		String builtinCommandName = "egit://builtin/test/smudge";
		FilterCommandRegistry.register(builtinCommandName,
				new TestCommandFactory('a', 'x'));
		StoredConfig config = repo.getConfig();
		config.setString("filter", "test", "smudge", builtinCommandName);
		config.save();
		if (commit) {
			try (Git git = new Git(repo)) {
				git.add().addFilepattern(".").call();
				return git.commit().setMessage("Add .gitattributes").call();
			}
		}
		return null;
	}

	@Test
	public void testWithAttributesCheckedIn() throws Exception {
		java.nio.file.Path filePath = createFile(project.getProject(),
				"attr.txt", "a");
		try (Git git = new Git(repository)) {
			git.add().addFilepattern(".").call();
			git.commit().setMessage("Initial commit").call();
			// Verify that we do have "a" in the repo: modify the file, do a
			// hard reset, verify the contents to be "a" again
			createFile(project.getProject(), "attr.txt", "aa");
			git.reset().setMode(ResetType.HARD).call();
			List<String> content = Files.readAllLines(filePath,
					StandardCharsets.UTF_8);
			assertEquals(1, content.size());
			assertEquals("a", content.get(0));
			// Now create a smudge filter that will replace all a's by x's, and
			// commit the .gitattributes file.
			RevCommit head = setupFilter(repository, project.getProject(),
					true);
			// Modify the file again and do a hard reset. We should end up with
			// a file containing "x".
			createFile(project.getProject(), "attr.txt", "aa");
			git.reset().setMode(ResetType.HARD).call();
			content = Files.readAllLines(filePath, StandardCharsets.UTF_8);
			assertEquals(1, content.size());
			assertEquals("x", content.get(0));
			// All right. Now get a CommitFileRevision and check its contents.
			String relativePath = getRevisionPath(filePath);
			CommitFileRevision fileRevision = new CommitFileRevision(repository,
					head, relativePath);
			ByteBuffer rawContent = null;
			try (InputStream blobStream = fileRevision
					.getStorage(new NullProgressMonitor()).getContents()) {
				rawContent = IO.readWholeStream(blobStream, 1);
			}
			assertNotNull(rawContent);
			String blobContent = new String(rawContent.array(), 0,
					rawContent.limit(), StandardCharsets.UTF_8);
			assertEquals("x", blobContent);
		} finally {
			FilterCommandRegistry.unregister("egit://builtin/test/smudge");
		}
	}

	@Test
	public void testWithAttributesNotCheckedIn() throws Exception {
		java.nio.file.Path filePath = createFile(project.getProject(),
				"attr.txt", "a");
		try (Git git = new Git(repository)) {
			git.add().addFilepattern(".").call();
			RevCommit head = git.commit().setMessage("Initial commit").call();
			// Verify that we do have "a" in the repo: modify the file, do a
			// hard reset, verify the contents to be "a" again
			createFile(project.getProject(), "attr.txt", "aa");
			git.reset().setMode(ResetType.HARD).call();
			List<String> content = Files.readAllLines(filePath,
					StandardCharsets.UTF_8);
			assertEquals(1, content.size());
			assertEquals("a", content.get(0));
			// Now create a smudge filter that will replace all a's by x's.
			setupFilter(repository, project.getProject(), false);
			// Modify the file again and do a hard reset. Interestingly we end
			// up with a file containing 'x': the checkout does apply the
			// not-yet-committed .gitattributes. I do not know whether that is
			// correct.
			createFile(project.getProject(), "attr.txt", "aa");
			git.reset().setMode(ResetType.HARD).call();
			content = Files.readAllLines(filePath, StandardCharsets.UTF_8);
			assertEquals(1, content.size());
			assertEquals("x", content.get(0));
			// All right. Now get a CommitFileRevision and check its contents.
			String relativePath = getRevisionPath(filePath);
			CommitFileRevision fileRevision = new CommitFileRevision(repository,
					head, relativePath);
			ByteBuffer rawContent = null;
			try (InputStream blobStream = fileRevision
					.getStorage(new NullProgressMonitor()).getContents()) {
				rawContent = IO.readWholeStream(blobStream, 1);
			}
			assertNotNull(rawContent);
			String blobContent = new String(rawContent.array(), 0,
					rawContent.limit(), StandardCharsets.UTF_8);
			// Prize question: what do we expect here? We explicitly say we
			// want to look at an older version. (OK, in this test it happens
			// to be HEAD, but that's immaterial.) I believe the correct
			// behavior is to apply only the attributes as they existed in that
			// commit, i.e., to disregard the .gitattributes in the file system.
			// Note, however, that this means that we get a different behavior
			// than if we checked out the commit. Also note that global/info
			// attributes get applied in any case -- that behavior is
			// unavoidable and appears to be wanted by the .gitattributes spec.
			assertEquals("a", blobContent);
		} finally {
			FilterCommandRegistry.unregister("egit://builtin/test/smudge");
		}
	}

	/**
	 * @param filePath
	 * @return path with {@code /} as separator
	 */
	private String getRevisionPath(Path filePath) {
		return repository.getWorkTree().toPath().relativize(filePath).toString()
				.replace('\\', '/');
	}

	@Test
	public void testWithAttributesNotCheckedInButWithGlobalAttributes()
			throws Exception {
		java.nio.file.Path filePath = createFile(project.getProject(),
				"attr.txt", "a");
		try (Git git = new Git(repository)) {
			git.add().addFilepattern(".").call();
			RevCommit head = git.commit().setMessage("Initial commit").call();
			// Verify that we do have "a" in the repo: modify the file, do a
			// hard reset, verify the contents to be "a" again
			createFile(project.getProject(), "attr.txt", "aa");
			git.reset().setMode(ResetType.HARD).call();
			List<String> content = Files.readAllLines(filePath,
					StandardCharsets.UTF_8);
			assertEquals(1, content.size());
			assertEquals("a", content.get(0));
			// Now create a smudge filter that will replace all a's by x's.
			setupFilter(repository, project.getProject(), false);
			// Set up the global attributes. For simplicity, we create the file
			// inside the work tree.
			java.nio.file.Path globalAttributes = createFile(
					project.getProject(), "globalattrs", "*.txt filter=test2");
			FilterCommandRegistry.register("egit://builtin/test/smudge2",
					new TestCommandFactory('a', 'y'));
			StoredConfig config = repository.getConfig();
			config.setString("core", null, "attributesFile",
					globalAttributes.toString());
			config.setString("filter", "test2", "smudge",
					"egit://builtin/test/smudge2");
			config.save();
			// Modify the file again and do a hard reset. File contains 'x'.
			createFile(project.getProject(), "attr.txt", "aa");
			git.reset().setMode(ResetType.HARD).call();
			content = Files.readAllLines(filePath, StandardCharsets.UTF_8);
			assertEquals(1, content.size());
			assertEquals("x", content.get(0));
			// All right. Now get a CommitFileRevision and check its contents.
			String relativePath = getRevisionPath(filePath);
			CommitFileRevision fileRevision = new CommitFileRevision(repository,
					head, relativePath);
			ByteBuffer rawContent = null;
			try (InputStream blobStream = fileRevision
					.getStorage(new NullProgressMonitor()).getContents()) {
				rawContent = IO.readWholeStream(blobStream, 1);
			}
			assertNotNull(rawContent);
			String blobContent = new String(rawContent.array(), 0,
					rawContent.limit(), StandardCharsets.UTF_8);
			// This should have ignored the not-yet-committed .gitignore and
			// applied the global smudge2 command.
			assertEquals("y", blobContent);
		} finally {
			FilterCommandRegistry.unregister("egit://builtin/test/smudge");
			FilterCommandRegistry.unregister("egit://builtin/test/smudge2");
		}
	}

	@Test
	public void testWithAttributesCheckedInAndWithGlobalAttributes()
			throws Exception {
		java.nio.file.Path filePath = createFile(project.getProject(),
				"attr.txt", "a");
		try (Git git = new Git(repository)) {
			git.add().addFilepattern(".").call();
			git.commit().setMessage("Initial commit").call();
			// Verify that we do have "a" in the repo: modify the file, do a
			// hard reset, verify the contents to be "a" again
			createFile(project.getProject(), "attr.txt", "aa");
			git.reset().setMode(ResetType.HARD).call();
			List<String> content = Files.readAllLines(filePath,
					StandardCharsets.UTF_8);
			assertEquals(1, content.size());
			assertEquals("a", content.get(0));
			// Now create a smudge filter that will replace all a's by x's.
			RevCommit head = setupFilter(repository, project.getProject(),
					true);
			// Set up the global attributes. For simplicity, we create the file
			// inside the work tree.
			java.nio.file.Path globalAttributes = createFile(
					project.getProject(), "globalattrs", "*.txt filter=test2");
			FilterCommandRegistry.register("egit://builtin/test/smudge2",
					new TestCommandFactory('a', 'y'));
			StoredConfig config = repository.getConfig();
			config.setString("core", null, "attributesFile",
					globalAttributes.toString());
			config.setString("filter", "test2", "smudge",
					"egit://builtin/test/smudge2");
			config.save();
			// Modify the file again and do a hard reset. File contains 'x'.
			createFile(project.getProject(), "attr.txt", "aa");
			git.reset().setMode(ResetType.HARD).call();
			content = Files.readAllLines(filePath, StandardCharsets.UTF_8);
			assertEquals(1, content.size());
			assertEquals("x", content.get(0));
			// All right. Now get a CommitFileRevision and check its contents.
			String relativePath = getRevisionPath(filePath);
			CommitFileRevision fileRevision = new CommitFileRevision(repository,
					head, relativePath);
			ByteBuffer rawContent = null;
			try (InputStream blobStream = fileRevision
					.getStorage(new NullProgressMonitor()).getContents()) {
				rawContent = IO.readWholeStream(blobStream, 1);
			}
			assertNotNull(rawContent);
			String blobContent = new String(rawContent.array(), 0,
					rawContent.limit(), StandardCharsets.UTF_8);
			assertEquals("x", blobContent);
		} finally {
			FilterCommandRegistry.unregister("egit://builtin/test/smudge");
			FilterCommandRegistry.unregister("egit://builtin/test/smudge2");
		}
	}

	private static class TestCommandFactory implements FilterCommandFactory {
		private final int toReplace;

		private final int replacement;

		public TestCommandFactory(int toReplace, int replacement) {
			this.toReplace = toReplace;
			this.replacement = replacement;
		}

		@Override
		public FilterCommand create(Repository repo, InputStream in,
				final OutputStream out) {
			FilterCommand cmd = new FilterCommand(in, out) {

				@Override
				public int run() throws IOException {
					int b = in.read();
					if (b == -1) {
						return b;
					} else if (b == toReplace) {
						out.write(replacement);
					} else {
						out.write(b);
					}
					return 1;
				}
			};
			return cmd;
		}
	}

}

Back to the top