Add branch to changeset collections

Branch information is added to a changeset collection and therefore
removed from single changesets for git repositories. Mercurial
repositories now also set the default branch in changesets.
This commit is contained in:
René Pfeuffer
2018-09-26 13:19:38 +02:00
parent 538cd4fe72
commit 9f80840c43
16 changed files with 209 additions and 66 deletions

View File

@@ -39,7 +39,6 @@ import com.google.common.collect.Lists;
import com.google.common.collect.Multimap;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.PersonIdent;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.revwalk.RevWalk;
import org.eclipse.jgit.treewalk.TreeWalk;
@@ -51,8 +50,8 @@ import java.io.Closeable;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Set;
//~--- JDK imports ------------------------------------------------------------
@@ -130,27 +129,9 @@ public class GitChangesetConverter implements Closeable
*
* @throws IOException
*/
public Changeset createChangeset(RevCommit commit) throws IOException
public Changeset createChangeset(RevCommit commit)
{
List<String> branches = Lists.newArrayList();
Set<Ref> refs = repository.getAllRefsByPeeledObjectId().get(commit.getId());
if (Util.isNotEmpty(refs))
{
for (Ref ref : refs)
{
String branch = GitUtil.getBranch(ref);
if (branch != null)
{
branches.add(branch);
}
}
}
return createChangeset(commit, branches);
return createChangeset(commit, Collections.emptyList());
}
/**
@@ -165,7 +146,6 @@ public class GitChangesetConverter implements Closeable
* @throws IOException
*/
public Changeset createChangeset(RevCommit commit, String branch)
throws IOException
{
return createChangeset(commit, Lists.newArrayList(branch));
}
@@ -183,7 +163,6 @@ public class GitChangesetConverter implements Closeable
* @throws IOException
*/
public Changeset createChangeset(RevCommit commit, List<String> branches)
throws IOException
{
String id = commit.getId().name();
List<String> parentList = null;

View File

@@ -34,7 +34,6 @@ package sonia.scm.repository.spi;
//~--- non-JDK imports --------------------------------------------------------
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Strings;
import org.eclipse.jgit.lib.Repository;
@@ -97,7 +96,15 @@ public class AbstractGitCommand
}
return commit;
}
protected String getBranchNameOrDefault(String requestedBranch) {
if ( Strings.isNullOrEmpty(requestedBranch) ) {
return getDefaultBranchName();
} else {
return requestedBranch;
}
}
protected ObjectId getBranchOrDefault(Repository gitRepository, String requestedBranch) throws IOException {
ObjectId head;
if ( Strings.isNullOrEmpty(requestedBranch) ) {
@@ -107,7 +114,16 @@ public class AbstractGitCommand
}
return head;
}
protected String getDefaultBranchName() {
String defaultBranchName = repository.getProperty(GitConstants.PROPERTY_DEFAULT_BRANCH);
if (!Strings.isNullOrEmpty(defaultBranchName)) {
return defaultBranchName;
} else {
return null;
}
}
protected ObjectId getDefaultBranch(Repository gitRepository) throws IOException {
ObjectId head;
String defaultBranchName = repository.getProperty(GitConstants.PROPERTY_DEFAULT_BRANCH);

View File

@@ -170,8 +170,8 @@ public class GitLogCommand extends AbstractGitCommand implements LogCommand
GitChangesetConverter converter = null;
RevWalk revWalk = null;
try (org.eclipse.jgit.lib.Repository gr = open()) {
if (!gr.getAllRefs().isEmpty()) {
try (org.eclipse.jgit.lib.Repository repository = open()) {
if (!repository.getAllRefs().isEmpty()) {
int counter = 0;
int start = request.getPagingStart();
@@ -188,18 +188,18 @@ public class GitLogCommand extends AbstractGitCommand implements LogCommand
ObjectId startId = null;
if (!Strings.isNullOrEmpty(request.getStartChangeset())) {
startId = gr.resolve(request.getStartChangeset());
startId = repository.resolve(request.getStartChangeset());
}
ObjectId endId = null;
if (!Strings.isNullOrEmpty(request.getEndChangeset())) {
endId = gr.resolve(request.getEndChangeset());
endId = repository.resolve(request.getEndChangeset());
}
revWalk = new RevWalk(gr);
revWalk = new RevWalk(repository);
converter = new GitChangesetConverter(gr, revWalk);
converter = new GitChangesetConverter(repository, revWalk);
if (!Strings.isNullOrEmpty(request.getPath())) {
revWalk.setTreeFilter(
@@ -207,7 +207,8 @@ public class GitLogCommand extends AbstractGitCommand implements LogCommand
PathFilter.create(request.getPath()), TreeFilter.ANY_DIFF));
}
ObjectId head = getBranchOrDefault(gr, request.getBranch());
ObjectId head = getBranchOrDefault(repository, request.getBranch());
String branch = getBranchNameOrDefault(request.getBranch());
if (head != null) {
if (startId != null) {
@@ -234,10 +235,14 @@ public class GitLogCommand extends AbstractGitCommand implements LogCommand
}
}
changesets = new ChangesetPagingResult(counter, changesetList);
if (branch != null) {
changesets = new ChangesetPagingResult(counter, changesetList, branch);
} else {
changesets = new ChangesetPagingResult(counter, changesetList);
}
} else if (logger.isWarnEnabled()) {
logger.warn("the repository {} seems to be empty",
repository.getName());
this.repository.getName());
changesets = new ChangesetPagingResult(0, Collections.EMPTY_LIST);
}

View File

@@ -45,6 +45,7 @@ import static org.hamcrest.Matchers.contains;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
@@ -72,6 +73,8 @@ public class GitLogCommandTest extends AbstractGitCommandTestBase
assertEquals("86a6645eceefe8b9a247db5eb16e3d89a7e6e6d1", result.getChangesets().get(1).getId());
assertEquals("592d797cd36432e591416e8b2b98154f4f163411", result.getChangesets().get(2).getId());
assertEquals("435df2f061add3589cb326cc64be9b9c3897ceca", result.getChangesets().get(3).getId());
assertNull(result.getBranchName());
assertTrue(result.getChangesets().stream().allMatch(r -> r.getBranches().isEmpty()));
// set default branch and fetch again
repository.setProperty(GitConstants.PROPERTY_DEFAULT_BRANCH, "test-branch");
@@ -83,6 +86,8 @@ public class GitLogCommandTest extends AbstractGitCommandTestBase
assertEquals("3f76a12f08a6ba0dc988c68b7f0b2cd190efc3c4", result.getChangesets().get(0).getId());
assertEquals("592d797cd36432e591416e8b2b98154f4f163411", result.getChangesets().get(1).getId());
assertEquals("435df2f061add3589cb326cc64be9b9c3897ceca", result.getChangesets().get(2).getId());
assertEquals("test-branch", result.getBranchName());
assertTrue(result.getChangesets().stream().allMatch(r -> r.getBranches().isEmpty()));
}
@Test