]> source.dussan.org Git - jgit.git/commitdiff
Use Instant for smudge time in DirCache and DirCacheEntry 84/146284/1
authorMatthias Sohn <matthias.sohn@sap.com>
Thu, 18 Jul 2019 01:36:18 +0000 (03:36 +0200)
committerMatthias Sohn <matthias.sohn@sap.com>
Thu, 18 Jul 2019 01:36:18 +0000 (03:36 +0200)
Change-Id: I98050a51baf4726c5717ef62ce7f026173666bdf
Signed-off-by: Matthias Sohn <matthias.sohn@sap.com>
org.eclipse.jgit/.settings/.api_filters
org.eclipse.jgit/src/org/eclipse/jgit/dircache/DirCache.java
org.eclipse.jgit/src/org/eclipse/jgit/dircache/DirCacheEntry.java

index 951a5e30f8ffc23b7cd6949164d2cf83fd220fdb..01215a8c70ab5ee1c3e0d2e9736ef119d5bc5908 100644 (file)
                 <message_argument value="getLastModifiedInstant()"/>
             </message_arguments>
         </filter>
+        <filter id="1142947843">
+            <message_arguments>
+                <message_argument value="5.1.9"/>
+                <message_argument value="mightBeRacilyClean(Instant)"/>
+            </message_arguments>
+        </filter>
         <filter id="1142947843">
             <message_arguments>
                 <message_argument value="5.1.9"/>
index 340214b068465dcf6a816f16b7b9e311cb9bf72a..0cfd16b58afaa2be100721b12bed27fea3262058 100644 (file)
@@ -58,6 +58,7 @@ import java.io.OutputStream;
 import java.security.DigestOutputStream;
 import java.security.MessageDigest;
 import java.text.MessageFormat;
+import java.time.Instant;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Comparator;
@@ -497,9 +498,7 @@ public class DirCache {
                        throw new CorruptObjectException(JGitText.get().DIRCHasTooManyEntries);
 
                snapshot = FileSnapshot.save(liveFile);
-               // TODO (ms) combine smudge_s and smudge_ns into Duration
-               int smudge_s = (int) (snapshot.lastModifiedInstant().getEpochSecond());
-               int smudge_ns = snapshot.lastModifiedInstant().getNano();
+               Instant smudge = snapshot.lastModifiedInstant();
 
                // Load the individual file entries.
                //
@@ -508,8 +507,9 @@ public class DirCache {
                sortedEntries = new DirCacheEntry[entryCnt];
 
                final MutableInteger infoAt = new MutableInteger();
-               for (int i = 0; i < entryCnt; i++)
-                       sortedEntries[i] = new DirCacheEntry(infos, infoAt, in, md, smudge_s, smudge_ns);
+               for (int i = 0; i < entryCnt; i++) {
+                       sortedEntries[i] = new DirCacheEntry(infos, infoAt, in, md, smudge);
+               }
 
                // After the file entries are index extensions, and then a footer.
                //
index 4b36f6b96b103018f4e9faec229334f22c0d77e2..d2a59c131098ce25edcb298556e4a6353cbbd6cf 100644 (file)
@@ -145,10 +145,9 @@ public class DirCacheEntry {
        /** Flags which are never stored to disk. */
        private byte inCoreFlags;
 
-       // TODO (ms): use Instant to combine smudge_s and smudge_ns
        DirCacheEntry(final byte[] sharedInfo, final MutableInteger infoAt,
-                       final InputStream in, final MessageDigest md, final int smudge_s,
-                       final int smudge_ns) throws IOException {
+                       final InputStream in, final MessageDigest md, final Instant smudge)
+                       throws IOException {
                info = sharedInfo;
                infoOffset = infoAt.value;
 
@@ -217,8 +216,9 @@ public class DirCacheEntry {
                        md.update(nullpad, 0, padLen);
                }
 
-               if (mightBeRacilyClean(smudge_s, smudge_ns))
+               if (mightBeRacilyClean(smudge)) {
                        smudgeRacilyClean();
+               }
        }
 
        /**
@@ -346,8 +346,29 @@ public class DirCacheEntry {
         * @param smudge_ns
         *            nanoseconds component of the index's last modified time.
         * @return true if extra careful checks should be used.
+        * @deprecated use {@link #mightBeRacilyClean(Instant)} instead
         */
+       @Deprecated
        public final boolean mightBeRacilyClean(int smudge_s, int smudge_ns) {
+               return mightBeRacilyClean(Instant.ofEpochSecond(smudge_s, smudge_ns));
+       }
+
+       /**
+        * Is it possible for this entry to be accidentally assumed clean?
+        * <p>
+        * The "racy git" problem happens when a work file can be updated faster
+        * than the filesystem records file modification timestamps. It is possible
+        * for an application to edit a work file, update the index, then edit it
+        * again before the filesystem will give the work file a new modification
+        * timestamp. This method tests to see if file was written out at the same
+        * time as the index.
+        *
+        * @param smudge
+        *            index's last modified time.
+        * @return true if extra careful checks should be used.
+        * @since 5.1.9
+        */
+       public final boolean mightBeRacilyClean(Instant smudge) {
                // If the index has a modification time then it came from disk
                // and was not generated from scratch in memory. In such cases
                // the entry is 'racily clean' if the entry's cached modification
@@ -357,8 +378,9 @@ public class DirCacheEntry {
                //
                final int base = infoOffset + P_MTIME;
                final int mtime = NB.decodeInt32(info, base);
-               if (smudge_s == mtime)
-                       return smudge_ns <= NB.decodeInt32(info, base + 4);
+               if (smudge.getEpochSecond() == mtime) {
+                       return smudge.getNano() <= NB.decodeInt32(info, base + 4);
+               }
                return false;
        }