Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to 'org.eclipse.egit.core/src/org/eclipse/egit/core/synchronize/GitCommitsModelCache.java')
-rw-r--r--org.eclipse.egit.core/src/org/eclipse/egit/core/synchronize/GitCommitsModelCache.java126
1 files changed, 102 insertions, 24 deletions
diff --git a/org.eclipse.egit.core/src/org/eclipse/egit/core/synchronize/GitCommitsModelCache.java b/org.eclipse.egit.core/src/org/eclipse/egit/core/synchronize/GitCommitsModelCache.java
index 04dd4ac00e..79c2e91b6a 100644
--- a/org.eclipse.egit.core/src/org/eclipse/egit/core/synchronize/GitCommitsModelCache.java
+++ b/org.eclipse.egit.core/src/org/eclipse/egit/core/synchronize/GitCommitsModelCache.java
@@ -25,7 +25,9 @@ import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.revwalk.RevFlag;
import org.eclipse.jgit.revwalk.RevFlagSet;
import org.eclipse.jgit.revwalk.RevWalk;
+import org.eclipse.jgit.treewalk.EmptyTreeIterator;
import org.eclipse.jgit.treewalk.TreeWalk;
+import org.eclipse.jgit.treewalk.filter.AndTreeFilter;
import org.eclipse.jgit.treewalk.filter.TreeFilter;
/**
@@ -244,6 +246,43 @@ public class GitCommitsModelCache {
return true;
}
+ @Override
+ public String toString() {
+ StringBuilder change = new StringBuilder("Change("); //$NON-NLS-1$
+ if ((kind & LEFT) != 0)
+ change.append("OUTGOING "); //$NON-NLS-1$
+ else
+ // should be RIGHT
+ change.append("INCOMING "); //$NON-NLS-1$
+ if ((kind & ADDITION) != 0)
+ change.append("ADDITION "); //$NON-NLS-1$
+ else if ((kind & DELETION) != 0)
+ change.append("DELETION "); //$NON-NLS-1$
+ else
+ // should be CHANGE
+ change.append("CHANGE "); //$NON-NLS-1$
+
+ change.append(name);
+ change.append(";\n\tcurrent objectId: "); //$NON-NLS-1$
+ change.append(getObjectId(objectId));
+ change.append(";\n\tparent objectId: "); //$NON-NLS-1$
+ change.append(getObjectId(remoteObjectId));
+ change.append(";\n\tcurrent commit: "); //$NON-NLS-1$
+ change.append(getObjectId(commitId));
+ change.append(";\n\tparent commit: "); //$NON-NLS-1$
+ change.append(remoteCommitId.toObjectId().getName());
+ change.append("\n)"); //$NON-NLS-1$
+
+ return change.toString();
+ }
+
+ private String getObjectId(AbbreviatedObjectId object) {
+ if (object != null)
+ return object.toObjectId().getName();
+ else
+ return ObjectId.zeroId().getName();
+ }
+
}
static final AbbreviatedObjectId ZERO_ID = AbbreviatedObjectId
@@ -259,12 +298,15 @@ public class GitCommitsModelCache {
* RevCommit id that git history traverse will start from
* @param dstId
* RevCommit id that git history traverse will end
+ * @param pathFilter
+ * path filter definition or {@code null} when all paths should
+ * be included
* @return list of {@link Commit} object's between {@code srcId} and
* {@code dstId}
* @throws IOException
*/
public static List<Commit> build(Repository repo, ObjectId srcId,
- ObjectId dstId) throws IOException {
+ ObjectId dstId, TreeFilter pathFilter) throws IOException {
if (dstId.equals(srcId))
return new ArrayList<Commit>(0);
@@ -287,6 +329,9 @@ public class GitCommitsModelCache {
rw.markStart(dstCommit);
dstCommit = null; // free not needed resources
+ if (pathFilter != null)
+ rw.setTreeFilter(pathFilter);
+
List<Commit> result = new ArrayList<Commit>();
for (RevCommit revCommit : rw) {
if (revCommit.hasAll(allFlags))
@@ -299,41 +344,54 @@ public class GitCommitsModelCache {
commit.committerName = revCommit.getCommitterIdent().getName();
commit.commitDate = revCommit.getAuthorIdent().getWhen();
- if (revCommit.has(localFlag))
+ RevCommit actualCommit, parentCommit;
+ if (revCommit.has(localFlag)) {
+ actualCommit = revCommit;
+ parentCommit = getParentCommit(revCommit);
commit.direction = RIGHT;
- else if (revCommit.has(remoteFlag))
+ } else if (revCommit.has(remoteFlag)) {
+ actualCommit = getParentCommit(revCommit);
+ parentCommit = revCommit;
commit.direction = LEFT;
- else
+ } else
throw new GitCommitsModelDirectionException();
- RevCommit[] parents = revCommit.getParents();
- if (parents.length == 1) // don't show changes in merge commits
- commit.children = getChangedObjects(repo, revCommit,
- parents[0], commit.direction);
+ commit.children = getChangedObjects(repo, actualCommit,
+ parentCommit, pathFilter, commit.direction);
- result.add(commit);
+ if (commit.children != null)
+ result.add(commit);
}
rw.dispose();
return result;
}
+ private static RevCommit getParentCommit(RevCommit commit) {
+ if (commit.getParents().length > 0)
+ return commit.getParents()[0];
+ else
+ return null;
+ }
+
private static Map<String, Change> getChangedObjects(Repository repo,
- RevCommit parentCommit, RevCommit remoteCommit, final int direction)
- throws IOException {
+ RevCommit parentCommit, RevCommit remoteCommit,
+ TreeFilter pathFilter, final int direction) throws IOException {
final TreeWalk tw = new TreeWalk(repo);
- tw.addTree(parentCommit.getTree());
- tw.addTree(remoteCommit.getTree());
- tw.setFilter(TreeFilter.ANY_DIFF);
+ addTreeFilter(tw, parentCommit);
+ addTreeFilter(tw, remoteCommit);
+
tw.setRecursive(true);
+ if (pathFilter == null)
+ tw.setFilter(TreeFilter.ANY_DIFF);
+ else
+ tw.setFilter(AndTreeFilter.create(TreeFilter.ANY_DIFF, pathFilter));
final int localTreeId = direction == LEFT ? 1 : 0;
final int remoteTreeId = direction == LEFT ? 0 : 1;
final Map<String, Change> result = new HashMap<String, GitCommitsModelCache.Change>();
- final AbbreviatedObjectId actualCommit = AbbreviatedObjectId
- .fromObjectId(parentCommit);
- final AbbreviatedObjectId remoteCommitAbb = AbbreviatedObjectId
- .fromObjectId(remoteCommit);
+ final AbbreviatedObjectId actualCommit = getAbbreviatedObjectId(parentCommit);
+ final AbbreviatedObjectId remoteCommitAbb = getAbbreviatedObjectId(remoteCommit);
MutableObjectId idBuf = new MutableObjectId();
while (tw.next()) {
@@ -355,14 +413,34 @@ public class GitCommitsModelCache {
return result.size() > 0 ? result : null;
}
- static void calculateAndSetChangeKind(final int direction,
- Change change) {
- if (ZERO_ID.equals(change.objectId)) {
+ private static void addTreeFilter(TreeWalk tw, RevCommit commit)
+ throws IOException {
+ if (commit != null)
+ tw.addTree(commit.getTree());
+ else
+ tw.addTree(new EmptyTreeIterator());
+ }
+
+ private static AbbreviatedObjectId getAbbreviatedObjectId(RevCommit commit) {
+ if (commit != null)
+ return AbbreviatedObjectId.fromObjectId(commit);
+ else
+ return ZERO_ID;
+ }
+
+ static void calculateAndSetChangeKind(final int direction, Change change) {
+ if (ZERO_ID.equals(change.objectId)) { // missing locally
change.objectId = null; // clear zero id;
- change.kind = direction | DELETION;
- } else if (ZERO_ID.equals(change.remoteObjectId)) {
+ if (direction == LEFT)
+ change.kind = direction | ADDITION;
+ else // should be Differencer.RIGHT
+ change.kind = direction | DELETION;
+ } else if (ZERO_ID.equals(change.remoteObjectId)) { // missing remotely
change.remoteObjectId = null; // clear zero id;
- change.kind = direction | ADDITION;
+ if (direction == LEFT)
+ change.kind = direction | DELETION;
+ else // should be Differencer.RIGHT
+ change.kind = direction | ADDITION;
} else
change.kind = direction | CHANGE;
}

Back to the top