improve repository integration tests

This commit is contained in:
Sebastian Sdorra
2011-02-22 11:47:23 +01:00
parent 208c71df9a
commit f11a4f52d8
3 changed files with 350 additions and 43 deletions

View File

@@ -40,11 +40,23 @@ 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.dircache.DirCache;
import org.eclipse.jgit.dircache.DirCacheCheckout;
import org.eclipse.jgit.errors.IncorrectObjectTypeException;
import org.eclipse.jgit.errors.MissingObjectException;
import org.eclipse.jgit.errors.NotSupportedException;
import org.eclipse.jgit.errors.TransportException;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.lib.RefComparator;
import org.eclipse.jgit.lib.RefUpdate;
import org.eclipse.jgit.lib.TextProgressMonitor;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.revwalk.RevWalk;
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.FetchResult;
import org.eclipse.jgit.transport.RefSpec;
import org.eclipse.jgit.transport.RemoteConfig;
import org.eclipse.jgit.transport.RemoteRefUpdate;
@@ -61,6 +73,7 @@ import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
/**
@@ -131,6 +144,28 @@ public class GitRepositoryClient implements RepositoryClient
callCommand(cmd);
}
/**
* Method description
*
*
* @throws RepositoryClientException
*/
@Override
public void checkout() throws RepositoryClientException
{
try
{
final FetchResult r = runFetch();
final Ref branch = guessHEAD(r);
doCheckout(branch);
}
catch (Exception ex)
{
throw new RepositoryClientException(ex);
}
}
/**
* Method description
*
@@ -242,19 +277,6 @@ public class GitRepositoryClient implements RepositoryClient
}
}
/**
* Method description
*
*
* @throws RepositoryClientException
*/
@Override
public void update() throws RepositoryClientException
{
// todo pull
}
/**
* Method description
*
@@ -275,6 +297,148 @@ public class GitRepositoryClient implements RepositoryClient
}
}
/**
* Method description
*
*
* @param branch
*
* @throws IOException
*/
private void doCheckout(final Ref branch) throws IOException
{
if (!Constants.HEAD.equals(branch.getName()))
{
RefUpdate u = repository.updateRef(Constants.HEAD);
u.disableRefLog();
u.link(branch.getName());
}
RevCommit commit = parseCommit(branch);
RefUpdate u = repository.updateRef(Constants.HEAD);
u.setNewObjectId(commit);
u.forceUpdate();
DirCache dc = repository.lockDirCache();
DirCacheCheckout co = new DirCacheCheckout(repository, dc,
commit.getTree());
co.checkout();
}
/**
* Method description
*
*
* @param result
*
* @return
*/
private Ref guessHEAD(final FetchResult result)
{
final Ref idHEAD = result.getAdvertisedRef(Constants.HEAD);
final List<Ref> availableRefs = new ArrayList<Ref>();
Ref head = null;
for (final Ref r : result.getAdvertisedRefs())
{
final String n = r.getName();
if (!n.startsWith(Constants.R_HEADS))
{
continue;
}
availableRefs.add(r);
if ((idHEAD == null) || (head != null))
{
continue;
}
if (r.getObjectId().equals(idHEAD.getObjectId()))
{
head = r;
}
}
Collections.sort(availableRefs, RefComparator.INSTANCE);
if ((idHEAD != null) && (head == null))
{
head = idHEAD;
}
return head;
}
/**
* Method description
*
*
* @param branch
*
* @return
*
* @throws IOException
* @throws IncorrectObjectTypeException
* @throws MissingObjectException
*/
private RevCommit parseCommit(Ref branch)
throws MissingObjectException, IncorrectObjectTypeException,
IOException
{
final RevWalk rw = new RevWalk(repository);
final RevCommit commit;
try
{
commit = rw.parseCommit(branch.getObjectId());
}
finally
{
rw.release();
}
return commit;
}
/**
* Method description
*
*
* @return
*
* @throws NotSupportedException
* @throws TransportException
* @throws URISyntaxException
*/
private FetchResult runFetch()
throws NotSupportedException, URISyntaxException, TransportException
{
Transport tn = Transport.open(repository, Constants.HEAD);
if (credentialsProvider != null)
{
tn.setCredentialsProvider(credentialsProvider);
}
FetchResult r;
try
{
r = tn.fetch(new TextProgressMonitor(), null);
}
finally
{
tn.close();
}
return r;
}
//~--- fields ---------------------------------------------------------------
/** Field description */

View File

@@ -88,5 +88,5 @@ public interface RepositoryClient
*
* @throws RepositoryClientException
*/
public void update() throws RepositoryClientException;
public void checkout() throws RepositoryClientException;
}