Skip to content

The Unified and Autonomous Development Platform

BlobChange implementation

robin shinea392c0e31c12
diff --git a/commons.git/src/main/java/com/pmease/commons/git/BlobChange.java b/commons.git/src/main/java/com/pmease/commons/git/BlobChange.java
deleted file mode 100644
index 6f4b31b898..0000000000
--- a/commons.git/src/main/java/com/pmease/commons/git/BlobChange.java
+++ /dev/null
@@ -1,25 +0,0 @@
-package com.pmease.commons.git;
-
-import java.io.Serializable;
-
-@SuppressWarnings("serial")
-public class BlobChange implements Serializable {
-
- private final BlobIdent oldBlobIdent;
-
- private final BlobIdent newBlobIdent;
-
- public BlobChange(BlobIdent oldBlobIdent, BlobIdent newBlobIdent) {
- this.oldBlobIdent = oldBlobIdent;
- this.newBlobIdent = newBlobIdent;
- }
-
- public BlobIdent getOldBlobIdent() {
- return oldBlobIdent;
- }
-
- public BlobIdent getNewBlobIdent() {
- return newBlobIdent;
- }
-
-}
diff --git a/commons.git/src/main/java/com/pmease/commons/git/BlobDiff.java b/commons.git/src/main/java/com/pmease/commons/git/BlobDiff.java
deleted file mode 100644
index 50a58650ab..0000000000
--- a/commons.git/src/main/java/com/pmease/commons/git/BlobDiff.java
+++ /dev/null
@@ -1,23 +0,0 @@
-package com.pmease.commons.git;
-
-import java.util.List;
-
-import com.pmease.commons.lang.diff.DiffBlock;
-
-public class BlobDiff extends BlobChange {
-
- private static final long serialVersionUID = 1L;
-
- private final List<DiffBlock> diffs;
-
- public BlobDiff(BlobIdent oldBlobIdent, BlobIdent newBlobIdent, List<DiffBlock> diffs) {
- super(oldBlobIdent, newBlobIdent);
-
- this.diffs = diffs;
- }
-
- public List<DiffBlock> getDiffs() {
- return diffs;
- }
-
-}
diff --git a/commons.git/src/main/java/com/pmease/commons/git/BlobIdent.java b/commons.git/src/main/java/com/pmease/commons/git/BlobIdent.java
index 77801255cf..111f7f1092 100644
--- a/commons.git/src/main/java/com/pmease/commons/git/BlobIdent.java
+++ b/commons.git/src/main/java/com/pmease/commons/git/BlobIdent.java
@@ -7,6 +7,7 @@ import javax.annotation.Nullable;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
+import org.eclipse.jgit.lib.AnyObjectId;
import org.eclipse.jgit.lib.FileMode;
import com.google.common.base.Objects;
@@ -24,6 +25,9 @@ public class BlobIdent implements Serializable, Comparable<BlobIdent> {
@Nullable
public Integer mode;
+ @Nullable
+ public AnyObjectId id;
+
public BlobIdent() {
}
@@ -53,6 +57,10 @@ public class BlobIdent implements Serializable, Comparable<BlobIdent> {
return (FileMode.TYPE_MASK & mode) == FileMode.TYPE_FILE;
}
+ public String getFileName() {
+ return isFile()?path:null;
+ }
+
public String getName() {
if (path != null) {
if (path.contains("/"))
@@ -75,6 +83,7 @@ public class BlobIdent implements Serializable, Comparable<BlobIdent> {
.append(revision, otherIdent.revision)
.append(path, otherIdent.path)
.append(mode, otherIdent.mode)
+ .append(id, otherIdent.id)
.isEquals();
}
@@ -84,6 +93,7 @@ public class BlobIdent implements Serializable, Comparable<BlobIdent> {
.append(revision)
.append(path)
.append(mode)
+ .append(id)
.toHashCode();
}
diff --git a/commons.git/src/main/java/com/pmease/commons/git/GitUtils.java b/commons.git/src/main/java/com/pmease/commons/git/GitUtils.java
index 14e6cf811f..8f8a045526 100644
--- a/commons.git/src/main/java/com/pmease/commons/git/GitUtils.java
+++ b/commons.git/src/main/java/com/pmease/commons/git/GitUtils.java
@@ -1,6 +1,5 @@
package com.pmease.commons.git;
-import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@@ -10,19 +9,9 @@ import javax.annotation.Nullable;
import org.apache.commons.lang3.StringUtils;
import org.eclipse.jgit.diff.DiffEntry;
-import org.eclipse.jgit.diff.DiffFormatter;
-import org.eclipse.jgit.errors.CorruptObjectException;
-import org.eclipse.jgit.errors.IncorrectObjectTypeException;
-import org.eclipse.jgit.errors.MissingObjectException;
import org.eclipse.jgit.lib.FileMode;
-import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.PersonIdent;
-import org.eclipse.jgit.lib.Repository;
-import org.eclipse.jgit.revwalk.RevWalk;
-import org.eclipse.jgit.treewalk.TreeWalk;
-import org.eclipse.jgit.treewalk.filter.TreeFilter;
import org.eclipse.jgit.util.SystemReader;
-import org.eclipse.jgit.util.io.NullOutputStream;
import com.google.common.base.Joiner;
import com.google.common.base.Objects;
@@ -191,17 +180,4 @@ public class GitUtils {
return Git.REFS_HEADS + branch;
}
- public static List<BlobChange> getChanges(Repository repository, ObjectId oldCommitId, ObjectId newCommitId) {
- DiffFormatter diffFormatter = new DiffFormatter(NullOutputStream.INSTANCE);
- diffFormatter.setRepository(repository);
- try {
- diffFormatter.scan(oldCommitId, newCommitId);
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
- }
-
- public static List<BlobDiff> diff(Repository repository, ObjectId oldCommitId, ObjectId newCommitId,
- LineProcessor lineProcessor) {
- }
}
diff --git a/commons.loader/src/main/java/com/pmease/commons/loader/AppLoaderModule.java b/commons.loader/src/main/java/com/pmease/commons/loader/AppLoaderModule.java
index 64310cd274..ec494730bf 100644
--- a/commons.loader/src/main/java/com/pmease/commons/loader/AppLoaderModule.java
+++ b/commons.loader/src/main/java/com/pmease/commons/loader/AppLoaderModule.java
@@ -2,6 +2,7 @@ package com.pmease.commons.loader;
import java.util.Collection;
import java.util.concurrent.ExecutorService;
+import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
@@ -42,6 +43,8 @@ public class AppLoaderModule extends AbstractModule {
bind(PluginManager.class).to(DefaultPluginManager.class);
bind(ImplementationRegistry.class).to(DefaultImplementationRegistry.class);
+ bind(ForkJoinPool.class).toInstance(new ForkJoinPool());
+
bind(ExecutorService.class).toProvider(new Provider<ExecutorService>() {
@Override
diff --git a/gitplex.core/src/main/java/com/pmease/gitplex/core/model/Repository.java b/gitplex.core/src/main/java/com/pmease/gitplex/core/model/Repository.java
index 15d6c3fb3f..c0a3ad6b8d 100644
--- a/gitplex.core/src/main/java/com/pmease/gitplex/core/model/Repository.java
+++ b/gitplex.core/src/main/java/com/pmease/gitplex/core/model/Repository.java
@@ -30,6 +30,7 @@ import org.apache.commons.lang3.SerializationUtils;
import org.eclipse.jgit.errors.RevisionSyntaxException;
import org.eclipse.jgit.internal.storage.file.FileRepository;
import org.eclipse.jgit.lib.AnyObjectId;
+import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.FileMode;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.ObjectLoader;
@@ -59,6 +60,7 @@ import com.pmease.commons.git.Commit;
import com.pmease.commons.git.Git;
import com.pmease.commons.git.GitUtils;
import com.pmease.commons.git.Submodule;
+import com.pmease.commons.git.exception.NotFileException;
import com.pmease.commons.git.exception.ObjectNotExistException;
import com.pmease.commons.hibernate.AbstractEntity;
import com.pmease.commons.loader.AppLoader;
@@ -128,15 +130,15 @@ public class Repository extends AbstractEntity implements UserBelonging {
@OneToMany(mappedBy="forkedFrom")
private Collection<Repository> forks = new ArrayList<>();
- private transient Map<BlobIdent, Blob> blobCache = new HashMap<>();
+ private transient Map<BlobIdent, Blob> blobCache;
- private transient Map<CommitRange, List<Change>> changesCache = new HashMap<>();
+ private transient Map<CommitRange, List<Change>> changesCache;
- private transient Map<String, Commit> commitCache = new HashMap<>();
+ private transient Map<String, Commit> commitCache;
- private transient Map<String, Optional<ObjectId>> objectIdCache = new HashMap<>();
+ private transient Map<String, Optional<ObjectId>> objectIdCache;
- private transient Map<String, Map<String, Ref>> refsCache = new HashMap<>();
+ private transient Map<String, Map<String, Ref>> refsCache;
private transient Collection<String> branches;
@@ -382,6 +384,21 @@ public class Repository extends AbstractEntity implements UserBelonging {
return getDefaultBranch();
}
+ private Blob readBlob(ObjectLoader objectLoader, BlobIdent ident) {
+ long blobSize = objectLoader.getSize();
+ if (blobSize > MAX_READ_BLOB_SIZE) {
+ try (InputStream is = objectLoader.openStream()) {
+ byte[] bytes = new byte[MAX_READ_BLOB_SIZE];
+ is.read(bytes);
+ return new Blob(ident, bytes, blobSize);
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+ } else {
+ return new Blob(ident, objectLoader.getCachedBytes());
+ }
+ }
+
/**
* Read blob content and cache result in repository in case the same blob
* content is requested again.
@@ -398,38 +415,54 @@ public class Repository extends AbstractEntity implements UserBelonging {
Preconditions.checkArgument(ident.revision!=null && ident.path!=null && ident.mode!=null,
"Revision, path and mode of ident param should be specified");
+ if (blobCache == null)
+ blobCache = new HashMap<>();
+
Blob blob = blobCache.get(ident);
if (blob == null) {
- try (FileRepository jgitRepo = openAsJGitRepo(); RevWalk revWalk = new RevWalk(jgitRepo)) {
- ObjectId commitId = getObjectId(ident.revision);
- RevTree revTree = revWalk.parseCommit(commitId).getTree();
- TreeWalk treeWalk = TreeWalk.forPath(jgitRepo, ident.path, revTree);
- if (treeWalk != null) {
+ if (ident.id != null) {
+ try (FileRepository jgitRepo = openAsJGitRepo()) {
if (ident.isGitLink()) {
String url = getSubmodules(ident.revision).get(ident.path);
if (url == null)
throw new ObjectNotExistException("Unable to find submodule '" + ident.path + "' in .gitmodules");
- String hash = treeWalk.getObjectId(0).name();
+ String hash = ident.id.name();
blob = new Blob(ident, new Submodule(url, hash).toString().getBytes());
+ } else if (ident.isTree()) {
+ throw new NotFileException("Path '" + ident.path + "' is a tree");
} else {
- ObjectLoader objectLoader = treeWalk.getObjectReader().open(treeWalk.getObjectId(0));
- long blobSize = objectLoader.getSize();
- if (blobSize > MAX_READ_BLOB_SIZE) {
- try (InputStream is = objectLoader.openStream()) {
- byte[] bytes = new byte[MAX_READ_BLOB_SIZE];
- is.read(bytes);
- blob = new Blob(ident, bytes, blobSize);
- }
+ ObjectLoader objectLoader = jgitRepo.open(ident.id, Constants.OBJ_BLOB);
+ blob = readBlob(objectLoader, ident);
+ }
+ blobCache.put(ident, blob);
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+ } else {
+ try (FileRepository jgitRepo = openAsJGitRepo(); RevWalk revWalk = new RevWalk(jgitRepo)) {
+ ObjectId commitId = getObjectId(ident.revision);
+ RevTree revTree = revWalk.parseCommit(commitId).getTree();
+ TreeWalk treeWalk = TreeWalk.forPath(jgitRepo, ident.path, revTree);
+ if (treeWalk != null) {
+ if (ident.isGitLink()) {
+ String url = getSubmodules(ident.revision).get(ident.path);
+ if (url == null)
+ throw new ObjectNotExistException("Unable to find submodule '" + ident.path + "' in .gitmodules");
+ String hash = treeWalk.getObjectId(0).name();
+ blob = new Blob(ident, new Submodule(url, hash).toString().getBytes());
+ } else if (ident.isTree()) {
+ throw new NotFileException("Path '" + ident.path + "' is a tree");
} else {
- blob = new Blob(ident, objectLoader.getCachedBytes());
+ ObjectLoader objectLoader = treeWalk.getObjectReader().open(treeWalk.getObjectId(0));
+ blob = readBlob(objectLoader, ident);
}
+ blobCache.put(ident, blob);
+ } else {
+ throw new ObjectNotExistException("Unable to find blob path '" + ident.path + "' in revision '" + ident.revision + "'");
}
- blobCache.put(ident, blob);
- } else {
- throw new ObjectNotExistException("Unable to find blob path '" + ident.path + "' in revision '" + ident.revision + "'");
+ } catch (IOException e) {
+ throw new RuntimeException(e);
}
- } catch (IOException e) {
- throw new RuntimeException(e);
}
}
return blob;
@@ -465,6 +498,9 @@ public class Repository extends AbstractEntity implements UserBelonging {
*/
@Nullable
public ObjectId getObjectId(String revision, boolean mustExist) {
+ if (objectIdCache == null)
+ objectIdCache = new HashMap<>();
+
Optional<ObjectId> optional = objectIdCache.get(revision);
if (optional == null) {
try (FileRepository jgitRepo = openAsJGitRepo()) {
@@ -485,10 +521,16 @@ public class Repository extends AbstractEntity implements UserBelonging {
}
public void cacheObjectId(String revision, @Nullable ObjectId objectId) {
+ if (objectIdCache == null)
+ objectIdCache = new HashMap<>();
+
objectIdCache.put(revision, Optional.fromNullable(objectId));
}
public List<Change> getChanges(String fromCommit, String toCommit) {
+ if (changesCache == null)
+ changesCache = new HashMap<>();
+
CommitRange range = new CommitRange(fromCommit, toCommit);
List<Change> changes = changesCache.get(range);
if (changes == null) {
@@ -499,6 +541,9 @@ public class Repository extends AbstractEntity implements UserBelonging {
}
public Commit getCommit(String commitHash) {
+ if (commitCache == null)
+ commitCache = new HashMap<>();
+
Commit commit = commitCache.get(commitHash);
if (commit == null) {
commit = git().showRevision(commitHash);
@@ -508,6 +553,9 @@ public class Repository extends AbstractEntity implements UserBelonging {
}
public void cacheCommits(List<Commit> commits) {
+ if (commitCache == null)
+ commitCache = new HashMap<>();
+
for (Commit commit: commits)
commitCache.put(commit.getHash(), commit);
}
@@ -605,6 +653,9 @@ public class Repository extends AbstractEntity implements UserBelonging {
}
public Map<String, Ref> getRefs(String prefix) {
+ if (refsCache == null)
+ refsCache = new HashMap<>();
+
Map<String, Ref> cached = refsCache.get(prefix);
if (cached == null) {
try (FileRepository jgitRepo = openAsJGitRepo()) {
diff --git a/gitplex.product/src/test/java/gitplex/product/Test.java b/gitplex.product/src/test/java/gitplex/product/Test.java
index 7030a181b2..e04b2bf2a7 100644
--- a/gitplex.product/src/test/java/gitplex/product/Test.java
+++ b/gitplex.product/src/test/java/gitplex/product/Test.java
@@ -1,20 +1,10 @@
package gitplex.product;
import java.io.IOException;
-import java.util.ArrayList;
-import java.util.List;
-
-import com.google.common.collect.Lists;
-import com.pmease.commons.lang.tokenizers.CmToken;
public class Test {
@org.junit.Test
public void test() throws IOException {
- List<List<CmToken>> lines = new ArrayList<>();
- lines.add(Lists.newArrayList(new CmToken("comment", "hello"), new CmToken("comment", "world")));
- lines.add(Lists.newArrayList(new CmToken("keyword", "for"), new CmToken("keyword", "while")));
- System.out.println(lines);
}
-
}
\ No newline at end of file
diff --git a/gitplex.web/src/main/java/com/pmease/gitplex/web/component/diff/BlobChange.java b/gitplex.web/src/main/java/com/pmease/gitplex/web/component/diff/BlobChange.java
new file mode 100644
index 0000000000..46a8bda68f
--- /dev/null
+++ b/gitplex.web/src/main/java/com/pmease/gitplex/web/component/diff/BlobChange.java
@@ -0,0 +1,99 @@
+package com.pmease.gitplex.web.component.diff;
+
+import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.eclipse.jgit.diff.DiffEntry;
+import org.eclipse.jgit.diff.DiffEntry.ChangeType;
+
+import com.pmease.commons.git.Blob;
+import com.pmease.commons.git.BlobIdent;
+import com.pmease.commons.git.LineProcessor;
+import com.pmease.commons.lang.diff.DiffBlock;
+import com.pmease.commons.lang.diff.DiffUtils;
+import com.pmease.gitplex.core.model.Repository;
+
+@SuppressWarnings("serial")
+public abstract class BlobChange implements Serializable {
+
+ private final ChangeType changeType;
+
+ private final BlobIdent oldBlobIdent;
+
+ private final BlobIdent newBlobIdent;
+
+ private transient List<DiffBlock> diffs;
+
+ public BlobChange(String oldCommitHash, String newCommitHash, DiffEntry diffEntry) {
+ changeType = diffEntry.getChangeType();
+ if (changeType != ChangeType.DELETE) {
+ newBlobIdent = new BlobIdent(newCommitHash, diffEntry.getNewPath(), diffEntry.getNewMode().getBits());
+ newBlobIdent.id = diffEntry.getNewId().toObjectId();
+ } else {
+ newBlobIdent = new BlobIdent(newCommitHash, null, 0);
+ }
+ if (changeType != ChangeType.ADD) {
+ oldBlobIdent = new BlobIdent(oldCommitHash, diffEntry.getOldPath(), diffEntry.getOldMode().getBits());
+ oldBlobIdent.id = diffEntry.getOldId().toObjectId();
+ } else {
+ oldBlobIdent = new BlobIdent(oldCommitHash, null, null);
+ }
+ }
+
+ public ChangeType getChangeType() {
+ return changeType;
+ }
+
+ public BlobIdent getOldBlobIdent() {
+ return oldBlobIdent;
+ }
+
+ public BlobIdent getNewBlobIdent() {
+ return newBlobIdent;
+ }
+
+ public List<DiffBlock> getDiffs() {
+ if (diffs == null) {
+ if (changeType == ChangeType.ADD) {
+ Blob newBlob = getRepository().getBlob(newBlobIdent);
+ if (newBlob.getText() != null) {
+ List<String> newLines = newBlob.getText().getLines(getLineProcessor());
+ List<String> oldLines = new ArrayList<>();
+ diffs = DiffUtils.diff(oldLines, null, newLines, newBlobIdent.getFileName());
+ } else {
+ diffs = new ArrayList<>();
+ }
+ } else if (changeType == ChangeType.DELETE) {
+ Blob oldBlob = getRepository().getBlob(oldBlobIdent);
+ if (oldBlob.getText() != null) {
+ List<String> oldLines = oldBlob.getText().getLines(getLineProcessor());
+ List<String> newLines = new ArrayList<>();
+ diffs = DiffUtils.diff(oldLines, oldBlobIdent.getFileName(), newLines, null);
+ } else {
+ diffs = new ArrayList<>();
+ }
+ } else {
+ Blob oldBlob = getRepository().getBlob(oldBlobIdent);
+ Blob newBlob = getRepository().getBlob(newBlobIdent);
+ if (oldBlob.getText() != null && newBlob.getText() != null) {
+ List<String> oldLines = oldBlob.getText().getLines(getLineProcessor());
+ List<String> newLines = newBlob.getText().getLines(getLineProcessor());
+ diffs = DiffUtils.diff(oldLines, oldBlobIdent.getFileName(), newLines, newBlobIdent.getFileName());
+ } else {
+ diffs = new ArrayList<>();
+ }
+ }
+ }
+ return diffs;
+ }
+
+ public void setDiffs(List<DiffBlock> diffs) {
+ this.diffs = diffs;
+ }
+
+ protected abstract Repository getRepository();
+
+ protected abstract LineProcessor getLineProcessor();
+
+}
diff --git a/gitplex.web/src/main/java/com/pmease/gitplex/web/component/diff/DiffStatBar.java b/gitplex.web/src/main/java/com/pmease/gitplex/web/component/diff/DiffStatBar.java
index a359bd55ff..a08d946eb0 100644
--- a/gitplex.web/src/main/java/com/pmease/gitplex/web/component/diff/DiffStatBar.java
+++ b/gitplex.web/src/main/java/com/pmease/gitplex/web/component/diff/DiffStatBar.java
@@ -8,7 +8,8 @@ import org.apache.wicket.model.AbstractReadOnlyModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.LoadableDetachableModel;
-import com.pmease.commons.util.diff.DiffLine;
+import com.pmease.commons.lang.diff.DiffBlock;
+import com.pmease.commons.lang.diff.DiffMatchPatch.Operation;
import com.pmease.commons.wicket.behavior.TooltipBehavior;
import de.agilecoders.wicket.core.markup.html.bootstrap.components.TooltipConfig;
@@ -19,44 +20,40 @@ public class DiffStatBar extends Panel {
private static final int MAX_BLOCKS = 8;
- private IModel<List<DiffLine>> diffsModel;
+ private final IModel<List<DiffBlock>> diffsModel;
- private IModel<Integer> additionsModel;
+ private final IModel<Integer> additionsModel = new LoadableDetachableModel<Integer>() {
+
+ @Override
+ protected Integer load() {
+ int additions = 0;
+ for (DiffBlock diff: diffsModel.getObject()) {
+ if (diff.getOperation() == Operation.INSERT)
+ additions++;
+ }
+ return additions;
+ }
+
+ };
- private IModel<Integer> deletionsModel;
+ private final IModel<Integer> deletionsModel = new LoadableDetachableModel<Integer>() {
+
+ @Override
+ protected Integer load() {
+ int deletions = 0;
+ for (DiffBlock diff: diffsModel.getObject()) {
+ if (diff.getOperation() == Operation.DELETE)
+ deletions++;
+ }
+ return deletions;
+ }
+
+ };
- public DiffStatBar(String id, IModel<List<DiffLine>> diffsModel) {
+ public DiffStatBar(String id, IModel<List<DiffBlock>> diffsModel) {
super(id);
this.diffsModel = diffsModel;
-
- additionsModel = new LoadableDetachableModel<Integer>() {
-
- @Override
- protected Integer load() {
- int additions = 0;
- for (DiffLine diff: DiffStatBar.this.diffsModel.getObject()) {
- if (diff.getAction() == DiffLine.Action.ADD)
- additions++;
- }
- return additions;
- }
-
- };
-
- deletionsModel = new LoadableDetachableModel<Integer>() {
-
- @Override
- protected Integer load() {
- int deletions = 0;
- for (DiffLine diff: DiffStatBar.this.diffsModel.getObject()) {
- if (diff.getAction() == DiffLine.Action.DELETE)
- deletions++;
- }
- return deletions;
- }
-
- };
}
@Override
diff --git a/gitplex.web/src/main/java/com/pmease/gitplex/web/component/diff/RevisionDiffPanel.java b/gitplex.web/src/main/java/com/pmease/gitplex/web/component/diff/RevisionDiffPanel.java
index 1053699b6e..b005a06b97 100644
--- a/gitplex.web/src/main/java/com/pmease/gitplex/web/component/diff/RevisionDiffPanel.java
+++ b/gitplex.web/src/main/java/com/pmease/gitplex/web/component/diff/RevisionDiffPanel.java
@@ -1,13 +1,23 @@
package com.pmease.gitplex.web.component.diff;
+import java.io.IOException;
import java.util.ArrayList;
+import java.util.Collection;
import java.util.List;
+import java.util.concurrent.Callable;
+import java.util.concurrent.ForkJoinPool;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.IModel;
+import org.apache.wicket.model.LoadableDetachableModel;
+import org.eclipse.jgit.diff.DiffEntry;
+import org.eclipse.jgit.diff.DiffFormatter;
+import org.eclipse.jgit.internal.storage.file.FileRepository;
+import org.eclipse.jgit.lib.AnyObjectId;
+import org.eclipse.jgit.util.io.NullOutputStream;
-import com.pmease.commons.git.Change;
-import com.pmease.commons.git.GitUtils;
+import com.pmease.commons.git.LineProcessor;
+import com.pmease.gitplex.core.GitPlex;
import com.pmease.gitplex.core.comment.InlineCommentSupport;
import com.pmease.gitplex.core.model.Repository;
@@ -22,18 +32,74 @@ public class RevisionDiffPanel extends Panel {
private final InlineCommentSupport commentSupport;
- private final List<Change> changes;
+ private LineProcessOption lineProcessor = LineProcessOption.IGNORE_NOTHING;
+
+ private IModel<List<BlobChange>> changesModel = new LoadableDetachableModel<List<BlobChange>>() {
+
+ @Override
+ protected List<BlobChange> load() {
+ List<BlobChange> changes = new ArrayList<>();
+ try ( FileRepository jgitRepo = repoModel.getObject().openAsJGitRepo();
+ DiffFormatter diffFormatter = new DiffFormatter(NullOutputStream.INSTANCE);) {
+ diffFormatter.setRepository(jgitRepo);
+ AnyObjectId oldCommitId = repoModel.getObject().getObjectId(oldRev);
+ AnyObjectId newCommitId = repoModel.getObject().getObjectId(newRev);
+ for (DiffEntry entry: diffFormatter.scan(oldCommitId, newCommitId)) {
+ changes.add(new BlobChange(oldCommitId.name(), newCommitId.name(), entry) {
+
+ @Override
+ protected Repository getRepository() {
+ return repoModel.getObject();
+ }
+
+ @Override
+ protected LineProcessor getLineProcessor() {
+ return lineProcessor;
+ }
+
+ });
+ }
+
+ // Diff calculation can be slow, so we pre-load diffs of each change
+ // concurrently
+ Collection<Callable<Void>> tasks = new ArrayList<>();
+ for (final BlobChange change: changes) {
+ // to avoid race conditions during concurrent diff calculation, we
+ // pre-populate blob data in repository
+ if (change.getOldBlobIdent().path != null)
+ repoModel.getObject().getBlob(change.getOldBlobIdent());
+ if (change.getNewBlobIdent().path != null)
+ repoModel.getObject().getBlob(change.getNewBlobIdent());
+
+ tasks.add(new Callable<Void>() {
+
+ @Override
+ public Void call() throws Exception {
+ change.getDiffs();
+ return null;
+ }
+
+ });
+ }
+
+ GitPlex.getInstance(ForkJoinPool.class).invokeAll(tasks);
+
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+ return changes;
+ }
+ };
public RevisionDiffPanel(String id, IModel<Repository> repoModel, String oldRev, String newRev,
InlineCommentSupport commentSupport) {
super(id);
- this.repoModel = repoModel;
this.oldRev = oldRev;
this.newRev = newRev;
+ this.repoModel = repoModel;
this.commentSupport = commentSupport;
- changes = GitUtils.getChanges(oldRev, newRev);
}
@Override
@@ -44,6 +110,7 @@ public class RevisionDiffPanel extends Panel {
@Override
protected void onDetach() {
+ changesModel.detach();
repoModel.detach();
super.onDetach();
diff --git a/gitplex.web/src/main/java/com/pmease/gitplex/web/component/diff/TextDiffPanel.java b/gitplex.web/src/main/java/com/pmease/gitplex/web/component/diff/TextDiffPanel.java
index d0391823a1..e8ac0fbc44 100644
--- a/gitplex.web/src/main/java/com/pmease/gitplex/web/component/diff/TextDiffPanel.java
+++ b/gitplex.web/src/main/java/com/pmease/gitplex/web/component/diff/TextDiffPanel.java
@@ -213,6 +213,7 @@ public class TextDiffPanel extends Panel {
head.add(new StickyBehavior());
+ /*
head.add(new DiffStatBar("diffStat", new AbstractReadOnlyModel<List<DiffLine>>() {
@Override
@@ -221,6 +222,7 @@ public class TextDiffPanel extends Panel {
}
}));
+ */
add(head);
head.add(new FileDiffTitle("title", change));