aboutsummaryrefslogtreecommitdiffstats
path: root/org.eclipse.jgit.test/tst/org/eclipse/jgit/merge/SymlinkMergeTest.java
blob: 3cdc8da34ef125ebbe145461e80f040d03876f1d (plain)
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
/*
 * Copyright (C) 2022 Thomas Wolf <twolf@apache.org> and others
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Distribution License v. 1.0 which is available at
 * https://www.eclipse.org/org/documents/edl-v10.php.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */
package org.eclipse.jgit.merge;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assume.assumeTrue;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.LinkOption;

import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.MergeResult;
import org.eclipse.jgit.api.MergeResult.MergeStatus;
import org.eclipse.jgit.api.ResetCommand.ResetType;
import org.eclipse.jgit.junit.RepositoryTestCase;
import org.eclipse.jgit.junit.TestRepository;
import org.eclipse.jgit.lib.ConfigConstants;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.lib.StoredConfig;
import org.eclipse.jgit.revwalk.RevCommit;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameter;
import org.junit.runners.Parameterized.Parameters;

/**
 * Tests for merges involving symlinks.
 */
@RunWith(Parameterized.class)
public class SymlinkMergeTest extends RepositoryTestCase {

	@Parameters(name = "target={0}, core.symlinks={1}")
	public static Object[][] parameters() {
		return new Object[][] {
			{ Target.NONE, Boolean.TRUE },
			{ Target.FILE, Boolean.TRUE },
			{ Target.DIRECTORY, Boolean.TRUE },
			{ Target.NONE, Boolean.FALSE },
			{ Target.FILE, Boolean.FALSE },
			{ Target.DIRECTORY, Boolean.FALSE },
		};
	}

	public enum Target {
		NONE, FILE, DIRECTORY
	}

	@Parameter(0)
	public Target target;

	@Parameter(1)
	public boolean useSymLinks;

	private void setTargets() throws IOException {
		switch (target) {
		case DIRECTORY:
			assertTrue(new File(trash, "target").mkdir());
			assertTrue(new File(trash, "target1").mkdir());
			assertTrue(new File(trash, "target2").mkdir());
			break;
		case FILE:
			writeTrashFile("target", "t");
			writeTrashFile("target1", "t1");
			writeTrashFile("target2", "t2");
			break;
		default:
			break;
		}
	}

	private void checkTargets() throws IOException {
		File t = new File(trash, "target");
		File t1 = new File(trash, "target1");
		File t2 = new File(trash, "target2");
		switch (target) {
		case DIRECTORY:
			assertTrue(t.isDirectory());
			assertTrue(t1.isDirectory());
			assertTrue(t2.isDirectory());
			break;
		case FILE:
			checkFile(t, "t");
			checkFile(t1, "t1");
			checkFile(t2, "t2");
			break;
		default:
			assertFalse(Files.exists(t.toPath(), LinkOption.NOFOLLOW_LINKS));
			assertFalse(Files.exists(t1.toPath(), LinkOption.NOFOLLOW_LINKS));
			assertFalse(Files.exists(t2.toPath(), LinkOption.NOFOLLOW_LINKS));
			break;
		}
	}

	private void assertSymLink(File link, String content) throws Exception {
		if (useSymLinks) {
			assertTrue(Files.isSymbolicLink(link.toPath()));
			assertEquals(content, db.getFS().readSymLink(link));
		} else {
			assertFalse(Files.isSymbolicLink(link.toPath()));
			assertTrue(link.isFile());
			checkFile(link, content);
		}
	}

	// Link/link conflict: C git records the conflict but leaves the link in the
	// working tree unchanged.

	@Test
	public void mergeWithSymlinkConflict() throws Exception {
		assumeTrue(db.getFS().supportsSymlinks() || !useSymLinks);
		StoredConfig config = db.getConfig();
		config.setBoolean(ConfigConstants.CONFIG_CORE_SECTION, null,
				ConfigConstants.CONFIG_KEY_SYMLINKS, useSymLinks);
		config.save();
		try (TestRepository<Repository> repo = new TestRepository<>(db)) {
			db.incrementOpen();
			// Create the links directly in the git repo, then use a hard reset
			// to get them into the workspace. This enables us to run these
			// tests also with core.symLinks = false.
			RevCommit base = repo
					.commit(repo.tree(repo.link("link", repo.blob("target"))));
			RevCommit side = repo.commit(
					repo.tree(repo.link("link", repo.blob("target1"))), base);
			RevCommit head = repo.commit(
					repo.tree(repo.link("link", repo.blob("target2"))), base);
			try (Git git = new Git(db)) {
				setTargets();
				git.reset().setMode(ResetType.HARD).setRef(head.name()).call();
				File link = new File(trash, "link");
				assertSymLink(link, "target2");
				MergeResult result = git.merge().include(side)
						.setMessage("merged").call();
				assertEquals(MergeStatus.CONFLICTING, result.getMergeStatus());
				// Link should be unmodified
				assertSymLink(link, "target2");
				checkTargets();
				assertEquals("[link, mode:120000, stage:1, content:target]"
						+ "[link, mode:120000, stage:2, content:target2]"
						+ "[link, mode:120000, stage:3, content:target1]",
						indexState(CONTENT));
			}
		}
	}

	// In file/link conflicts, C git never does a content merge. It records the
	// stages in the index, and always puts the file into the workspace.

	@Test
	public void mergeWithFileSymlinkConflict() throws Exception {
		assumeTrue(db.getFS().supportsSymlinks() || !useSymLinks);
		StoredConfig config = db.getConfig();
		config.setBoolean(ConfigConstants.CONFIG_CORE_SECTION, null,
				ConfigConstants.CONFIG_KEY_SYMLINKS, useSymLinks);
		config.save();
		try (TestRepository<Repository> repo = new TestRepository<>(db)) {
			db.incrementOpen();
			RevCommit base = repo.commit(repo.tree());
			RevCommit side = repo.commit(
					repo.tree(repo.link("link", repo.blob("target1"))), base);
			RevCommit head = repo.commit(
					repo.tree(repo.file("link", repo.blob("not a link"))),
					base);
			try (Git git = new Git(db)) {
				setTargets();
				git.reset().setMode(ResetType.HARD).setRef(head.name()).call();
				File link = new File(trash, "link");
				assertFalse(Files.isSymbolicLink(link.toPath()));
				checkFile(link, "not a link");
				MergeResult result = git.merge().include(side)
						.setMessage("merged").call();
				assertEquals(MergeStatus.CONFLICTING, result.getMergeStatus());
				// File should be unmodified
				assertFalse(Files.isSymbolicLink(link.toPath()));
				checkFile(link, "not a link");
				checkTargets();
				assertEquals("[link, mode:100644, stage:2, content:not a link]"
						+ "[link, mode:120000, stage:3, content:target1]",
						indexState(CONTENT));
			}
		}
	}

	@Test
	public void mergeWithSymlinkFileConflict() throws Exception {
		assumeTrue(db.getFS().supportsSymlinks() || !useSymLinks);
		StoredConfig config = db.getConfig();
		config.setBoolean(ConfigConstants.CONFIG_CORE_SECTION, null,
				ConfigConstants.CONFIG_KEY_SYMLINKS, useSymLinks);
		config.save();
		try (TestRepository<Repository> repo = new TestRepository<>(db)) {
			db.incrementOpen();
			RevCommit base = repo.commit(repo.tree());
			RevCommit side = repo.commit(
					repo.tree(repo.file("link", repo.blob("not a link"))),
					base);
			RevCommit head = repo.commit(
					repo.tree(repo.link("link", repo.blob("target2"))), base);
			try (Git git = new Git(db)) {
				setTargets();
				git.reset().setMode(ResetType.HARD).setRef(head.name()).call();
				File link = new File(trash, "link");
				assertSymLink(link, "target2");
				MergeResult result = git.merge().include(side)
						.setMessage("merged").call();
				assertEquals(MergeStatus.CONFLICTING, result.getMergeStatus());
				// Should now be a file!
				assertFalse(Files.isSymbolicLink(link.toPath()));
				checkFile(link, "not a link");
				checkTargets();
				assertEquals("[link, mode:120000, stage:2, content:target2]"
						+ "[link, mode:100644, stage:3, content:not a link]",
						indexState(CONTENT));
			}
		}
	}

	// In Delete/modify conflicts with the non-deleted side a link, C git puts
	// the link into the working tree.

	@Test
	public void mergeWithSymlinkDeleteModify() throws Exception {
		assumeTrue(db.getFS().supportsSymlinks() || !useSymLinks);
		StoredConfig config = db.getConfig();
		config.setBoolean(ConfigConstants.CONFIG_CORE_SECTION, null,
				ConfigConstants.CONFIG_KEY_SYMLINKS, useSymLinks);
		config.save();
		try (TestRepository<Repository> repo = new TestRepository<>(db)) {
			db.incrementOpen();
			RevCommit base = repo
					.commit(repo.tree(repo.link("link", repo.blob("target"))));
			RevCommit side = repo.commit(
					repo.tree(repo.link("link", repo.blob("target1"))), base);
			RevCommit head = repo.commit(repo.tree(), base);
			try (Git git = new Git(db)) {
				setTargets();
				git.reset().setMode(ResetType.HARD).setRef(head.name()).call();
				File link = new File(trash, "link");
				assertFalse(
						Files.exists(link.toPath(), LinkOption.NOFOLLOW_LINKS));
				MergeResult result = git.merge().include(side)
						.setMessage("merged").call();
				assertEquals(MergeStatus.CONFLICTING, result.getMergeStatus());
				// Link should have the content from side
				assertSymLink(link, "target1");
				checkTargets();
				assertEquals("[link, mode:120000, stage:1, content:target]"
						+ "[link, mode:120000, stage:3, content:target1]",
						indexState(CONTENT));
			}
		}
	}

	@Test
	public void mergeWithSymlinkModifyDelete() throws Exception {
		assumeTrue(db.getFS().supportsSymlinks() || !useSymLinks);
		StoredConfig config = db.getConfig();
		config.setBoolean(ConfigConstants.CONFIG_CORE_SECTION, null,
				ConfigConstants.CONFIG_KEY_SYMLINKS, useSymLinks);
		config.save();
		try (TestRepository<Repository> repo = new TestRepository<>(db)) {
			db.incrementOpen();
			RevCommit base = repo
					.commit(repo.tree(repo.link("link", repo.blob("target"))));
			RevCommit side = repo.commit(repo.tree(), base);
			RevCommit head = repo.commit(
					repo.tree(repo.link("link", repo.blob("target2"))), base);
			try (Git git = new Git(db)) {
				setTargets();
				git.reset().setMode(ResetType.HARD).setRef(head.name()).call();
				File link = new File(trash, "link");
				assertSymLink(link, "target2");
				MergeResult result = git.merge().include(side)
						.setMessage("merged").call();
				assertEquals(MergeStatus.CONFLICTING, result.getMergeStatus());
				// Link should be unmodified
				assertSymLink(link, "target2");
				checkTargets();
				assertEquals("[link, mode:120000, stage:1, content:target]"
						+ "[link, mode:120000, stage:2, content:target2]",
						indexState(CONTENT));
			}
		}
	}
}