aboutsummaryrefslogtreecommitdiffstats
path: root/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/RacyGitTests.java
blob: f25e5d10ff32eaad9c33e11cf4eab787f040999b (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
/*
 * Copyright (C) 2010, Christian Halstrick <christian.halstrick@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.lib;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.time.Instant;

import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.dircache.DirCache;
import org.eclipse.jgit.junit.RepositoryTestCase;
import org.eclipse.jgit.junit.time.TimeUtil;
import org.eclipse.jgit.treewalk.FileTreeIterator;
import org.eclipse.jgit.treewalk.WorkingTreeOptions;
import org.eclipse.jgit.util.FS;
import org.junit.Test;

public class RacyGitTests extends RepositoryTestCase {

	/**
	 * This test is inherently flaky in nature since using clocks in a computer
	 * to determine file modifications in a filesystem from Java is difficult
	 * and depends on many factors and we can't test all combinations
	 *
	 * If this test fails on your computer, don't worry but let us know if you
	 * are willing to provide details which may help to further improve handling
	 * of the racy git problem in JGit.
	 *
	 * Despite not being completely reproducible this test is still useful to
	 * detect regressions when running this test repeatedly on the same
	 * OS/filesystem/Java version (which we do on the CI used to build JGit).
	 *
	 * @see "https://git-scm.com/docs/racy-git"
	 * @see "https://www.youtube.com/watch?v=m44cAozuLNI"
	 */
	@Test
	public void testRacyGitDetection() throws Exception {
		// Reset to force creation of index file
		try (Git git = new Git(db)) {
			git.reset().call();
		}

		// wait to ensure that modtimes of the file doesn't match last index
		// file modtime
		fsTick(db.getIndexFile());

		// create two files
		File a = writeToWorkDir("a", "a");
		File b = writeToWorkDir("b", "b");
		TimeUtil.setLastModifiedOf(a.toPath(), b.toPath());
		TimeUtil.setLastModifiedOf(b.toPath(), b.toPath());

		// wait to ensure that file-modTimes and therefore index entry modTime
		// doesn't match the modtime of index-file after next persistance
		fsTick(b);

		// now add both files to the index. No racy git expected
		resetIndex(new FileTreeIterator(db));

		assertEquals(
				"[a, mode:100644, time:t0, length:1, content:a]"
						+ "[b, mode:100644, time:t0, length:1, content:b]",
				indexState(SMUDGE | MOD_TIME | LENGTH | CONTENT));

		// wait to ensure the file 'a' is updated at t1.
		fsTick(db.getIndexFile());

		// Create a racy git situation. This is a situation that the index is
		// updated and then a file is modified within the same tick of the
		// filesystem timestamp resolution. By changing the index file
		// artificially, we create a fake racy situation.
		File updatedA = writeToWorkDir("a", "a2");
		Instant newLastModified = TimeUtil
				.setLastModifiedWithOffset(updatedA.toPath(), 100L);
		resetIndex(new FileTreeIterator(db));
		FS.DETECTED.setLastModified(db.getIndexFile().toPath(),
				newLastModified);

		DirCache dc = db.readDirCache();
		// check index state: although racily clean a should not be reported as
		// being dirty since we forcefully reset the index to match the working
		// tree
		assertEquals(
				"[a, mode:100644, time:t1, smudged, length:0, content:a2]"
						+ "[b, mode:100644, time:t0, length:1, content:b]",
				indexState(SMUDGE | MOD_TIME | LENGTH | CONTENT));

		// compare state of files in working tree with index to check that
		// FileTreeIterator.isModified() works as expected
		FileTreeIterator f = new FileTreeIterator(db.getWorkTree(), db.getFS(),
				db.getConfig().get(WorkingTreeOptions.KEY));
		assertTrue(f.findFile("a"));
		try (ObjectReader reader = db.newObjectReader()) {
			assertFalse(f.isModified(dc.getEntry("a"), false, reader));
		}
	}

	private File writeToWorkDir(String path, String content) throws IOException {
		File f = new File(db.getWorkTree(), path);
		try (FileOutputStream fos = new FileOutputStream(f)) {
			fos.write(content.getBytes(UTF_8));
			return f;
		}
	}
}