From 3463a5ed01f38b6134c0b4b1075702f328b8e7e1 Mon Sep 17 00:00:00 2001 From: Sebastian Sdorra Date: Thu, 28 Jul 2011 22:13:19 +0200 Subject: [PATCH] fix issue with git repository without HEAD ref --- .../scm/repository/GitChangesetViewer.java | 22 ++++-- .../java/sonia/scm/repository/GitUtil.java | 69 ++++++++++++++++++- 2 files changed, 84 insertions(+), 7 deletions(-) diff --git a/scm-plugins/scm-git-plugin/src/main/java/sonia/scm/repository/GitChangesetViewer.java b/scm-plugins/scm-git-plugin/src/main/java/sonia/scm/repository/GitChangesetViewer.java index ab6ea607d8..7af74596ea 100644 --- a/scm-plugins/scm-git-plugin/src/main/java/sonia/scm/repository/GitChangesetViewer.java +++ b/scm-plugins/scm-git-plugin/src/main/java/sonia/scm/repository/GitChangesetViewer.java @@ -37,6 +37,7 @@ package sonia.scm.repository; import org.eclipse.jgit.api.Git; import org.eclipse.jgit.api.errors.NoHeadException; +import org.eclipse.jgit.lib.ObjectId; import org.eclipse.jgit.revwalk.RevCommit; import org.slf4j.Logger; @@ -51,6 +52,7 @@ import java.io.IOException; import java.util.ArrayList; import java.util.List; +import org.eclipse.jgit.api.LogCommand; /** * @@ -107,16 +109,26 @@ public class GitChangesetViewer implements ChangesetViewer if (!gr.getAllRefs().isEmpty()) { converter = new GitChangesetConverter(gr, GitUtil.ID_LENGTH); + Git git = new Git(gr); + ObjectId headId = GitUtil.getRepositoryHead(gr); - for (RevCommit commit : git.log().call()) + if (headId != null) { - if ((counter >= start) && (counter < start + max)) + for (RevCommit commit : git.log().add(headId).call()) { - changesetList.add(converter.createChangeset(commit)); - } + if ((counter >= start) && (counter < start + max)) + { + changesetList.add(converter.createChangeset(commit)); + } - counter++; + counter++; + } + } + else if (logger.isWarnEnabled()) + { + logger.warn("could not find repository head of repository {}", + repository.getName()); } } 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 31658127eb..845bcf898b 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 @@ -37,12 +37,16 @@ package sonia.scm.repository; import org.eclipse.jgit.lib.Constants; import org.eclipse.jgit.lib.ObjectId; +import org.eclipse.jgit.lib.Ref; import org.eclipse.jgit.lib.RepositoryCache; import org.eclipse.jgit.revwalk.RevCommit; import org.eclipse.jgit.revwalk.RevWalk; import org.eclipse.jgit.treewalk.TreeWalk; import org.eclipse.jgit.util.FS; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import sonia.scm.util.Util; //~--- JDK imports ------------------------------------------------------------ @@ -50,6 +54,8 @@ import sonia.scm.util.Util; import java.io.File; import java.io.IOException; +import java.util.Map; + /** * * @author Sebastian Sdorra @@ -57,9 +63,23 @@ import java.io.IOException; public class GitUtil { - /** Field description */ + /** Field description */ public static final int ID_LENGTH = 20; - + + /** Field description */ + public static final String REF_HEAD = "HEAD"; + + /** Field description */ + public static final String REF_HEAD_PREFIX = "refs/heads/"; + + /** Field description */ + public static final String REF_MASTER = "master"; + + /** the logger for GitUtil */ + private static final Logger logger = LoggerFactory.getLogger(GitUtil.class); + + //~--- methods -------------------------------------------------------------- + /** * Method description * @@ -138,6 +158,51 @@ public class GitUtil return date; } + /** + * Method description + * + * + * @param repo + * + * @return + */ + public static ObjectId getRepositoryHead(org.eclipse.jgit.lib.Repository repo) + { + ObjectId id = null; + String head = null; + Map refs = repo.getAllRefs(); + + for (Map.Entry e : refs.entrySet()) + { + String key = e.getKey(); + + if (REF_HEAD.equals(key)) + { + head = REF_HEAD; + id = e.getValue().getObjectId(); + + break; + } + else if (key.startsWith(REF_HEAD_PREFIX)) + { + id = e.getValue().getObjectId(); + head = key.substring(REF_HEAD_PREFIX.length()); + + if (REF_MASTER.equals(head)) + { + break; + } + } + } + + if (logger.isInfoEnabled()) + { + logger.info("use {}:{} as repository head", head, id); + } + + return id; + } + /** * Method description *