added repository client api for tests

This commit is contained in:
Sebastian Sdorra
2011-02-21 21:13:57 +01:00
parent ef2d5f450d
commit c404b1ec10
5 changed files with 587 additions and 0 deletions

View File

@@ -47,6 +47,18 @@
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jgit</groupId>
<artifactId>org.eclipse.jgit</artifactId>
<version>0.10.1</version>
</dependency>
<dependency>
<groupId>com.jcraft</groupId>
<artifactId>jsch</artifactId>
<version>0.1.42</version>
</dependency>
</dependencies>
</project>

View File

@@ -0,0 +1,294 @@
/**
* 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;
//~--- non-JDK imports --------------------------------------------------------
import org.eclipse.jgit.api.AddCommand;
import org.eclipse.jgit.api.CommitCommand;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.GitCommand;
import org.eclipse.jgit.api.RmCommand;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.TextProgressMonitor;
import org.eclipse.jgit.storage.file.FileBasedConfig;
import org.eclipse.jgit.storage.file.FileRepository;
import org.eclipse.jgit.transport.CredentialsProvider;
import org.eclipse.jgit.transport.RefSpec;
import org.eclipse.jgit.transport.RemoteConfig;
import org.eclipse.jgit.transport.RemoteRefUpdate;
import org.eclipse.jgit.transport.Transport;
import org.eclipse.jgit.transport.URIish;
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;
//~--- JDK imports ------------------------------------------------------------
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
/**
*
* @author Sebastian Sdorra
*/
public class GitRepositoryClient implements RepositoryClient
{
/**
* Constructs ...
*
*
* @param localRepository
* @param remoteRepository
* @param username
* @param password
*
*
* @throws IOException
* @throws URISyntaxException
*/
GitRepositoryClient(File localRepository, String remoteRepository,
String username, String password)
throws URISyntaxException, IOException
{
uri = new URIish(remoteRepository);
if ((username != null) && (password != null))
{
uri.setUser(username);
uri.setPass(password);
credentialsProvider = new UsernamePasswordCredentialsProvider(username,
password);
}
this.repository = new FileRepository(new File(localRepository,
Constants.DOT_GIT));
}
//~--- methods --------------------------------------------------------------
/**
* Method description
*
*
* @param file
* @param others
*
* @throws RepositoryClientException
*/
@Override
public void add(String file, String... others)
throws RepositoryClientException
{
AddCommand cmd = new Git(repository).add();
cmd.addFilepattern(file);
if (others != null)
{
for (String f : others)
{
cmd.addFilepattern(f);
}
}
callCommand(cmd);
}
/**
* Method description
*
*
* @param message
*
* @throws RepositoryClientException
*/
@Override
public void commit(String message) throws RepositoryClientException
{
List<RefSpec> refSpecs = new ArrayList<RefSpec>();
refSpecs.add(Transport.REFSPEC_PUSH_ALL);
try
{
CommitCommand cmd = new Git(repository).commit();
cmd.setMessage(message);
callCommand(cmd);
List<Transport> transports = Transport.openAll(repository, remoteConfig,
Transport.Operation.PUSH);
for (Transport transport : transports)
{
if (credentialsProvider != null)
{
transport.setCredentialsProvider(credentialsProvider);
}
transport.setPushThin(Transport.DEFAULT_PUSH_THIN);
Collection<RemoteRefUpdate> toPush =
transport.findRemoteRefUpdatesFor(refSpecs);
try
{
transport.push(new TextProgressMonitor(), toPush);
}
finally
{
transport.close();
}
}
}
catch (Exception ex)
{
throw new RepositoryClientException(ex);
}
}
/**
* Method description
*
*
* @param file
* @param others
*
* @throws RepositoryClientException
*/
@Override
public void delete(String file, String... others)
throws RepositoryClientException
{
RmCommand cmd = new Git(repository).rm();
cmd = cmd.addFilepattern(file);
if (others != null)
{
for (String f : others)
{
cmd = cmd.addFilepattern(f);
}
}
callCommand(cmd);
}
/**
* Method description
*
*
* @throws RepositoryClientException
*/
@Override
public void init() throws RepositoryClientException
{
try
{
repository.create(false);
FileBasedConfig fc = repository.getConfig();
remoteConfig = new RemoteConfig(fc, Constants.HEAD);
remoteConfig.addURI(uri);
remoteConfig.addFetchRefSpec(
new RefSpec().setForceUpdate(true).setSourceDestination(
Constants.R_HEADS + "*",
Constants.R_REMOTES + Constants.HEAD + "/*"));
remoteConfig.update(fc);
fc.save();
}
catch (Exception ex)
{
throw new RepositoryClientException(ex);
}
}
/**
* Method description
*
*
* @throws RepositoryClientException
*/
@Override
public void update() throws RepositoryClientException
{
// todo pull
}
/**
* Method description
*
*
* @param cmd
*
* @throws RepositoryClientException
*/
private void callCommand(GitCommand cmd) throws RepositoryClientException
{
try
{
cmd.call();
}
catch (Exception ex)
{
throw new RepositoryClientException(ex);
}
}
//~--- fields ---------------------------------------------------------------
/** Field description */
private CredentialsProvider credentialsProvider;
/** Field description */
private RemoteConfig remoteConfig;
/** Field description */
private FileRepository repository;
/** Field description */
private URIish uri;
/** Field description */
private String username;
}

View File

@@ -0,0 +1,92 @@
/**
* 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;
/**
*
* @author Sebastian Sdorra
*/
public interface RepositoryClient
{
/**
* Method description
*
*
* @param file
* @param others
*
* @throws RepositoryClientException
*/
public void add(String file, String... others)
throws RepositoryClientException;
/**
* Method description
*
*
* @param message
*
* @throws RepositoryClientException
*/
public void commit(String message) throws RepositoryClientException;
/**
* Method description
*
*
* @param file
* @param others
*
* @throws RepositoryClientException
*/
public void delete(String file, String... others)
throws RepositoryClientException;
/**
* Method description
*
*
* @throws RepositoryClientException
*/
public void init() throws RepositoryClientException;
/**
* Method description
*
*
* @throws RepositoryClientException
*/
public void update() throws RepositoryClientException;
}

View File

@@ -0,0 +1,87 @@
/**
* 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;
/**
*
* @author Sebastian Sdorra
*/
public class RepositoryClientException extends Exception
{
/** Field description */
private static final long serialVersionUID = -6017272751596608450L;
//~--- constructors ---------------------------------------------------------
/**
* Constructs ...
*
*/
public RepositoryClientException() {}
/**
* Constructs ...
*
*
* @param message
*/
public RepositoryClientException(String message)
{
super(message);
}
/**
* Constructs ...
*
*
* @param throwable
*/
public RepositoryClientException(Throwable throwable)
{
super(throwable);
}
/**
* Constructs ...
*
*
* @param message
* @param throwable
*/
public RepositoryClientException(String message, Throwable throwable)
{
super(message, throwable);
}
}

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;
//~--- JDK imports ------------------------------------------------------------
import java.io.File;
/**
*
* @author Sebastian Sdorra
*/
public class RepositoryClientFactory
{
/**
* Method description
*
*
* @param type
* @param localRepository
* @param remoteRepository
*
* @return
*
* @throws RepositoryClientException
*/
public static RepositoryClient createClient(String type,
File localRepository, String remoteRepository)
throws RepositoryClientException
{
return createClient(type, localRepository, remoteRepository, null, null);
}
/**
* Method description
*
*
* @param type
* @param localRepository
* @param remoteRepository
* @param username
* @param password
*
* @return
*
* @throws RepositoryClientException
*/
public static RepositoryClient createClient(String type,
File localRepository, String remoteRepository, String username,
String password)
throws RepositoryClientException
{
RepositoryClient client = null;
try
{
if ("git".equals(type))
{
client = new GitRepositoryClient(localRepository, remoteRepository,
username, password);
}
}
catch (Exception ex)
{
throw new RepositoryClientException(ex);
}
return client;
}
}