mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-02 11:35:57 +01:00
initial implementation
This commit is contained in:
@@ -42,6 +42,8 @@ import org.eclipse.jgit.lib.Constants;
|
||||
import org.eclipse.jgit.lib.ObjectId;
|
||||
import org.eclipse.jgit.lib.Ref;
|
||||
import org.eclipse.jgit.revwalk.RevCommit;
|
||||
import org.eclipse.jgit.revwalk.RevObject;
|
||||
import org.eclipse.jgit.revwalk.RevTag;
|
||||
import org.eclipse.jgit.revwalk.RevWalk;
|
||||
import org.eclipse.jgit.revwalk.filter.RevFilter;
|
||||
import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
|
||||
@@ -387,6 +389,44 @@ public final class GitUtil
|
||||
return ref;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param repository
|
||||
* @param revWalk
|
||||
* @param ref
|
||||
*
|
||||
* @return
|
||||
*
|
||||
* @throws IOException
|
||||
*
|
||||
* @since 2.5.0
|
||||
*/
|
||||
public static Long getTagTime(org.eclipse.jgit.lib.Repository repository,
|
||||
RevWalk revWalk, Ref ref)
|
||||
throws IOException
|
||||
{
|
||||
ObjectId id = ref.getObjectId();
|
||||
|
||||
if (id != null)
|
||||
{
|
||||
if (revWalk == null)
|
||||
{
|
||||
revWalk = new RevWalk(repository);
|
||||
}
|
||||
|
||||
final RevObject revObject = revWalk.parseAny(id);
|
||||
if (revObject instanceof RevTag) {
|
||||
return ((RevTag) revObject).getTaggerIdent().getWhen().getTime();
|
||||
} else if (revObject instanceof RevCommit) {
|
||||
return getCommitTime((RevCommit) revObject);
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
|
||||
@@ -21,12 +21,17 @@
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
|
||||
package sonia.scm.repository.api;
|
||||
|
||||
import com.google.common.base.Strings;
|
||||
import com.google.common.collect.ImmutableList;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
import org.eclipse.jgit.lib.Repository;
|
||||
import org.eclipse.jgit.revwalk.RevWalk;
|
||||
import org.eclipse.jgit.transport.ReceiveCommand;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
@@ -35,52 +40,60 @@ import sonia.scm.repository.Tag;
|
||||
|
||||
/**
|
||||
* Git provider implementation of {@link HookTagProvider}.
|
||||
*
|
||||
* @since 1.50
|
||||
*
|
||||
* @author Sebastian Sdorra
|
||||
* @since 1.50
|
||||
*/
|
||||
public class GitHookTagProvider implements HookTagProvider {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(GitHookTagProvider.class);
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(GitHookTagProvider.class);
|
||||
|
||||
private final List<Tag> createdTags;
|
||||
private final List<Tag> deletedTags;
|
||||
|
||||
/**
|
||||
* Constructs new instance.
|
||||
*
|
||||
*
|
||||
* @param commands received commands
|
||||
*/
|
||||
public GitHookTagProvider(List<ReceiveCommand> commands) {
|
||||
public GitHookTagProvider(List<ReceiveCommand> commands, Repository repository) {
|
||||
ImmutableList.Builder<Tag> createdTagBuilder = ImmutableList.builder();
|
||||
ImmutableList.Builder<Tag> deletedTagBuilder = ImmutableList.builder();
|
||||
|
||||
for ( ReceiveCommand rc : commands ){
|
||||
|
||||
for (ReceiveCommand rc : commands) {
|
||||
String refName = rc.getRefName();
|
||||
String tag = GitUtil.getTagName(refName);
|
||||
|
||||
if (Strings.isNullOrEmpty(tag)){
|
||||
logger.debug("received ref name {} is not a tag", refName);
|
||||
} else if (isCreate(rc)) {
|
||||
createdTagBuilder.add(createTagFromNewId(rc, tag));
|
||||
} else if (isDelete(rc)){
|
||||
deletedTagBuilder.add(createTagFromOldId(rc, tag));
|
||||
} else if (isUpdate(rc)) {
|
||||
createdTagBuilder.add(createTagFromNewId(rc, tag));
|
||||
deletedTagBuilder.add(createTagFromOldId(rc, tag));
|
||||
|
||||
if (Strings.isNullOrEmpty(tag)) {
|
||||
LOG.debug("received ref name {} is not a tag", refName);
|
||||
} else {
|
||||
Long tagTime = null;
|
||||
try (RevWalk walk = new RevWalk(repository)) {
|
||||
tagTime = GitUtil.getTagTime(repository, walk, rc.getRef());
|
||||
} catch (IOException e) {
|
||||
LOG.error("Could not read tag time", e);
|
||||
}
|
||||
if (isCreate(rc)) {
|
||||
createdTagBuilder.add(createTagFromNewId(rc, tag, tagTime));
|
||||
} else if (isDelete(rc)) {
|
||||
deletedTagBuilder.add(createTagFromOldId(rc, tag, tagTime));
|
||||
} else if (isUpdate(rc)) {
|
||||
createdTagBuilder.add(createTagFromNewId(rc, tag, tagTime));
|
||||
deletedTagBuilder.add(createTagFromOldId(rc, tag, tagTime));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
createdTags = createdTagBuilder.build();
|
||||
deletedTags = deletedTagBuilder.build();
|
||||
}
|
||||
|
||||
private Tag createTagFromNewId(ReceiveCommand rc, String tag) {
|
||||
return new Tag(tag, GitUtil.getId(rc.getNewId()));
|
||||
private Tag createTagFromNewId(ReceiveCommand rc, String tag, Long tagTime) {
|
||||
return new Tag(tag, GitUtil.getId(rc.getNewId()), tagTime);
|
||||
}
|
||||
|
||||
private Tag createTagFromOldId(ReceiveCommand rc, String tag) {
|
||||
return new Tag(tag, GitUtil.getId(rc.getOldId()));
|
||||
private Tag createTagFromOldId(ReceiveCommand rc, String tag, Long tagTime) {
|
||||
return new Tag(tag, GitUtil.getId(rc.getOldId()), tagTime);
|
||||
}
|
||||
|
||||
private boolean isUpdate(ReceiveCommand rc) {
|
||||
|
||||
@@ -103,7 +103,7 @@ public class GitHookContextProvider extends HookContextProvider
|
||||
|
||||
@Override
|
||||
public HookTagProvider getTagProvider() {
|
||||
return new GitHookTagProvider(receiveCommands);
|
||||
return new GitHookTagProvider(receiveCommands, repository);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -31,7 +31,7 @@ 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.RevObject;
|
||||
import org.eclipse.jgit.revwalk.RevWalk;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
@@ -45,34 +45,28 @@ import java.util.List;
|
||||
//~--- JDK imports ------------------------------------------------------------
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Sebastian Sdorra
|
||||
*/
|
||||
public class GitTagsCommand extends AbstractGitCommand implements TagsCommand
|
||||
{
|
||||
public class GitTagsCommand extends AbstractGitCommand implements TagsCommand {
|
||||
|
||||
/**
|
||||
* Constructs ...
|
||||
*
|
||||
* @param context
|
||||
*
|
||||
* @param context
|
||||
*/
|
||||
public GitTagsCommand(GitContext context)
|
||||
{
|
||||
public GitTagsCommand(GitContext context) {
|
||||
super(context);
|
||||
}
|
||||
|
||||
//~--- get methods ----------------------------------------------------------
|
||||
|
||||
@Override
|
||||
public List<Tag> getTags() throws IOException
|
||||
{
|
||||
public List<Tag> getTags() throws IOException {
|
||||
List<Tag> tags = null;
|
||||
|
||||
RevWalk revWalk = null;
|
||||
|
||||
try
|
||||
{
|
||||
try {
|
||||
final Git git = new Git(open());
|
||||
|
||||
revWalk = new RevWalk(git.getRepository());
|
||||
@@ -81,13 +75,9 @@ public class GitTagsCommand extends AbstractGitCommand implements TagsCommand
|
||||
|
||||
tags = Lists.transform(tagList,
|
||||
new TransformFuntion(git.getRepository(), revWalk));
|
||||
}
|
||||
catch (GitAPIException ex)
|
||||
{
|
||||
} catch (GitAPIException ex) {
|
||||
throw new InternalRepositoryException(repository, "could not read tags from repository", ex);
|
||||
}
|
||||
finally
|
||||
{
|
||||
} finally {
|
||||
GitUtil.release(revWalk);
|
||||
}
|
||||
|
||||
@@ -99,12 +89,10 @@ public class GitTagsCommand extends AbstractGitCommand implements TagsCommand
|
||||
/**
|
||||
* Class description
|
||||
*
|
||||
*
|
||||
* @version Enter version here..., 12/07/06
|
||||
* @author Enter your name here...
|
||||
* @author Enter your name here...
|
||||
* @version Enter version here..., 12/07/06
|
||||
*/
|
||||
private static class TransformFuntion implements Function<Ref, Tag>
|
||||
{
|
||||
private static class TransformFuntion implements Function<Ref, Tag> {
|
||||
|
||||
/**
|
||||
* the logger for TransformFuntion
|
||||
@@ -117,13 +105,11 @@ public class GitTagsCommand extends AbstractGitCommand implements TagsCommand
|
||||
/**
|
||||
* Constructs ...
|
||||
*
|
||||
*
|
||||
* @param repository
|
||||
* @param revWalk
|
||||
*/
|
||||
public TransformFuntion(org.eclipse.jgit.lib.Repository repository,
|
||||
RevWalk revWalk)
|
||||
{
|
||||
RevWalk revWalk) {
|
||||
this.repository = repository;
|
||||
this.revWalk = revWalk;
|
||||
}
|
||||
@@ -133,30 +119,23 @@ public class GitTagsCommand extends AbstractGitCommand implements TagsCommand
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param ref
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public Tag apply(Ref ref)
|
||||
{
|
||||
public Tag apply(Ref ref) {
|
||||
Tag tag = null;
|
||||
|
||||
try
|
||||
{
|
||||
RevCommit commit = GitUtil.getCommit(repository, revWalk, ref);
|
||||
try {
|
||||
RevObject revObject = revWalk.parseAny(ref.getObjectId());
|
||||
|
||||
if (commit != null)
|
||||
{
|
||||
if (revObject != null) {
|
||||
String name = GitUtil.getTagName(ref);
|
||||
|
||||
tag = new Tag(name, commit.getId().name());
|
||||
tag = new Tag(name, revObject.getId().name(), GitUtil.getTagTime(repository, revWalk, ref));
|
||||
}
|
||||
|
||||
}
|
||||
catch (IOException ex)
|
||||
{
|
||||
} catch (IOException ex) {
|
||||
logger.error("could not get commit for tag", ex);
|
||||
}
|
||||
|
||||
@@ -165,10 +144,14 @@ public class GitTagsCommand extends AbstractGitCommand implements TagsCommand
|
||||
|
||||
//~--- fields -------------------------------------------------------------
|
||||
|
||||
/** Field description */
|
||||
/**
|
||||
* Field description
|
||||
*/
|
||||
private org.eclipse.jgit.lib.Repository repository;
|
||||
|
||||
/** Field description */
|
||||
/**
|
||||
* Field description
|
||||
*/
|
||||
private RevWalk revWalk;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -78,6 +78,7 @@ public class GitTagCommand implements TagCommand
|
||||
String revision = request.getRevision();
|
||||
|
||||
RevObject revObject = null;
|
||||
Long tagTime = null;
|
||||
|
||||
if (!Strings.isNullOrEmpty(revision))
|
||||
{
|
||||
@@ -88,6 +89,7 @@ public class GitTagCommand implements TagCommand
|
||||
{
|
||||
walk = new RevWalk(git.getRepository());
|
||||
revObject = walk.parseAny(id);
|
||||
tagTime = GitUtil.getTagTime(git.getRepository(), walk, GitUtil.getRefForCommit(git.getRepository(), id));
|
||||
}
|
||||
finally
|
||||
{
|
||||
@@ -110,9 +112,9 @@ public class GitTagCommand implements TagCommand
|
||||
}
|
||||
|
||||
if (ref.isPeeled()) {
|
||||
tag = new Tag(request.getName(), ref.getPeeledObjectId().toString());
|
||||
tag = new Tag(request.getName(), ref.getPeeledObjectId().toString(), tagTime);
|
||||
} else {
|
||||
tag = new Tag(request.getName(), ref.getObjectId().toString());
|
||||
tag = new Tag(request.getName(), ref.getObjectId().toString(), tagTime);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
/*
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
package sonia.scm.repository.spi;
|
||||
|
||||
import com.github.sdorra.shiro.ShiroRule;
|
||||
import com.github.sdorra.shiro.SubjectAware;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.TemporaryFolder;
|
||||
import sonia.scm.repository.Tag;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
@SubjectAware(configuration = "classpath:sonia/scm/configuration/shiro.ini", username = "admin", password = "secret")
|
||||
public class GitTagsCommandTest extends AbstractGitCommandTestBase {
|
||||
|
||||
@Rule
|
||||
public TemporaryFolder temporaryFolder = new TemporaryFolder();
|
||||
|
||||
@Rule
|
||||
public BindTransportProtocolRule transportProtocolRule = new BindTransportProtocolRule();
|
||||
|
||||
@Rule
|
||||
public ShiroRule shiro = new ShiroRule();
|
||||
|
||||
@Test
|
||||
public void shouldGetDatesCorrectly() throws IOException {
|
||||
final GitContext gitContext = createContext();
|
||||
final GitTagsCommand tagsCommand = new GitTagsCommand(gitContext);
|
||||
final List<Tag> tags = tagsCommand.getTags();
|
||||
assertThat(tags).hasSize(2);
|
||||
assertThat(tags.get(0).getName()).isEqualTo("1.0.0");
|
||||
assertThat(tags.get(0).getDate()).isEqualTo(1598348105000L); // Annotated - Take tag date
|
||||
assertThat(tags.get(1).getName()).isEqualTo("test-tag");
|
||||
assertThat(tags.get(1).getDate()).isEqualTo(1339416344000L); // Lightweight - Take commit date
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getZippedRepositoryResource() {
|
||||
return "sonia/scm/repository/spi/scm-git-spi-test-tags.zip";
|
||||
}
|
||||
}
|
||||
Binary file not shown.
Reference in New Issue
Block a user