Store in IndexChangedEvent if it was caused by JGit itself

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>
This commit is contained in:
Matthias Sohn 2018-05-13 21:37:21 +02:00
parent 08d2e0188c
commit e9e150fdd2
5 changed files with 52 additions and 18 deletions

View File

@ -737,12 +737,14 @@ public boolean commit() {
final LockFile tmp = myLock; final LockFile tmp = myLock;
requireLocked(tmp); requireLocked(tmp);
myLock = null; myLock = null;
if (!tmp.commit()) if (!tmp.commit()) {
return false; return false;
}
snapshot = tmp.getCommitSnapshot(); snapshot = tmp.getCommitSnapshot();
if (indexChangedListener != null if (indexChangedListener != null
&& !Arrays.equals(readIndexChecksum, writeIndexChecksum)) && !Arrays.equals(readIndexChecksum, writeIndexChecksum)) {
indexChangedListener.onIndexChanged(new IndexChangedEvent()); indexChangedListener.onIndexChanged(new IndexChangedEvent(true));
}
return true; return true;
} }

View File

@ -47,6 +47,28 @@
* Describes a change to one or more paths in the index file. * Describes a change to one or more paths in the index file.
*/ */
public class IndexChangedEvent extends RepositoryEvent<IndexChangedListener> { 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} */ /** {@inheritDoc} */
@Override @Override
public Class<IndexChangedListener> getListenerType() { public Class<IndexChangedListener> getListenerType() {

View File

@ -133,7 +133,7 @@ public void scanForRepoChanges() throws IOException {
/** {@inheritDoc} */ /** {@inheritDoc} */
@Override @Override
public void notifyIndexChanged() { public void notifyIndexChanged(boolean internal) {
// Do not send notifications. // Do not send notifications.
// There is no index, as there is no working tree. // There is no index, as there is no working tree.
} }

View File

@ -58,6 +58,7 @@
import java.util.Locale; import java.util.Locale;
import java.util.Objects; import java.util.Objects;
import java.util.Set; import java.util.Set;
import java.util.concurrent.atomic.AtomicReference;
import org.eclipse.jgit.annotations.Nullable; import org.eclipse.jgit.annotations.Nullable;
import org.eclipse.jgit.api.errors.JGitInternalException; import org.eclipse.jgit.api.errors.JGitInternalException;
@ -125,7 +126,8 @@ public class FileRepository extends Repository {
private final FileBasedConfig repoConfig; private final FileBasedConfig repoConfig;
private final RefDatabase refs; private final RefDatabase refs;
private final ObjectDirectory objectDatabase; private final ObjectDirectory objectDatabase;
private FileSnapshot snapshot;
private AtomicReference<FileSnapshot> snapshot = new AtomicReference<>();
/** /**
* Construct a representation of a Git repository. * Construct a representation of a Git repository.
@ -241,7 +243,7 @@ public void onConfigChanged(ConfigChangedEvent event) {
} }
if (!isBare()) if (!isBare())
snapshot = FileSnapshot.save(getIndexFile()); snapshot.getAndSet(FileSnapshot.save(getIndexFile()));
} }
private void loadSystemConfig() throws IOException { private void loadSystemConfig() throws IOException {
@ -544,21 +546,23 @@ public void scanForRepoChanges() throws IOException {
/** Detect index changes. */ /** Detect index changes. */
private void detectIndexChanges() { private void detectIndexChanges() {
if (isBare()) if (isBare()) {
return; return;
}
File indexFile = getIndexFile(); File indexFile = getIndexFile();
if (snapshot == null) if (snapshot.get() == null) {
snapshot = FileSnapshot.save(indexFile); snapshot.getAndSet(FileSnapshot.save(indexFile));
else if (snapshot.isModified(indexFile)) } else if (snapshot.get().isModified(indexFile)) {
notifyIndexChanged(); notifyIndexChanged(false);
}
} }
/** {@inheritDoc} */ /** {@inheritDoc} */
@Override @Override
public void notifyIndexChanged() { public void notifyIndexChanged(boolean internal) {
snapshot = FileSnapshot.save(getIndexFile()); snapshot.getAndSet(FileSnapshot.save(getIndexFile()));
fireEvent(new IndexChangedEvent()); fireEvent(new IndexChangedEvent(internal));
} }
/** {@inheritDoc} */ /** {@inheritDoc} */

View File

@ -1267,7 +1267,7 @@ public DirCache lockDirCache() throws NoWorkTreeException,
IndexChangedListener l = new IndexChangedListener() { IndexChangedListener l = new IndexChangedListener() {
@Override @Override
public void onIndexChanged(IndexChangedEvent event) { public void onIndexChanged(IndexChangedEvent event) {
notifyIndexChanged(); notifyIndexChanged(true);
} }
}; };
return DirCache.lock(this, l); return DirCache.lock(this, l);
@ -1560,16 +1560,22 @@ public File getWorkTree() throws NoWorkTreeException {
} }
/** /**
* Force a scan for changed refs. * Force a scan for changed refs. Fires an IndexChangedEvent(false) if
* changes are detected.
* *
* @throws java.io.IOException * @throws java.io.IOException
*/ */
public abstract void scanForRepoChanges() throws 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 * Get a shortened more user friendly ref name