From 895af89c127a82e8981c9cb2f21cf1f9e27d6ae0 Mon Sep 17 00:00:00 2001 From: Sebastian Sdorra Date: Mon, 1 Dec 2014 14:45:43 +0100 Subject: [PATCH] fix wrong changeset count for git push and pull commands --- .../java/sonia/scm/repository/GitUtil.java | 26 +++++++++++ .../spi/AbstractGitPushOrPullCommand.java | 41 ++++++++++-------- .../scm/repository/spi/GitPullCommand.java | 43 +++++++++++-------- 3 files changed, 74 insertions(+), 36 deletions(-) diff --git a/scm-plugins/scm-git-plugin/src/main/java/sonia/scm/repository/GitUtil.java b/scm-plugins/scm-git-plugin/src/main/java/sonia/scm/repository/GitUtil.java index 6fe3211ca8..8985966220 100644 --- a/scm-plugins/scm-git-plugin/src/main/java/sonia/scm/repository/GitUtil.java +++ b/scm-plugins/scm-git-plugin/src/main/java/sonia/scm/repository/GitUtil.java @@ -697,6 +697,32 @@ public final class GitUtil //J+ } + /** + * Method description + * + * + * @param ref + * + * @return + */ + public static boolean isHead(String ref) + { + return ref.startsWith(REF_HEAD_PREFIX); + } + + /** + * Method description + * + * + * @param id + * + * @return + */ + public static boolean isValidObjectId(ObjectId id) + { + return (id != null) &&!id.equals(ObjectId.zeroId()); + } + //~--- methods -------------------------------------------------------------- /** diff --git a/scm-plugins/scm-git-plugin/src/main/java/sonia/scm/repository/spi/AbstractGitPushOrPullCommand.java b/scm-plugins/scm-git-plugin/src/main/java/sonia/scm/repository/spi/AbstractGitPushOrPullCommand.java index 04838403f3..764dde47f3 100644 --- a/scm-plugins/scm-git-plugin/src/main/java/sonia/scm/repository/spi/AbstractGitPushOrPullCommand.java +++ b/scm-plugins/scm-git-plugin/src/main/java/sonia/scm/repository/spi/AbstractGitPushOrPullCommand.java @@ -49,6 +49,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import sonia.scm.repository.GitRepositoryHandler; +import sonia.scm.repository.GitUtil; import sonia.scm.repository.RepositoryException; //~--- JDK imports ------------------------------------------------------------ @@ -253,21 +254,24 @@ public abstract class AbstractGitPushOrPullCommand extends AbstractGitCommand { long counter = 0; - try + if (GitUtil.isHead(update.getRemoteName())) { - org.eclipse.jgit.api.LogCommand log = git.log(); - ObjectId oldId = update.getExpectedOldObjectId(); - - if (oldId != null) + try { - log.not(oldId); - } + org.eclipse.jgit.api.LogCommand log = git.log(); + ObjectId oldId = update.getExpectedOldObjectId(); - ObjectId newId = update.getNewObjectId(); + if (GitUtil.isValidObjectId(oldId)) + { + log.not(oldId); + } - if (newId != null) - { - log.add(newId); + ObjectId newId = update.getNewObjectId(); + + if (GitUtil.isValidObjectId(newId)) + { + log.add(newId); + } Iterable commits = log.call(); @@ -275,16 +279,17 @@ public abstract class AbstractGitPushOrPullCommand extends AbstractGitCommand { counter += Iterables.size(commits); } - } - else - { - logger.warn("update without new object id"); - } + logger.trace("counting {} commits for ref update {}", counter, update); + } + catch (Exception ex) + { + logger.error("could not count pushed/pulled changesets", ex); + } } - catch (Exception ex) + else { - logger.error("could not count pushed/pulled changesets", ex); + logger.debug("do not count non branch ref update {}", update); } 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 0c4795c619..ec0f468a57 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 @@ -152,6 +152,8 @@ public class GitPullCommand extends AbstractGitPushOrPullCommand counter += count(git, tru); } + logger.debug("received {} changesets by pull", counter); + return new PullResponse(counter); } @@ -168,21 +170,25 @@ public class GitPullCommand extends AbstractGitPushOrPullCommand { long counter = 0; - try + if (GitUtil.isHead(tru.getLocalName())) { - org.eclipse.jgit.api.LogCommand log = git.log(); - ObjectId oldId = tru.getOldObjectId(); - - if (oldId != null) + try { - log.not(oldId); - } + org.eclipse.jgit.api.LogCommand log = git.log(); - ObjectId newId = tru.getNewObjectId(); + ObjectId oldId = tru.getOldObjectId(); - if (newId != null) - { - log.add(newId); + if (GitUtil.isValidObjectId(oldId)) + { + log.not(oldId); + } + + ObjectId newId = tru.getNewObjectId(); + + if (GitUtil.isValidObjectId(newId)) + { + log.add(newId); + } Iterable commits = log.call(); @@ -190,16 +196,17 @@ public class GitPullCommand extends AbstractGitPushOrPullCommand { counter += Iterables.size(commits); } - } - else - { - logger.warn("update without new object id"); - } + logger.trace("counting {} commits for ref update {}", counter, tru); + } + catch (Exception ex) + { + logger.error("could not count pushed/pulled changesets", ex); + } } - catch (Exception ex) + else { - logger.error("could not count pushed/pulled changesets", ex); + logger.debug("do not count non branch ref update {}", tru); } return counter;