The Unified and Autonomous Development Platform
Tests for PullRequest and PullRequestUpdate.
robin shine763ba391e368
diff --git a/commons.git/src/main/java/com/pmease/commons/git/BriefCommit.java b/commons.git/src/main/java/com/pmease/commons/git/BriefCommit.javaindex 98f348ffe7..c9ba069df1 100644--- a/commons.git/src/main/java/com/pmease/commons/git/BriefCommit.java+++ b/commons.git/src/main/java/com/pmease/commons/git/BriefCommit.java@@ -10,13 +10,13 @@ public class BriefCommit {private final UserInfo author;- private final String summary;+ private final String subject;- public BriefCommit(String hash, UserInfo committer, UserInfo author, String summary) {+ public BriefCommit(String hash, UserInfo committer, UserInfo author, String subject) {this.hash = hash;this.committer = committer;this.author = author;- this.summary = summary;+ this.subject = subject;}public String getHash() {@@ -31,8 +31,8 @@ public class BriefCommit {return author;}- public String getSummary() {- return summary;+ public String getSubject() {+ return subject;}@Override@@ -41,7 +41,7 @@ public class BriefCommit {.add("hash", hash).add("committer", committer.getName()).add("date", committer.getDate())- .add("summary", summary)+ .add("subject", subject).toString();}diff --git a/commons.git/src/main/java/com/pmease/commons/git/Commit.java b/commons.git/src/main/java/com/pmease/commons/git/Commit.javaindex b8b43212d8..bad0d6c915 100644--- a/commons.git/src/main/java/com/pmease/commons/git/Commit.java+++ b/commons.git/src/main/java/com/pmease/commons/git/Commit.java@@ -19,18 +19,21 @@ public class Commit extends BriefCommit {private final List<FileChange> fileChanges;public Commit(String hash, UserInfo committer, UserInfo author,- String summary, @Nullable String message,+ String subject, @Nullable String messageBody,@Nullable String note, List<String> parentHashes,List<FileChange> fileChanges) {- super(hash, committer, author, summary);+ super(hash, committer, author, subject);- this.message = message;+ if (messageBody != null)+ message = subject + "\n\n" + messageBody;+ else+ message = subject;this.note = note;this.parentHashes = new ArrayList<>(parentHashes);this.fileChanges = new ArrayList<>(fileChanges);}- public @Nullable String getMessage() {+ public String getMessage() {return message;}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 f6af8ee961..0bb2263546 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@@ -30,6 +30,7 @@ import com.pmease.commons.git.command.ListTagsCommand;import com.pmease.commons.git.command.ListTreeCommand;import com.pmease.commons.git.command.LogCommand;import com.pmease.commons.git.command.MergeCommand;+import com.pmease.commons.git.command.PushCommand;import com.pmease.commons.git.command.RemoveCommand;import com.pmease.commons.git.command.ResetCommand;import com.pmease.commons.git.command.ShowCommand;@@ -130,7 +131,7 @@ public class Git implements Serializable {return new ListChangedFilesCommand(repoDir).fromRev(fromRev).toRev(toRev).call();}- public Git checkout(String revision, boolean newBranch) {+ public Git checkout(String revision, @Nullable String newBranch) {new CheckoutCommand(repoDir).revision(revision).newBranch(newBranch).call();return this;}@@ -157,6 +158,11 @@ public class Git implements Serializable {return this;}+ public Git push(String to, String refspec) {+ new PushCommand(repoDir).to(to).refspec(refspec).call();+ return this;+ }+public List<RefInfo> showRefs(String pattern) {return new ShowRefCommand(repoDir).pattern(pattern).call();}@@ -246,5 +252,10 @@ public class Git implements Serializable {public Map<String, String> listSubModules(String revision) {return new ListSubModulesCommand(repoDir).revision(revision).call();}-++ @Override+ public String toString() {+ return repoDir.toString();+ }+}diff --git a/commons.git/src/main/java/com/pmease/commons/git/command/CheckoutCommand.java b/commons.git/src/main/java/com/pmease/commons/git/command/CheckoutCommand.javaindex ab357cac5e..74957ceeec 100644--- a/commons.git/src/main/java/com/pmease/commons/git/command/CheckoutCommand.java+++ b/commons.git/src/main/java/com/pmease/commons/git/command/CheckoutCommand.java@@ -4,12 +4,13 @@ import java.io.File;import com.google.common.base.Preconditions;import com.pmease.commons.util.execution.Commandline;+import com.pmease.commons.util.execution.LineConsumer;public class CheckoutCommand extends GitCommand<Void> {private String revision;- private boolean newBranch;+ private String newBranch;public CheckoutCommand(final File repoDir) {super(repoDir);@@ -28,7 +29,7 @@ public class CheckoutCommand extends GitCommand<Void> {return this;}- public CheckoutCommand newBranch(boolean newBranch) {+ public CheckoutCommand newBranch(String newBranch) {this.newBranch = newBranch;return this;}@@ -38,12 +39,22 @@ public class CheckoutCommand extends GitCommand<Void> {Preconditions.checkNotNull(revision, "revision has to be specified.");Commandline cmd = cmd().addArgs("checkout");- if (newBranch)- cmd.addArgs("-b");+ if (newBranch != null)+ cmd.addArgs("-b", newBranch);cmd.addArgs(revision);- cmd.execute(debugLogger(), errorLogger()).checkReturnCode();+ cmd.execute(debugLogger(), new LineConsumer() {++ @Override+ public void consume(String line) {+ if (line.startsWith("Switched to a new branch"))+ info(line);+ else+ error(line);+ }++ }).checkReturnCode();return null;}diff --git a/commons.git/src/main/java/com/pmease/commons/git/command/PushCommand.java b/commons.git/src/main/java/com/pmease/commons/git/command/PushCommand.javanew file mode 100644index 0000000000..118e6874f6--- /dev/null+++ b/commons.git/src/main/java/com/pmease/commons/git/command/PushCommand.java@@ -0,0 +1,41 @@+package com.pmease.commons.git.command;++import java.io.File;++import com.google.common.base.Preconditions;+import com.pmease.commons.util.execution.Commandline;++public class PushCommand extends GitCommand<Void> {++ private String to;++ private String refspec;++ public PushCommand(final File repoDir) {+ super(repoDir);+ }++ public PushCommand to(String to) {+ this.to = to;+ return this;+ }++ public PushCommand refspec(String refspec) {+ this.refspec = refspec;+ return this;+ }++ @Override+ public Void call() {+ Preconditions.checkNotNull(to, "to param has to be specified.");+ Preconditions.checkNotNull(refspec, "refspec param has to be specified.");++ Commandline cmd = cmd().addArgs("push");+ cmd.addArgs(to, refspec);++ cmd.execute(debugLogger(), errorLogger()).checkReturnCode();++ return null;+ }++}diff --git a/commons.git/src/main/java/com/pmease/commons/git/command/ShowRefCommand.java b/commons.git/src/main/java/com/pmease/commons/git/command/ShowRefCommand.javaindex fcca8d4615..a54f2159c3 100644--- a/commons.git/src/main/java/com/pmease/commons/git/command/ShowRefCommand.java+++ b/commons.git/src/main/java/com/pmease/commons/git/command/ShowRefCommand.java@@ -9,6 +9,7 @@ import org.apache.commons.lang3.StringUtils;import com.google.common.base.Preconditions;import com.pmease.commons.git.RefInfo;import com.pmease.commons.util.execution.Commandline;+import com.pmease.commons.util.execution.ExecuteResult;import com.pmease.commons.util.execution.LineConsumer;public class ShowRefCommand extends GitCommand<List<RefInfo>> {@@ -31,7 +32,8 @@ public class ShowRefCommand extends GitCommand<List<RefInfo>> {Commandline cmd = cmd().addArgs("show-ref", pattern);final List<RefInfo> refs = new ArrayList<RefInfo>();- cmd.execute(new LineConsumer() {+ final boolean[] hasError = new boolean[]{false};+ ExecuteResult result = cmd.execute(new LineConsumer() {@Overridepublic void consume(String line) {@@ -40,7 +42,21 @@ public class ShowRefCommand extends GitCommand<List<RefInfo>> {refs.add(new RefInfo(refName, commitHash));}- }, errorLogger()).checkReturnCode();+ }, new LineConsumer() {++ @Override+ public void consume(String line) {+ error(line);+ hasError[0] = true;+ }++ });++ // When no refs are found, git returns error code 1, but we do not want+ // to treat this as an error. Instead only when there are error messages+ // being printed to stderr, we then check the return code.+ if (hasError[0])+ result.checkReturnCode();return refs;}diff --git a/commons.git/src/test/java/com/pmease/commons/git/command/DiffCommandTest.java b/commons.git/src/test/java/com/pmease/commons/git/command/DiffCommandTest.javaindex 74e59b3eb0..4832b636da 100644--- a/commons.git/src/test/java/com/pmease/commons/git/command/DiffCommandTest.java+++ b/commons.git/src/test/java/com/pmease/commons/git/command/DiffCommandTest.java@@ -48,7 +48,7 @@ public class DiffCommandTest {workGit.add("dir").commit("add readme, dir/file1 and dir/file2", false, false);- workGit.checkout("dev", true);+ workGit.checkout("head", "dev");FileUtils.writeFile(readme, "reame for dev");workGit.add("readme");diff --git a/commons.git/src/test/java/com/pmease/commons/git/command/ListTreeCommandTest.java b/commons.git/src/test/java/com/pmease/commons/git/command/ListTreeCommandTest.javaindex d2ef1ee5ab..2f290b962c 100644--- a/commons.git/src/test/java/com/pmease/commons/git/command/ListTreeCommandTest.java+++ b/commons.git/src/test/java/com/pmease/commons/git/command/ListTreeCommandTest.java@@ -57,7 +57,7 @@ public class ListTreeCommandTest {workGit.addSubModule(moduleGit.repoDir().getAbsolutePath(), "module");workGit.commit("commit", false, false);- workGit.checkout("dev", true);+ workGit.checkout("head", "dev");workGit.remove("dir/file");workGit.commit("commit", false, false);diff --git a/commons.git/src/test/java/com/pmease/commons/git/command/LogCommandTest.java b/commons.git/src/test/java/com/pmease/commons/git/command/LogCommandTest.javaindex b8f4cedee5..46c7fdca62 100644--- a/commons.git/src/test/java/com/pmease/commons/git/command/LogCommandTest.java+++ b/commons.git/src/test/java/com/pmease/commons/git/command/LogCommandTest.java@@ -50,7 +50,7 @@ public class LogCommandTest {workGit.add("dir/file2");workGit.commit("add dir/file\nadd dir/file to test files under a directory", false, false);- workGit.checkout("dev", true);+ workGit.checkout("head", "dev");workGit.remove("dir/file");workGit.commit("remove dir/file", false, false);@@ -61,7 +61,7 @@ public class LogCommandTest {List<Commit> commits = bareGit.log(null, "master", null, 0);assertEquals(commits.size(), 6);- assertEquals(commits.get(0).getSummary(), "add dir/file");+ assertEquals(commits.get(0).getSubject(), "add dir/file");assertEquals(commits.get(0).getMessage(), "add dir/file to test files under a directory");assertEquals("hello\nworld", commits.get(0).getNote());assertEquals(commits.get(0).getFileChanges().size(), 2);@@ -72,7 +72,7 @@ public class LogCommandTest {assertEquals(null, commits.get(1).getNote());- workGit.checkout("master", false).remove("a").commit("remove a", false, false);+ workGit.checkout("master", null).remove("a").commit("remove a", false, false);FileUtils.writeFile(new File(workGit.repoDir(), "dir/file2"), "file2");workGit.add("dir/file2").commit("add dir/file2", false, false);workGit.merge("dev", null, null, null);diff --git a/commons.util/src/main/java/com/pmease/commons/util/execution/LineConsumer.java b/commons.util/src/main/java/com/pmease/commons/util/execution/LineConsumer.javaindex 212ed9f4fe..817609b0e3 100644--- a/commons.util/src/main/java/com/pmease/commons/util/execution/LineConsumer.java+++ b/commons.util/src/main/java/com/pmease/commons/util/execution/LineConsumer.java@@ -101,7 +101,27 @@ public abstract class LineConsumer extends OutputStream {*/public abstract void consume(String line);- @Override+ protected void debug(String line) {+ logger.debug(line);+ }++ protected void trace(String line) {+ logger.trace(line);+ }++ protected void info(String line) {+ logger.info(line);+ }++ protected void warn(String line) {+ logger.warn(line);+ }++ protected void error(String line) {+ logger.error(line);+ }++ @Overridepublic void flush() throws IOException {if (buffer.size() > 0) {processBuffer();diff --git a/gitop.core/src/test/java/com/pmease/gitop/core/model/PullRequestTest.java b/gitop.core/src/test/java/com/pmease/gitop/core/model/PullRequestTest.javadeleted file mode 100644index 882ac1496b..0000000000--- a/gitop.core/src/test/java/com/pmease/gitop/core/model/PullRequestTest.java+++ /dev/null@@ -1,153 +0,0 @@-package com.pmease.gitop.core.model;--import java.io.File;--import org.junit.Assert;-import org.junit.Test;-import org.mockito.Mock;-import org.mockito.Mockito;--import com.pmease.commons.git.Git;-import com.pmease.commons.git.command.GitCommand;-import com.pmease.commons.loader.AppLoader;-import com.pmease.commons.loader.AppLoaderMocker;-import com.pmease.commons.util.FileUtils;-import com.pmease.gitop.model.Branch;-import com.pmease.gitop.model.Project;-import com.pmease.gitop.model.PullRequest;-import com.pmease.gitop.model.PullRequestUpdate;-import com.pmease.gitop.model.storage.ProjectStorage;-import com.pmease.gitop.model.storage.StorageManager;--public class PullRequestTest extends AppLoaderMocker {-- private File projectDir;-- private Git git;-- @Mock- private StorageManager storageManager;-- @Override- public void setup() {- Assert.assertTrue(GitCommand.checkError() == null);- projectDir = FileUtils.createTempDir();-- git = new Git(new File(projectDir, "code"));- git.init(false);- }-- @Override- public void teardown() {- FileUtils.deleteDir(projectDir);- }-- @Test- public void shouldReturnAllUpdatesAsEffectiveIfTheyAreFastForward() {- FileUtils.touchFile(new File(git.repoDir(), "a"));- git.add("a");- git.commit("commit", false, false);-- git.checkout("dev", true);-- FileUtils.touchFile(new File(git.repoDir(), "b"));- git.add("b");- git.commit("commit", false, false);-- git.updateRef("refs/updates/1", "HEAD", null, null);-- FileUtils.touchFile(new File(git.repoDir(), "c"));- git.add("c");- git.commit("commit", false, false);-- git.updateRef("refs/updates/2", "HEAD", null, null);-- Mockito.when(AppLoader.getInstance(StorageManager.class)).thenReturn(storageManager);-- Mockito.when(storageManager.getStorage(Mockito.any(Project.class)))- .thenReturn(new ProjectStorage(projectDir));-- PullRequest pullRequest = new PullRequest();- Branch destBranch = new Branch();- destBranch.setName("master");- destBranch.setProject(new Project());- pullRequest.setTarget(destBranch);-- PullRequestUpdate update = new PullRequestUpdate();- update.setId(1L);- update.setRequest(pullRequest);- pullRequest.getUpdates().add(update);-- update = new PullRequestUpdate();- update.setId(2L);- update.setRequest(pullRequest);- pullRequest.getUpdates().add(update);-- Assert.assertEquals(pullRequest.getEffectiveUpdates().size(), 2);- }-- @Test- public void shouldReturnLatestUpdateAsEffectiveIfAllOthersHaveBeenMerged() {- FileUtils.touchFile(new File(git.repoDir(), "a"));- git.add("a");- git.commit("commit", false, false);-- git.checkout("dev", true);-- FileUtils.touchFile(new File(git.repoDir(), "b"));- git.add("b");- git.commit("commit", false, false);-- git.updateRef("refs/updates/1", "HEAD", null, null);-- FileUtils.touchFile(new File(git.repoDir(), "c"));- git.add("c");- git.commit("commit", false, false);-- git.updateRef("refs/updates/2", "HEAD", null, null);-- FileUtils.touchFile(new File(git.repoDir(), "d"));- git.add("d");- git.commit("commit", false, false);-- git.updateRef("refs/updates/3", "HEAD", null, null);-- git.checkout("master", false);-- FileUtils.touchFile(new File(git.repoDir(), "e"));- git.add("e");- git.commit("commit", false, false);-- git.merge("refs/updates/2", null, null, null);-- Mockito.when(AppLoader.getInstance(StorageManager.class)).thenReturn(storageManager);-- Mockito.when(storageManager.getStorage(Mockito.any(Project.class)))- .thenReturn(new ProjectStorage(projectDir));-- PullRequest pullRequest = new PullRequest();- Branch destBranch = new Branch();- destBranch.setName("master");- destBranch.setProject(new Project());- pullRequest.setTarget(destBranch);-- PullRequestUpdate update = new PullRequestUpdate();- update.setId(1L);- update.setRequest(pullRequest);- pullRequest.getUpdates().add(update);-- update = new PullRequestUpdate();- update.setId(2L);- update.setRequest(pullRequest);- pullRequest.getUpdates().add(update);-- update = new PullRequestUpdate();- update.setId(3L);- update.setRequest(pullRequest);- pullRequest.getUpdates().add(update);-- Assert.assertEquals(pullRequest.getEffectiveUpdates().size(), 2);- Assert.assertEquals(pullRequest.getEffectiveUpdates().get(0).getId().longValue(), 3L);- }--}diff --git a/gitop.model/src/main/java/com/pmease/gitop/model/PullRequest.java b/gitop.model/src/main/java/com/pmease/gitop/model/PullRequest.javaindex 70265d6775..bb5fad6752 100644--- a/gitop.model/src/main/java/com/pmease/gitop/model/PullRequest.java+++ b/gitop.model/src/main/java/com/pmease/gitop/model/PullRequest.java@@ -192,7 +192,8 @@ public class PullRequest extends AbstractEntity {* Get list of effective updates in reverse order. Update is considered* effective if it is not ancestor of target branch head.*- * @return list of effective updates in reverse order.+ * @return+ * list of effective updates in reverse order.*/public List<PullRequestUpdate> getEffectiveUpdates() {if (effectiveUpdates == null) {diff --git a/gitop.model/src/main/java/com/pmease/gitop/model/PullRequestUpdate.java b/gitop.model/src/main/java/com/pmease/gitop/model/PullRequestUpdate.javaindex 15e0fb1717..a2a1b4b2ba 100644--- a/gitop.model/src/main/java/com/pmease/gitop/model/PullRequestUpdate.java+++ b/gitop.model/src/main/java/com/pmease/gitop/model/PullRequestUpdate.java@@ -82,8 +82,10 @@ public class PullRequestUpdate extends AbstractEntity {/*** Calculate base commit for change calculation of this update. Base commit is merged* commit of:+ ** <li> merge base of head ref and target branch head* <li> head ref of previous update+ ** Changed files of this update will be calculated between change base and head ref* and this effectively represents changes made since previous update with merged* changes from target branch excluded if there is any.@@ -99,10 +101,10 @@ public class PullRequestUpdate extends AbstractEntity {Preconditions.checkState(index != -1);String previousUpdate;- if (index == 0) {+ if (index >= getRequest().getSortedUpdates().size()-1) {previousUpdate = mergeBase;} else {- previousUpdate = getRequest().getSortedUpdates().get(index-1).getHeadCommit();+ previousUpdate = getRequest().getSortedUpdates().get(index+1).getHeadCommit();}if (git.isAncestor(previousUpdate, mergeBase)) {diff --git a/gitop.model/src/test/java/com/pmease/gitop/model/PullRequestTest.java b/gitop.model/src/test/java/com/pmease/gitop/model/PullRequestTest.javanew file mode 100644index 0000000000..0192e476c6--- /dev/null+++ b/gitop.model/src/test/java/com/pmease/gitop/model/PullRequestTest.java@@ -0,0 +1,139 @@+package com.pmease.gitop.model;++import java.io.File;++import org.junit.After;+import org.junit.Assert;+import org.junit.Before;+import org.junit.Test;+import org.mockito.Mockito;++import com.pmease.commons.git.Git;+import com.pmease.commons.git.command.GitCommand;+import com.pmease.commons.util.FileUtils;++public class PullRequestTest {++ private File projectDir;++ private Git git;++ private Project project;++ @Before+ public void setup() {+ Assert.assertTrue(GitCommand.checkError() == null);+ projectDir = FileUtils.createTempDir();++ git = new Git(new File(projectDir, "code"));+ git.init(false);++ project = Mockito.mock(Project.class);+ Mockito.when(project.code()).thenReturn(git);+ }++ @After+ public void teardown() {+ FileUtils.deleteDir(projectDir);+ }++ @Test+ public void shouldReturnAllUpdatesAsEffectiveIfTheyAreFastForward() {+ PullRequest request = new PullRequest();+ Branch target = new Branch();+ target.setName("master");+ target.setProject(project);+ request.setTarget(target);++ FileUtils.touchFile(new File(git.repoDir(), "a"));+ git.add("a");+ git.commit("commit", false, false);++ git.checkout("head", "dev");++ FileUtils.touchFile(new File(git.repoDir(), "b"));+ git.add("b");+ git.commit("commit", false, false);++ PullRequestUpdate update = new PullRequestUpdate();+ update.setId(1L);+ update.setRequest(request);+ git.updateRef(update.getHeadRef(), "HEAD", null, null);+ update.setHeadCommit(git.resolveRef(update.getHeadRef(), true));+ request.getUpdates().add(update);++ FileUtils.touchFile(new File(git.repoDir(), "c"));+ git.add("c");+ git.commit("commit", false, false);++ update = new PullRequestUpdate();+ update.setId(2L);+ update.setRequest(request);+ git.updateRef(update.getHeadRef(), "HEAD", null, null);+ update.setHeadCommit(git.resolveRef(update.getHeadRef(), true));+ request.getUpdates().add(update);++ Assert.assertEquals(request.getEffectiveUpdates().size(), 2);+ }++ @Test+ public void shouldReturnLatestUpdateAsEffectiveIfAllOthersHaveBeenMerged() {+ PullRequest request = new PullRequest();+ Branch target = new Branch();+ target.setName("master");+ target.setProject(project);+ request.setTarget(target);++ FileUtils.touchFile(new File(git.repoDir(), "a"));+ git.add("a");+ git.commit("master:1", false, false);++ git.checkout("head", "dev");++ FileUtils.touchFile(new File(git.repoDir(), "b"));+ git.add("b");+ git.commit("dev:2", false, false);++ PullRequestUpdate update = new PullRequestUpdate();+ update.setId(1L);+ update.setRequest(request);+ git.updateRef(update.getHeadRef(), "HEAD", null, null);+ update.setHeadCommit(git.resolveRef(update.getHeadRef(), true));+ request.getUpdates().add(update);++ FileUtils.touchFile(new File(git.repoDir(), "c"));+ git.add("c");+ git.commit("dev:3", false, false);++ update = new PullRequestUpdate();+ update.setId(2L);+ update.setRequest(request);+ git.updateRef(update.getHeadRef(), "HEAD", null, null);+ update.setHeadCommit(git.resolveRef(update.getHeadRef(), true));+ String secondRef = update.getHeadRef();+ request.getUpdates().add(update);++ FileUtils.touchFile(new File(git.repoDir(), "d"));+ git.add("d");+ git.commit("dev:4", false, false);++ update = new PullRequestUpdate();+ update.setId(3L);+ update.setRequest(request);+ git.updateRef(update.getHeadRef(), "HEAD", null, null);+ update.setHeadCommit(git.resolveRef(update.getHeadRef(), true));+ request.getUpdates().add(update);++ git.checkout("master", null);++ FileUtils.touchFile(new File(git.repoDir(), "e"));+ git.add("e");+ git.commit("master:5", false, false);++ git.merge(secondRef, null, null, null);++ Assert.assertEquals(1, request.getEffectiveUpdates().size());+ Assert.assertEquals(3L, request.getEffectiveUpdates().get(0).getId().longValue());+ }++}\ No newline at end of filediff --git a/gitop.model/src/test/java/com/pmease/gitop/model/PullRequestUpdateTest.java b/gitop.model/src/test/java/com/pmease/gitop/model/PullRequestUpdateTest.javanew file mode 100644index 0000000000..32e7f1a5be--- /dev/null+++ b/gitop.model/src/test/java/com/pmease/gitop/model/PullRequestUpdateTest.java@@ -0,0 +1,146 @@+package com.pmease.gitop.model;++import java.io.File;++import org.junit.After;+import org.junit.Assert;+import org.junit.Before;+import org.junit.Test;+import org.mockito.Mockito;++import com.pmease.commons.git.Commit;+import com.pmease.commons.git.Git;+import com.pmease.commons.git.command.GitCommand;+import com.pmease.commons.util.FileUtils;++public class PullRequestUpdateTest {++ private File projectDir;++ private Git workGit;++ private Git bareGit;++ private Project project;++ @Before+ public void setup() {+ Assert.assertTrue(GitCommand.checkError() == null);+ projectDir = FileUtils.createTempDir();++ bareGit = new Git(new File(projectDir, "code"));+ bareGit.init(true);++ workGit = new Git(new File(projectDir, "work"));+ workGit.clone(bareGit.repoDir().getAbsolutePath(), false);++ project = Mockito.mock(Project.class);+ Mockito.when(project.code()).thenReturn(bareGit);+ }++ @After+ public void teardown() {+ FileUtils.deleteDir(projectDir);+ }++ @Test+ public void testResolveBaseCommitWhenThereIsNoMerge() {+ PullRequest request = new PullRequest();+ Branch target = new Branch();+ target.setProject(project);+ target.setName("master");+ request.setTarget(target);++ FileUtils.touchFile(new File(workGit.repoDir(), "a"));+ workGit.add("a");+ workGit.commit("master:1", false, false);++ workGit.checkout("head", "dev");++ FileUtils.touchFile(new File(workGit.repoDir(), "b"));+ workGit.add("b");+ workGit.commit("dev:2", false, false);++ FileUtils.touchFile(new File(workGit.repoDir(), "c"));+ workGit.add("c");+ workGit.commit("dev:3", false, false);++ workGit.checkout("master", null);++ FileUtils.touchFile(new File(workGit.repoDir(), "d"));+ workGit.add("d");+ workGit.commit("master:4", false, false);++ workGit.push(bareGit.repoDir().getAbsolutePath(), "master:master");+ workGit.push(bareGit.repoDir().getAbsolutePath(), "dev:dev");++ PullRequestUpdate update = new PullRequestUpdate();+ update.setId(1L);+ update.setRequest(request);+ update.setHeadCommit(bareGit.resolveRevision("dev~1").getHash());+ bareGit.updateRef(update.getHeadRef(), update.getHeadCommit(), null, null);+ request.getUpdates().add(update);++ update = new PullRequestUpdate();+ update.setId(2L);+ update.setRequest(request);+ update.setHeadCommit(bareGit.resolveRevision("dev").getHash());+ bareGit.updateRef(update.getHeadRef(), update.getHeadCommit(), null, null);+ request.getUpdates().add(update);++ Assert.assertEquals(bareGit.resolveRevision("dev~1").getHash(), request.getLatestUpdate().getBaseCommit());+ Assert.assertEquals(bareGit.resolveRevision("master~1").getHash(), request.getSortedUpdates().get(1).getBaseCommit());+ }++ @Test+ public void testResolveBaseCommitWhenThereIsMerge() {+ PullRequest request = new PullRequest();+ Branch target = new Branch();+ target.setProject(project);+ target.setName("master");+ request.setTarget(target);++ FileUtils.touchFile(new File(workGit.repoDir(), "1"));+ workGit.add("1");+ workGit.commit("master:1", false, false);++ FileUtils.touchFile(new File(workGit.repoDir(), "2"));+ workGit.add("2");+ workGit.commit("master:2", false, false);++ workGit.checkout("master~1", "dev");++ FileUtils.touchFile(new File(workGit.repoDir(), "3"));+ workGit.add("3");+ workGit.commit("dev:3", false, false);++ workGit.merge("master", null, null, null);++ FileUtils.touchFile(new File(workGit.repoDir(), "4"));+ workGit.add("4");+ workGit.commit("dev:4", false, false);++ workGit.push(bareGit.repoDir().getAbsolutePath(), "master:master");+ workGit.push(bareGit.repoDir().getAbsolutePath(), "dev:dev");++ PullRequestUpdate update = new PullRequestUpdate();+ update.setId(1L);+ update.setRequest(request);+ update.setHeadCommit(bareGit.resolveRevision("dev~2").getHash());+ bareGit.updateRef(update.getHeadRef(), update.getHeadCommit(), null, null);+ request.getUpdates().add(update);++ update = new PullRequestUpdate();+ update.setId(2L);+ update.setRequest(request);+ update.setHeadCommit(bareGit.resolveRevision("dev").getHash());+ bareGit.updateRef(update.getHeadRef(), update.getHeadCommit(), null, null);+ request.getUpdates().add(update);++ Commit baseCommit = bareGit.resolveRevision(request.getLatestUpdate().getBaseCommit());+ Assert.assertTrue(baseCommit.getParentHashes().contains(bareGit.resolveRevision("master").getHash()));+ Assert.assertTrue(baseCommit.getParentHashes().contains(bareGit.resolveRevision("dev~2").getHash()));+ Assert.assertEquals(bareGit.resolveRevision("master~1").getHash(), request.getSortedUpdates().get(1).getBaseCommit());+ }++}\ No newline at end of filediff --git a/gitop.web/src/main/java/com/pmease/gitop/web/util/GitUtils.java b/gitop.web/src/main/java/com/pmease/gitop/web/util/GitUtils.javaindex 465550eaf4..28874f5536 100644--- a/gitop.web/src/main/java/com/pmease/gitop/web/util/GitUtils.java+++ b/gitop.web/src/main/java/com/pmease/gitop/web/util/GitUtils.java@@ -27,7 +27,7 @@ public class GitUtils {}public static String getCommitSummary(Commit commit) {- return commit.getSummary();+ return commit.getSubject();}public static boolean hasCommits(File gitDir) {