mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-12-21 15:59:48 +01:00
Enable repository import with credentials
This commit is contained in:
@@ -27,12 +27,13 @@ package sonia.scm.repository.spi;
|
||||
//~--- non-JDK imports --------------------------------------------------------
|
||||
|
||||
import com.google.common.base.Preconditions;
|
||||
import com.google.common.base.Strings;
|
||||
import com.google.common.collect.Iterables;
|
||||
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.revwalk.RevCommit;
|
||||
import org.eclipse.jgit.transport.CredentialsProvider;
|
||||
import org.eclipse.jgit.transport.FetchResult;
|
||||
import org.eclipse.jgit.transport.RefSpec;
|
||||
import org.eclipse.jgit.transport.TagOpt;
|
||||
@@ -49,22 +50,21 @@ import sonia.scm.repository.api.PullResponse;
|
||||
import javax.inject.Inject;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.net.URL;
|
||||
|
||||
//~--- JDK imports ------------------------------------------------------------
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Sebastian Sdorra
|
||||
*/
|
||||
public class GitPullCommand extends AbstractGitPushOrPullCommand
|
||||
implements PullCommand
|
||||
{
|
||||
implements PullCommand {
|
||||
|
||||
/** Field description */
|
||||
/**
|
||||
* Field description
|
||||
*/
|
||||
private static final String REF_SPEC = "refs/heads/*:refs/heads/*";
|
||||
|
||||
/** Field description */
|
||||
/**
|
||||
* Field description
|
||||
*/
|
||||
private static final Logger logger =
|
||||
LoggerFactory.getLogger(GitPullCommand.class);
|
||||
|
||||
@@ -73,12 +73,11 @@ public class GitPullCommand extends AbstractGitPushOrPullCommand
|
||||
/**
|
||||
* Constructs ...
|
||||
*
|
||||
* @param handler
|
||||
* @param handler
|
||||
* @param context
|
||||
*/
|
||||
@Inject
|
||||
public GitPullCommand(GitRepositoryHandler handler, GitContext context)
|
||||
{
|
||||
public GitPullCommand(GitRepositoryHandler handler, GitContext context) {
|
||||
super(handler, context);
|
||||
}
|
||||
|
||||
@@ -87,30 +86,21 @@ public class GitPullCommand extends AbstractGitPushOrPullCommand
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param request
|
||||
*
|
||||
* @return
|
||||
*
|
||||
* @throws IOException
|
||||
*/
|
||||
@Override
|
||||
public PullResponse pull(PullCommandRequest request)
|
||||
throws IOException
|
||||
{
|
||||
throws IOException {
|
||||
PullResponse response;
|
||||
Repository sourceRepository = request.getRemoteRepository();
|
||||
|
||||
if (sourceRepository != null)
|
||||
{
|
||||
if (sourceRepository != null) {
|
||||
response = pullFromScmRepository(sourceRepository);
|
||||
}
|
||||
else if (request.getRemoteUrl() != null)
|
||||
{
|
||||
response = pullFromUrl(request.getRemoteUrl());
|
||||
}
|
||||
else
|
||||
{
|
||||
} else if (request.getRemoteUrl() != null) {
|
||||
response = pullFromUrl(request);
|
||||
} else {
|
||||
throw new IllegalArgumentException("repository or url is required");
|
||||
}
|
||||
|
||||
@@ -120,8 +110,7 @@ public class GitPullCommand extends AbstractGitPushOrPullCommand
|
||||
private PullResponse convert(Git git, FetchResult fetch) {
|
||||
long counter = 0l;
|
||||
|
||||
for (TrackingRefUpdate tru : fetch.getTrackingRefUpdates())
|
||||
{
|
||||
for (TrackingRefUpdate tru : fetch.getTrackingRefUpdates()) {
|
||||
counter += count(git, tru);
|
||||
}
|
||||
|
||||
@@ -133,52 +122,40 @@ public class GitPullCommand extends AbstractGitPushOrPullCommand
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param git
|
||||
* @param tru
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
private long count(Git git, TrackingRefUpdate tru)
|
||||
{
|
||||
private long count(Git git, TrackingRefUpdate tru) {
|
||||
long counter = 0;
|
||||
|
||||
if (GitUtil.isHead(tru.getLocalName()))
|
||||
{
|
||||
try
|
||||
{
|
||||
if (GitUtil.isHead(tru.getLocalName())) {
|
||||
try {
|
||||
org.eclipse.jgit.api.LogCommand log = git.log();
|
||||
|
||||
ObjectId oldId = tru.getOldObjectId();
|
||||
|
||||
if (GitUtil.isValidObjectId(oldId))
|
||||
{
|
||||
if (GitUtil.isValidObjectId(oldId)) {
|
||||
log.not(oldId);
|
||||
}
|
||||
|
||||
ObjectId newId = tru.getNewObjectId();
|
||||
|
||||
if (GitUtil.isValidObjectId(newId))
|
||||
{
|
||||
if (GitUtil.isValidObjectId(newId)) {
|
||||
log.add(newId);
|
||||
}
|
||||
|
||||
Iterable<RevCommit> commits = log.call();
|
||||
|
||||
if (commits != null)
|
||||
{
|
||||
if (commits != null) {
|
||||
counter += Iterables.size(commits);
|
||||
}
|
||||
|
||||
logger.trace("counting {} commits for ref update {}", counter, tru);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
} catch (Exception ex) {
|
||||
logger.error("could not count pushed/pulled changesets", ex);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
logger.debug("do not count non branch ref update {}", tru);
|
||||
}
|
||||
|
||||
@@ -186,8 +163,7 @@ public class GitPullCommand extends AbstractGitPushOrPullCommand
|
||||
}
|
||||
|
||||
private PullResponse pullFromScmRepository(Repository sourceRepository)
|
||||
throws IOException
|
||||
{
|
||||
throws IOException {
|
||||
File sourceDirectory = handler.getDirectory(sourceRepository.getId());
|
||||
|
||||
Preconditions.checkArgument(sourceDirectory.exists(),
|
||||
@@ -205,41 +181,42 @@ public class GitPullCommand extends AbstractGitPushOrPullCommand
|
||||
|
||||
org.eclipse.jgit.lib.Repository source = null;
|
||||
|
||||
try
|
||||
{
|
||||
try {
|
||||
source = Git.open(sourceDirectory).getRepository();
|
||||
response = new PullResponse(push(source, getRemoteUrl(targetDirectory)));
|
||||
}
|
||||
finally
|
||||
{
|
||||
} finally {
|
||||
GitUtil.close(source);
|
||||
}
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
private PullResponse pullFromUrl(URL url)
|
||||
throws IOException
|
||||
{
|
||||
logger.debug("pull changes from {} to {}", url, repository.getId());
|
||||
private PullResponse pullFromUrl(PullCommandRequest request)
|
||||
throws IOException {
|
||||
logger.debug("pull changes from {} to {}", request.getRemoteUrl(), repository.getId());
|
||||
|
||||
PullResponse response;
|
||||
Git git = Git.wrap(open());
|
||||
|
||||
try
|
||||
{
|
||||
try {
|
||||
//J-
|
||||
FetchResult result = git.fetch()
|
||||
FetchCommand fetchCommand = git.fetch();
|
||||
|
||||
if (!Strings.isNullOrEmpty(request.getUsername()) && !Strings.isNullOrEmpty(request.getPassword())) {
|
||||
fetchCommand.setCredentialsProvider(
|
||||
new UsernamePasswordCredentialsProvider(request.getUsername(), request.getPassword())
|
||||
);
|
||||
}
|
||||
|
||||
FetchResult result = fetchCommand
|
||||
.setRefSpecs(new RefSpec(REF_SPEC))
|
||||
.setRemote(url.toExternalForm())
|
||||
.setRemote(request.getRemoteUrl().toExternalForm())
|
||||
.setTagOpt(TagOpt.FETCH_TAGS)
|
||||
.call();
|
||||
//J+
|
||||
|
||||
response = convert(git, result);
|
||||
}
|
||||
catch (GitAPIException ex)
|
||||
{
|
||||
} catch (GitAPIException ex) {
|
||||
throw new InternalRepositoryException(repository, "error during pull", ex);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user