Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 83308efcd2be1d615e422f7b9bca09fee69a1d09 (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
/*******************************************************************************
 * 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.ui.internal;

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

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;

import org.eclipse.compare.ITypedElement;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.egit.core.Activator;
import org.eclipse.egit.core.op.CommitOperation;
import org.eclipse.egit.core.op.ResetOperation;
import org.eclipse.egit.ui.common.LocalRepositoryTestCase;
import org.eclipse.egit.ui.internal.revision.EditableRevision;
import org.eclipse.egit.ui.test.TestUtil;
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.Constants;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.lib.StoredConfig;
import org.eclipse.jgit.util.IO;
import org.junit.Before;
import org.junit.Test;

/**
 * Tests for CompareUtils; in particular editing index content as it can be done
 * in the compare editor.
 */
public class CompareUtilsTest extends LocalRepositoryTestCase {

	private Repository repository;

	@Before
	public void setup() throws Exception {
		File repoFile = createProjectAndCommitToRepository();
		assertNotNull(repoFile);
		repository = Activator.getDefault().getRepositoryCache()
				.lookupRepository(repoFile);
		assertNotNull(repository);
	}

	private String get(InputStream in) throws IOException {
		ByteBuffer buffer = IO.readWholeStream(in, 1);
		return new String(buffer.array(), 0, buffer.limit(),
				StandardCharsets.UTF_8);
	}

	@Test
	public void testIndexEdit() throws Exception {
		IFile testFile = touch("a");
		stage(testFile);
		// Get the index file revision.
		ITypedElement element = CompareUtils.getIndexTypedElement(testFile);
		assert (element instanceof EditableRevision);
		EditableRevision revision = (EditableRevision) element;
		// Check that its contents are 'a'.
		try (InputStream in = revision.getContents()) {
			assertEquals("a", get(in));
		}
		// Change the contents to 'xx'
		revision.setContent("xx".getBytes(StandardCharsets.UTF_8));
		// Commit the index
		CommitOperation op = new CommitOperation(repository,
				TestUtil.TESTAUTHOR, TestUtil.TESTCOMMITTER,
				"Commit modified index");
		op.execute(null);
		TestUtil.waitForJobs(50, 5000);
		// Do a reset --hard
		ResetOperation reset = new ResetOperation(repository, Constants.HEAD,
				ResetType.HARD);
		reset.execute(null);
		TestUtil.waitForJobs(50, 5000);
		// Should have 'xx' now
		try (InputStream in = testFile.getContents()) {
			assertEquals("xx", get(in));
		}
	}

	@Test
	public void testIndexEditWithAttributes() throws Exception {
		IFile testFile = touch("a");
		stage(testFile);
		// Set up .gitattributes such that 'a's are changed to 'x' on smudge
		IFile gitAttributes = touch(PROJ1, FOLDER + "/.gitattributes",
				FILE1 + " filter=test");
		try {
			FilterCommandRegistry.register("egitui://builtin/test/smudge",
					new TestCommandFactory('a', 'x'));
			StoredConfig config = repository.getConfig();
			config.setString("filter", "test", "smudge",
					"egitui://builtin/test/smudge");
			config.save();
			// Get the index file revision.
			ITypedElement element = CompareUtils.getIndexTypedElement(testFile);
			assert (element instanceof EditableRevision);
			EditableRevision revision = (EditableRevision) element;
			// Check that its contents are 'x'.
			try (InputStream in = revision.getContents()) {
				assertEquals("x", get(in));
			}
			// Modify the filter to transform 'x' to 'a' on clean
			FilterCommandRegistry.register("egitui://builtin/test/clean",
					new TestCommandFactory('x', 'a'));
			config.setString("filter", "test", "clean",
					"egitui://builtin/test/clean");
			config.save();
			// Change the contents to 'xx'. This should apply the above clean
			// filter.
			revision.setContent("xx".getBytes(StandardCharsets.UTF_8));
			// Commit the index
			CommitOperation op = new CommitOperation(repository,
					TestUtil.TESTAUTHOR, TestUtil.TESTCOMMITTER,
					"Commit modified index");
			op.execute(null);
			TestUtil.waitForJobs(50, 5000);
			// Remove filters
			gitAttributes.delete(true, true, new NullProgressMonitor());
			config.unsetSection("filter", "test");
			config.save();
			// Do a reset --hard
			ResetOperation reset = new ResetOperation(repository,
					Constants.HEAD, ResetType.HARD);
			reset.execute(null);
			TestUtil.waitForJobs(50, 5000);
			// Should have 'aa' now since we never committed the .gitattributes
			try (InputStream in = testFile.getContents()) {
				assertEquals("aa", get(in));
			}
		} finally {
			FilterCommandRegistry.unregister("egitui://builtin/test/smudge");
			FilterCommandRegistry.unregister("egitui://builtin/test/clean");
		}
	}

	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