fix issue with git repository without HEAD ref

This commit is contained in:
Sebastian Sdorra
2011-07-28 22:13:19 +02:00
parent fe78d8545e
commit 3463a5ed01
2 changed files with 84 additions and 7 deletions

View File

@@ -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());
}
}

View File

@@ -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<String, Ref> refs = repo.getAllRefs();
for (Map.Entry<String, Ref> 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
*