mirror of
https://github.com/scm-manager/scm-manager.git
synced 2026-07-04 11:49:53 +02:00
fix bug in git push command and added unit test for git push
This commit is contained in:
@@ -39,6 +39,9 @@ import com.google.common.annotations.VisibleForTesting;
|
||||
import com.google.common.collect.ArrayListMultimap;
|
||||
import com.google.common.collect.Multimap;
|
||||
|
||||
import org.eclipse.jgit.api.FetchCommand;
|
||||
import org.eclipse.jgit.api.Git;
|
||||
import org.eclipse.jgit.api.errors.GitAPIException;
|
||||
import org.eclipse.jgit.diff.DiffFormatter;
|
||||
import org.eclipse.jgit.errors.IncorrectObjectTypeException;
|
||||
import org.eclipse.jgit.errors.MissingObjectException;
|
||||
@@ -48,6 +51,8 @@ import org.eclipse.jgit.lib.Ref;
|
||||
import org.eclipse.jgit.lib.RepositoryCache;
|
||||
import org.eclipse.jgit.revwalk.RevCommit;
|
||||
import org.eclipse.jgit.revwalk.RevWalk;
|
||||
import org.eclipse.jgit.transport.FetchResult;
|
||||
import org.eclipse.jgit.transport.RefSpec;
|
||||
import org.eclipse.jgit.treewalk.TreeWalk;
|
||||
import org.eclipse.jgit.util.FS;
|
||||
|
||||
@@ -62,6 +67,7 @@ import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
*
|
||||
@@ -85,6 +91,15 @@ public final class GitUtil
|
||||
/** Field description */
|
||||
private static final String PREFIX_TAG = "refs/tags/";
|
||||
|
||||
/** Field description */
|
||||
private static final String REFSPEC = "+refs/heads/*:refs/remote/scm/%s/*";
|
||||
|
||||
/** Field description */
|
||||
private static final String REMOTE_REF = "refs/remote/scm/%s/%s";
|
||||
|
||||
/** Field description */
|
||||
private static final int TIMEOUT = 5;
|
||||
|
||||
/** the logger for GitUtil */
|
||||
private static final Logger logger = LoggerFactory.getLogger(GitUtil.class);
|
||||
|
||||
@@ -160,6 +175,40 @@ public final class GitUtil
|
||||
return tags;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param git
|
||||
* @param directory
|
||||
* @param remoteRepository
|
||||
*
|
||||
* @return
|
||||
*
|
||||
* @throws GitAPIException
|
||||
*
|
||||
* @throws RepositoryException
|
||||
*/
|
||||
public static FetchResult fetch(Git git, File directory,
|
||||
Repository remoteRepository)
|
||||
throws RepositoryException
|
||||
{
|
||||
try
|
||||
{
|
||||
FetchCommand fetch = git.fetch();
|
||||
|
||||
fetch.setRemote(directory.getAbsolutePath());
|
||||
fetch.setRefSpecs(createRefSpec(remoteRepository));
|
||||
fetch.setTimeout((int) TimeUnit.MINUTES.toSeconds(TIMEOUT));
|
||||
|
||||
return fetch.call();
|
||||
}
|
||||
catch (GitAPIException ex)
|
||||
{
|
||||
throw new RepositoryException("could not fetch", ex);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
@@ -522,6 +571,36 @@ public final class GitUtil
|
||||
return revId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param repository
|
||||
* @param localBranch
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static String getScmRemoteRefName(Repository repository,
|
||||
Ref localBranch)
|
||||
{
|
||||
return getScmRemoteRefName(repository, localBranch.getName());
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param repository
|
||||
* @param localBranch
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static String getScmRemoteRefName(Repository repository,
|
||||
String localBranch)
|
||||
{
|
||||
return String.format(REMOTE_REF, repository.getId(), localBranch);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
@@ -575,4 +654,17 @@ public final class GitUtil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param repository
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
private static RefSpec createRefSpec(Repository repository)
|
||||
{
|
||||
return new RefSpec(String.format(REFSPEC, repository.getId()));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,14 +36,11 @@ package sonia.scm.repository.spi;
|
||||
import com.google.common.collect.Lists;
|
||||
import com.google.common.io.Closeables;
|
||||
|
||||
import org.eclipse.jgit.api.FetchCommand;
|
||||
import org.eclipse.jgit.api.Git;
|
||||
import org.eclipse.jgit.api.errors.GitAPIException;
|
||||
import org.eclipse.jgit.lib.ObjectId;
|
||||
import org.eclipse.jgit.lib.Ref;
|
||||
import org.eclipse.jgit.revwalk.RevCommit;
|
||||
import org.eclipse.jgit.revwalk.RevWalk;
|
||||
import org.eclipse.jgit.transport.RefSpec;
|
||||
|
||||
import sonia.scm.repository.Changeset;
|
||||
import sonia.scm.repository.ChangesetPagingResult;
|
||||
@@ -59,7 +56,6 @@ import java.io.IOException;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
*
|
||||
@@ -69,18 +65,9 @@ public abstract class AbstractGitIncomingOutgoingCommand
|
||||
extends AbstractGitCommand
|
||||
{
|
||||
|
||||
/** Field description */
|
||||
private static final String REFSPEC = "+refs/heads/*:refs/remote/scm/%s/*";
|
||||
|
||||
/** Field description */
|
||||
private static final String REMOTE_REF = "refs/remote/scm/%s/%s";
|
||||
|
||||
/** Field description */
|
||||
private static final String REMOTE_REF_PREFIX = "refs/remote/scm/%s/";
|
||||
|
||||
/** Field description */
|
||||
private static final int TIMEOUT = 5;
|
||||
|
||||
//~--- constructors ---------------------------------------------------------
|
||||
|
||||
/**
|
||||
@@ -115,6 +102,18 @@ public abstract class AbstractGitIncomingOutgoingCommand
|
||||
ObjectId remoteId)
|
||||
throws IOException;
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param localId
|
||||
* @param remoteId
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
protected abstract boolean retrieveChangesets(ObjectId localId,
|
||||
ObjectId remoteId);
|
||||
|
||||
//~--- get methods ----------------------------------------------------------
|
||||
|
||||
/**
|
||||
@@ -135,29 +134,17 @@ public abstract class AbstractGitIncomingOutgoingCommand
|
||||
Repository remoteRepository = request.getRemoteRepository();
|
||||
|
||||
Git git = Git.wrap(open());
|
||||
FetchCommand fetch = git.fetch();
|
||||
|
||||
fetch.setRemote(handler.getDirectory(remoteRepository).getAbsolutePath());
|
||||
fetch.setRefSpecs(createRefSpec(remoteRepository));
|
||||
fetch.setTimeout((int) TimeUnit.MINUTES.toSeconds(TIMEOUT));
|
||||
|
||||
try
|
||||
{
|
||||
fetch.call();
|
||||
}
|
||||
catch (GitAPIException ex)
|
||||
{
|
||||
throw new RepositoryException("could not fetch", ex);
|
||||
}
|
||||
GitUtil.fetch(git, handler.getDirectory(repository), remoteRepository);
|
||||
|
||||
ObjectId localId = GitUtil.getRepositoryHead(git.getRepository());
|
||||
ObjectId remoteId = null;
|
||||
|
||||
|
||||
Ref remoteBranch = getRemoteBranch(git.getRepository(), localId,
|
||||
remoteRepository);
|
||||
|
||||
if ( remoteBranch != null ){
|
||||
|
||||
if (remoteBranch != null)
|
||||
{
|
||||
remoteId = remoteBranch.getObjectId();
|
||||
}
|
||||
|
||||
@@ -202,25 +189,6 @@ public abstract class AbstractGitIncomingOutgoingCommand
|
||||
|
||||
return new ChangesetPagingResult(changesets.size(), changesets);
|
||||
}
|
||||
|
||||
protected abstract boolean retrieveChangesets(ObjectId localId, ObjectId remoteId);
|
||||
|
||||
//~--- methods --------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param repository
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
private RefSpec createRefSpec(Repository repository)
|
||||
{
|
||||
return new RefSpec(String.format(REFSPEC, repository.getId()));
|
||||
}
|
||||
|
||||
//~--- get methods ----------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Method description
|
||||
@@ -247,13 +215,14 @@ public abstract class AbstractGitIncomingOutgoingCommand
|
||||
|
||||
if (localBranch != null)
|
||||
{
|
||||
ref = repository.getRef(getScmRemoteRefName(remoteRepository,
|
||||
ref = repository.getRef(GitUtil.getScmRemoteRefName(remoteRepository,
|
||||
localBranch));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
ref = repository.getRef(getScmRemoteRefName(remoteRepository, "master"));
|
||||
ref = repository.getRef(GitUtil.getScmRemoteRefName(remoteRepository,
|
||||
"master"));
|
||||
|
||||
if (ref == null)
|
||||
{
|
||||
@@ -280,34 +249,6 @@ public abstract class AbstractGitIncomingOutgoingCommand
|
||||
return ref;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param repository
|
||||
* @param localBranch
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
private String getScmRemoteRefName(Repository repository, Ref localBranch)
|
||||
{
|
||||
return getScmRemoteRefName(repository, localBranch.getName());
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param repository
|
||||
* @param localBranch
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
private String getScmRemoteRefName(Repository repository, String localBranch)
|
||||
{
|
||||
return String.format(REMOTE_REF, repository.getId(), localBranch);
|
||||
}
|
||||
|
||||
//~--- fields ---------------------------------------------------------------
|
||||
|
||||
/** Field description */
|
||||
|
||||
@@ -33,10 +33,7 @@ package sonia.scm.repository.spi;
|
||||
|
||||
//~--- non-JDK imports --------------------------------------------------------
|
||||
|
||||
import com.google.common.collect.Iterables;
|
||||
|
||||
import org.eclipse.jgit.api.Git;
|
||||
import org.eclipse.jgit.transport.PushResult;
|
||||
|
||||
import sonia.scm.repository.GitRepositoryHandler;
|
||||
import sonia.scm.repository.Repository;
|
||||
@@ -96,9 +93,8 @@ public class GitPushCommand extends AbstractGitCommand implements PushCommand
|
||||
|
||||
try
|
||||
{
|
||||
Iterable<PushResult> results = push.call();
|
||||
|
||||
response = new PushResponse(Iterables.size(results));
|
||||
push.call();
|
||||
response = new PushResponse();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user