#873 implemented default branch repository property for git

This commit is contained in:
Sebastian Sdorra
2016-11-10 19:28:46 +01:00
parent 807eccf459
commit df7b554b80
12 changed files with 182 additions and 42 deletions

View File

@@ -34,11 +34,17 @@ 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;
//~--- JDK imports ------------------------------------------------------------
import java.io.IOException;
import org.eclipse.jgit.lib.ObjectId;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sonia.scm.repository.GitUtil;
/**
*
@@ -46,6 +52,14 @@ import java.io.IOException;
*/
public class AbstractGitCommand
{
/**
* the logger for AbstractGitCommand
*/
private static final Logger logger = LoggerFactory.getLogger(AbstractGitCommand.class);
@VisibleForTesting
static final String PROPERTY_DEFAULT_BRANCH = "git.default-branch";
/**
* Constructs ...
@@ -54,7 +68,6 @@ public class AbstractGitCommand
*
* @param context
* @param repository
* @param repositoryDirectory
*/
protected AbstractGitCommand(GitContext context,
sonia.scm.repository.Repository repository)
@@ -77,6 +90,38 @@ public class AbstractGitCommand
{
return context.open();
}
protected ObjectId getCommitOrDefault(Repository gitRepository, String requestedCommit) throws IOException {
ObjectId commit;
if ( Strings.isNullOrEmpty(requestedCommit) ) {
commit = getDefaultBranch(gitRepository);
} else {
commit = gitRepository.resolve(requestedCommit);
}
return commit;
}
protected ObjectId getBranchOrDefault(Repository gitRepository, String requestedBranch) throws IOException {
ObjectId head;
if ( Strings.isNullOrEmpty(requestedBranch) ) {
head = getDefaultBranch(gitRepository);
} else {
head = GitUtil.getBranchId(gitRepository, requestedBranch);
}
return head;
}
protected ObjectId getDefaultBranch(Repository gitRepository) throws IOException {
ObjectId head;
String defaultBranchName = repository.getProperty(PROPERTY_DEFAULT_BRANCH);
if (!Strings.isNullOrEmpty(defaultBranchName)) {
head = GitUtil.getBranchId(gitRepository, defaultBranchName);
} else {
logger.trace("no default branch configured, use repository head as default");
head = GitUtil.getRepositoryHead(gitRepository);
}
return head;
}
//~--- fields ---------------------------------------------------------------

View File

@@ -138,7 +138,7 @@ public abstract class AbstractGitIncomingOutgoingCommand
GitUtil.fetch(git, handler.getDirectory(remoteRepository), remoteRepository);
ObjectId localId = GitUtil.getRepositoryHead(git.getRepository());
ObjectId localId = getDefaultBranch(git.getRepository());
ObjectId remoteId = null;
Ref remoteBranch = getRemoteBranch(git.getRepository(), localId,

View File

@@ -124,7 +124,7 @@ public class GitBlameCommand extends AbstractGitCommand implements BlameCommand
blame.setFilePath(request.getPath());
ObjectId revId = GitUtil.getRevisionId(gr, request.getRevision());
ObjectId revId = getCommitOrDefault(gr, request.getRevision());
blame.setStartCommit(revId);

View File

@@ -45,7 +45,6 @@ import org.eclipse.jgit.lib.ObjectLoader;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.revwalk.RevTree;
import org.eclipse.jgit.revwalk.RevWalk;
import org.eclipse.jgit.submodule.SubmoduleWalk;
import org.eclipse.jgit.treewalk.TreeWalk;
import org.eclipse.jgit.treewalk.filter.AndTreeFilter;
import org.eclipse.jgit.treewalk.filter.PathFilter;
@@ -72,7 +71,6 @@ import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
/**
*
@@ -96,11 +94,8 @@ public class GitBrowseCommand extends AbstractGitCommand
/**
* Constructs ...
*
*
*
* @param context
* @param repository
* @param repositoryDirectory
*/
public GitBrowseCommand(GitContext context, Repository repository)
{
@@ -124,18 +119,15 @@ public class GitBrowseCommand extends AbstractGitCommand
public BrowserResult getBrowserResult(BrowseCommandRequest request)
throws IOException, RepositoryException
{
if (logger.isDebugEnabled())
{
logger.debug("try to create browse result for {}", request);
}
logger.debug("try to create browse result for {}", request);
BrowserResult result = null;
BrowserResult result;
org.eclipse.jgit.lib.Repository repo = open();
ObjectId revId = null;
ObjectId revId;
if (Util.isEmpty(request.getRevision()))
{
revId = GitUtil.getRepositoryHead(repo);
revId = getDefaultBranch(repo);
}
else
{

View File

@@ -34,6 +34,7 @@ package sonia.scm.repository.spi;
//~--- non-JDK imports --------------------------------------------------------
import com.google.common.base.Strings;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.ObjectLoader;
@@ -78,7 +79,6 @@ public class GitCatCommand extends AbstractGitCommand implements CatCommand
*
* @param context
* @param repository
* @param repositoryDirectory
*/
public GitCatCommand(GitContext context,
sonia.scm.repository.Repository repository)
@@ -102,17 +102,11 @@ public class GitCatCommand extends AbstractGitCommand implements CatCommand
public void getCatResult(CatCommandRequest request, OutputStream output)
throws IOException, RepositoryException
{
if (logger.isDebugEnabled())
{
logger.debug("try to read content for {}", request);
}
org.eclipse.jgit.lib.Repository repo = null;
repo = open();
ObjectId revId = GitUtil.getRevisionId(repo, request.getRevision());
logger.debug("try to read content for {}", request);
org.eclipse.jgit.lib.Repository repo = open();
ObjectId revId = getCommitOrDefault(repo, request.getRevision());
getContent(repo, revId, request.getPath(), output);
}

View File

@@ -79,7 +79,6 @@ public class GitDiffCommand extends AbstractGitCommand implements DiffCommand
*
* @param context
* @param repository
* @param repositoryDirectory
*/
public GitDiffCommand(GitContext context, Repository repository)
{

View File

@@ -220,17 +220,8 @@ public class GitLogCommand extends AbstractGitCommand implements LogCommand
AndTreeFilter.create(
PathFilter.create(request.getPath()), TreeFilter.ANY_DIFF));
}
ObjectId head = null;
if (!Strings.isNullOrEmpty(request.getBranch()))
{
head = GitUtil.getBranchId(gr, request.getBranch());
}
else
{
head = GitUtil.getRepositoryHead(gr);
}
ObjectId head = getBranchOrDefault(gr, request.getBranch());
if (head != null)
{

View File

@@ -47,12 +47,39 @@ import static org.junit.Assert.*;
import java.io.IOException;
/**
*
* Unit tests for {@link GitBlameCommand}.
*
* @author Sebastian Sdorra
*/
public class GitBlameCommandTest extends AbstractGitCommandTestBase
{
/**
* Tests blame command with default branch.
*
* @throws IOException
* @throws RepositoryException
*/
@Test
public void testDefaultBranch() throws IOException, RepositoryException {
// without default branch, the repository head should be used
BlameCommandRequest request = new BlameCommandRequest();
request.setPath("a.txt");
BlameResult result = createCommand().getBlameResult(request);
assertNotNull(result);
assertEquals(2, result.getTotal());
assertEquals("435df2f061add3589cb326cc64be9b9c3897ceca", result.getLine(0).getRevision());
assertEquals("fcd0ef1831e4002ac43ea539f4094334c79ea9ec", result.getLine(1).getRevision());
// set default branch and test again
repository.setProperty(AbstractGitCommand.PROPERTY_DEFAULT_BRANCH, "test-branch");
result = createCommand().getBlameResult(request);
assertNotNull(result);
assertEquals(1, result.getTotal());
assertEquals("3f76a12f08a6ba0dc988c68b7f0b2cd190efc3c4", result.getLine(0).getRevision());
}
/**
* Method description
*

View File

@@ -50,11 +50,48 @@ import java.io.IOException;
import java.util.List;
/**
*
* Unit tests for {@link GitBrowseCommand}.
*
* @author Sebastian Sdorra
*/
public class GitBrowseCommandTest extends AbstractGitCommandTestBase
{
/**
* Test browse command with default branch.
*
* @throws IOException
* @throws RepositoryException
*/
@Test
public void testDefaultBranch() throws IOException, RepositoryException {
// without default branch, the repository head should be used
BrowserResult result = createCommand().getBrowserResult(new BrowseCommandRequest());
assertNotNull(result);
List<FileObject> foList = result.getFiles();
assertNotNull(foList);
assertFalse(foList.isEmpty());
assertEquals(4, foList.size());
assertEquals("a.txt", foList.get(0).getName());
assertEquals("b.txt", foList.get(1).getName());
assertEquals("c", foList.get(2).getName());
assertEquals("f.txt", foList.get(3).getName());
// set default branch and fetch again
repository.setProperty(AbstractGitCommand.PROPERTY_DEFAULT_BRANCH, "test-branch");
result = createCommand().getBrowserResult(new BrowseCommandRequest());
assertNotNull(result);
foList = result.getFiles();
assertNotNull(foList);
assertFalse(foList.isEmpty());
assertEquals(2, foList.size());
assertEquals("a.txt", foList.get(0).getName());
assertEquals("c", foList.get(1).getName());
}
/**
* Method description

View File

@@ -46,12 +46,33 @@ import java.io.ByteArrayOutputStream;
import java.io.IOException;
/**
* Unit tests for {@link GitCatCommand}.
*
* TODO add not found test
*
* @author Sebastian Sdorra
*/
public class GitCatCommandTest extends AbstractGitCommandTestBase
{
/**
* Tests cat command with default branch.
*
* @throws IOException
* @throws RepositoryException
*/
@Test
public void testDefaultBranch() throws IOException, RepositoryException {
// without default branch, the repository head should be used
CatCommandRequest request = new CatCommandRequest();
request.setPath("a.txt");
assertEquals("a\nline for blame", execute(request));
// set default branch for repository and check again
repository.setProperty(AbstractGitCommand.PROPERTY_DEFAULT_BRANCH, "test-branch");
assertEquals("a and b", execute(request));
}
/**
* Method description

View File

@@ -49,14 +49,47 @@ import static org.junit.Assert.*;
//~--- JDK imports ------------------------------------------------------------
import java.io.IOException;
import org.eclipse.jgit.api.errors.GitAPIException;
/**
*
* Unit tests for {@link GitLogCommand}.
*
* @author Sebastian Sdorra
*/
public class GitLogCommandTest extends AbstractGitCommandTestBase
{
/**
* Tests log command with the usage of a default branch.
*
* @throws IOException
* @throws GitAPIException
* @throws RepositoryException
*/
@Test
public void testGetDefaultBranch() throws IOException, GitAPIException, RepositoryException {
// without default branch, the repository head should be used
ChangesetPagingResult result = createCommand().getChangesets(new LogCommandRequest());
assertNotNull(result);
assertEquals(4, result.getTotal());
assertEquals("fcd0ef1831e4002ac43ea539f4094334c79ea9ec", result.getChangesets().get(0).getId());
assertEquals("86a6645eceefe8b9a247db5eb16e3d89a7e6e6d1", result.getChangesets().get(1).getId());
assertEquals("592d797cd36432e591416e8b2b98154f4f163411", result.getChangesets().get(2).getId());
assertEquals("435df2f061add3589cb326cc64be9b9c3897ceca", result.getChangesets().get(3).getId());
// set default branch and fetch again
repository.setProperty(AbstractGitCommand.PROPERTY_DEFAULT_BRANCH, "test-branch");
result = createCommand().getChangesets(new LogCommandRequest());
assertNotNull(result);
assertEquals(3, result.getTotal());
assertEquals("3f76a12f08a6ba0dc988c68b7f0b2cd190efc3c4", result.getChangesets().get(0).getId());
assertEquals("592d797cd36432e591416e8b2b98154f4f163411", result.getChangesets().get(1).getId());
assertEquals("435df2f061add3589cb326cc64be9b9c3897ceca", result.getChangesets().get(2).getId());
}
/**
* Method description
*

View File

@@ -51,7 +51,8 @@ import static org.junit.Assert.assertNotNull;
import java.io.IOException;
/**
*
* Unit tests for {@link OutgoingCommand}.
*
* @author Sebastian Sdorra
*/
public class GitOutgoingCommandTest extends AbstractRemoteCommandTestBase