aboutsummaryrefslogtreecommitdiffstats
path: root/org.eclipse.jgit.test/tst/org/eclipse/jgit/dircache/DirCacheAfterCloneTest.java
blob: f210760bf5870b89603712497de3330feb6c9841 (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
/*
 * Copyright (C) 2020 Thomas Wolf <thomas.wolf@paranor.ch> 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.dircache;

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

import java.io.File;
import java.util.EnumSet;
import java.util.Set;

import org.eclipse.jgit.api.CloneCommand;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.ResetCommand.ResetType;
import org.eclipse.jgit.dircache.DirCache.DirCacheVersion;
import org.eclipse.jgit.junit.RepositoryTestCase;
import org.eclipse.jgit.lib.StoredConfig;
import org.eclipse.jgit.util.FileUtils;
import org.eclipse.jgit.util.SystemReader;
import org.junit.Test;

/**
 * Tests for initial DirCache version after a clone or after a mixed or hard
 * reset.
 */
public class DirCacheAfterCloneTest extends RepositoryTestCase {

	@Override
	public void setUp() throws Exception {
		super.setUp();
		try (Git git = new Git(db)) {
			writeTrashFile("Test.txt", "Hello world");
			git.add().addFilepattern("Test.txt").call();
			git.commit().setMessage("Initial commit").call();
		}
	}

	private DirCacheVersion cloneAndCheck(Set<DirCacheVersion> expected)
			throws Exception {
		File directory = createTempDirectory("testCloneRepository");
		CloneCommand command = Git.cloneRepository();
		command.setDirectory(directory);
		command.setURI("file://" + db.getWorkTree().getAbsolutePath());
		Git git2 = command.call();
		addRepoToClose(git2.getRepository());
		assertNotNull(git2);
		DirCache dc = DirCache.read(git2.getRepository());
		DirCacheVersion version = dc.getVersion();
		assertTrue(expected.contains(version));
		return version;
	}

	@Test
	public void testCloneV3OrV2() throws Exception {
		cloneAndCheck(EnumSet.of(DirCacheVersion.DIRC_VERSION_MINIMUM,
				DirCacheVersion.DIRC_VERSION_EXTENDED));
	}

	@Test
	public void testCloneV4() throws Exception {
		StoredConfig cfg = SystemReader.getInstance().getUserConfig();
		cfg.load();
		cfg.setInt("index", null, "version", 4);
		cfg.save();
		cloneAndCheck(EnumSet.of(DirCacheVersion.DIRC_VERSION_PATHCOMPRESS));
	}

	@Test
	public void testCloneV4manyFiles() throws Exception {
		StoredConfig cfg = SystemReader.getInstance().getUserConfig();
		cfg.load();
		cfg.setBoolean("feature", null, "manyFiles", true);
		cfg.save();
		cloneAndCheck(EnumSet.of(DirCacheVersion.DIRC_VERSION_PATHCOMPRESS));
	}

	@Test
	public void testCloneV3CommitNoVersionChange() throws Exception {
		DirCacheVersion initial = cloneAndCheck(
				EnumSet.of(DirCacheVersion.DIRC_VERSION_MINIMUM,
						DirCacheVersion.DIRC_VERSION_EXTENDED));
		StoredConfig cfg = db.getConfig();
		cfg.setInt("index", null, "version", 4);
		cfg.save();
		try (Git git = new Git(db)) {
			writeTrashFile("Test.txt2", "Hello again");
			git.add().addFilepattern("Test.txt2").call();
			git.commit().setMessage("Second commit").call();
		}
		assertEquals("DirCache version should be unchanged", initial,
				DirCache.read(db).getVersion());
	}

	@Test
	public void testCloneV3ResetHardVersionChange() throws Exception {
		cloneAndCheck(EnumSet.of(DirCacheVersion.DIRC_VERSION_MINIMUM,
						DirCacheVersion.DIRC_VERSION_EXTENDED));
		StoredConfig cfg = db.getConfig();
		cfg.setInt("index", null, "version", 4);
		cfg.save();
		FileUtils.delete(new File(db.getDirectory(), "index"));
		try (Git git = new Git(db)) {
			git.reset().setMode(ResetType.HARD).call();
		}
		assertEquals("DirCache version should have changed",
				DirCacheVersion.DIRC_VERSION_PATHCOMPRESS,
				DirCache.read(db).getVersion());
	}

	@Test
	public void testCloneV3ResetMixedVersionChange() throws Exception {
		cloneAndCheck(EnumSet.of(DirCacheVersion.DIRC_VERSION_MINIMUM,
				DirCacheVersion.DIRC_VERSION_EXTENDED));
		StoredConfig cfg = db.getConfig();
		cfg.setInt("index", null, "version", 4);
		cfg.save();
		FileUtils.delete(new File(db.getDirectory(), "index"));
		try (Git git = new Git(db)) {
			git.reset().setMode(ResetType.MIXED).call();
		}
		assertEquals("DirCache version should have changed",
				DirCacheVersion.DIRC_VERSION_PATHCOMPRESS,
				DirCache.read(db).getVersion());
	}
}