View Javadoc
1   /*
2    * Copyright (C) 2020 Thomas Wolf <thomas.wolf@paranor.ch> and others.
3    *
4    * This program and the accompanying materials are made available under the
5    * terms of the Eclipse Distribution License v. 1.0 which is available at
6    * https://www.eclipse.org/org/documents/edl-v10.php.
7    *
8    * SPDX-License-Identifier: BSD-3-Clause
9    */
10  
11  package org.eclipse.jgit.dircache;
12  
13  import static org.junit.Assert.assertEquals;
14  import static org.junit.Assert.assertNotNull;
15  import static org.junit.Assert.assertTrue;
16  
17  import java.io.File;
18  import java.util.EnumSet;
19  import java.util.Set;
20  
21  import org.eclipse.jgit.api.CloneCommand;
22  import org.eclipse.jgit.api.Git;
23  import org.eclipse.jgit.api.ResetCommand.ResetType;
24  import org.eclipse.jgit.dircache.DirCache.DirCacheVersion;
25  import org.eclipse.jgit.junit.RepositoryTestCase;
26  import org.eclipse.jgit.lib.StoredConfig;
27  import org.eclipse.jgit.util.FileUtils;
28  import org.eclipse.jgit.util.SystemReader;
29  import org.junit.Test;
30  
31  /**
32   * Tests for initial DirCache version after a clone or after a mixed or hard
33   * reset.
34   */
35  public class DirCacheAfterCloneTest extends RepositoryTestCase {
36  
37  	@Override
38  	public void setUp() throws Exception {
39  		super.setUp();
40  		try (Git git = new Git(db)) {
41  			writeTrashFile("Test.txt", "Hello world");
42  			git.add().addFilepattern("Test.txt").call();
43  			git.commit().setMessage("Initial commit").call();
44  		}
45  	}
46  
47  	private DirCacheVersion cloneAndCheck(Set<DirCacheVersion> expected)
48  			throws Exception {
49  		File directory = createTempDirectory("testCloneRepository");
50  		CloneCommand command = Git.cloneRepository();
51  		command.setDirectory(directory);
52  		command.setURI("file://" + db.getWorkTree().getAbsolutePath());
53  		Git git2 = command.call();
54  		addRepoToClose(git2.getRepository());
55  		assertNotNull(git2);
56  		DirCache dc = DirCache.read(git2.getRepository());
57  		DirCacheVersion version = dc.getVersion();
58  		assertTrue(expected.contains(version));
59  		return version;
60  	}
61  
62  	@Test
63  	public void testCloneV3OrV2() throws Exception {
64  		cloneAndCheck(EnumSet.of(DirCacheVersion.DIRC_VERSION_MINIMUM,
65  				DirCacheVersion.DIRC_VERSION_EXTENDED));
66  	}
67  
68  	@Test
69  	public void testCloneV4() throws Exception {
70  		StoredConfig cfg = SystemReader.getInstance().getUserConfig();
71  		cfg.load();
72  		cfg.setInt("index", null, "version", 4);
73  		cfg.save();
74  		cloneAndCheck(EnumSet.of(DirCacheVersion.DIRC_VERSION_PATHCOMPRESS));
75  	}
76  
77  	@Test
78  	public void testCloneV4manyFiles() throws Exception {
79  		StoredConfig cfg = SystemReader.getInstance().getUserConfig();
80  		cfg.load();
81  		cfg.setBoolean("feature", null, "manyFiles", true);
82  		cfg.save();
83  		cloneAndCheck(EnumSet.of(DirCacheVersion.DIRC_VERSION_PATHCOMPRESS));
84  	}
85  
86  	@Test
87  	public void testCloneV3CommitNoVersionChange() throws Exception {
88  		DirCacheVersion initial = cloneAndCheck(
89  				EnumSet.of(DirCacheVersion.DIRC_VERSION_MINIMUM,
90  						DirCacheVersion.DIRC_VERSION_EXTENDED));
91  		StoredConfig cfg = db.getConfig();
92  		cfg.setInt("index", null, "version", 4);
93  		cfg.save();
94  		try (Git git = new Git(db)) {
95  			writeTrashFile("Test.txt2", "Hello again");
96  			git.add().addFilepattern("Test.txt2").call();
97  			git.commit().setMessage("Second commit").call();
98  		}
99  		assertEquals("DirCache version should be unchanged", initial,
100 				DirCache.read(db).getVersion());
101 	}
102 
103 	@Test
104 	public void testCloneV3ResetHardVersionChange() throws Exception {
105 		cloneAndCheck(EnumSet.of(DirCacheVersion.DIRC_VERSION_MINIMUM,
106 						DirCacheVersion.DIRC_VERSION_EXTENDED));
107 		StoredConfig cfg = db.getConfig();
108 		cfg.setInt("index", null, "version", 4);
109 		cfg.save();
110 		FileUtils.delete(new File(db.getDirectory(), "index"));
111 		try (Git git = new Git(db)) {
112 			git.reset().setMode(ResetType.HARD).call();
113 		}
114 		assertEquals("DirCache version should have changed",
115 				DirCacheVersion.DIRC_VERSION_PATHCOMPRESS,
116 				DirCache.read(db).getVersion());
117 	}
118 
119 	@Test
120 	public void testCloneV3ResetMixedVersionChange() throws Exception {
121 		cloneAndCheck(EnumSet.of(DirCacheVersion.DIRC_VERSION_MINIMUM,
122 				DirCacheVersion.DIRC_VERSION_EXTENDED));
123 		StoredConfig cfg = db.getConfig();
124 		cfg.setInt("index", null, "version", 4);
125 		cfg.save();
126 		FileUtils.delete(new File(db.getDirectory(), "index"));
127 		try (Git git = new Git(db)) {
128 			git.reset().setMode(ResetType.MIXED).call();
129 		}
130 		assertEquals("DirCache version should have changed",
131 				DirCacheVersion.DIRC_VERSION_PATHCOMPRESS,
132 				DirCache.read(db).getVersion());
133 	}
134 }