aboutsummaryrefslogtreecommitdiffstats
path: root/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/file/AlternatesTest.java
blob: f3a367c148549b42f75b305be073376a186c248d (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
/*
 * Copyright (C) 2017, Matthias Sohn <matthias.sohn@sap.com> 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.internal.storage.file;

import static org.eclipse.jgit.lib.Constants.INFO_ALTERNATES;
import static org.junit.Assert.assertTrue;

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;

import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.AbortedByHookException;
import org.eclipse.jgit.api.errors.ConcurrentRefUpdateException;
import org.eclipse.jgit.api.errors.GitAPIException;
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.UnmergedPathsException;
import org.eclipse.jgit.api.errors.WrongRepositoryStateException;
import org.eclipse.jgit.junit.JGitTestUtil;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.test.resources.SampleDataRepositoryTestCase;
import org.junit.Test;

public class AlternatesTest extends SampleDataRepositoryTestCase {

	private FileRepository db2;

	@Override
	public void setUp() throws Exception {
		super.setUp();
		db2 = createWorkRepository();
	}

	private void setAlternate(FileRepository from, FileRepository to)
			throws IOException {
		File alt = new File(from.getObjectDatabase().getDirectory(),
				INFO_ALTERNATES);
		alt.getParentFile().mkdirs();
		File fromDir = from.getObjectDatabase().getDirectory();
		File toDir = to.getObjectDatabase().getDirectory();
		Path relative = fromDir.toPath().relativize(toDir.toPath());
		write(alt, relative.toString() + "\n");
	}

	@Test
	public void testAlternate() throws Exception {
		setAlternate(db2, db);
		RevCommit c = createCommit();
		assertCommit(c);
		assertAlternateObjects(db2);
	}

	@Test
	public void testAlternateCyclic2() throws Exception {
		setAlternate(db2, db);
		setAlternate(db, db2);
		RevCommit c = createCommit();
		assertCommit(c);
		assertAlternateObjects(db2);
	}

	@Test
	public void testAlternateCyclic3() throws Exception {
		FileRepository db3 = createBareRepository();
		setAlternate(db2, db3);
		setAlternate(db3, db);
		setAlternate(db, db2);
		RevCommit c = createCommit();
		assertCommit(c);
		assertAlternateObjects(db2);
	}

	private RevCommit createCommit() throws IOException, GitAPIException,
			NoFilepatternException, NoHeadException, NoMessageException,
			UnmergedPathsException, ConcurrentRefUpdateException,
			WrongRepositoryStateException, AbortedByHookException {
		JGitTestUtil.writeTrashFile(db, "test", "test");
		Git git = Git.wrap(db2);
		git.add().addFilepattern("test").call();
		RevCommit c = git.commit().setMessage("adding test").call();
		return c;
	}

	private void assertCommit(RevCommit c) {
		ObjectDirectory od = db2.getObjectDatabase();
		assertTrue("can't find expected commit" + c.name(),
				od.has(c.toObjectId()));
	}

	private void assertAlternateObjects(FileRepository repo) {
		// check some objects in alternate
		final ObjectId alternateObjects[] = new ObjectId[] {
				ObjectId.fromString("49322bb17d3acc9146f98c97d078513228bbf3c0"),
				ObjectId.fromString("d0114ab8ac326bab30e3a657a0397578c5a1af88"),
				ObjectId.fromString("f73b95671f326616d66b2afb3bdfcdbbce110b44"),
				ObjectId.fromString("6020a3b8d5d636e549ccbd0c53e2764684bb3125"),
				ObjectId.fromString("0a3d7772488b6b106fb62813c4d6d627918d9181"),
				ObjectId.fromString("da0f8ed91a8f2f0f067b3bdf26265d5ca48cf82c"),
				ObjectId.fromString(
						"cd4bcfc27da62c6b840de700be1c60a7e69952a5") };
		ObjectDirectory od = repo.getObjectDatabase();
		for (ObjectId o : alternateObjects) {
			assertTrue(String.format("can't find object %s in alternate",
					o.getName()), od.has(o));
		}
	}
}