Merge "Ensure index change event is fired when index snapshot changed" into stable-5.0

This commit is contained in:
Jonathan Nieder 2018-06-08 01:34:12 -04:00 committed by Gerrit Code Review @ Eclipse.org
commit f98112289c
1 changed files with 26 additions and 9 deletions

View File

@ -56,7 +56,7 @@
import java.util.Locale;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.locks.ReentrantLock;
import org.eclipse.jgit.annotations.Nullable;
import org.eclipse.jgit.api.errors.JGitInternalException;
@ -125,7 +125,10 @@ public class FileRepository extends Repository {
private final RefDatabase refs;
private final ObjectDirectory objectDatabase;
private AtomicReference<FileSnapshot> snapshot = new AtomicReference<>();
private final ReentrantLock snapshotLock = new ReentrantLock();
// protected by snapshotLock
private FileSnapshot snapshot;
/**
* Construct a representation of a Git repository.
@ -240,8 +243,9 @@ public void onConfigChanged(ConfigChangedEvent event) {
Long.valueOf(repositoryFormatVersion)));
}
if (!isBare())
snapshot.getAndSet(FileSnapshot.save(getIndexFile()));
if (!isBare()) {
snapshot = FileSnapshot.save(getIndexFile());
}
}
private void loadSystemConfig() throws IOException {
@ -549,17 +553,30 @@ private void detectIndexChanges() {
}
File indexFile = getIndexFile();
if (snapshot.get() == null) {
snapshot.getAndSet(FileSnapshot.save(indexFile));
} else if (snapshot.get().isModified(indexFile)) {
notifyIndexChanged(false);
snapshotLock.lock();
try {
if (snapshot == null) {
snapshot = FileSnapshot.save(indexFile);
} else if (snapshot.isModified(indexFile)) {
snapshotLock.unlock();
notifyIndexChanged(false);
}
} finally {
if (snapshotLock.isHeldByCurrentThread()) {
snapshotLock.unlock();
}
}
}
/** {@inheritDoc} */
@Override
public void notifyIndexChanged(boolean internal) {
snapshot.getAndSet(FileSnapshot.save(getIndexFile()));
snapshotLock.lock();
try {
snapshot = FileSnapshot.save(getIndexFile());
} finally {
snapshotLock.unlock();
}
fireEvent(new IndexChangedEvent(internal));
}