Merge "Fix DirCache re-read."

This commit is contained in:
Shawn Pearce 2011-03-04 10:19:06 -05:00 committed by Code Review
commit b9df3e6019
2 changed files with 17 additions and 12 deletions

View File

@ -66,6 +66,7 @@
import org.eclipse.jgit.lib.Constants; import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.ObjectId; import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.ObjectInserter; import org.eclipse.jgit.lib.ObjectInserter;
import org.eclipse.jgit.storage.file.FileSnapshot;
import org.eclipse.jgit.storage.file.LockFile; import org.eclipse.jgit.storage.file.LockFile;
import org.eclipse.jgit.util.FS; import org.eclipse.jgit.util.FS;
import org.eclipse.jgit.util.IO; import org.eclipse.jgit.util.IO;
@ -205,9 +206,6 @@ public static DirCache lock(final File indexLocation, final FS fs)
/** Location of the current version of the index file. */ /** Location of the current version of the index file. */
private final File liveFile; private final File liveFile;
/** Modification time of the file at the last read/write we did. */
private long lastModified;
/** Individual file index entries, sorted by path name. */ /** Individual file index entries, sorted by path name. */
private DirCacheEntry[] sortedEntries; private DirCacheEntry[] sortedEntries;
@ -223,6 +221,9 @@ public static DirCache lock(final File indexLocation, final FS fs)
/** file system abstraction **/ /** file system abstraction **/
private final FS fs; private final FS fs;
/** Keep track of whether the index has changed or not */
private FileSnapshot snapshot;
/** /**
* Create a new in-core index representation. * Create a new in-core index representation.
* <p> * <p>
@ -290,7 +291,7 @@ public void read() throws IOException, CorruptObjectException {
throw new IOException(JGitText.get().dirCacheDoesNotHaveABackingFile); throw new IOException(JGitText.get().dirCacheDoesNotHaveABackingFile);
if (!liveFile.exists()) if (!liveFile.exists())
clear(); clear();
else if (liveFile.lastModified() != lastModified) { else if (snapshot == null || snapshot.isModified(liveFile)) {
try { try {
final FileInputStream inStream = new FileInputStream(liveFile); final FileInputStream inStream = new FileInputStream(liveFile);
try { try {
@ -309,6 +310,7 @@ else if (liveFile.lastModified() != lastModified) {
// //
clear(); clear();
} }
snapshot = FileSnapshot.save(liveFile);
} }
} }
@ -319,12 +321,12 @@ else if (liveFile.lastModified() != lastModified) {
public boolean isOutdated() throws IOException { public boolean isOutdated() throws IOException {
if (liveFile == null || !liveFile.exists()) if (liveFile == null || !liveFile.exists())
return false; return false;
return liveFile.lastModified() != lastModified; return snapshot.isModified(liveFile);
} }
/** Empty this index, removing all entries. */ /** Empty this index, removing all entries. */
public void clear() { public void clear() {
lastModified = 0; snapshot = null;
sortedEntries = NO_ENTRIES; sortedEntries = NO_ENTRIES;
entryCnt = 0; entryCnt = 0;
tree = null; tree = null;
@ -361,7 +363,7 @@ else if (ver != 2)
final MutableInteger infoAt = new MutableInteger(); final MutableInteger infoAt = new MutableInteger();
for (int i = 0; i < entryCnt; i++) for (int i = 0; i < entryCnt; i++)
sortedEntries[i] = new DirCacheEntry(infos, infoAt, in, md); sortedEntries[i] = new DirCacheEntry(infos, infoAt, in, md);
lastModified = liveFile.lastModified(); snapshot = FileSnapshot.save(liveFile);
// After the file entries are index extensions, and then a footer. // After the file entries are index extensions, and then a footer.
// //
@ -516,14 +518,14 @@ void writeTo(final OutputStream os) throws IOException {
// Write the individual file entries. // Write the individual file entries.
// //
if (lastModified <= 0) { if (snapshot == null) {
// Write a new index, as no entries require smudging. // Write a new index, as no entries require smudging.
// //
for (int i = 0; i < entryCnt; i++) for (int i = 0; i < entryCnt; i++)
sortedEntries[i].write(dos); sortedEntries[i].write(dos);
} else { } else {
final int smudge_s = (int) (lastModified / 1000); final int smudge_s = (int) (snapshot.lastModified() / 1000);
final int smudge_ns = ((int) (lastModified % 1000)) * 1000000; final int smudge_ns = ((int) (snapshot.lastModified() % 1000)) * 1000000;
for (int i = 0; i < entryCnt; i++) { for (int i = 0; i < entryCnt; i++) {
final DirCacheEntry e = sortedEntries[i]; final DirCacheEntry e = sortedEntries[i];
if (e.mightBeRacilyClean(smudge_s, smudge_ns)) if (e.mightBeRacilyClean(smudge_s, smudge_ns))
@ -564,7 +566,7 @@ public boolean commit() {
myLock = null; myLock = null;
if (!tmp.commit()) if (!tmp.commit())
return false; return false;
lastModified = tmp.getCommitLastModified(); snapshot = tmp.getCommitSnapshot();
return true; return true;
} }

View File

@ -102,7 +102,10 @@ private FileSnapshot(long read, long modified) {
this.cannotBeRacilyClean = notRacyClean(read); this.cannotBeRacilyClean = notRacyClean(read);
} }
long lastModified() { /**
* @return time of last snapshot update
*/
public long lastModified() {
return lastModified; return lastModified;
} }