aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKevin Sawicki2011-04-23 13:13:08 (EDT)
committerChris Aniszczyk2011-04-28 13:02:26 (EDT)
commit3aeffedf135a1cbd07e17223cd9e25f268290c0d (patch)
tree5eb65d576f133dc0e09610eb192ba33602cb3430
parent5dd02568393fc66583c1954190303b7a31c4acd1 (diff)
downloadegit-3aeffedf135a1cbd07e17223cd9e25f268290c0d.zip
egit-3aeffedf135a1cbd07e17223cd9e25f268290c0d.tar.gz
egit-3aeffedf135a1cbd07e17223cd9e25f268290c0d.tar.bz2
Add repository commit class that provides file diffsrefs/changes/41/3141/12
Bug: 343000 Change-Id: Id1b3da0d3cf4c076e270cdbef50aee39cb57e14f 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/RepositoryCommitTest.java102
-rw-r--r--org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/commit/RepositoryCommit.java132
2 files changed, 234 insertions, 0 deletions
diff --git a/org.eclipse.egit.ui.test/src/org/eclipse/egit/ui/test/commit/RepositoryCommitTest.java b/org.eclipse.egit.ui.test/src/org/eclipse/egit/ui/test/commit/RepositoryCommitTest.java
new file mode 100644
index 0000000..c122f05
--- /dev/null
+++ b/org.eclipse.egit.ui.test/src/org/eclipse/egit/ui/test/commit/RepositoryCommitTest.java
@@ -0,0 +1,102 @@
+/*******************************************************************************
+ * 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.assertNull;
+import static org.junit.Assert.assertTrue;
+
+import java.io.File;
+
+import org.eclipse.core.runtime.AssertionFailedException;
+import org.eclipse.egit.core.Activator;
+import org.eclipse.egit.ui.common.LocalRepositoryTestCase;
+import org.eclipse.egit.ui.internal.commit.RepositoryCommit;
+import org.eclipse.egit.ui.internal.history.FileDiff;
+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.junit.BeforeClass;
+import org.junit.Test;
+
+/**
+ * Unit tests of {@link RepositoryCommit}
+ */
+public class RepositoryCommitTest 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 testConstructorAsserts() throws Exception {
+ try {
+ assertNull(new RepositoryCommit(null, null));
+ } catch (AssertionFailedException afe) {
+ assertNotNull(afe);
+ }
+ try {
+ assertNull(new RepositoryCommit(repository, null));
+ } catch (AssertionFailedException afe) {
+ assertNotNull(afe);
+ }
+ try {
+ assertNull(new RepositoryCommit(null, commit));
+ } catch (AssertionFailedException afe) {
+ assertNotNull(afe);
+ }
+ }
+
+ @Test
+ public void testAdapters() throws Exception {
+ RepositoryCommit repoCommit = new RepositoryCommit(repository, commit);
+ assertEquals(repository, repoCommit.getAdapter(Repository.class));
+ assertEquals(commit, repoCommit.getAdapter(RevCommit.class));
+ }
+
+ public void testGetters() {
+ RepositoryCommit repoCommit = new RepositoryCommit(repository, commit);
+ assertEquals(repository, repoCommit.getRepository());
+ assertEquals(commit, repoCommit.getRevCommit());
+ assertNotNull(repoCommit.getRepositoryName());
+ assertNotNull(repoCommit.abbreviate());
+ }
+
+ @Test
+ public void testDiffs() throws Exception {
+ RepositoryCommit repoCommit = new RepositoryCommit(repository, commit);
+ FileDiff[] diffs = repoCommit.getDiffs();
+ assertNotNull(diffs);
+ assertTrue(diffs.length > 0);
+ for (FileDiff diff : diffs)
+ assertNotNull(diff);
+ }
+
+}
diff --git a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/commit/RepositoryCommit.java b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/commit/RepositoryCommit.java
new file mode 100644
index 0000000..9c677fe
--- /dev/null
+++ b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/commit/RepositoryCommit.java
@@ -0,0 +1,132 @@
+/*******************************************************************************
+ * 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.IOException;
+
+import org.eclipse.core.runtime.Assert;
+import org.eclipse.core.runtime.PlatformObject;
+import org.eclipse.egit.ui.internal.history.FileDiff;
+import org.eclipse.jgit.lib.Repository;
+import org.eclipse.jgit.revwalk.RevCommit;
+import org.eclipse.jgit.revwalk.RevWalk;
+import org.eclipse.jgit.treewalk.TreeWalk;
+import org.eclipse.jgit.treewalk.filter.TreeFilter;
+
+/**
+ * Class that encapsulates a particular {@link Repository} instance and
+ * {@link RevCommit} instance.
+ *
+ * This class computes and provides access to the {@link FileDiff} objects
+ * introduced by the commit.
+ */
+public class RepositoryCommit extends PlatformObject {
+
+ /**
+ * NAME_LENGTH
+ */
+ public static final int NAME_LENGTH = 8;
+
+ private Repository repository;
+
+ private RevCommit commit;
+
+ private FileDiff[] diffs;
+
+ /**
+ * Create a repository commit
+ *
+ * @param repository
+ * @param commit
+ */
+ public RepositoryCommit(Repository repository, RevCommit commit) {
+ Assert.isNotNull(repository, "Repository cannot be null"); //$NON-NLS-1$
+ Assert.isNotNull(commit, "Commit cannot be null"); //$NON-NLS-1$
+ this.repository = repository;
+ this.commit = commit;
+ }
+
+ /**
+ * @see org.eclipse.core.runtime.PlatformObject#getAdapter(java.lang.Class)
+ */
+ public Object getAdapter(Class adapter) {
+ if (Repository.class == adapter)
+ return repository;
+
+ if (RevCommit.class == adapter)
+ return commit;
+
+ return super.getAdapter(adapter);
+ }
+
+ /**
+ * Abbreviate commit id to {@link #NAME_LENGTH} size.
+ *
+ * @return abbreviated commit id
+ */
+ public String abbreviate() {
+ return commit.abbreviate(NAME_LENGTH).name();
+ }
+
+ /**
+ * Get repository name
+ *
+ * @return repo name
+ */
+ public String getRepositoryName() {
+ return repository.getDirectory().getParentFile().getName();
+ }
+
+ /**
+ * Get repository
+ *
+ * @return repository
+ */
+ public Repository getRepository() {
+ return repository;
+ }
+
+ /**
+ * Get rev commit
+ *
+ * @return rev commit
+ */
+ public RevCommit getRevCommit() {
+ return commit;
+ }
+
+ /**
+ * Get file diffs
+ *
+ * @return non-null but possibly empty array of {@link FileDiff} instances.
+ */
+ public FileDiff[] getDiffs() {
+ if (diffs == null) {
+ RevWalk revWalk = new RevWalk(repository);
+ TreeWalk treewalk = new TreeWalk(repository);
+ treewalk.setRecursive(true);
+ treewalk.setFilter(TreeFilter.ANY_DIFF);
+ try {
+ for (RevCommit parent : commit.getParents())
+ if (parent.getTree() == null)
+ revWalk.parseBody(parent);
+ diffs = FileDiff.compute(treewalk, commit);
+ } catch (IOException e) {
+ diffs = new FileDiff[0];
+ } finally {
+ revWalk.release();
+ treewalk.release();
+ }
+ }
+ return diffs;
+ }
+
+}