You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. import java.io.File;
  45. import java.io.FileOutputStream;
  46. import java.io.IOException;
  47. import java.util.TreeSet;
  48. import org.eclipse.jgit.treewalk.FileTreeIterator;
  49. import org.eclipse.jgit.treewalk.FileTreeIteratorWithTimeControl;
  50. import org.eclipse.jgit.treewalk.NameConflictTreeWalk;
  51. public class RacyGitTests extends RepositoryTestCase {
  52. public void testIterator() throws IllegalStateException, IOException,
  53. InterruptedException {
  54. TreeSet<Long> modTimes = new TreeSet<Long>();
  55. File lastFile = null;
  56. for (int i = 0; i < 10; i++) {
  57. lastFile = new File(db.getWorkTree(), "0." + i);
  58. lastFile.createNewFile();
  59. if (i == 5)
  60. fsTick(lastFile);
  61. }
  62. modTimes.add(fsTick(lastFile));
  63. for (int i = 0; i < 10; i++) {
  64. lastFile = new File(db.getWorkTree(), "1." + i);
  65. lastFile.createNewFile();
  66. }
  67. modTimes.add(fsTick(lastFile));
  68. for (int i = 0; i < 10; i++) {
  69. lastFile = new File(db.getWorkTree(), "2." + i);
  70. lastFile.createNewFile();
  71. if (i % 4 == 0)
  72. fsTick(lastFile);
  73. }
  74. FileTreeIteratorWithTimeControl fileIt = new FileTreeIteratorWithTimeControl(
  75. db, modTimes);
  76. NameConflictTreeWalk tw = new NameConflictTreeWalk(db);
  77. tw.addTree(fileIt);
  78. tw.setRecursive(true);
  79. FileTreeIterator t;
  80. long t0 = 0;
  81. for (int i = 0; i < 10; i++) {
  82. assertTrue(tw.next());
  83. t = tw.getTree(0, FileTreeIterator.class);
  84. if (i == 0)
  85. t0 = t.getEntryLastModified();
  86. else
  87. assertEquals(t0, t.getEntryLastModified());
  88. }
  89. long t1 = 0;
  90. for (int i = 0; i < 10; i++) {
  91. assertTrue(tw.next());
  92. t = tw.getTree(0, FileTreeIterator.class);
  93. if (i == 0) {
  94. t1 = t.getEntryLastModified();
  95. assertTrue(t1 > t0);
  96. } else
  97. assertEquals(t1, t.getEntryLastModified());
  98. }
  99. long t2 = 0;
  100. for (int i = 0; i < 10; i++) {
  101. assertTrue(tw.next());
  102. t = tw.getTree(0, FileTreeIterator.class);
  103. if (i == 0) {
  104. t2 = t.getEntryLastModified();
  105. assertTrue(t2 > t1);
  106. } else
  107. assertEquals(t2, t.getEntryLastModified());
  108. }
  109. }
  110. public void testRacyGitDetection() throws IOException,
  111. IllegalStateException, InterruptedException {
  112. TreeSet<Long> modTimes = new TreeSet<Long>();
  113. File lastFile;
  114. // wait to ensure that modtimes of the file doesn't match last index
  115. // file modtime
  116. modTimes.add(fsTick(db.getIndexFile()));
  117. // create two files
  118. addToWorkDir("a", "a");
  119. lastFile = addToWorkDir("b", "b");
  120. // wait to ensure that file-modTimes and therefore index entry modTime
  121. // doesn't match the modtime of index-file after next persistance
  122. modTimes.add(fsTick(lastFile));
  123. // now add both files to the index. No racy git expected
  124. resetIndex(new FileTreeIteratorWithTimeControl(db, modTimes));
  125. assertEquals(
  126. "[a, mode:100644, time:t0, length:1, content:a]" +
  127. "[b, mode:100644, time:t0, length:1, content:b]",
  128. indexState(SMUDGE | MOD_TIME | LENGTH | CONTENT));
  129. // Remember the last modTime of index file. All modifications times of
  130. // further modification are translated to this value so it looks that
  131. // files have been modified in the same time slot as the index file
  132. modTimes.add(Long.valueOf(db.getIndexFile().lastModified()));
  133. // modify one file
  134. addToWorkDir("a", "a2");
  135. // now update the index the index. 'a' has to be racily clean -- because
  136. // it's modification time is exactly the same as the previous index file
  137. // mod time.
  138. resetIndex(new FileTreeIteratorWithTimeControl(db, modTimes));
  139. db.readDirCache();
  140. // although racily clean a should not be reported as being dirty
  141. assertEquals(
  142. "[a, mode:100644, time:t1, smudged, length:0, content:a2]" +
  143. "[b, mode:100644, time:t0, length:1, content:b]",
  144. indexState(SMUDGE|MOD_TIME|LENGTH|CONTENT));
  145. }
  146. private File addToWorkDir(String path, String content) throws IOException {
  147. File f = new File(db.getWorkTree(), path);
  148. FileOutputStream fos = new FileOutputStream(f);
  149. try {
  150. fos.write(content.getBytes(Constants.CHARACTER_ENCODING));
  151. return f;
  152. } finally {
  153. fos.close();
  154. }
  155. }
  156. }