The Unified and Autonomous Development Platform
Utilize "git rev-list --cherry-pick" to do cherry pick.
robin shined27f1390aaa4
diff --git a/commons.git/src/main/java/com/pmease/commons/git/Git.java b/commons.git/src/main/java/com/pmease/commons/git/Git.javaindex 97dcf3a401..9465148b53 100644--- a/commons.git/src/main/java/com/pmease/commons/git/Git.java+++ b/commons.git/src/main/java/com/pmease/commons/git/Git.java@@ -40,9 +40,10 @@ import com.pmease.commons.git.command.IsAncestorCommand;import com.pmease.commons.git.command.IsBinaryCommand;import com.pmease.commons.git.command.IsTreeLinkCommand;import com.pmease.commons.git.command.ListBranchCommand;-import com.pmease.commons.git.command.ListHeadCommitCommand;import com.pmease.commons.git.command.ListChangedFilesCommand;+import com.pmease.commons.git.command.ListCherriesCommand;import com.pmease.commons.git.command.ListFileChangesCommand;+import com.pmease.commons.git.command.ListHeadCommitCommand;import com.pmease.commons.git.command.ListSubModulesCommand;import com.pmease.commons.git.command.ListTagsCommand;import com.pmease.commons.git.command.ListTreeCommand;@@ -574,10 +575,14 @@ public class Git implements Serializable {updateSymbolicRef("HEAD", Git.REFS_HEADS + defaultBranch, null);}- public String cherryPick(String revisions) {+ public String cherryPick(String...revisions) {return new CherryPickCommand(repoDir).revisions(revisions).call();}+ public List<String> listCherries(String fromRev, String toRev) {+ return new ListCherriesCommand(repoDir).fromRev(fromRev).toRev(toRev).call();+ }+public boolean hasCommits() {File headsDir = new File(repoDir, Git.REFS_HEADS);return headsDir.exists() && headsDir.list().length != 0 || new File(repoDir, "packed-refs").exists();diff --git a/commons.git/src/main/java/com/pmease/commons/git/command/CherryPickCommand.java b/commons.git/src/main/java/com/pmease/commons/git/command/CherryPickCommand.javaindex 868feb784b..b7ed17b2d1 100644--- a/commons.git/src/main/java/com/pmease/commons/git/command/CherryPickCommand.java+++ b/commons.git/src/main/java/com/pmease/commons/git/command/CherryPickCommand.java@@ -1,12 +1,15 @@package com.pmease.commons.git.command;import java.io.File;+import java.util.Arrays;+import java.util.List;import java.util.concurrent.atomic.AtomicBoolean;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import com.google.common.base.Preconditions;+import com.google.common.collect.Iterables;import com.pmease.commons.git.Git;import com.pmease.commons.util.execution.Commandline;import com.pmease.commons.util.execution.ExecuteResult;@@ -16,13 +19,15 @@ public class CherryPickCommand extends GitCommand<String> {private static final Logger logger = LoggerFactory.getLogger(CherryPickCommand.class);- private String revisions;+ private static final int MAX_ARGUMENTS_NUM = 20;++ private String revisions[];public CherryPickCommand(final File repoDir) {super(repoDir);}- public CherryPickCommand revisions(String revisions) {+ public CherryPickCommand revisions(String...revisions) {this.revisions = revisions;return this;}@@ -30,47 +35,51 @@ public class CherryPickCommand extends GitCommand<String> {@Overridepublic String call() {Preconditions.checkNotNull(revisions, "revisions has to be specified.");-- Commandline cmd = cmd().addArgs("cherry-pick");- cmd.addArgs("--no-merges", "--ff", "--quiet");-- cmd.addArgs(revisions);- final AtomicBoolean emptyCommitSet = new AtomicBoolean(false);- final AtomicBoolean hasConflict = new AtomicBoolean(false);+ if (revisions.length != 0) {+ for (List<String> partition: Iterables.partition(Arrays.asList(revisions), MAX_ARGUMENTS_NUM)) {+ Commandline cmd = cmd().addArgs("cherry-pick");+ cmd.addArgs("--ff", "--quiet");++ for (String each: partition)+ cmd.addArgs(each);++ final AtomicBoolean emptyCommitSet = new AtomicBoolean(false);+ final AtomicBoolean hasConflict = new AtomicBoolean(false);- ExecuteResult result = cmd.execute(new LineConsumer() {+ ExecuteResult result = cmd.execute(new LineConsumer() {- @Override- public void consume(String line) {- logger.debug(line);- }-- }, new LineConsumer() {+ @Override+ public void consume(String line) {+ logger.debug(line);+ }++ }, new LineConsumer() {- @Override- public void consume(String line) {- if (line.equals("fatal: empty commit set passed")) {- emptyCommitSet.set(true);- } else if (line.equals("nothing to commit, working directory clean")- || line.equals("hint: after resolving the conflicts, mark the corrected paths")- || line.startsWith("The previous cherry-pick is now empty")) {- hasConflict.set(true);- } else {- logger.error(line);- }- }-- });-- if (emptyCommitSet.get())- return new Git(repoDir).parseRevision("HEAD", true);-- if (hasConflict.get())- return null;+ @Override+ public void consume(String line) {+ if (line.equals("fatal: empty commit set passed")) {+ emptyCommitSet.set(true);+ } else if (line.equals("nothing to commit, working directory clean")+ || line.equals("hint: after resolving the conflicts, mark the corrected paths")+ || line.startsWith("The previous cherry-pick is now empty")) {+ hasConflict.set(true);+ } else {+ logger.error(line);+ }+ }++ });++ if (emptyCommitSet.get())+ return new Git(repoDir).parseRevision("HEAD", true);++ if (hasConflict.get())+ return null;- result.checkReturnCode();-+ result.checkReturnCode();+ }+ }return new Git(repoDir).parseRevision("HEAD", true);}diff --git a/commons.git/src/main/java/com/pmease/commons/git/command/ListCherriesCommand.java b/commons.git/src/main/java/com/pmease/commons/git/command/ListCherriesCommand.javanew file mode 100644index 0000000000..d8b61857b9--- /dev/null+++ b/commons.git/src/main/java/com/pmease/commons/git/command/ListCherriesCommand.java@@ -0,0 +1,63 @@+package com.pmease.commons.git.command;++import java.io.File;+import java.util.ArrayList;+import java.util.List;++import org.slf4j.Logger;+import org.slf4j.LoggerFactory;++import com.google.common.base.Preconditions;+import com.pmease.commons.util.execution.Commandline;+import com.pmease.commons.util.execution.LineConsumer;++public class ListCherriesCommand extends GitCommand<List<String>> {++ private static final Logger logger = LoggerFactory.getLogger(ListCherriesCommand.class);++ private String fromRev;++ private String toRev;++ public ListCherriesCommand(final File repoDir) {+ super(repoDir);+ }++ public ListCherriesCommand fromRev(String fromRev) {+ this.fromRev = fromRev;+ return this;+ }++ public ListCherriesCommand toRev(String toRev) {+ this.toRev = toRev;+ return this;+ }++ @Override+ public List<String> call() {+ Preconditions.checkNotNull(fromRev, "fromRev has to be specified.");+ Preconditions.checkNotNull(toRev, "toRev has to be specified.");++ final List<String> cherries = new ArrayList<>();+ Commandline cmd = cmd();+ cmd.addArgs("rev-list", "--cherry-pick", "--right-only", "--no-merges", "--reverse", fromRev + "..." + toRev);+ cmd.execute(new LineConsumer() {++ @Override+ public void consume(String line) {+ cherries.add(line);+ }++ }, new LineConsumer() {++ @Override+ public void consume(String line) {+ logger.error(line);+ }++ }).checkReturnCode();++ return cherries;+ }++}diff --git a/gitplex.core/src/main/java/com/pmease/gitplex/core/manager/impl/DefaultPullRequestManager.java b/gitplex.core/src/main/java/com/pmease/gitplex/core/manager/impl/DefaultPullRequestManager.javaindex 07ca379590..9dec27f759 100644--- a/gitplex.core/src/main/java/com/pmease/gitplex/core/manager/impl/DefaultPullRequestManager.java+++ b/gitplex.core/src/main/java/com/pmease/gitplex/core/manager/impl/DefaultPullRequestManager.java@@ -404,12 +404,14 @@ public class DefaultPullRequestManager implements PullRequestManager {if (preview.getIntegrationStrategy() == REBASE_TARGET_ONTO_SOURCE) {tempGit.updateRef("HEAD", requestHead, null, null);tempGit.reset(null, null);- integrated = tempGit.cherryPick(".." + targetHead);+ List<String> cherries = tempGit.listCherries("HEAD", targetHead);+ integrated = tempGit.cherryPick(cherries.toArray(new String[cherries.size()]));} else {tempGit.updateRef("HEAD", targetHead, null, null);tempGit.reset(null, null);if (preview.getIntegrationStrategy() == REBASE_SOURCE_ONTO_TARGET) {- integrated = tempGit.cherryPick(".." + requestHead);+ List<String> cherries = tempGit.listCherries("HEAD", requestHead);+ integrated = tempGit.cherryPick(cherries.toArray(new String[cherries.size()]));} else if (preview.getIntegrationStrategy() == MERGE_WITH_SQUASH) {String commitMessage = request.getTitle() + "\n\n";if (request.getDescription() != null)diff --git a/gitplex.web/src/main/java/com/pmease/gitplex/web/page/repository/pullrequest/RequestComparePage.java b/gitplex.web/src/main/java/com/pmease/gitplex/web/page/repository/pullrequest/RequestComparePage.javaindex cbc28b5684..7a40ccdfd4 100644--- a/gitplex.web/src/main/java/com/pmease/gitplex/web/page/repository/pullrequest/RequestComparePage.java+++ b/gitplex.web/src/main/java/com/pmease/gitplex/web/page/repository/pullrequest/RequestComparePage.java@@ -116,8 +116,7 @@ public class RequestComparePage extends RequestDetailPage {if (request.isOpen()) {IntegrationPreview preview = GitPlex.getInstance(PullRequestManager.class).previewIntegration(request);- if (preview != null && preview.getIntegrated() != null- && !getRepository().getChanges(preview.getRequestHead(), preview.getIntegrated()).isEmpty()) {+ if (preview != null && preview.getIntegrated() != null && !preview.getIntegrated().equals(preview.getRequestHead())) {Commit commit = getRepository().getCommit(preview.getIntegrated());choices.put(commit.getHash(), new CommitDescription(INTEGRATION_PREVIEW, commit.getSubject()));}diff --git a/gitplex.web/src/main/java/com/pmease/gitplex/web/page/repository/pullrequest/RequestDetailPage.html b/gitplex.web/src/main/java/com/pmease/gitplex/web/page/repository/pullrequest/RequestDetailPage.htmlindex 3b69b43349..eb2671ae28 100644--- a/gitplex.web/src/main/java/com/pmease/gitplex/web/page/repository/pullrequest/RequestDetailPage.html+++ b/gitplex.web/src/main/java/com/pmease/gitplex/web/page/repository/pullrequest/RequestDetailPage.html@@ -44,7 +44,7 @@<div wicket:id="sourceRebased"><p>Per the integration strategy, source branch <span wicket:id="source"></span> was rebased on topof target branch <span wicket:id="target"></span>, and then target- branch was fast forwarded to rebased result.</p>+ branch was fast forwarded to the rebase result.</p><p>If you have a local branch whose upstream tracking branchpoints to source branch, please run this command to do the sync:<kbd>git fetch origin && git rebase</kbd>diff --git a/gitplex.web/src/main/java/com/pmease/gitplex/web/page/repository/pullrequest/RequestDetailPage.java b/gitplex.web/src/main/java/com/pmease/gitplex/web/page/repository/pullrequest/RequestDetailPage.javaindex 801ed326e5..3a36f8c87a 100644--- a/gitplex.web/src/main/java/com/pmease/gitplex/web/page/repository/pullrequest/RequestDetailPage.java+++ b/gitplex.web/src/main/java/com/pmease/gitplex/web/page/repository/pullrequest/RequestDetailPage.java@@ -561,6 +561,10 @@ public abstract class RequestDetailPage extends RepositoryPage {final List<IntegrationStrategy> strategies =GitPlex.getInstance(PullRequestManager.class).getApplicableIntegrationStrategies(request);+ if (!strategies.contains(request.getIntegrationStrategy())) {+ request.setIntegrationStrategy(strategies.get(0));+ GitPlex.getInstance(Dao.class).persist(request);+ }IModel<IntegrationStrategy> strategyModel = new IModel<IntegrationStrategy>() {@Override@@ -704,8 +708,7 @@ public abstract class RequestDetailPage extends RepositoryPage {PullRequest request = getPullRequest();PullRequestManager manager = GitPlex.getInstance(PullRequestManager.class);IntegrationPreview preview = manager.previewIntegration(request);- setVisible(!preview.getIntegrated().equals(preview.getRequestHead())- && !getRepository().getChanges(preview.getRequestHead(), preview.getIntegrated()).isEmpty());+ setVisible(!preview.getIntegrated().equals(preview.getRequestHead()));}};