#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)
{