aboutsummaryrefslogtreecommitdiffstats
path: root/org.eclipse.jgit.test/tst/org/eclipse/jgit/attributes/merge/MergeGitAttributeTest.java
blob: 009ca8a15203b44ae54d995c0e5c6c5acadb200e (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
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
/*
 * Copyright (C) 2017, Obeo (mathieu.cartaud@obeo.fr) 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.attributes.merge;

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

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.util.function.Consumer;

import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.MergeResult;
import org.eclipse.jgit.api.MergeResult.MergeStatus;
import org.eclipse.jgit.api.errors.CheckoutConflictException;
import org.eclipse.jgit.api.errors.ConcurrentRefUpdateException;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.api.errors.InvalidMergeHeadsException;
import org.eclipse.jgit.api.errors.NoFilepatternException;
import org.eclipse.jgit.api.errors.NoHeadException;
import org.eclipse.jgit.api.errors.NoMessageException;
import org.eclipse.jgit.api.errors.WrongRepositoryStateException;
import org.eclipse.jgit.attributes.Attribute;
import org.eclipse.jgit.attributes.Attributes;
import org.eclipse.jgit.errors.NoWorkTreeException;
import org.eclipse.jgit.junit.RepositoryTestCase;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.treewalk.FileTreeIterator;
import org.eclipse.jgit.treewalk.TreeWalk;
import org.eclipse.jgit.treewalk.filter.PathFilter;
import org.junit.Test;

public class MergeGitAttributeTest extends RepositoryTestCase {

	private static final String REFS_HEADS_RIGHT = "refs/heads/right";

	private static final String REFS_HEADS_MASTER = "refs/heads/master";

	private static final String REFS_HEADS_LEFT = "refs/heads/left";

	private static final String DISABLE_CHECK_BRANCH = "refs/heads/disabled_checked";

	private static final String ENABLE_CHECKED_BRANCH = "refs/heads/enabled_checked";

	private static final String ENABLED_CHECKED_GIF = "enabled_checked.gif";

	public Git createRepositoryBinaryConflict(Consumer<Git> initialCommit,
			Consumer<Git> leftCommit, Consumer<Git> rightCommit)
			throws NoFilepatternException, GitAPIException, NoWorkTreeException,
			IOException {
		// Set up a git whith conflict commits on images
		Git git = new Git(db);

		// First commit
		initialCommit.accept(git);
		git.add().addFilepattern(".").call();
		RevCommit firstCommit = git.commit().setAll(true)
				.setMessage("initial commit adding git attribute file").call();

		// Create branch and add an icon Checked_Boxe (enabled_checked)
		createBranch(firstCommit, REFS_HEADS_LEFT);
		checkoutBranch(REFS_HEADS_LEFT);
		leftCommit.accept(git);
		git.add().addFilepattern(".").call();
		git.commit().setMessage("Left").call();

		// Create a second branch from master Unchecked_Boxe
		checkoutBranch(REFS_HEADS_MASTER);
		createBranch(firstCommit, REFS_HEADS_RIGHT);
		checkoutBranch(REFS_HEADS_RIGHT);
		rightCommit.accept(git);
		git.add().addFilepattern(".").call();
		git.commit().setMessage("Right").call();

		checkoutBranch(REFS_HEADS_LEFT);
		return git;

	}

	@Test
	public void mergeTextualFile_NoAttr() throws NoWorkTreeException,
			NoFilepatternException, GitAPIException, IOException {
		try (Git git = createRepositoryBinaryConflict(g -> {
			try {
				writeTrashFile("main.cat", "A\n" + "B\n" + "C\n" + "D\n");
			} catch (IOException e) {
				throw new UncheckedIOException(e);
			}
		}, g -> {
			try {
				writeTrashFile("main.cat", "A\n" + "B\n" + "C\n" + "F\n");
			} catch (IOException e) {
				throw new UncheckedIOException(e);
			}
		}, g -> {
			try {
				writeTrashFile("main.cat", "A\n" + "E\n" + "C\n" + "D\n");
			} catch (IOException e) {
				throw new UncheckedIOException(e);
			}
		})) {
			checkoutBranch(REFS_HEADS_LEFT);
			// Merge refs/heads/enabled_checked -> refs/heads/disabled_checked

			MergeResult mergeResult = git.merge()
					.include(git.getRepository().resolve(REFS_HEADS_RIGHT))
					.call();
			assertEquals(MergeStatus.MERGED, mergeResult.getMergeStatus());

			assertNull(mergeResult.getConflicts());

			// Check that the image was not modified (not conflict marker added)
			String result = read(
					writeTrashFile("res.cat", "A\n" + "E\n" + "C\n" + "F\n"));
			assertEquals(result, read(git.getRepository().getWorkTree().toPath()
					.resolve("main.cat").toFile()));
		}
	}

	@Test
	public void mergeTextualFile_UnsetMerge_Conflict()
			throws NoWorkTreeException, NoFilepatternException, GitAPIException,
			IOException {
		try (Git git = createRepositoryBinaryConflict(g -> {
			try {
				writeTrashFile(".gitattributes", "*.cat -merge");
				writeTrashFile("main.cat", "A\n" + "B\n" + "C\n" + "D\n");
			} catch (IOException e) {
				throw new UncheckedIOException(e);
			}
		}, g -> {
			try {
				writeTrashFile("main.cat", "A\n" + "B\n" + "C\n" + "F\n");
			} catch (IOException e) {
				throw new UncheckedIOException(e);
			}
		}, g -> {
			try {
				writeTrashFile("main.cat", "A\n" + "E\n" + "C\n" + "D\n");
			} catch (IOException e) {
				throw new UncheckedIOException(e);
			}
		})) {
			// Check that the merge attribute is unset
			assertAddMergeAttributeUnset(REFS_HEADS_LEFT, "main.cat");
			assertAddMergeAttributeUnset(REFS_HEADS_RIGHT, "main.cat");

			checkoutBranch(REFS_HEADS_LEFT);
			// Merge refs/heads/enabled_checked -> refs/heads/disabled_checked

			String catContent = read(git.getRepository().getWorkTree().toPath()
					.resolve("main.cat").toFile());

			MergeResult mergeResult = git.merge()
					.include(git.getRepository().resolve(REFS_HEADS_RIGHT))
					.call();
			assertEquals(MergeStatus.CONFLICTING, mergeResult.getMergeStatus());

			// Check that the image was not modified (not conflict marker added)
			assertEquals(catContent, read(git.getRepository().getWorkTree()
					.toPath().resolve("main.cat").toFile()));
		}
	}

	@Test
	public void mergeTextualFile_UnsetMerge_NoConflict()
			throws NoWorkTreeException, NoFilepatternException, GitAPIException,
			IOException {
		try (Git git = createRepositoryBinaryConflict(g -> {
			try {
				writeTrashFile(".gitattributes", "*.txt -merge");
				writeTrashFile("main.cat", "A\n" + "B\n" + "C\n" + "D\n");
			} catch (IOException e) {
				throw new UncheckedIOException(e);
			}
		}, g -> {
			try {
				writeTrashFile("main.cat", "A\n" + "B\n" + "C\n" + "F\n");
			} catch (IOException e) {
				throw new UncheckedIOException(e);
			}
		}, g -> {
			try {
				writeTrashFile("main.cat", "A\n" + "E\n" + "C\n" + "D\n");
			} catch (IOException e) {
				throw new UncheckedIOException(e);
			}
		})) {
			// Check that the merge attribute is unset
			assertAddMergeAttributeUndefined(REFS_HEADS_LEFT, "main.cat");
			assertAddMergeAttributeUndefined(REFS_HEADS_RIGHT, "main.cat");

			checkoutBranch(REFS_HEADS_LEFT);
			// Merge refs/heads/enabled_checked -> refs/heads/disabled_checked

			MergeResult mergeResult = git.merge()
					.include(git.getRepository().resolve(REFS_HEADS_RIGHT))
					.call();
			assertEquals(MergeStatus.MERGED, mergeResult.getMergeStatus());

			// Check that the image was not modified (not conflict marker added)
			String result = read(
					writeTrashFile("res.cat", "A\n" + "E\n" + "C\n" + "F\n"));
			assertEquals(result, read(git.getRepository().getWorkTree()
					.toPath().resolve("main.cat").toFile()));
		}
	}

	@Test
	public void mergeTextualFile_SetBinaryMerge_Conflict()
			throws NoWorkTreeException, NoFilepatternException, GitAPIException,
			IOException {
		try (Git git = createRepositoryBinaryConflict(g -> {
			try {
				writeTrashFile(".gitattributes", "*.cat merge=binary");
				writeTrashFile("main.cat", "A\n" + "B\n" + "C\n" + "D\n");
			} catch (IOException e) {
				throw new UncheckedIOException(e);
			}
		}, g -> {
			try {
				writeTrashFile("main.cat", "A\n" + "B\n" + "C\n" + "F\n");
			} catch (IOException e) {
				throw new UncheckedIOException(e);
			}
		}, g -> {
			try {
				writeTrashFile("main.cat", "A\n" + "E\n" + "C\n" + "D\n");
			} catch (IOException e) {
				throw new UncheckedIOException(e);
			}
		})) {
			// Check that the merge attribute is set to binary
			assertAddMergeAttributeCustom(REFS_HEADS_LEFT, "main.cat",
					"binary");
			assertAddMergeAttributeCustom(REFS_HEADS_RIGHT, "main.cat",
					"binary");

			checkoutBranch(REFS_HEADS_LEFT);
			// Merge refs/heads/enabled_checked -> refs/heads/disabled_checked

			String catContent = read(git.getRepository().getWorkTree().toPath()
					.resolve("main.cat").toFile());

			MergeResult mergeResult = git.merge()
					.include(git.getRepository().resolve(REFS_HEADS_RIGHT))
					.call();
			assertEquals(MergeStatus.CONFLICTING, mergeResult.getMergeStatus());

			// Check that the image was not modified (not conflict marker added)
			assertEquals(catContent, read(git.getRepository().getWorkTree()
					.toPath().resolve("main.cat").toFile()));
		}
	}

	@Test
	public void mergeBinaryFile_NoAttr_Conflict() throws IllegalStateException,
			IOException, NoHeadException, ConcurrentRefUpdateException,
			CheckoutConflictException, InvalidMergeHeadsException,
			WrongRepositoryStateException, NoMessageException, GitAPIException {

		RevCommit disableCheckedCommit;
		// Set up a git with conflict commits on images
		try (Git git = new Git(db)) {
			// First commit
			write(new File(db.getWorkTree(), ".gitattributes"), "");
			git.add().addFilepattern(".gitattributes").call();
			RevCommit firstCommit = git.commit()
					.setMessage("initial commit adding git attribute file")
					.call();

			// Create branch and add an icon Checked_Boxe (enabled_checked)
			createBranch(firstCommit, ENABLE_CHECKED_BRANCH);
			checkoutBranch(ENABLE_CHECKED_BRANCH);
			copy(ENABLED_CHECKED_GIF, ENABLED_CHECKED_GIF, "");
			git.add().addFilepattern(ENABLED_CHECKED_GIF).call();
			git.commit().setMessage("enabled_checked commit").call();

			// Create a second branch from master Unchecked_Boxe
			checkoutBranch(REFS_HEADS_MASTER);
			createBranch(firstCommit, DISABLE_CHECK_BRANCH);
			checkoutBranch(DISABLE_CHECK_BRANCH);
			copy("disabled_checked.gif", ENABLED_CHECKED_GIF, "");
			git.add().addFilepattern(ENABLED_CHECKED_GIF).call();
			disableCheckedCommit = git.commit()
					.setMessage("disabled_checked commit").call();

			// Check that the merge attribute is unset
			assertAddMergeAttributeUndefined(ENABLE_CHECKED_BRANCH,
					ENABLED_CHECKED_GIF);
			assertAddMergeAttributeUndefined(DISABLE_CHECK_BRANCH,
					ENABLED_CHECKED_GIF);

			checkoutBranch(ENABLE_CHECKED_BRANCH);
			// Merge refs/heads/enabled_checked -> refs/heads/disabled_checked
			MergeResult mergeResult = git.merge().include(disableCheckedCommit)
					.call();
			assertEquals(MergeStatus.CONFLICTING, mergeResult.getMergeStatus());

			// Check that the image was not modified (no conflict marker added)
			try (FileInputStream mergeResultFile = new FileInputStream(
					db.getWorkTree().toPath().resolve(ENABLED_CHECKED_GIF)
							.toFile())) {
				assertTrue(contentEquals(
						getClass().getResourceAsStream(ENABLED_CHECKED_GIF),
						mergeResultFile));
			}
		}
	}

	@Test
	public void mergeBinaryFile_UnsetMerge_Conflict()
			throws IllegalStateException,
			IOException, NoHeadException, ConcurrentRefUpdateException,
			CheckoutConflictException, InvalidMergeHeadsException,
			WrongRepositoryStateException, NoMessageException, GitAPIException {

		RevCommit disableCheckedCommit;
		// Set up a git whith conflict commits on images
		try (Git git = new Git(db)) {
			// First commit
			write(new File(db.getWorkTree(), ".gitattributes"), "*.gif -merge");
			git.add().addFilepattern(".gitattributes").call();
			RevCommit firstCommit = git.commit()
					.setMessage("initial commit adding git attribute file")
					.call();

			// Create branch and add an icon Checked_Boxe (enabled_checked)
			createBranch(firstCommit, ENABLE_CHECKED_BRANCH);
			checkoutBranch(ENABLE_CHECKED_BRANCH);
			copy(ENABLED_CHECKED_GIF, ENABLED_CHECKED_GIF, "");
			git.add().addFilepattern(ENABLED_CHECKED_GIF).call();
			git.commit().setMessage("enabled_checked commit").call();

			// Create a second branch from master Unchecked_Boxe
			checkoutBranch(REFS_HEADS_MASTER);
			createBranch(firstCommit, DISABLE_CHECK_BRANCH);
			checkoutBranch(DISABLE_CHECK_BRANCH);
			copy("disabled_checked.gif", ENABLED_CHECKED_GIF, "");
			git.add().addFilepattern(ENABLED_CHECKED_GIF).call();
			disableCheckedCommit = git.commit()
					.setMessage("disabled_checked commit").call();

			// Check that the merge attribute is unset
			assertAddMergeAttributeUnset(ENABLE_CHECKED_BRANCH,
					ENABLED_CHECKED_GIF);
			assertAddMergeAttributeUnset(DISABLE_CHECK_BRANCH,
					ENABLED_CHECKED_GIF);

			checkoutBranch(ENABLE_CHECKED_BRANCH);
			// Merge refs/heads/enabled_checked -> refs/heads/disabled_checked
			MergeResult mergeResult = git.merge().include(disableCheckedCommit)
					.call();
			assertEquals(MergeStatus.CONFLICTING, mergeResult.getMergeStatus());

			// Check that the image was not modified (not conflict marker added)
			try (FileInputStream mergeResultFile = new FileInputStream(
					db.getWorkTree().toPath().resolve(ENABLED_CHECKED_GIF)
							.toFile())) {
				assertTrue(contentEquals(
						getClass().getResourceAsStream(ENABLED_CHECKED_GIF),
						mergeResultFile));
			}
		}
	}

	@Test
	public void mergeBinaryFile_SetMerge_Conflict()
			throws IllegalStateException, IOException, NoHeadException,
			ConcurrentRefUpdateException, CheckoutConflictException,
			InvalidMergeHeadsException, WrongRepositoryStateException,
			NoMessageException, GitAPIException {

		RevCommit disableCheckedCommit;
		// Set up a git whith conflict commits on images
		try (Git git = new Git(db)) {
			// First commit
			write(new File(db.getWorkTree(), ".gitattributes"), "*.gif merge");
			git.add().addFilepattern(".gitattributes").call();
			RevCommit firstCommit = git.commit()
					.setMessage("initial commit adding git attribute file")
					.call();

			// Create branch and add an icon Checked_Boxe (enabled_checked)
			createBranch(firstCommit, ENABLE_CHECKED_BRANCH);
			checkoutBranch(ENABLE_CHECKED_BRANCH);
			copy(ENABLED_CHECKED_GIF, ENABLED_CHECKED_GIF, "");
			git.add().addFilepattern(ENABLED_CHECKED_GIF).call();
			git.commit().setMessage("enabled_checked commit").call();

			// Create a second branch from master Unchecked_Boxe
			checkoutBranch(REFS_HEADS_MASTER);
			createBranch(firstCommit, DISABLE_CHECK_BRANCH);
			checkoutBranch(DISABLE_CHECK_BRANCH);
			copy("disabled_checked.gif", ENABLED_CHECKED_GIF, "");
			git.add().addFilepattern(ENABLED_CHECKED_GIF).call();
			disableCheckedCommit = git.commit()
					.setMessage("disabled_checked commit").call();

			// Check that the merge attribute is set
			assertAddMergeAttributeSet(ENABLE_CHECKED_BRANCH,
					ENABLED_CHECKED_GIF);
			assertAddMergeAttributeSet(DISABLE_CHECK_BRANCH,
					ENABLED_CHECKED_GIF);

			checkoutBranch(ENABLE_CHECKED_BRANCH);
			// Merge refs/heads/enabled_checked -> refs/heads/disabled_checked
			MergeResult mergeResult = git.merge().include(disableCheckedCommit)
					.call();
			assertEquals(MergeStatus.CONFLICTING, mergeResult.getMergeStatus());

			// Check that the image was not modified (not conflict marker added)
			try (FileInputStream mergeResultFile = new FileInputStream(
					db.getWorkTree().toPath().resolve(ENABLED_CHECKED_GIF)
							.toFile())) {
				assertTrue(contentEquals(
						getClass().getResourceAsStream(ENABLED_CHECKED_GIF),
						mergeResultFile));
			}
		}
	}

	/*
	 * Copied from org.apache.commons.io.IOUtils
	 */
	private boolean contentEquals(InputStream input1, InputStream input2)
			throws IOException {
		if (input1 == input2) {
			return true;
		}
		if (!(input1 instanceof BufferedInputStream)) {
			input1 = new BufferedInputStream(input1);
		}
		if (!(input2 instanceof BufferedInputStream)) {
			input2 = new BufferedInputStream(input2);
		}

		int ch = input1.read();
		while (-1 != ch) {
			final int ch2 = input2.read();
			if (ch != ch2) {
				return false;
			}
			ch = input1.read();
		}

		final int ch2 = input2.read();
		return ch2 == -1;
	}

	private void assertAddMergeAttributeUnset(String branch, String fileName)
			throws IllegalStateException, IOException {
		checkoutBranch(branch);

		try (TreeWalk treeWaklEnableChecked = new TreeWalk(db)) {
			treeWaklEnableChecked.addTree(new FileTreeIterator(db));
			treeWaklEnableChecked.setFilter(PathFilter.create(fileName));

			assertTrue(treeWaklEnableChecked.next());
			Attributes attributes = treeWaklEnableChecked.getAttributes();
			Attribute mergeAttribute = attributes.get("merge");
			assertNotNull(mergeAttribute);
			assertEquals(Attribute.State.UNSET, mergeAttribute.getState());
		}
	}

	private void assertAddMergeAttributeSet(String branch, String fileName)
			throws IllegalStateException, IOException {
		checkoutBranch(branch);

		try (TreeWalk treeWaklEnableChecked = new TreeWalk(db)) {
			treeWaklEnableChecked.addTree(new FileTreeIterator(db));
			treeWaklEnableChecked.setFilter(PathFilter.create(fileName));

			assertTrue(treeWaklEnableChecked.next());
			Attributes attributes = treeWaklEnableChecked.getAttributes();
			Attribute mergeAttribute = attributes.get("merge");
			assertNotNull(mergeAttribute);
			assertEquals(Attribute.State.SET, mergeAttribute.getState());
		}
	}

	private void assertAddMergeAttributeUndefined(String branch,
			String fileName) throws IllegalStateException, IOException {
		checkoutBranch(branch);

		try (TreeWalk treeWaklEnableChecked = new TreeWalk(db)) {
			treeWaklEnableChecked.addTree(new FileTreeIterator(db));
			treeWaklEnableChecked.setFilter(PathFilter.create(fileName));

			assertTrue(treeWaklEnableChecked.next());
			Attributes attributes = treeWaklEnableChecked.getAttributes();
			Attribute mergeAttribute = attributes.get("merge");
			assertNull(mergeAttribute);
		}
	}

	private void assertAddMergeAttributeCustom(String branch, String fileName,
			String value) throws IllegalStateException, IOException {
		checkoutBranch(branch);

		try (TreeWalk treeWaklEnableChecked = new TreeWalk(db)) {
			treeWaklEnableChecked.addTree(new FileTreeIterator(db));
			treeWaklEnableChecked.setFilter(PathFilter.create(fileName));

			assertTrue(treeWaklEnableChecked.next());
			Attributes attributes = treeWaklEnableChecked.getAttributes();
			Attribute mergeAttribute = attributes.get("merge");
			assertNotNull(mergeAttribute);
			assertEquals(Attribute.State.CUSTOM, mergeAttribute.getState());
			assertEquals(value, mergeAttribute.getValue());
		}
	}

	private void copy(String resourcePath, String resourceNewName,
			String pathInRepo) throws IOException {
		InputStream input = getClass().getResourceAsStream(resourcePath);
		Files.copy(input, db.getWorkTree().toPath().resolve(pathInRepo)
				.resolve(resourceNewName));
	}

}