use commit id instead of ref id for git tags command

This commit is contained in:
Sebastian Sdorra
2012-07-06 08:33:02 +02:00
parent da4a133b05
commit 8947f78658
3 changed files with 190 additions and 96 deletions

View File

@@ -103,8 +103,13 @@ public class GitChangesetConverter implements Closeable
RevWalk revWalk, int idLength)
{
this.idLength = idLength;
this.revWalk = revWalk;
createTagMap(repository);
if (revWalk == null)
{
revWalk = new RevWalk(repository);
}
this.tags = GitUtil.createTagMap(repository, revWalk);
treeWalk = new TreeWalk(repository);
}
@@ -278,85 +283,6 @@ public class GitChangesetConverter implements Closeable
return modifications;
}
/**
* TODO cache
*
*
* @param repository
*
*/
private void createTagMap(org.eclipse.jgit.lib.Repository repository)
{
tags = ArrayListMultimap.create();
Map<String, Ref> tagMap = repository.getTags();
if (tagMap != null)
{
for (Map.Entry<String, Ref> e : tagMap.entrySet())
{
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 ---------------------------------------------------------------
/** Field description */

View File

@@ -35,6 +35,9 @@ package sonia.scm.repository;
//~--- non-JDK imports --------------------------------------------------------
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;
import org.eclipse.jgit.diff.DiffFormatter;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.Ref;
@@ -97,6 +100,54 @@ public class GitUtil
}
}
/**
* TODO cache
*
*
* @param repository
* @param revWalk
*
*
* @return
*/
public static Multimap<ObjectId,
String> createTagMap(org.eclipse.jgit.lib.Repository repository,
RevWalk revWalk)
{
Multimap<ObjectId, String> tags = ArrayListMultimap.create();
Map<String, Ref> tagMap = repository.getTags();
if (tagMap != null)
{
for (Map.Entry<String, Ref> e : tagMap.entrySet())
{
try
{
RevCommit c = getCommit(repository, revWalk, 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);
}
}
}
return tags;
}
/**
* Method description
*
@@ -299,4 +350,41 @@ public class GitUtil
return name;
}
/**
* Method description
*
*
* @param repository
* @param revWalk
* @param ref
*
* @return
*
* @throws IOException
*/
public static RevCommit getCommit(
org.eclipse.jgit.lib.Repository repository, RevWalk revWalk, 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;
}
}

View File

@@ -39,6 +39,11 @@ import com.google.common.collect.Lists;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.revwalk.RevWalk;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sonia.scm.repository.GitUtil;
import sonia.scm.repository.Repository;
@@ -86,31 +91,106 @@ public class GitTagsCommand extends AbstractGitCommand implements TagsCommand
{
List<Tag> tags = null;
RevWalk revWalk = null;
try
{
Git git = new Git(open());
final Git git = new Git(open());
revWalk = new RevWalk(git.getRepository());
List<Ref> tagList = git.tagList().call();
tags = Lists.transform(tagList, new Function<Ref, Tag>()
{
@Override
public Tag apply(Ref input)
{
String name = GitUtil.getTagName(input);
// TODO show commit id, not the ref id
return new Tag(name, input.getObjectId().name());
}
});
tags = Lists.transform(tagList,
new TransformFuntion(git.getRepository(), revWalk));
}
catch (GitAPIException ex)
{
throw new RepositoryException("could not read tags from repository", ex);
}
finally
{
GitUtil.release(revWalk);
}
return tags;
}
//~--- inner classes --------------------------------------------------------
/**
* Class description
*
*
* @version Enter version here..., 12/07/06
* @author Enter your name here...
*/
private static class TransformFuntion implements Function<Ref, Tag>
{
/**
* the logger for TransformFuntion
*/
private static final Logger logger =
LoggerFactory.getLogger(TransformFuntion.class);
//~--- constructors -------------------------------------------------------
/**
* Constructs ...
*
*
* @param repository
* @param revWalk
*/
public TransformFuntion(org.eclipse.jgit.lib.Repository repository,
RevWalk revWalk)
{
this.repository = repository;
this.revWalk = revWalk;
}
//~--- methods ------------------------------------------------------------
/**
* Method description
*
*
* @param ref
*
* @return
*/
@Override
public Tag apply(Ref ref)
{
Tag tag = null;
try
{
RevCommit commit = GitUtil.getCommit(repository, revWalk, ref);
if (commit != null)
{
String name = GitUtil.getTagName(ref);
tag = new Tag(commit.getId().name(), name);
}
}
catch (IOException ex)
{
logger.error("could not get commit for tag", ex);
}
return tag;
}
//~--- fields -------------------------------------------------------------
/** Field description */
private org.eclipse.jgit.lib.Repository repository;
/** Field description */
private RevWalk revWalk;
}
}