diff options
| author | Kevin Sawicki | 2011-04-23 00:29:48 +0000 |
|---|---|---|
| committer | Chris Aniszczyk | 2011-04-28 17:06:27 +0000 |
| commit | bfc867ee6ee0ae1c9b9c6fa24db09eec141919eb (patch) | |
| tree | bb30dd4644829d578868b767958a748edd3c18f1 | |
| parent | c3d63bf11eb61968623865806057493b7dc3ad0a (diff) | |
| download | egit-bfc867ee6ee0ae1c9b9c6fa24db09eec141919eb.tar.gz egit-bfc867ee6ee0ae1c9b9c6fa24db09eec141919eb.tar.xz egit-bfc867ee6ee0ae1c9b9c6fa24db09eec141919eb.zip | |
Add persistence support for commit editor input
Bug: 343000
Change-Id: I32ee9168d500edfd7c038e954f1c4c9c49804617
Signed-off-by: Kevin Sawicki <kevin@github.com>
Signed-off-by: Chris Aniszczyk <caniszczyk@gmail.com>
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 0000000000..9419d399b9 --- /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 4d72360d04..199773bfad 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 769ca0db22..735275dc4b 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 0000000000..7a6db5830f --- /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)); + } +} |
