From 2818ed1a76eb67ba8ad1c3e0a313f819cc806885 Mon Sep 17 00:00:00 2001 From: Sebastian Sdorra Date: Sun, 26 May 2013 10:52:31 +0200 Subject: [PATCH] count pushed and pulled changesets --- .../spi/AbstractPushOrPullCommand.java | 114 +++++++++++++++++- .../scm/repository/spi/GitPullCommand.java | 7 +- .../scm/repository/spi/GitPushCommand.java | 4 +- .../repository/spi/GitPushCommandTest.java | 1 + 4 files changed, 115 insertions(+), 11 deletions(-) diff --git a/scm-plugins/scm-git-plugin/src/main/java/sonia/scm/repository/spi/AbstractPushOrPullCommand.java b/scm-plugins/scm-git-plugin/src/main/java/sonia/scm/repository/spi/AbstractPushOrPullCommand.java index 83130fc150..17d31dae98 100644 --- a/scm-plugins/scm-git-plugin/src/main/java/sonia/scm/repository/spi/AbstractPushOrPullCommand.java +++ b/scm-plugins/scm-git-plugin/src/main/java/sonia/scm/repository/spi/AbstractPushOrPullCommand.java @@ -34,18 +34,28 @@ package sonia.scm.repository.spi; //~--- non-JDK imports -------------------------------------------------------- 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.lib.ObjectId; 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.api.PushResponse; //~--- JDK imports ------------------------------------------------------------ import java.io.File; import java.io.IOException; +import java.util.Collection; + /** * * @author Sebastian Sdorra @@ -56,6 +66,12 @@ public abstract class AbstractPushOrPullCommand extends AbstractGitCommand /** Field description */ private static final String SCHEME = "scm://"; + /** + * the logger for AbstractPushOrPullCommand + */ + private static final Logger logger = + LoggerFactory.getLogger(AbstractPushOrPullCommand.class); + //~--- constructors --------------------------------------------------------- /** @@ -87,25 +103,37 @@ public abstract class AbstractPushOrPullCommand extends AbstractGitCommand * @throws IOException * @throws RepositoryException */ - protected PushResponse push(Repository source, File target) + protected long push(Repository source, File target) throws IOException, RepositoryException { - PushResponse response = null; - org.eclipse.jgit.api.PushCommand push = Git.wrap(source).push(); + Git git = Git.wrap(source); + org.eclipse.jgit.api.PushCommand push = git.push(); push.setPushAll().setPushTags(); push.setRemote(SCHEME.concat(target.getAbsolutePath())); + long counter = -1; + try { - push.call(); + Iterable results = push.call(); + + if (results != null) + { + counter = 0; + + for (PushResult result : results) + { + counter += count(git, result); + } + } } catch (Exception ex) { throw new RepositoryException("could not execute push command", ex); } - return response; + return counter; } //~--- get methods ---------------------------------------------------------- @@ -131,4 +159,78 @@ public abstract class AbstractPushOrPullCommand extends AbstractGitCommand return remoteRepository; } + + //~--- methods -------------------------------------------------------------- + + /** + * Method description + * + * + * @param git + * @param result + * + * @return + */ + private long count(Git git, PushResult result) + { + long counter = 0; + Collection 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 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; + } } diff --git a/scm-plugins/scm-git-plugin/src/main/java/sonia/scm/repository/spi/GitPullCommand.java b/scm-plugins/scm-git-plugin/src/main/java/sonia/scm/repository/spi/GitPullCommand.java index d39f71eece..9a5d32d1b0 100644 --- a/scm-plugins/scm-git-plugin/src/main/java/sonia/scm/repository/spi/GitPullCommand.java +++ b/scm-plugins/scm-git-plugin/src/main/java/sonia/scm/repository/spi/GitPullCommand.java @@ -100,19 +100,22 @@ public class GitPullCommand extends AbstractPushOrPullCommand Preconditions.checkArgument(sourceDirectory.exists(), "target repository directory does not exists"); + PullResponse response = null; + org.eclipse.jgit.lib.Repository source = null; try { source = new FileRepository(sourceDirectory); - push(source, targetDirectory); + response = new PullResponse(push(source, targetDirectory)); + } finally { GitUtil.close(source); } - return new PullResponse(); + return response; } //~--- fields --------------------------------------------------------------- diff --git a/scm-plugins/scm-git-plugin/src/main/java/sonia/scm/repository/spi/GitPushCommand.java b/scm-plugins/scm-git-plugin/src/main/java/sonia/scm/repository/spi/GitPushCommand.java index 097ee4d0ad..9711827c07 100644 --- a/scm-plugins/scm-git-plugin/src/main/java/sonia/scm/repository/spi/GitPushCommand.java +++ b/scm-plugins/scm-git-plugin/src/main/java/sonia/scm/repository/spi/GitPushCommand.java @@ -91,9 +91,7 @@ public class GitPushCommand extends AbstractPushOrPullCommand Preconditions.checkArgument(targetDirectory.exists(), "target repository directory does not exists"); - push(open(), targetDirectory); - - return new PushResponse(); + return new PushResponse(push(open(), targetDirectory)); } //~--- fields --------------------------------------------------------------- diff --git a/scm-plugins/scm-git-plugin/src/test/java/sonia/scm/repository/spi/GitPushCommandTest.java b/scm-plugins/scm-git-plugin/src/test/java/sonia/scm/repository/spi/GitPushCommandTest.java index a8eb82c6b6..eaff5c2032 100644 --- a/scm-plugins/scm-git-plugin/src/test/java/sonia/scm/repository/spi/GitPushCommandTest.java +++ b/scm-plugins/scm-git-plugin/src/test/java/sonia/scm/repository/spi/GitPushCommandTest.java @@ -91,6 +91,7 @@ public class GitPushCommandTest extends AbstractRemoteCommandTestBase PushResponse response = cmd.push(request); assertNotNull(response); + assertEquals(2l, response.getChangesetCount()); Iterator commits = incoming.log().call().iterator();