]> source.dussan.org Git - jgit.git/commitdiff
Store in IndexChangedEvent if it was caused by JGit itself 48/122548/2
authorMatthias Sohn <matthias.sohn@sap.com>
Sun, 13 May 2018 19:37:21 +0000 (21:37 +0200)
committerMatthias Sohn <matthias.sohn@sap.com>
Sun, 13 May 2018 20:38:54 +0000 (22:38 +0200)
This allows to differentiate if index was changed by an external git
command or by JGit itself.

Change-Id: Iae692ba7d9bf01a288b3fb2dc2d07aec9891c712
Signed-off-by: Matthias Sohn <matthias.sohn@sap.com>
org.eclipse.jgit/src/org/eclipse/jgit/dircache/DirCache.java
org.eclipse.jgit/src/org/eclipse/jgit/events/IndexChangedEvent.java
org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/dfs/DfsRepository.java
org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/FileRepository.java
org.eclipse.jgit/src/org/eclipse/jgit/lib/Repository.java

index d7f975b003f26184e6de2804ded9b78808f484f5..db1cfd5540904be6698a11cb5967aac79334007b 100644 (file)
@@ -737,12 +737,14 @@ public class DirCache {
                final LockFile tmp = myLock;
                requireLocked(tmp);
                myLock = null;
-               if (!tmp.commit())
+               if (!tmp.commit()) {
                        return false;
+               }
                snapshot = tmp.getCommitSnapshot();
                if (indexChangedListener != null
-                               && !Arrays.equals(readIndexChecksum, writeIndexChecksum))
-                       indexChangedListener.onIndexChanged(new IndexChangedEvent());
+                               && !Arrays.equals(readIndexChecksum, writeIndexChecksum)) {
+                       indexChangedListener.onIndexChanged(new IndexChangedEvent(true));
+               }
                return true;
        }
 
index 647ec8a20c4a7fc28b256179ff36007c84f8452a..dcce86a33c5927208eeebc2e6d6c73c8c12f7b94 100644 (file)
@@ -47,6 +47,28 @@ package org.eclipse.jgit.events;
  * Describes a change to one or more paths in the index file.
  */
 public class IndexChangedEvent extends RepositoryEvent<IndexChangedListener> {
+       private boolean internal;
+
+       /**
+        * Notify that the index changed
+        *
+        * @param internal
+        *                     {@code true} if the index was changed by the same
+        *                     JGit process
+        * @since 5.0
+        */
+       public IndexChangedEvent(boolean internal) {
+               this.internal = internal;
+       }
+
+       /**
+        * @return {@code true} if the index was changed by the same JGit process
+        * @since 5.0
+        */
+       public boolean isInternal() {
+               return internal;
+       }
+
        /** {@inheritDoc} */
        @Override
        public Class<IndexChangedListener> getListenerType() {
index 197681658ef70af32e8fd316d1ca783f8ae43f99..5169e929e4e5b82ec89ee0f15bd10c38d5c8ffe1 100644 (file)
@@ -133,7 +133,7 @@ public abstract class DfsRepository extends Repository {
 
        /** {@inheritDoc} */
        @Override
-       public void notifyIndexChanged() {
+       public void notifyIndexChanged(boolean internal) {
                // Do not send notifications.
                // There is no index, as there is no working tree.
        }
index 9b82210e6c0fe2c42f1d570d272ba60c8caeed6d..9bbf7466b6a60ff0dd228bc85f2e14c7b6144357 100644 (file)
@@ -58,6 +58,7 @@ import java.util.HashSet;
 import java.util.Locale;
 import java.util.Objects;
 import java.util.Set;
+import java.util.concurrent.atomic.AtomicReference;
 
 import org.eclipse.jgit.annotations.Nullable;
 import org.eclipse.jgit.api.errors.JGitInternalException;
@@ -125,7 +126,8 @@ public class FileRepository extends Repository {
        private final FileBasedConfig repoConfig;
        private final RefDatabase refs;
        private final ObjectDirectory objectDatabase;
-       private FileSnapshot snapshot;
+
+       private AtomicReference<FileSnapshot> snapshot = new AtomicReference<>();
 
        /**
         * Construct a representation of a Git repository.
@@ -241,7 +243,7 @@ public class FileRepository extends Repository {
                }
 
                if (!isBare())
-                       snapshot = FileSnapshot.save(getIndexFile());
+                       snapshot.getAndSet(FileSnapshot.save(getIndexFile()));
        }
 
        private void loadSystemConfig() throws IOException {
@@ -544,21 +546,23 @@ public class FileRepository extends Repository {
 
        /** Detect index changes. */
        private void detectIndexChanges() {
-               if (isBare())
+               if (isBare()) {
                        return;
+               }
 
                File indexFile = getIndexFile();
-               if (snapshot == null)
-                       snapshot = FileSnapshot.save(indexFile);
-               else if (snapshot.isModified(indexFile))
-                       notifyIndexChanged();
+               if (snapshot.get() == null) {
+                       snapshot.getAndSet(FileSnapshot.save(indexFile));
+               } else if (snapshot.get().isModified(indexFile)) {
+                       notifyIndexChanged(false);
+               }
        }
 
        /** {@inheritDoc} */
        @Override
-       public void notifyIndexChanged() {
-               snapshot = FileSnapshot.save(getIndexFile());
-               fireEvent(new IndexChangedEvent());
+       public void notifyIndexChanged(boolean internal) {
+               snapshot.getAndSet(FileSnapshot.save(getIndexFile()));
+               fireEvent(new IndexChangedEvent(internal));
        }
 
        /** {@inheritDoc} */
index 3d85ee4e472fe1d0105d35de8032bead790a5c2b..9b508518934b9e0f233c7dddf66497d736fa01b1 100644 (file)
@@ -1267,7 +1267,7 @@ public abstract class Repository implements AutoCloseable {
                IndexChangedListener l = new IndexChangedListener() {
                        @Override
                        public void onIndexChanged(IndexChangedEvent event) {
-                               notifyIndexChanged();
+                               notifyIndexChanged(true);
                        }
                };
                return DirCache.lock(this, l);
@@ -1560,16 +1560,22 @@ public abstract class Repository implements AutoCloseable {
        }
 
        /**
-        * Force a scan for changed refs.
+        * Force a scan for changed refs. Fires an IndexChangedEvent(false) if
+        * changes are detected.
         *
         * @throws java.io.IOException
         */
        public abstract void scanForRepoChanges() throws IOException;
 
        /**
-        * Notify that the index changed
+        * Notify that the index changed by firing an IndexChangedEvent.
+        *
+        * @param internal
+        *                     {@code true} if the index was changed by the same
+        *                     JGit process
+        * @since 5.0
         */
-       public abstract void notifyIndexChanged();
+       public abstract void notifyIndexChanged(boolean internal);
 
        /**
         * Get a shortened more user friendly ref name