mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-13 17:05:43 +01:00
count pushed and pulled changesets
This commit is contained in:
@@ -34,18 +34,28 @@ package sonia.scm.repository.spi;
|
|||||||
//~--- non-JDK imports --------------------------------------------------------
|
//~--- non-JDK imports --------------------------------------------------------
|
||||||
|
|
||||||
import com.google.common.base.Preconditions;
|
import com.google.common.base.Preconditions;
|
||||||
|
import com.google.common.collect.Iterables;
|
||||||
|
import com.google.common.collect.Iterators;
|
||||||
|
|
||||||
import org.eclipse.jgit.api.Git;
|
import org.eclipse.jgit.api.Git;
|
||||||
|
import org.eclipse.jgit.lib.ObjectId;
|
||||||
import org.eclipse.jgit.lib.Repository;
|
import org.eclipse.jgit.lib.Repository;
|
||||||
|
import org.eclipse.jgit.revwalk.RevCommit;
|
||||||
|
import org.eclipse.jgit.transport.PushResult;
|
||||||
|
import org.eclipse.jgit.transport.RemoteRefUpdate;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import sonia.scm.repository.RepositoryException;
|
import sonia.scm.repository.RepositoryException;
|
||||||
import sonia.scm.repository.api.PushResponse;
|
|
||||||
|
|
||||||
//~--- JDK imports ------------------------------------------------------------
|
//~--- JDK imports ------------------------------------------------------------
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import java.util.Collection;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @author Sebastian Sdorra
|
* @author Sebastian Sdorra
|
||||||
@@ -56,6 +66,12 @@ public abstract class AbstractPushOrPullCommand extends AbstractGitCommand
|
|||||||
/** Field description */
|
/** Field description */
|
||||||
private static final String SCHEME = "scm://";
|
private static final String SCHEME = "scm://";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* the logger for AbstractPushOrPullCommand
|
||||||
|
*/
|
||||||
|
private static final Logger logger =
|
||||||
|
LoggerFactory.getLogger(AbstractPushOrPullCommand.class);
|
||||||
|
|
||||||
//~--- constructors ---------------------------------------------------------
|
//~--- constructors ---------------------------------------------------------
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -87,25 +103,37 @@ public abstract class AbstractPushOrPullCommand extends AbstractGitCommand
|
|||||||
* @throws IOException
|
* @throws IOException
|
||||||
* @throws RepositoryException
|
* @throws RepositoryException
|
||||||
*/
|
*/
|
||||||
protected PushResponse push(Repository source, File target)
|
protected long push(Repository source, File target)
|
||||||
throws IOException, RepositoryException
|
throws IOException, RepositoryException
|
||||||
{
|
{
|
||||||
PushResponse response = null;
|
Git git = Git.wrap(source);
|
||||||
org.eclipse.jgit.api.PushCommand push = Git.wrap(source).push();
|
org.eclipse.jgit.api.PushCommand push = git.push();
|
||||||
|
|
||||||
push.setPushAll().setPushTags();
|
push.setPushAll().setPushTags();
|
||||||
push.setRemote(SCHEME.concat(target.getAbsolutePath()));
|
push.setRemote(SCHEME.concat(target.getAbsolutePath()));
|
||||||
|
|
||||||
|
long counter = -1;
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
push.call();
|
Iterable<PushResult> results = push.call();
|
||||||
|
|
||||||
|
if (results != null)
|
||||||
|
{
|
||||||
|
counter = 0;
|
||||||
|
|
||||||
|
for (PushResult result : results)
|
||||||
|
{
|
||||||
|
counter += count(git, result);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
throw new RepositoryException("could not execute push command", ex);
|
throw new RepositoryException("could not execute push command", ex);
|
||||||
}
|
}
|
||||||
|
|
||||||
return response;
|
return counter;
|
||||||
}
|
}
|
||||||
|
|
||||||
//~--- get methods ----------------------------------------------------------
|
//~--- get methods ----------------------------------------------------------
|
||||||
@@ -131,4 +159,78 @@ public abstract class AbstractPushOrPullCommand extends AbstractGitCommand
|
|||||||
|
|
||||||
return remoteRepository;
|
return remoteRepository;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//~--- methods --------------------------------------------------------------
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Method description
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* @param git
|
||||||
|
* @param result
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
private long count(Git git, PushResult result)
|
||||||
|
{
|
||||||
|
long counter = 0;
|
||||||
|
Collection<RemoteRefUpdate> updates = result.getRemoteUpdates();
|
||||||
|
|
||||||
|
for (RemoteRefUpdate update : updates)
|
||||||
|
{
|
||||||
|
counter += count(git, update);
|
||||||
|
}
|
||||||
|
|
||||||
|
return counter;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Method description
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* @param git
|
||||||
|
* @param update
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
private long count(Git git, RemoteRefUpdate update)
|
||||||
|
{
|
||||||
|
long counter = 0;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
org.eclipse.jgit.api.LogCommand log = git.log();
|
||||||
|
ObjectId oldId = update.getExpectedOldObjectId();
|
||||||
|
|
||||||
|
if (oldId != null)
|
||||||
|
{
|
||||||
|
log.not(oldId);
|
||||||
|
}
|
||||||
|
|
||||||
|
ObjectId newId = update.getNewObjectId();
|
||||||
|
|
||||||
|
if (newId != null)
|
||||||
|
{
|
||||||
|
log.add(newId);
|
||||||
|
|
||||||
|
Iterable<RevCommit> commits = log.call();
|
||||||
|
|
||||||
|
if (commits != null)
|
||||||
|
{
|
||||||
|
counter += Iterables.size(commits);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
logger.warn("update without new object id");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
logger.error("could not count pushed changesets", ex);
|
||||||
|
}
|
||||||
|
|
||||||
|
return counter;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -100,19 +100,22 @@ public class GitPullCommand extends AbstractPushOrPullCommand
|
|||||||
Preconditions.checkArgument(sourceDirectory.exists(),
|
Preconditions.checkArgument(sourceDirectory.exists(),
|
||||||
"target repository directory does not exists");
|
"target repository directory does not exists");
|
||||||
|
|
||||||
|
PullResponse response = null;
|
||||||
|
|
||||||
org.eclipse.jgit.lib.Repository source = null;
|
org.eclipse.jgit.lib.Repository source = null;
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
source = new FileRepository(sourceDirectory);
|
source = new FileRepository(sourceDirectory);
|
||||||
push(source, targetDirectory);
|
response = new PullResponse(push(source, targetDirectory));
|
||||||
|
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
GitUtil.close(source);
|
GitUtil.close(source);
|
||||||
}
|
}
|
||||||
|
|
||||||
return new PullResponse();
|
return response;
|
||||||
}
|
}
|
||||||
|
|
||||||
//~--- fields ---------------------------------------------------------------
|
//~--- fields ---------------------------------------------------------------
|
||||||
|
|||||||
@@ -91,9 +91,7 @@ public class GitPushCommand extends AbstractPushOrPullCommand
|
|||||||
Preconditions.checkArgument(targetDirectory.exists(),
|
Preconditions.checkArgument(targetDirectory.exists(),
|
||||||
"target repository directory does not exists");
|
"target repository directory does not exists");
|
||||||
|
|
||||||
push(open(), targetDirectory);
|
return new PushResponse(push(open(), targetDirectory));
|
||||||
|
|
||||||
return new PushResponse();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//~--- fields ---------------------------------------------------------------
|
//~--- fields ---------------------------------------------------------------
|
||||||
|
|||||||
@@ -91,6 +91,7 @@ public class GitPushCommandTest extends AbstractRemoteCommandTestBase
|
|||||||
PushResponse response = cmd.push(request);
|
PushResponse response = cmd.push(request);
|
||||||
|
|
||||||
assertNotNull(response);
|
assertNotNull(response);
|
||||||
|
assertEquals(2l, response.getChangesetCount());
|
||||||
|
|
||||||
Iterator<RevCommit> commits = incoming.log().call().iterator();
|
Iterator<RevCommit> commits = incoming.log().call().iterator();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user