implement git push command

This commit is contained in:
Sebastian Sdorra
2012-07-14 17:19:35 +02:00
parent 4ec9cdccbc
commit de01c03642
3 changed files with 140 additions and 4 deletions

View File

@@ -0,0 +1,102 @@
/**
* Copyright (c) 2010, Sebastian Sdorra All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer. 2. Redistributions in
* binary form must reproduce the above copyright notice, this list of
* conditions and the following disclaimer in the documentation and/or other
* materials provided with the distribution. 3. Neither the name of SCM-Manager;
* nor the names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* http://bitbucket.org/sdorra/scm-manager
*
*/
package sonia.scm.repository.client.spi;
//~--- non-JDK imports --------------------------------------------------------
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.transport.CredentialsProvider;
import sonia.scm.repository.client.api.RepositoryClientException;
//~--- JDK imports ------------------------------------------------------------
import java.io.IOException;
/**
*
* @author Sebastian Sdorra
*/
public class GitPushCommand implements PushCommand
{
/**
* Constructs ...
*
*
* @param git
* @param credentialsProvider
*/
public GitPushCommand(Git git, CredentialsProvider credentialsProvider)
{
this.git = git;
this.credentialsProvider = credentialsProvider;
}
//~--- methods --------------------------------------------------------------
/**
* Method description
*
*
* @throws IOException
*/
@Override
public void push() throws IOException
{
try
{
org.eclipse.jgit.api.PushCommand cmd = git.push().setPushAll();
if (credentialsProvider != null)
{
cmd.setCredentialsProvider(credentialsProvider);
}
cmd.call();
}
catch (GitAPIException ex)
{
throw new RepositoryClientException(
"could not push to remote repository", ex);
}
}
//~--- fields ---------------------------------------------------------------
/** Field description */
private CredentialsProvider credentialsProvider;
/** Field description */
private Git git;
}

View File

@@ -35,6 +35,7 @@ package sonia.scm.repository.client.spi;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.transport.CredentialsProvider;
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;
import sonia.scm.repository.client.api.RepositoryClientException;
@@ -102,17 +103,19 @@ public class GitRepositoryClientFactoryProvider
{
Git git = null;
CredentialsProvider credentialsProvider =
new UsernamePasswordCredentialsProvider(username, password);
try
{
git = Git.cloneRepository().setURI(url).setDirectory(
workingCopy).setCredentialsProvider(
new UsernamePasswordCredentialsProvider(username, password)).call();
workingCopy).setCredentialsProvider(credentialsProvider).call();
}
catch (GitAPIException ex)
{
throw new RepositoryClientException("could not clone repository", ex);
}
return new GitRepositoryClientProvider(git);
return new GitRepositoryClientProvider(git, credentialsProvider);
}
}

View File

@@ -36,6 +36,7 @@ package sonia.scm.repository.client.spi;
import com.google.common.collect.ImmutableSet;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.transport.CredentialsProvider;
import sonia.scm.repository.client.api.ClientCommand;
@@ -53,7 +54,8 @@ public class GitRepositoryClientProvider extends RepositoryClientProvider
/** Field description */
private static final Set<ClientCommand> SUPPORTED_COMMANDS =
ImmutableSet.of(ClientCommand.ADD, ClientCommand.REMOVE,
ClientCommand.COMMIT, ClientCommand.TAG, ClientCommand.BANCH);
ClientCommand.COMMIT, ClientCommand.TAG, ClientCommand.BANCH,
ClientCommand.PUSH);
//~--- constructors ---------------------------------------------------------
@@ -62,10 +64,24 @@ public class GitRepositoryClientProvider extends RepositoryClientProvider
*
*
* @param git
* @param credentialsProvider
*/
GitRepositoryClientProvider(Git git)
{
this(git, null);
}
/**
* Constructs ...
*
*
* @param git
* @param credentialsProvider
*/
GitRepositoryClientProvider(Git git, CredentialsProvider credentialsProvider)
{
this.git = git;
this.credentialsProvider = credentialsProvider;
}
//~--- get methods ----------------------------------------------------------
@@ -106,6 +122,18 @@ public class GitRepositoryClientProvider extends RepositoryClientProvider
return new GitCommitCommand(git);
}
/**
* Method description
*
*
* @return
*/
@Override
public PushCommand getPushCommand()
{
return new GitPushCommand(git, credentialsProvider);
}
/**
* Method description
*
@@ -144,6 +172,9 @@ public class GitRepositoryClientProvider extends RepositoryClientProvider
//~--- fields ---------------------------------------------------------------
/** Field description */
private CredentialsProvider credentialsProvider;
/** Field description */
private Git git;
}