summaryrefslogtreecommitdiffstatsabout
diff options
context:
space:
mode:
authorKevin Sawicki2011-04-22 20:29:48 (EDT)
committer Chris Aniszczyk2011-04-28 13:06:27 (EDT)
commitbfc867ee6ee0ae1c9b9c6fa24db09eec141919eb (patch)
treebb30dd4644829d578868b767958a748edd3c18f1
parentc3d63bf11eb61968623865806057493b7dc3ad0a (diff)
downloadegit-bfc867ee6ee0ae1c9b9c6fa24db09eec141919eb.zip
egit-bfc867ee6ee0ae1c9b9c6fa24db09eec141919eb.tar.gz
egit-bfc867ee6ee0ae1c9b9c6fa24db09eec141919eb.tar.bz2
Add persistence support for commit editor inputrefs/changes/68/3168/6
Bug: 343000 Change-Id: I32ee9168d500edfd7c038e954f1c4c9c49804617 Signed-off-by: Kevin Sawicki <kevin@github.com> Signed-off-by: Chris Aniszczyk <caniszczyk@gmail.com>
-rw-r--r--org.eclipse.egit.ui.test/src/org/eclipse/egit/ui/test/commit/CommitEditorInputFactoryTest.java76
-rw-r--r--org.eclipse.egit.ui.test/src/org/eclipse/egit/ui/test/commit/CommitEditorInputTest.java2
-rw-r--r--org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/commit/CommitEditorInput.java31
-rw-r--r--org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/commit/CommitEditorInputFactory.java121
4 files changed, 227 insertions, 3 deletions
diff --git a/org.eclipse.egit.ui.test/src/org/eclipse/egit/ui/test/commit/CommitEditorInputFactoryTest.java b/org.eclipse.egit.ui.test/src/org/eclipse/egit/ui/test/commit/CommitEditorInputFactoryTest.java
new file mode 100644
index 0000000..9419d39
--- /dev/null
+++ b/org.eclipse.egit.ui.test/src/org/eclipse/egit/ui/test/commit/CommitEditorInputFactoryTest.java
@@ -0,0 +1,76 @@
+/*******************************************************************************
+ * Copyright (c) 2011 GitHub Inc.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Kevin Sawicki (GitHub Inc.) - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.egit.ui.test.commit;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
+import java.io.File;
+
+import org.eclipse.core.runtime.IAdaptable;
+import org.eclipse.egit.core.Activator;
+import org.eclipse.egit.ui.common.LocalRepositoryTestCase;
+import org.eclipse.egit.ui.internal.commit.CommitEditorInput;
+import org.eclipse.egit.ui.internal.commit.CommitEditorInputFactory;
+import org.eclipse.egit.ui.internal.commit.RepositoryCommit;
+import org.eclipse.jgit.lib.Constants;
+import org.eclipse.jgit.lib.Repository;
+import org.eclipse.jgit.revwalk.RevCommit;
+import org.eclipse.jgit.revwalk.RevWalk;
+import org.eclipse.ui.XMLMemento;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+/**
+ * Unit tests of {@link CommitEditorInputFactory}
+ */
+public class CommitEditorInputFactoryTest extends LocalRepositoryTestCase {
+
+ private static Repository repository;
+
+ private static RevCommit commit;
+
+ @BeforeClass
+ public static void setup() throws Exception {
+ File repoFile = createProjectAndCommitToRepository();
+ assertNotNull(repoFile);
+ repository = Activator.getDefault().getRepositoryCache()
+ .lookupRepository(repoFile);
+ assertNotNull(repository);
+
+ RevWalk walk = new RevWalk(repository);
+ try {
+ commit = walk.parseCommit(repository.resolve(Constants.HEAD));
+ assertNotNull(commit);
+ } finally {
+ walk.release();
+ }
+ }
+
+ @Test
+ public void testPersistable() {
+ CommitEditorInput input = new CommitEditorInput(new RepositoryCommit(
+ repository, commit));
+ XMLMemento memento = XMLMemento.createWriteRoot("test");
+ input.getPersistable().saveState(memento);
+ CommitEditorInputFactory factory = new CommitEditorInputFactory();
+ IAdaptable created = factory.createElement(memento);
+ assertNotNull(created);
+ assertTrue(created instanceof CommitEditorInput);
+ CommitEditorInput createdInput = (CommitEditorInput) created;
+ assertEquals(input.getCommit().getRevCommit().name(), createdInput
+ .getCommit().getRevCommit().name());
+ assertEquals(input.getCommit().getRepository().getDirectory(),
+ createdInput.getCommit().getRepository().getDirectory());
+ }
+
+}
diff --git a/org.eclipse.egit.ui.test/src/org/eclipse/egit/ui/test/commit/CommitEditorInputTest.java b/org.eclipse.egit.ui.test/src/org/eclipse/egit/ui/test/commit/CommitEditorInputTest.java
index 4d72360..199773b 100644
--- a/org.eclipse.egit.ui.test/src/org/eclipse/egit/ui/test/commit/CommitEditorInputTest.java
+++ b/org.eclipse.egit.ui.test/src/org/eclipse/egit/ui/test/commit/CommitEditorInputTest.java
@@ -79,6 +79,8 @@ public class CommitEditorInputTest extends LocalRepositoryTestCase {
assertNotNull(input.getImageDescriptor());
assertNotNull(input.getToolTipText());
assertNotNull(input.getName());
+ assertEquals(repoCommit, input.getCommit());
+ assertNotNull(input.getPersistable());
}
}
diff --git a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/commit/CommitEditorInput.java b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/commit/CommitEditorInput.java
index 769ca0d..735275d 100644
--- a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/commit/CommitEditorInput.java
+++ b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/commit/CommitEditorInput.java
@@ -20,13 +20,15 @@ import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.ui.IEditorInput;
+import org.eclipse.ui.IMemento;
import org.eclipse.ui.IPersistableElement;
/**
* Commit editor input class. This class wraps a {@link RepositoryCommit} to be
* viewed in an editor.
*/
-public class CommitEditorInput extends PlatformObject implements IEditorInput {
+public class CommitEditorInput extends PlatformObject implements IEditorInput,
+ IPersistableElement {
private RepositoryCommit commit;
@@ -89,7 +91,7 @@ public class CommitEditorInput extends PlatformObject implements IEditorInput {
* @see org.eclipse.ui.IEditorInput#exists()
*/
public boolean exists() {
- return false;
+ return true;
}
/**
@@ -111,7 +113,7 @@ public class CommitEditorInput extends PlatformObject implements IEditorInput {
* @see org.eclipse.ui.IEditorInput#getPersistable()
*/
public IPersistableElement getPersistable() {
- return null;
+ return this;
}
/**
@@ -122,4 +124,27 @@ public class CommitEditorInput extends PlatformObject implements IEditorInput {
.getRevCommit().name(), commit.getRepositoryName());
}
+ /**
+ * Get repository commit
+ *
+ * @return commit
+ */
+ public RepositoryCommit getCommit() {
+ return this.commit;
+ }
+
+ /**
+ * @see org.eclipse.ui.IPersistable#saveState(org.eclipse.ui.IMemento)
+ */
+ public void saveState(IMemento memento) {
+ CommitEditorInputFactory.saveState(memento, this);
+ }
+
+ /**
+ * @see org.eclipse.ui.IPersistableElement#getFactoryId()
+ */
+ public String getFactoryId() {
+ return CommitEditorInputFactory.ID;
+ }
+
}
diff --git a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/commit/CommitEditorInputFactory.java b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/commit/CommitEditorInputFactory.java
new file mode 100644
index 0000000..7a6db58
--- /dev/null
+++ b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/commit/CommitEditorInputFactory.java
@@ -0,0 +1,121 @@
+/*******************************************************************************
+ * Copyright (c) 2011 GitHub Inc.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Kevin Sawicki (GitHub Inc.) - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.egit.ui.internal.commit;
+
+import java.io.File;
+import java.io.IOException;
+
+import org.eclipse.core.runtime.IAdaptable;
+import org.eclipse.egit.core.Activator;
+import org.eclipse.jgit.lib.ObjectId;
+import org.eclipse.jgit.lib.Repository;
+import org.eclipse.jgit.revwalk.RevCommit;
+import org.eclipse.jgit.revwalk.RevWalk;
+import org.eclipse.ui.IElementFactory;
+import org.eclipse.ui.IMemento;
+
+/**
+ * Element factory for saving and restoring the state of a
+ * {@link CommitEditorInput} instance.
+ */
+public class CommitEditorInputFactory implements IElementFactory {
+
+ /**
+ * ID
+ */
+ public static final String ID = "org.eclipse.egit.ui.internal.commit.CommitEditorInputFactory"; //$NON-NLS-1$
+
+ /**
+ * COMMIT
+ */
+ public static final String COMMIT = "commit"; //$NON-NLS-1$
+
+ /**
+ * PATH
+ */
+ public static final String PATH = "path"; //$NON-NLS-1$
+
+ /**
+ * Save state of input to memento
+ *
+ * @param memento
+ * @param input
+ */
+ public static void saveState(IMemento memento, CommitEditorInput input) {
+ RepositoryCommit commit = input.getCommit();
+ memento.putString(COMMIT, commit.getRevCommit().name());
+ memento.putString(PATH, commit.getRepository().getDirectory()
+ .getAbsolutePath());
+ }
+
+ /**
+ * Get repository from memento
+ *
+ * @param memento
+ * @return repository
+ */
+ protected Repository getRepository(IMemento memento) {
+ String path = memento.getString(PATH);
+ if (path == null)
+ return null;
+
+ File gitDir = new File(path);
+ if (!gitDir.exists())
+ return null;
+
+ try {
+ return Activator.getDefault().getRepositoryCache()
+ .lookupRepository(gitDir);
+ } catch (IOException e) {
+ return null;
+ }
+ }
+
+ /**
+ * Get commit from memento and repository
+ *
+ * @param memento
+ * @param repository
+ * @return rev commit
+ */
+ protected RevCommit getCommit(IMemento memento, Repository repository) {
+ String id = memento.getString(COMMIT);
+ if (id == null)
+ return null;
+
+ RevWalk walk = new RevWalk(repository);
+ try {
+ RevCommit commit = walk.parseCommit(ObjectId.fromString(id));
+ for (RevCommit parent : commit.getParents())
+ walk.parseBody(parent);
+ return commit;
+ } catch (IOException e) {
+ return null;
+ } finally {
+ walk.release();
+ }
+ }
+
+ /**
+ * @see org.eclipse.ui.IElementFactory#createElement(org.eclipse.ui.IMemento)
+ */
+ public IAdaptable createElement(IMemento memento) {
+ Repository repository = getRepository(memento);
+ if (repository == null)
+ return null;
+
+ RevCommit commit = getCommit(memento, repository);
+ if (commit == null)
+ return null;
+
+ return new CommitEditorInput(new RepositoryCommit(repository, commit));
+ }
+}