fix missing git tags in commit detail view

This commit is contained in:
Sebastian Sdorra
2012-07-05 21:21:37 +02:00
parent 3e4b0d19a3
commit 058ab265b9
3 changed files with 104 additions and 38 deletions

View File

@@ -35,6 +35,9 @@ package sonia.scm.repository;
//~--- non-JDK imports -------------------------------------------------------- //~--- non-JDK imports --------------------------------------------------------
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;
import org.eclipse.jgit.diff.DiffEntry; import org.eclipse.jgit.diff.DiffEntry;
import org.eclipse.jgit.lib.ObjectId; import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.PersonIdent; import org.eclipse.jgit.lib.PersonIdent;
@@ -56,7 +59,7 @@ import java.io.Closeable;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@@ -163,11 +166,11 @@ public class GitChangesetConverter implements Closeable
changeset.setModifications(modifications); changeset.setModifications(modifications);
} }
String tag = tags.get(commit.getId()); Collection<String> tagCollection = tags.get(commit.getId());
if (tag != null) if (tagCollection != null)
{ {
changeset.getTags().add(tag); changeset.getTags().addAll(tagCollection);
} }
return changeset; return changeset;
@@ -276,7 +279,7 @@ public class GitChangesetConverter implements Closeable
} }
/** /**
* Method description * TODO cache
* *
* *
* @param repository * @param repository
@@ -284,7 +287,7 @@ public class GitChangesetConverter implements Closeable
*/ */
private void createTagMap(org.eclipse.jgit.lib.Repository repository) private void createTagMap(org.eclipse.jgit.lib.Repository repository)
{ {
tags = new HashMap<ObjectId, String>(); tags = ArrayListMultimap.create();
Map<String, Ref> tagMap = repository.getTags(); Map<String, Ref> tagMap = repository.getTags();
@@ -292,11 +295,68 @@ public class GitChangesetConverter implements Closeable
{ {
for (Map.Entry<String, Ref> e : tagMap.entrySet()) for (Map.Entry<String, Ref> e : tagMap.entrySet())
{ {
tags.put(e.getValue().getObjectId(), e.getKey()); try
{
RevCommit c = getCommit(repository, e.getValue());
if (c != null)
{
tags.put(c.getId(), e.getKey());
}
else if (logger.isWarnEnabled())
{
logger.warn("could not find commit for tag {}", e.getKey());
}
}
catch (IOException ex)
{
logger.error("could not read commit for ref", ex);
}
} }
} }
} }
//~--- get methods ----------------------------------------------------------
/**
* Method description
*
*
* @param repository
* @param ref
*
* @return
*
* @throws IOException
*/
private RevCommit getCommit(org.eclipse.jgit.lib.Repository repository,
Ref ref)
throws IOException
{
RevCommit commit = null;
ObjectId id = ref.getPeeledObjectId();
if (id == null)
{
id = ref.getObjectId();
}
if (id != null)
{
if (revWalk == null)
{
revWalk = new RevWalk(repository);
}
commit = revWalk.parseCommit(id);
}
return commit;
}
//~--- fields --------------------------------------------------------------- //~--- fields ---------------------------------------------------------------
/** Field description */ /** Field description */
@@ -306,7 +366,7 @@ public class GitChangesetConverter implements Closeable
private RevWalk revWalk; private RevWalk revWalk;
/** Field description */ /** Field description */
private Map<ObjectId, String> tags; private Multimap<ObjectId, String> tags;
/** Field description */ /** Field description */
private TreeWalk treeWalk; private TreeWalk treeWalk;

View File

@@ -75,6 +75,9 @@ public class GitUtil
/** Field description */ /** Field description */
public static final String REF_MASTER = "master"; public static final String REF_MASTER = "master";
/** Field description */
private static final String PREFIX_TAG = "refs/tags/";
/** the logger for GitUtil */ /** the logger for GitUtil */
private static final Logger logger = LoggerFactory.getLogger(GitUtil.class); private static final Logger logger = LoggerFactory.getLogger(GitUtil.class);
@@ -275,4 +278,25 @@ public class GitUtil
return revId; return revId;
} }
/**
* Method description
*
*
* @param ref
*
* @return
*/
public static String getTagName(Ref ref)
{
String name = ref.getName();
if (name.startsWith(PREFIX_TAG))
{
name = name.substring(PREFIX_TAG.length());
}
return name;
}
} }

View File

@@ -40,9 +40,7 @@ import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException; import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.lib.Ref; import org.eclipse.jgit.lib.Ref;
import org.slf4j.Logger; import sonia.scm.repository.GitUtil;
import org.slf4j.LoggerFactory;
import sonia.scm.repository.Repository; import sonia.scm.repository.Repository;
import sonia.scm.repository.RepositoryException; import sonia.scm.repository.RepositoryException;
import sonia.scm.repository.Tag; import sonia.scm.repository.Tag;
@@ -60,17 +58,6 @@ import java.util.List;
public class GitTagsCommand extends AbstractGitCommand implements TagsCommand public class GitTagsCommand extends AbstractGitCommand implements TagsCommand
{ {
/** Field description */
private static final String PREFIX_TAG = "refs/tags/";
/**
* the logger for GitTagsCommand
*/
private static final Logger logger =
LoggerFactory.getLogger(GitTagsCommand.class);
//~--- constructors ---------------------------------------------------------
/** /**
* Constructs ... * Constructs ...
* *
@@ -110,12 +97,7 @@ public class GitTagsCommand extends AbstractGitCommand implements TagsCommand
@Override @Override
public Tag apply(Ref input) public Tag apply(Ref input)
{ {
String name = input.getName(); String name = GitUtil.getTagName(input);
if (name.startsWith(PREFIX_TAG))
{
name = name.substring(PREFIX_TAG.length());
}
return new Tag(name, input.getObjectId().name()); return new Tag(name, input.getObjectId().name());
} }