View Javadoc
1   /*
2    * Copyright (C) 2010, Christian Halstrick <christian.halstrick@sap.com>
3    * and other copyright owners as documented in the project's IP log.
4    *
5    * This program and the accompanying materials are made available
6    * under the terms of the Eclipse Distribution License v1.0 which
7    * accompanies this distribution, is reproduced below, and is
8    * available at http://www.eclipse.org/org/documents/edl-v10.php
9    *
10   * All rights reserved.
11   *
12   * Redistribution and use in source and binary forms, with or
13   * without modification, are permitted provided that the following
14   * conditions are met:
15   *
16   * - Redistributions of source code must retain the above copyright
17   *   notice, this list of conditions and the following disclaimer.
18   *
19   * - Redistributions in binary form must reproduce the above
20   *   copyright notice, this list of conditions and the following
21   *   disclaimer in the documentation and/or other materials provided
22   *   with the distribution.
23   *
24   * - Neither the name of the Eclipse Foundation, Inc. nor the
25   *   names of its contributors may be used to endorse or promote
26   *   products derived from this software without specific prior
27   *   written permission.
28   *
29   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
30   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
31   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
34   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42   */
43  package org.eclipse.jgit.lib;
44  
45  import static java.nio.charset.StandardCharsets.UTF_8;
46  import static org.junit.Assert.assertEquals;
47  import static org.junit.Assert.assertFalse;
48  import static org.junit.Assert.assertTrue;
49  
50  import java.io.File;
51  import java.io.FileOutputStream;
52  import java.io.IOException;
53  import java.time.Instant;
54  
55  import org.eclipse.jgit.api.Git;
56  import org.eclipse.jgit.dircache.DirCache;
57  import org.eclipse.jgit.junit.RepositoryTestCase;
58  import org.eclipse.jgit.junit.time.TimeUtil;
59  import org.eclipse.jgit.treewalk.FileTreeIterator;
60  import org.eclipse.jgit.treewalk.WorkingTreeOptions;
61  import org.eclipse.jgit.util.FS;
62  import org.junit.Test;
63  
64  public class RacyGitTests extends RepositoryTestCase {
65  
66  	@Test
67  	public void testRacyGitDetection() throws Exception {
68  		// Reset to force creation of index file
69  		try (Git git = new Git(db)) {
70  			git.reset().call();
71  		}
72  
73  		// wait to ensure that modtimes of the file doesn't match last index
74  		// file modtime
75  		fsTick(db.getIndexFile());
76  
77  		// create two files
78  		File a = writeToWorkDir("a", "a");
79  		File b = writeToWorkDir("b", "b");
80  		TimeUtil.setLastModifiedOf(a.toPath(), b.toPath());
81  		TimeUtil.setLastModifiedOf(b.toPath(), b.toPath());
82  
83  		// wait to ensure that file-modTimes and therefore index entry modTime
84  		// doesn't match the modtime of index-file after next persistance
85  		fsTick(b);
86  
87  		// now add both files to the index. No racy git expected
88  		resetIndex(new FileTreeIterator(db));
89  
90  		assertEquals(
91  				"[a, mode:100644, time:t0, length:1, content:a]"
92  						+ "[b, mode:100644, time:t0, length:1, content:b]",
93  				indexState(SMUDGE | MOD_TIME | LENGTH | CONTENT));
94  
95  		// wait to ensure the file 'a' is updated at t1.
96  		fsTick(db.getIndexFile());
97  
98  		// Create a racy git situation. This is a situation that the index is
99  		// updated and then a file is modified within the same tick of the
100 		// filesystem timestamp resolution. By changing the index file
101 		// artificially, we create a fake racy situation.
102 		File updatedA = writeToWorkDir("a", "a2");
103 		Instant newLastModified = TimeUtil
104 				.setLastModifiedWithOffset(updatedA.toPath(), 100L);
105 		resetIndex(new FileTreeIterator(db));
106 		FS.DETECTED.setLastModified(db.getIndexFile().toPath(),
107 				newLastModified);
108 
109 		DirCache dc = db.readDirCache();
110 		// check index state: although racily clean a should not be reported as
111 		// being dirty since we forcefully reset the index to match the working
112 		// tree
113 		assertEquals(
114 				"[a, mode:100644, time:t1, smudged, length:0, content:a2]"
115 						+ "[b, mode:100644, time:t0, length:1, content:b]",
116 				indexState(SMUDGE | MOD_TIME | LENGTH | CONTENT));
117 
118 		// compare state of files in working tree with index to check that
119 		// FileTreeIterator.isModified() works as expected
120 		FileTreeIterator f = new FileTreeIterator(db.getWorkTree(), db.getFS(),
121 				db.getConfig().get(WorkingTreeOptions.KEY));
122 		assertTrue(f.findFile("a"));
123 		try (ObjectReader reader = db.newObjectReader()) {
124 			assertFalse(f.isModified(dc.getEntry("a"), false, reader));
125 		}
126 	}
127 
128 	private File writeToWorkDir(String path, String content) throws IOException {
129 		File f = new File(db.getWorkTree(), path);
130 		try (FileOutputStream fos = new FileOutputStream(f)) {
131 			fos.write(content.getBytes(UTF_8));
132 			return f;
133 		}
134 	}
135 }