summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthias Sohn <matthias.sohn@sap.com>2018-06-03 13:32:51 +0200
committerMatthias Sohn <matthias.sohn@sap.com>2018-06-06 00:45:27 +0200
commita3738ef137caf27d3fc588d3027f46045ba58ef5 (patch)
tree12a099617d4445586ae15660bd9eff7c73f9f114
parentcb0abf787e576d676fc703cde6153bf8c0e08c1d (diff)
downloadjgit-a3738ef137caf27d3fc588d3027f46045ba58ef5.tar.gz
jgit-a3738ef137caf27d3fc588d3027f46045ba58ef5.zip
Ensure index change event is fired when index snapshot changed
Ensure that notifyIndexChanged is called every time we call FileSnapshot.save, except the first. Change-Id: I5a4e9826e791f518787366ae7c3a0ef3d416d2c1 Signed-off-by: Matthias Sohn <matthias.sohn@sap.com>
-rw-r--r--org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/FileRepository.java35
1 files changed, 26 insertions, 9 deletions
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/FileRepository.java b/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/FileRepository.java
index ed5cf2c9e2..13ef94b898 100644
--- a/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/FileRepository.java
+++ b/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/FileRepository.java
@@ -56,7 +56,7 @@ import java.util.HashSet;
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 class FileRepository extends Repository {
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 @@ public class FileRepository extends Repository {
}
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));
}