Skip to contentSkip to conversation

Eclipse (EGIT) Structured compare

PATCH In review Robin Rosenberg 2 participants 6 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

cover Eclipse (EGIT) Structured compare

Now Eclipse can compare two git version at the project level. Thanks to the SHA-1's the compare is very quick. There is some extra cost for version managed archives (zip/jars) as Eclipse insists on comparing the contents of those at the file level too. Not that I mind that. It is useful and cool. (No, it doesn't compare open office documents in any useful manner, unless you happen to have a plugin for it, in which case it might actually work.)

Along with this some experiments with caching and a minor bug fix in the commit reader.

If anyone besides me actually uses this, please send a comment.

-- robin

#1 lore raw

PATCH 1/4 Cache tree ObjectId's too

The caching part is experimental. We explicitly leave it up to the garbage collector when to recycle git objects. After trying this practically we will se what is best. The upside is that rescanning the history for a certain resource is very quick when data is still in the cache (tens of milliseconds), which is very good for interactivity. When memory is tight we go to disk and fetch the data, which could take a couple of seconds on a large repo.

---

 .../src/org/spearce/jgit/lib/Repository.java       |   18 +++++++++++++-----
 1 files changed, 13 insertions(+), 5 deletions(-)
org.spearce.jgit/src/org/spearce/jgit/lib/Repository.java +13 -5
index 8b1f42c..d3b2178 100644
@@ -47,7 +47,8 @@ public class Repository {
 
 	private WindowCache windows;
 
-	private Map cache = new WeakHashMap(30000); 
+	private Map treeCache = new WeakHashMap(30000); 
+	private Map commitCache = new WeakHashMap(30000); 
 
 	public Repository(final File d) throws IOException {
 		gitDir = d.getAbsoluteFile();
@@ -189,7 +190,7 @@ public class Repository {
 	}
 
 	public Commit mapCommit(final ObjectId id) throws IOException {
-		Commit ret = (Commit)cache.get(id);
+		Commit ret = (Commit)commitCache.get(id);
 		if (ret != null)
 			return ret;
 
@@ -201,7 +202,7 @@ public class Repository {
 			ret = new Commit(this, id, raw);
 			// The key must not be the referenced strongly
 			// by the value in WeakHashMaps
-			cache.put(new ObjectId(id.getBytes()), ret);
+			commitCache.put(new ObjectId(id.getBytes()), ret);
 			return ret;
 		}
 		throw new IncorrectObjectTypeException(id, Constants.TYPE_COMMIT);
@@ -213,12 +214,19 @@ public class Repository {
 	}
 
 	public Tree mapTree(final ObjectId id) throws IOException {
+		Tree ret = (Tree)treeCache.get(id);
+		if (ret != null)
+			return ret;
+
 		final ObjectLoader or = openObject(id);
 		if (or == null)
 			return null;
 		final byte[] raw = or.getBytes();
-		if (Constants.TYPE_TREE.equals(or.getType()))
-			return new Tree(this, id, raw);
+		if (Constants.TYPE_TREE.equals(or.getType())) {
+			ret = new Tree(this, id, raw);
+			treeCache.put(new ObjectId(id.getBytes()), ret);
+			return ret;
+		}
 		if (Constants.TYPE_COMMIT.equals(or.getType()))
 			return mapTree(ObjectId.fromString(raw, 5));
 		throw new IncorrectObjectTypeException(id, Constants.TYPE_TREE);
#2 lore raw

PATCH 2/4 Rework tree/commit cache

In reply to Robin Rosenberg

We now use SoftRefernces to hold the value objects and reuse the object id's as cache keys. Seems to work much better, i.e. the gc only throws out stuff from the cache when memory is tight, rather than at the first opportunity.

---

 .../src/org/spearce/jgit/lib/Repository.java       |   30 +++++++++++++++--------
 1 files changed, 20 insertions(+), 10 deletions(-)
org.spearce.jgit/src/org/spearce/jgit/lib/Repository.java +20 -10
index d3b2178..c7c47ae 100644
@@ -23,6 +23,8 @@ import java.io.FileNotFoundException;
 import java.io.FileReader;
 import java.io.FileWriter;
 import java.io.IOException;
+import java.lang.ref.Reference;
+import java.lang.ref.SoftReference;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Map;
@@ -190,19 +192,24 @@ public class Repository {
 	}
 
 	public Commit mapCommit(final ObjectId id) throws IOException {
-		Commit ret = (Commit)commitCache.get(id);
-		if (ret != null)
-			return ret;
+//		System.out.println("commitcache.size="+commitCache.size());
+		Reference retr = (Reference)commitCache.get(id);
+		if (retr != null) {
+			Commit ret = (Commit)retr.get();
+			if (ret != null)
+				return ret;
+			System.out.println("Found a null id, size was "+commitCache.size());
+		}
 
 		final ObjectLoader or = openObject(id);
 		if (or == null)
 			return null;
 		final byte[] raw = or.getBytes();
 		if (Constants.TYPE_COMMIT.equals(or.getType())) {
-			ret = new Commit(this, id, raw);
+			Commit ret = new Commit(this, id, raw);
 			// The key must not be the referenced strongly
 			// by the value in WeakHashMaps
-			commitCache.put(new ObjectId(id.getBytes()), ret);
+			commitCache.put(id, new SoftReference(ret));
 			return ret;
 		}
 		throw new IncorrectObjectTypeException(id, Constants.TYPE_COMMIT);
@@ -214,17 +221,20 @@ public class Repository {
 	}
 
 	public Tree mapTree(final ObjectId id) throws IOException {
-		Tree ret = (Tree)treeCache.get(id);
-		if (ret != null)
-			return ret;
+		Reference wret = (Reference)treeCache.get(id);
+		if (wret != null) {
+			Tree ret = (Tree)wret.get();
+			if (ret != null)
+				return ret;
+		}
 
 		final ObjectLoader or = openObject(id);
 		if (or == null)
 			return null;
 		final byte[] raw = or.getBytes();
 		if (Constants.TYPE_TREE.equals(or.getType())) {
-			ret = new Tree(this, id, raw);
-			treeCache.put(new ObjectId(id.getBytes()), ret);
+			Tree ret = new Tree(this, id, raw);
+			treeCache.put(id, new SoftReference(ret));
 			return ret;
 		}
 		if (Constants.TYPE_COMMIT.equals(or.getType()))
#3 lore raw

PATCH 3/4 Decode message when encoding line is present

In reply to Robin Rosenberg

Seems we left the newline between the encoding and the rest of the message

---

 .../src/org/spearce/jgit/lib/Commit.java           |    6 ++++--
 1 files changed, 4 insertions(+), 2 deletions(-)
org.spearce.jgit/src/org/spearce/jgit/lib/Commit.java +4 -2
index 4356149..c5c65c4 100644
@@ -161,16 +161,18 @@ public class Commit implements Treeish {
 				}
 				byte[] readBuf = new byte[br.available()]; // in-memory stream so this is all bytes left
 				br.read(readBuf);
+				int msgstart = readBuf[0] == '\n' ? 1 : 0;
+					
 				if (encoding != null) {
 					// TODO: this isn't reliable so we need to guess the encoding from the actual content
 					author = new PersonIdent(new String(rawAuthor.getBytes(),encoding.name()));
 					committer = new PersonIdent(new String(rawCommitter.getBytes(),encoding.name()));
-					message = new String(readBuf,encoding.name());
+					message = new String(readBuf,msgstart, readBuf.length-msgstart, encoding.name());
 				} else {
 					// TODO: use config setting / platform / ascii / iso-latin
 					author = new PersonIdent(new String(rawAuthor.getBytes()));
 					committer = new PersonIdent(new String(rawCommitter.getBytes()));
-					message = new String(readBuf);
+					message = new String(readBuf, msgstart, readBuf.length-msgstart);
 				}
 			} catch (IOException e) {
 				e.printStackTrace();
#4 lore raw

PATCH 4/4 Add support for structured comparison.

In reply to Robin Rosenberg

With this code we can now compare revisions at project and folder levels, rather than only file level. We use the compare api's that eclipse provide to do the display and detailed compare work, while we do the high level comparisons at the SHA-1 level.

A possible downside is that the API:s we use are internal, which could make us version dependent. This code works with both 3.2.x and 3.3M5 versions though.

---

 org.spearce.egit.core/META-INF/MANIFEST.MF         |    6 
 .../src/org/spearce/egit/core/GitBlobStorage.java  |  108 +++++
 .../src/org/spearce/egit/core/GitTreeStorage.java  |  484 +++++++++++++++++++++++
 .../egit/core/internal/mapping/GitFileHistory.java |    8 
 .../core/internal/mapping/GitFileRevision.java     |   25 +
 .../core/internal/mapping/GitResourceNode.java     |   97 +++++
 org.spearce.egit.ui/plugin.xml                     |   16 -
 .../src/org/spearce/egit/ui/GitHistoryPage.java    |   11 -
 .../GitCompareFileRevisionEditorInput.java         |  305 ++++++++++++++
 .../internal/actions/GitCompareRevisionAction.java |  228 +++++++++++
 10 files changed, 1268 insertions(+), 20 deletions(-)
org.spearce.egit.core/META-INF/MANIFEST.MF +5 -1
index f4c96cc..8dc62d6 100644
@@ -10,8 +10,12 @@ Require-Bundle: org.eclipse.core.runtime,
  org.eclipse.team.core,
  org.eclipse.core.resources,
  org.spearce.jgit,
- org.eclipse.core.filesystem
+ org.eclipse.core.filesystem,
+ org.eclipse.compare,
+ org.eclipse.swt
 Export-Package: org.spearce.egit.core.internal.mapping;x-friends:="org.spearce.egit.ui",
+ org.spearce.egit.core,
+ org.spearce.egit.core.internal.mapping,
  org.spearce.egit.core.op,
  org.spearce.egit.core.project
 Eclipse-LazyStart: true
org.spearce.egit.core/src/org/spearce/egit/core/GitBlobStorage.java new file +108 -0

108 lines not on this page · raw message

org.spearce.egit.core/src/org/spearce/egit/core/GitTreeStorage.java new file +484 -0

484 lines not on this page · raw message

org.spearce.egit.core/src/org/spearce/egit/core/internal/mapping/GitFileHistory.java +7 -1
index 78a2e68..51109fb 100644
@@ -28,6 +28,7 @@ import java.util.List;
 
 import org.eclipse.core.resources.IResource;
 import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IAdaptable;
 import org.eclipse.team.core.RepositoryProvider;
 import org.eclipse.team.core.history.IFileHistoryProvider;
 import org.eclipse.team.core.history.IFileRevision;
@@ -42,7 +43,7 @@ import org.spearce.jgit.lib.Repository;
 import org.spearce.jgit.lib.Tree;
 import org.spearce.jgit.lib.TreeEntry;
 
-public class GitFileHistory extends FileHistory {
+public class GitFileHistory extends FileHistory implements IAdaptable {
 
 	private final IResource resource;
 
@@ -341,4 +342,9 @@ public class GitFileHistory extends FileHistory {
 		return (IFileRevision[]) ret.toArray(new IFileRevision[ret.size()]);
 	}
 
+	public Object getAdapter(Class adapter) {
+		System.out.println("GitFileHistory.getAdapter "+adapter.getName());
+		return null;
+	}
+
 }
org.spearce.egit.core/src/org/spearce/egit/core/internal/mapping/GitFileRevision.java +25 -0
index ad15cc0..7b726f7 100644
@@ -33,10 +33,13 @@ import org.eclipse.team.core.history.provider.FileRevision;
 import org.spearce.egit.core.GitProvider;
 import org.spearce.egit.core.GitStorage;
 import org.spearce.egit.core.GitTag;
+import org.spearce.egit.core.project.RepositoryMapping;
 import org.spearce.jgit.lib.Commit;
 import org.spearce.jgit.lib.ObjectId;
 import org.spearce.jgit.lib.PersonIdent;
 import org.spearce.jgit.lib.Repository;
+import org.spearce.jgit.lib.Tree;
+import org.spearce.jgit.lib.TreeEntry;
 
 public class GitFileRevision extends FileRevision {
 
@@ -136,4 +139,26 @@ public class GitFileRevision extends FileRevision {
 	public int getCount() {
 		return count;
 	}
+
+	public TreeEntry getTreeEntry() {
+		GitProvider provider = (GitProvider) RepositoryProvider
+				.getProvider(resource.getProject());
+		RepositoryMapping repositoryMapping = provider.getData()
+				.getRepositoryMapping(resource.getProject());
+		Tree tree;
+		try {
+			tree = repositoryMapping.getRepository().mapTree(getCommit().getTreeId());
+			String prefix = repositoryMapping.getSubset();
+			if (prefix != null) {
+				prefix = prefix + "/";
+				String name = prefix + resource.getProjectRelativePath().toString();
+				return tree.findMember(name);
+			} else
+				return tree;
+		} catch (IOException e) {
+			// TODO Auto-generated catch block
+			e.printStackTrace();
+		}
+		return null;
+	}
 }
org.spearce.egit.core/src/org/spearce/egit/core/internal/mapping/GitResourceNode.java new file +97 -0

97 lines not on this page · raw message

org.spearce.egit.ui/plugin.xml +0 -16
index d81a0b6..3febce8 100644
@@ -56,12 +56,6 @@
                label="%ShowResourceInHistoryAction_label"
                menubarPath="team.main/group1"
                tooltip="%ShowResourceInHistoryAction_tooltip"/>
-		<action
-		       class="org.spearce.egit.ui.internal.actions.GitCompareRevisionAction"
-        	   id="org.spearce.egit.ui.internal.actions.GitCompareAction"
-               menubarPath="team.main/group1"
-        	   label="Structured Git Compare">
-		</action>
  	  </objectContribution>
       <objectContribution
             objectClass="org.eclipse.core.resources.IFile"
@@ -70,16 +64,6 @@
          <filter name="projectPersistentProperty"
                value="org.eclipse.team.core.repository=org.spearce.egit.core.GitProvider">
          </filter>
-         <action
-               class="org.spearce.egit.ui.internal.actions.CompareWithRevisionAction"
-               definitionId="org.spearce.egit.ui.compareWithRevision"
-               helpContextId="org.spearce.egit.ui.compare_with_revision_action_context"
-               id="org.spearce.egit.ui.compareWithRevision"
-               label="%CompareWithRevisionAction_label"
-               menubarPath="compareWithMenu/compareWithGroup"
-               overrideActionId="org.eclipse.team.ui.compareLocalHistory"
-               tooltip="%CompareWithRevisionAction_tooltip">
-         </action>
       </objectContribution>
    </extension>
 
org.spearce.egit.ui/src/org/spearce/egit/ui/GitHistoryPage.java +9 -2
index 54635f5..3057344 100644
@@ -63,6 +63,7 @@ import org.eclipse.team.ui.history.HistoryPage;
 import org.eclipse.team.ui.history.IHistoryCompareAdapter;
 import org.eclipse.team.ui.history.IHistoryPageSite;
 import org.spearce.egit.core.internal.mapping.GitFileRevision;
+import org.spearce.egit.ui.internal.actions.GitCompareRevisionAction;
 
 public class GitHistoryPage extends HistoryPage implements IAdaptable,
 		IHistoryCompareAdapter {
@@ -104,6 +105,8 @@ public class GitHistoryPage extends HistoryPage implements IAdaptable,
 
 		final CompareRevisionAction compareAction = new CompareRevisionAction(
 				"Compare");
+		final GitCompareRevisionAction compareActionNG = new GitCompareRevisionAction(
+				"Compare NG");
 		tree.addSelectionListener(new SelectionAdapter() {
 			public void widgetSelected(SelectionEvent e) {
 				// update the current
@@ -116,16 +119,20 @@ public class GitHistoryPage extends HistoryPage implements IAdaptable,
 				compareAction.setCurrentFileRevision(fileRevisions[0]);
 				compareAction.selectionChanged(new StructuredSelection(
 						selection2));
-				
-				compareAction.setEnabled(selection!=null && selection[0]!=null  && ((GitFileRevision)fileRevisions[0]).getResource().getType() == IResource.FILE);
+
+				compareActionNG.setCurrentFileRevision(fileRevisions[0]);
+				compareActionNG.selectionChanged(new StructuredSelection(
+						selection2));
 			}
 		});
 		compareAction.setPage(this);
+		compareActionNG.setPage(this);
 		MenuManager menuMgr = new MenuManager();
 		Menu menu = menuMgr.createContextMenu(tree);
 		menuMgr.addMenuListener(new IMenuListener() {
 			public void menuAboutToShow(IMenuManager menuMgr) {
 				menuMgr.add(compareAction);
+				menuMgr.add(compareActionNG);
 			}
 		});
 		menuMgr.setRemoveAllWhenShown(true);
org.spearce.egit.ui/src/org/spearce/egit/ui/internal/GitCompareFileRevisionEditorInput.java new file +305 -0

305 lines not on this page · raw message

org.spearce.egit.ui/src/org/spearce/egit/ui/internal/actions/GitCompareRevisionAction.java new file +228 -0

228 lines not on this page · raw message

#5 lore raw
In reply to Robin Rosenberg
Quoted 10 lines · Robin Rosenberg

Now Eclipse can compare two git version at the project level. Thanks to the SHA-1's the compare is very quick. There is some extra cost for version managed archives (zip/jars) as Eclipse insists on comparing the contents of those at the file level too. Not that I mind that. It is useful and cool. (No, it doesn't compare open office documents in any useful manner, unless you happen to have a plugin for it, in which case it might actually work.)

Along with this some experiments with caching and a minor bug fix in the commit reader.

Thanks for this series. I have applied it and pushed it out, along with a small fix on top.

It works nicely, at least the "NG" variant when comparing trees. ;-)

The non-NG variant shows the raw tree diff, which is uh, a little uninteresting, unless you are a true core Git hacker...

I tested it on a small-ish repository. The history browser works quite nicely once the cache starts to get filled. I don't know how well it works on bigger repositories. ;-)

--'da small fix--
From 6c402714c5a1850b871877cefe465a6a40ccdd0f Mon Sep 17 00:00:00 2001
From: Shawn O. Pearce <[email protected]>
Date: Sun, 18 Mar 2007 22:15:14 -0400
Subject: [PATCH] Move GitResourceNode to ui plugin

Because I am trying to keep the core plugin UI free, allowing it to be used in headless workbenches, such as from an Eclipse based build system, we don't want to depend on the org.eclipse.swt plugin. Relocating GitResourceNode to the UI plugin lets us continue to avoid that dependency.

---
 org.spearce.egit.core/META-INF/MANIFEST.MF         |    4 +---
 .../spearce/egit/ui/internal}/GitResourceNode.java |    3 ++-
 .../internal/actions/GitCompareRevisionAction.java |    2 +-
 3 files changed, 4 insertions(+), 5 deletions(-)
 rename {org.spearce.egit.core/src/org/spearce/egit/core/internal/mapping => org.spearce.egit.ui/src/org/spearce/egit/ui/internal}/GitResourceNode.java (96%)
org.spearce.egit.core/META-INF/MANIFEST.MF +1 -3
index 8dc62d6..fd39ec6 100644
@@ -10,9 +10,7 @@ Require-Bundle: org.eclipse.core.runtime,
  org.eclipse.team.core,
  org.eclipse.core.resources,
  org.spearce.jgit,
- org.eclipse.core.filesystem,
- org.eclipse.compare,
- org.eclipse.swt
+ org.eclipse.core.filesystem
 Export-Package: org.spearce.egit.core.internal.mapping;x-friends:="org.spearce.egit.ui",
  org.spearce.egit.core,
  org.spearce.egit.core.internal.mapping,
org.spearce.egit.ui/src/org/spearce/egit/ui/internal/GitResourceNode.java renamed from org.spearce.egit.core/src/org/spearce/egit/core/internal/mapping/GitResourceNode.java +2 -1
similarity index 96%
rename from org.spearce.egit.core/src/org/spearce/egit/core/internal/mapping/GitResourceNode.java
rename to org.spearce.egit.ui/src/org/spearce/egit/ui/internal/GitResourceNode.java
index 0bba684..bdf8902 100644
@@ -1,4 +1,4 @@
-package org.spearce.egit.core.internal.mapping;
+package org.spearce.egit.ui.internal;
 
 import java.io.ByteArrayInputStream;
 import java.io.IOException;
@@ -11,6 +11,7 @@ import org.eclipse.compare.structuremergeviewer.IStructureComparator;
 import org.eclipse.core.runtime.CoreException;
 import org.eclipse.swt.graphics.Image;
 import org.eclipse.team.core.history.IFileRevision;
+import org.spearce.egit.core.internal.mapping.GitFileRevision;
 import org.spearce.jgit.lib.FileTreeEntry;
 import org.spearce.jgit.lib.ObjectId;
 import org.spearce.jgit.lib.ObjectLoader;
org.spearce.egit.ui/src/org/spearce/egit/ui/internal/actions/GitCompareRevisionAction.java +1 -1
index 84eb506..9a0b378 100644
@@ -32,8 +32,8 @@ import org.eclipse.ui.IReusableEditor;
 import org.eclipse.ui.IWorkbenchPage;
 import org.eclipse.ui.actions.BaseSelectionListenerAction;
 import org.spearce.egit.core.GitWorkspaceFileRevision;
-import org.spearce.egit.core.internal.mapping.GitResourceNode;
 import org.spearce.egit.ui.internal.GitCompareFileRevisionEditorInput;
+import org.spearce.egit.ui.internal.GitResourceNode;
 
 /**
  * Action to invoke a Git based compare on selected revivsions in the history window.
Signature · 4 lines
1.5.0.4.1023.gb3ae

-- 
Shawn.