The Unified and Autonomous Development Platform
Add git pick command.
robin shineedeefc2cc26f
diff --git a/commons.git/pom.xml b/commons.git/pom.xmlindex de1f7a9dde..00f872001f 100644--- a/commons.git/pom.xml+++ b/commons.git/pom.xml@@ -46,7 +46,7 @@<dependency><groupId>org.eclipse.jgit</groupId><artifactId>org.eclipse.jgit.http.server</artifactId>- <version>3.0.0.201306101825-r</version>+ <version>3.4.1.201406201815-r</version></dependency><dependency><groupId>com.pmease</groupId>diff --git a/commons.git/src/main/java/com/pmease/commons/git/command/GitCommand.java b/commons.git/src/main/java/com/pmease/commons/git/command/GitCommand.javaindex 3ad09b1368..ad82f88cb9 100644--- a/commons.git/src/main/java/com/pmease/commons/git/command/GitCommand.java+++ b/commons.git/src/main/java/com/pmease/commons/git/command/GitCommand.java@@ -161,5 +161,5 @@ public abstract class GitCommand<V> implements Callable<V> {protected void error(String line) {logger.error(line);}-+}diff --git a/commons.git/src/main/java/com/pmease/commons/git/jgit/PickCommand.java b/commons.git/src/main/java/com/pmease/commons/git/jgit/PickCommand.javanew file mode 100644index 0000000000..f189d4c2d4--- /dev/null+++ b/commons.git/src/main/java/com/pmease/commons/git/jgit/PickCommand.java@@ -0,0 +1,119 @@+package com.pmease.commons.git.jgit;++import java.io.IOException;+import java.util.ArrayList;+import java.util.Collections;+import java.util.List;++import org.eclipse.jgit.api.Git;+import org.eclipse.jgit.api.GitCommand;+import org.eclipse.jgit.api.LogCommand;+import org.eclipse.jgit.api.errors.GitAPIException;+import org.eclipse.jgit.errors.IncorrectObjectTypeException;+import org.eclipse.jgit.errors.MissingObjectException;+import org.eclipse.jgit.lib.AnyObjectId;+import org.eclipse.jgit.lib.CommitBuilder;+import org.eclipse.jgit.lib.ObjectId;+import org.eclipse.jgit.lib.ObjectInserter;+import org.eclipse.jgit.lib.PersonIdent;+import org.eclipse.jgit.lib.Repository;+import org.eclipse.jgit.merge.MergeStrategy;+import org.eclipse.jgit.merge.ThreeWayMerger;+import org.eclipse.jgit.revwalk.RevCommit;++import com.google.common.base.Preconditions;++public class PickCommand extends GitCommand<AnyObjectId> {++ private ObjectId to;++ private ObjectId from;++ private PersonIdent committer;++ public PickCommand(Repository repo) {+ super(repo);+ }++ public PickCommand setTo(ObjectId to) {+ this.to = to;+ return this;+ }++ public PickCommand setFrom(ObjectId from) {+ this.from = from;+ return this;+ }++ public PickCommand setCommitter(PersonIdent committer) {+ this.committer = committer;+ return this;+ }++ @Override+ public ObjectId call() throws GitAPIException {+ Preconditions.checkNotNull(to, "Upstream has to be specified.");+ Preconditions.checkNotNull(from, "Downstream has to be specified.");+ Preconditions.checkNotNull("committer", "Committer has to be specified.");++ checkCallable();++ LogCommand log = new Git(getRepository()).log();+ try {+ log.addRange(to, from);+ } catch (MissingObjectException | IncorrectObjectTypeException e) {+ throw new RuntimeException(e);+ }++ List<RevCommit> commitsToPick = new ArrayList<>();++ for (RevCommit commit: log.call()) {+ if (commit.getParentCount() == 1)+ commitsToPick.add(commit);+ }++ Collections.reverse(commitsToPick);++ ObjectId currentId = to;++ final ObjectInserter inserter = getRepository().newObjectInserter();+ try {+ for (RevCommit commitToPick: commitsToPick) {+ if (commitToPick.getParent(0).equals(currentId)) {+ currentId = commitToPick;+ continue;+ }++ ThreeWayMerger merger = MergeStrategy.RECURSIVE.newMerger(getRepository(), true);+ merger.setObjectInserter(new ObjectInserter.Filter() {++ @Override+ protected ObjectInserter delegate() {+ return inserter;+ }+ });+ merger.setBase(commitToPick.getParent(0));++ if (merger.merge(currentId, commitToPick)) {+ CommitBuilder cb = new CommitBuilder();+ cb.setTreeId(Preconditions.checkNotNull(merger.getResultTreeId()));+ cb.setParentId(currentId);+ cb.setAuthor(commitToPick.getAuthorIdent());+ cb.setMessage(commitToPick.getFullMessage());+ cb.setCommitter(committer);+ currentId = inserter.insert(cb);+ inserter.flush();+ } else {+ return null;+ }+ }++ return currentId;+ } catch (IOException e) {+ throw new RuntimeException(e);+ } finally {+ inserter.release();+ }+ }++}diff --git a/commons.git/src/test/java/com/pmease/commons/git/AbstractGitTest.java b/commons.git/src/test/java/com/pmease/commons/git/AbstractGitTest.javaindex d110437362..af1ba1df76 100644--- a/commons.git/src/test/java/com/pmease/commons/git/AbstractGitTest.java+++ b/commons.git/src/test/java/com/pmease/commons/git/AbstractGitTest.java@@ -25,6 +25,8 @@ public abstract class AbstractGitTest extends AppLoaderMocker {});Assert.assertTrue(GitCommand.checkError("git") == null);++}/**diff --git a/commons.git/src/test/java/com/pmease/commons/git/GitUtils.java b/commons.git/src/test/java/com/pmease/commons/git/GitUtils.javanew file mode 100644index 0000000000..4dc1c50d8f--- /dev/null+++ b/commons.git/src/test/java/com/pmease/commons/git/GitUtils.java@@ -0,0 +1,59 @@+package com.pmease.commons.git;++import java.io.ByteArrayOutputStream;+import java.io.IOException;++import org.eclipse.jgit.api.GitCommand;+import org.eclipse.jgit.api.errors.GitAPIException;+import org.eclipse.jgit.lib.AnyObjectId;+import org.eclipse.jgit.lib.ObjectReader;+import org.eclipse.jgit.lib.Repository;+import org.eclipse.jgit.revwalk.RevCommit;+import org.eclipse.jgit.revwalk.RevWalk;+import org.eclipse.jgit.treewalk.AbstractTreeIterator;+import org.eclipse.jgit.treewalk.CanonicalTreeParser;+import org.eclipse.jgit.treewalk.TreeWalk;++public class GitUtils {++ public static <T> T call(GitCommand<T> command) {+ try {+ return command.call();+ } catch (GitAPIException e) {+ throw new RuntimeException(e);+ }+ }++ public static AbstractTreeIterator getTreeIterator(Repository repo, AnyObjectId commitId) {+ CanonicalTreeParser p = new CanonicalTreeParser();+ ObjectReader or = repo.newObjectReader();+ try {+ p.reset(or, new RevWalk(repo).parseTree(commitId));+ return p;+ } catch (IOException e) {+ throw new RuntimeException(e);+ } finally {+ or.release();+ }+ }++ public static byte[] readFile(Repository repo, AnyObjectId commitId, String path) {+ RevWalk revWalk = new RevWalk(repo);+ try {+ RevCommit commit = revWalk.parseCommit(commitId);+ TreeWalk treeWalk = TreeWalk.forPath(repo, "file", commit.getTree());+ try {+ ByteArrayOutputStream baos = new ByteArrayOutputStream();+ repo.open(treeWalk.getObjectId(0)).copyTo(baos);+ return baos.toByteArray();+ } finally {+ treeWalk.release();+ }+ } catch (IOException e) {+ throw new RuntimeException(e);+ } finally {+ revWalk.release();+ }++ }+}diff --git a/commons.git/src/test/java/com/pmease/commons/git/jgit/AbstractGitTest.java b/commons.git/src/test/java/com/pmease/commons/git/jgit/AbstractGitTest.javanew file mode 100644index 0000000000..9be940c0b5--- /dev/null+++ b/commons.git/src/test/java/com/pmease/commons/git/jgit/AbstractGitTest.java@@ -0,0 +1,117 @@+package com.pmease.commons.git.jgit;++import java.io.File;+import java.io.IOException;++import org.eclipse.jgit.api.Git;+import org.eclipse.jgit.api.GitCommand;+import org.eclipse.jgit.api.errors.GitAPIException;+import org.eclipse.jgit.lib.PersonIdent;+import org.eclipse.jgit.transport.RefSpec;++import com.pmease.commons.git.GitUtils;+import com.pmease.commons.loader.AppLoaderMocker;+import com.pmease.commons.util.FileUtils;++public class AbstractGitTest extends AppLoaderMocker {++ protected File tempDir;++ protected Git workGit;++ protected Git bareGit;++ @Override+ protected void setup() {+ tempDir = FileUtils.createTempDir();+ try {+ File bareGitDir = new File(tempDir, "bare");+ Git.init().setDirectory(bareGitDir).setBare(true).call();+ bareGit = Git.open(bareGitDir);++ File workGitDir = new File(tempDir, "work");++ Git.cloneRepository()+ .setBare(false)+ .setDirectory(workGitDir)+ .setURI(bareGit.getRepository().getDirectory().getAbsolutePath())+ .call();+ workGit = Git.open(workGitDir);++ addFileAndCommit("initial", "hello git", "Initial commit");+ push(new RefSpec("master:master"));+ } catch (GitAPIException | IOException e) {+ throw new RuntimeException(e);+ }+ }++ protected void push(RefSpec... specs) {+ call(workGit.push().setRemote("origin").setRefSpecs(specs));+ }++ protected void pull() {+ call(workGit.pull());+ }++ protected File getWorkDir() {+ return workGit.getRepository().getWorkTree();+ }++ protected void createDir(String path) {+ FileUtils.createDir(new File(getWorkDir(), path));+ }++ protected void writeFile(String path, String content) {+ File file = new File(getWorkDir(), path);+ FileUtils.writeFile(file, content);+ }++ protected void add(String filePatterns) {+ call(workGit.add().addFilepattern(filePatterns));+ }++ protected void rm(String filePattern) {+ call(workGit.rm().addFilepattern(filePattern));+ }++ protected void commit(String comment) {+ call(workGit.commit().setAuthor(getDefaultAuthor()).setCommitter(getDefaultCommitter()).setMessage(comment));+ }++ protected void addFile(String path, String content) {+ writeFile(path, content);+ add(path);+ }++ protected void addFileAndCommit(String path, String content, String comment) {+ addFile(path, content);+ commit(comment);+ }++ protected void removeFileAndCommit(String path, String comment) {+ rm(path);+ commit(comment);+ }++ protected PersonIdent getDefaultCommitter() {+ return new PersonIdent("A Tester", "[email protected]");+ }++ protected PersonIdent getDefaultAuthor() {+ return new PersonIdent("A Tester", "[email protected]");+ }++ protected <T> T call(GitCommand<T> command) {+ return GitUtils.call(command);+ }++ @Override+ protected void teardown() {+ if (bareGit != null)+ bareGit.close();+ if (workGit != null)+ workGit.close();++ FileUtils.deleteDir(tempDir);+ }+}diff --git a/commons.git/src/test/java/com/pmease/commons/git/jgit/PickCommandTest.java b/commons.git/src/test/java/com/pmease/commons/git/jgit/PickCommandTest.javanew file mode 100644index 0000000000..fe8f5dc398--- /dev/null+++ b/commons.git/src/test/java/com/pmease/commons/git/jgit/PickCommandTest.java@@ -0,0 +1,183 @@+package com.pmease.commons.git.jgit;++import static org.junit.Assert.assertArrayEquals;+import static org.junit.Assert.assertEquals;+import static org.junit.Assert.assertNotNull;+import static org.junit.Assert.assertNull;++import java.io.IOException;+import java.util.ArrayList;+import java.util.List;++import org.eclipse.jgit.api.errors.RefNotFoundException;+import org.eclipse.jgit.lib.AnyObjectId;+import org.eclipse.jgit.lib.Repository;+import org.eclipse.jgit.merge.MergeStrategy;+import org.eclipse.jgit.revwalk.RevCommit;+import org.junit.Test;++import com.pmease.commons.git.GitUtils;++public class PickCommandTest extends AbstractGitTest {++ @Test+ public void testPickIfThereAreNoConflicts() throws IOException, RefNotFoundException {+ addFileAndCommit("file", "line1\nline2\nline3\nline4\nline5\n", "0");+ call(workGit.checkout().setCreateBranch(true).setName("dev"));+ addFileAndCommit("file", "line2\nline3\nline4\nline5\n", "d1");+ addFileAndCommit("file", "line2\nline3\nline4\nline5\nline6\n", "d2");++ call(workGit.checkout().setName("master"));+ addFileAndCommit("file", "line1\nline2\nmaster3\nline4\nline5\n", "m1");++ Repository repo = workGit.getRepository();+ PickCommand pick = new PickCommand(repo);+ pick.setFrom(repo.resolve("dev"));+ pick.setTo(repo.resolve("master"));+ pick.setCommitter(getDefaultCommitter());++ AnyObjectId pickResult = call(pick);+ assertNotNull(pickResult);++ assertEquals(+ "line2\nmaster3\nline4\nline5\nline6\n",+ new String(GitUtils.readFile(repo, pickResult, "file")));+ }++ @Test+ public void testPickIfThereAreConflicts() throws IOException, RefNotFoundException {+ addFileAndCommit("file", "line1\nline2\nline3\nline4\nline5\n", "0");+ call(workGit.checkout().setCreateBranch(true).setName("dev"));+ addFileAndCommit("file", "line2\ndev3\nline4\nline5\n", "d1");+ addFileAndCommit("file", "line2\nline3\nline4\nline5\nline6\n", "d2");++ call(workGit.checkout().setName("master"));+ addFileAndCommit("file", "line1\nline2\nmaster3\nline4\nline5\n", "m1");++ Repository repo = workGit.getRepository();+ PickCommand pick = new PickCommand(repo);+ pick.setFrom(repo.resolve("dev"));+ pick.setTo(repo.resolve("master"));+ pick.setCommitter(getDefaultCommitter());++ assertNull(call(pick));+ }++ @Test+ public void testPickIfThereAreConflictMerges() throws IOException, RefNotFoundException {+ addFileAndCommit("file", "line1\n", "0");+ call(workGit.checkout().setCreateBranch(true).setName("dev"));+ addFileAndCommit("file", "dev1\n", "d1");+ call(workGit.checkout().setName("master"));+ addFileAndCommit("file", "master1\n", "m1");++ Repository repo = workGit.getRepository();++ call(workGit.checkout().setName("dev"));+ call(workGit.merge().setCommit(true).include(repo.resolve("master")).setStrategy(MergeStrategy.OURS));++ PickCommand pick = new PickCommand(repo);+ pick.setFrom(repo.resolve("dev"));+ pick.setTo(repo.resolve("master"));+ pick.setCommitter(getDefaultCommitter());++ assertNull(call(pick));+ }++ @Test+ public void testPickIfThereAreNonConflictMerges() throws IOException, RefNotFoundException {+ addFileAndCommit("file", "line1\nline2\nline3\n", "0");+ call(workGit.checkout().setCreateBranch(true).setName("dev"));+ addFileAndCommit("file", "dev1\nline2\nline3\n", "d1");+ call(workGit.checkout().setName("master"));+ addFileAndCommit("file", "line1\nline2\nmaster3\n", "m1");++ Repository repo = workGit.getRepository();++ call(workGit.checkout().setName("dev"));+ call(workGit.merge().setCommit(true).include(repo.resolve("master")));++ PickCommand pick = new PickCommand(repo);+ pick.setFrom(repo.resolve("dev"));+ pick.setTo(repo.resolve("master"));+ pick.setCommitter(getDefaultCommitter());++ AnyObjectId pickResult = call(pick);++ assertArrayEquals(+ GitUtils.readFile(repo, pickResult, "file"),+ GitUtils.readFile(repo, repo.resolve("dev"), "file"));+ }++ @Test+ public void testPickIfCanBeFastForwarded() throws IOException, RefNotFoundException {+ addFileAndCommit("file", "line1\n", "0");+ call(workGit.checkout().setCreateBranch(true).setName("dev"));+ addFileAndCommit("file", "line1\nline2\n", "d1");+ addFileAndCommit("file", "line1\nline2\nline3\n", "d2");++ Repository repo = workGit.getRepository();++ PickCommand pick = new PickCommand(repo);+ pick.setFrom(repo.resolve("dev"));+ pick.setTo(repo.resolve("master"));+ pick.setCommitter(getDefaultCommitter());++ AnyObjectId pickResult = call(pick);++ assertEquals(repo.resolve("dev"), pickResult);+ }++ @Test+ public void testPickIfAlreadyMerged() throws IOException, RefNotFoundException {+ addFileAndCommit("file", "line1\n", "0");+ call(workGit.checkout().setCreateBranch(true).setName("dev"));+ addFileAndCommit("file", "line1\nline2\n", "d1");+ addFileAndCommit("file", "line1\nline2\nline3\n", "d2");++ Repository repo = workGit.getRepository();++ PickCommand pick = new PickCommand(repo);+ pick.setFrom(repo.resolve("master"));+ pick.setTo(repo.resolve("dev"));+ pick.setCommitter(getDefaultCommitter());++ AnyObjectId pickResult = call(pick);++ assertEquals(repo.resolve("dev"), pickResult);+ }++ @Test+ public void testPickIfThereAreMultipleMerges() throws IOException, RefNotFoundException {+ Repository repo = workGit.getRepository();++ addFileAndCommit("file", "line1\nline2\nline3\nline4\nline5\nline6\nline7\nline8\n", "0");+ addFileAndCommit("file", "master1\nline2\nline3\nline4\nline5\nline6\nline7\nline8\n", "m1");+ addFileAndCommit("file", "master1\nmaster2\nline3\nline4\nline5\nline6\nline7\nline8\n", "m2");++ call(workGit.checkout().setStartPoint("master~2").setName("dev").setCreateBranch(true));+ addFileAndCommit("file", "line1\nline2\nline3\nline4\nline5\nline6\nline7\ndev8\n", "d1");+ call(workGit.merge().include(repo.resolve("master~1")));+ addFileAndCommit("file", "master1\nline2\nline3\nline4\nline5\nline6\ndev7\ndev8\n", "d2");+ call(workGit.merge().include(repo.resolve("master")));++ PickCommand pick = new PickCommand(repo);+ pick.setFrom(repo.resolve("dev"));+ pick.setTo(repo.resolve("master"));+ pick.setCommitter(getDefaultCommitter());++ AnyObjectId pickResult = call(pick);++ assertArrayEquals(+ GitUtils.readFile(repo, pickResult, "file"),+ GitUtils.readFile(repo, repo.resolve("dev"), "file"));+ List<RevCommit> commits = new ArrayList<>();+ for (RevCommit commit: call(workGit.log().addRange(repo.resolve("master"), pickResult)))+ commits.add(commit);++ assertEquals(2, commits.size());+ assertEquals("d2", commits.get(0).getFullMessage());+ assertEquals("d1", commits.get(1).getFullMessage());+ }++}