mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-06 21:45:43 +01:00
fix review findings
This commit is contained in:
@@ -82,60 +82,16 @@ import static java.util.Optional.of;
|
||||
public final class GitUtil {
|
||||
|
||||
private static final GitUserAgentProvider GIT_USER_AGENT_PROVIDER = new GitUserAgentProvider();
|
||||
|
||||
/**
|
||||
* 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";
|
||||
|
||||
/**
|
||||
* Field description
|
||||
*/
|
||||
private static final String DIRECTORY_DOTGIT = ".git";
|
||||
|
||||
/**
|
||||
* Field description
|
||||
*/
|
||||
private static final String DIRECTORY_OBJETCS = "objects";
|
||||
|
||||
/**
|
||||
* Field description
|
||||
*/
|
||||
private static final String DIRECTORY_REFS = "refs";
|
||||
|
||||
/**
|
||||
* Field description
|
||||
*/
|
||||
private static final String PREFIX_HEADS = "refs/heads/";
|
||||
|
||||
/**
|
||||
* Field description
|
||||
*/
|
||||
private static final String PREFIX_TAG = "refs/tags/";
|
||||
|
||||
/**
|
||||
* Field description
|
||||
*/
|
||||
private static final String REFSPEC = "+refs/heads/*:refs/remote/scm/%s/*";
|
||||
|
||||
/**
|
||||
* Field description
|
||||
*/
|
||||
private static final String REMOTE_REF = "refs/remote/scm/%s/%s";
|
||||
|
||||
/**
|
||||
* Field description
|
||||
*/
|
||||
private static final int TIMEOUT = 5;
|
||||
|
||||
/**
|
||||
@@ -145,19 +101,11 @@ public final class GitUtil {
|
||||
|
||||
//~--- constructors ---------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Constructs ...
|
||||
*/
|
||||
private GitUtil() {
|
||||
}
|
||||
|
||||
//~--- methods --------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
* @param repo
|
||||
*/
|
||||
public static void close(org.eclipse.jgit.lib.Repository repo) {
|
||||
if (repo != null) {
|
||||
repo.close();
|
||||
@@ -186,7 +134,7 @@ public final class GitUtil {
|
||||
|
||||
if (c != null) {
|
||||
tags.put(c.getId(), e.getKey());
|
||||
} else if (logger.isWarnEnabled()) {
|
||||
} else {
|
||||
logger.warn("could not find commit for tag {}", e.getKey());
|
||||
}
|
||||
|
||||
@@ -214,13 +162,6 @@ public final class GitUtil {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
* @param directory
|
||||
* @return
|
||||
* @throws IOException
|
||||
*/
|
||||
public static org.eclipse.jgit.lib.Repository open(File directory)
|
||||
throws IOException {
|
||||
FS fs = FS.DETECTED;
|
||||
@@ -239,33 +180,18 @@ public final class GitUtil {
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
* @param formatter
|
||||
*/
|
||||
public static void release(DiffFormatter formatter) {
|
||||
if (formatter != null) {
|
||||
formatter.close();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
* @param walk
|
||||
*/
|
||||
public static void release(TreeWalk walk) {
|
||||
if (walk != null) {
|
||||
walk.close();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
* @param walk
|
||||
*/
|
||||
public static void release(RevWalk walk) {
|
||||
if (walk != null) {
|
||||
walk.close();
|
||||
@@ -274,12 +200,6 @@ public final class GitUtil {
|
||||
|
||||
//~--- get methods ----------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
* @param ref
|
||||
* @return
|
||||
*/
|
||||
public static String getBranch(Ref ref) {
|
||||
String branch = null;
|
||||
|
||||
@@ -290,12 +210,6 @@ public final class GitUtil {
|
||||
return branch;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
* @param name
|
||||
* @return
|
||||
*/
|
||||
public static String getBranch(String name) {
|
||||
String branch = null;
|
||||
|
||||
|
||||
@@ -25,6 +25,7 @@
|
||||
package sonia.scm.repository.spi;
|
||||
|
||||
import com.google.common.base.Strings;
|
||||
import org.apache.shiro.SecurityUtils;
|
||||
import org.eclipse.jgit.api.Git;
|
||||
import org.eclipse.jgit.api.errors.GitAPIException;
|
||||
import org.eclipse.jgit.lib.ObjectId;
|
||||
@@ -34,6 +35,7 @@ import org.eclipse.jgit.lib.Repository;
|
||||
import org.eclipse.jgit.revwalk.RevCommit;
|
||||
import org.eclipse.jgit.revwalk.RevObject;
|
||||
import org.eclipse.jgit.revwalk.RevWalk;
|
||||
import sonia.scm.NotFoundException;
|
||||
import sonia.scm.event.ScmEventBus;
|
||||
import sonia.scm.repository.GitUtil;
|
||||
import sonia.scm.repository.InternalRepositoryException;
|
||||
@@ -41,7 +43,6 @@ import sonia.scm.repository.PostReceiveRepositoryHookEvent;
|
||||
import sonia.scm.repository.PreReceiveRepositoryHookEvent;
|
||||
import sonia.scm.repository.RepositoryHookEvent;
|
||||
import sonia.scm.repository.RepositoryHookType;
|
||||
import sonia.scm.repository.Signature;
|
||||
import sonia.scm.repository.Tag;
|
||||
import sonia.scm.repository.api.HookContext;
|
||||
import sonia.scm.repository.api.HookContextFactory;
|
||||
@@ -50,6 +51,7 @@ import sonia.scm.repository.api.HookTagProvider;
|
||||
import sonia.scm.repository.api.TagCreateRequest;
|
||||
import sonia.scm.repository.api.TagDeleteRequest;
|
||||
import sonia.scm.security.GPG;
|
||||
import sonia.scm.user.User;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import java.io.IOException;
|
||||
@@ -76,53 +78,72 @@ public class GitTagCommand extends AbstractGitCommand implements TagCommand {
|
||||
|
||||
@Override
|
||||
public Tag create(TagCreateRequest request) {
|
||||
final String name = request.getName();
|
||||
final String revision = request.getRevision();
|
||||
|
||||
if (Strings.isNullOrEmpty(revision)) {
|
||||
throw new IllegalArgumentException("Revision is required");
|
||||
}
|
||||
|
||||
if (Strings.isNullOrEmpty(name)) {
|
||||
throw new IllegalArgumentException("Name is required");
|
||||
}
|
||||
|
||||
try (Git git = new Git(context.open())) {
|
||||
String revision = request.getRevision();
|
||||
|
||||
RevObject revObject;
|
||||
Long tagTime;
|
||||
|
||||
if (Strings.isNullOrEmpty(revision)) {
|
||||
throw new IllegalArgumentException("Revision is required");
|
||||
}
|
||||
|
||||
ObjectId taggedCommitObjectId = git.getRepository().resolve(revision);
|
||||
|
||||
if (taggedCommitObjectId == null) {
|
||||
throw new NotFoundException("revision", revision);
|
||||
}
|
||||
|
||||
try (RevWalk walk = new RevWalk(git.getRepository())) {
|
||||
revObject = walk.parseAny(taggedCommitObjectId);
|
||||
tagTime = GitUtil.getTagTime(walk, taggedCommitObjectId);
|
||||
}
|
||||
|
||||
Tag tag = new Tag(request.getName(), revision, tagTime);
|
||||
Tag tag = new Tag(name, revision, tagTime);
|
||||
|
||||
RepositoryHookEvent hookEvent = createTagHookEvent(TagHookContextProvider.createHookEvent(tag));
|
||||
eventBus.post(new PreReceiveRepositoryHookEvent(hookEvent));
|
||||
|
||||
Ref ref =
|
||||
git.tag()
|
||||
.setObjectId(revObject)
|
||||
.setTagger(new PersonIdent("SCM-Manager", "noreply@scm-manager.org"))
|
||||
.setName(request.getName())
|
||||
.call();
|
||||
User user = SecurityUtils.getSubject().getPrincipals().oneByType(User.class);
|
||||
PersonIdent taggerIdent = new PersonIdent(user.getDisplayName(), user.getMail());
|
||||
|
||||
try (RevWalk walk = new RevWalk(git.getRepository())) {
|
||||
revObject = walk.parseTag(ref.getObjectId());
|
||||
final Optional<Signature> tagSignature = GitUtil.getTagSignature(revObject, gpg, walk);
|
||||
tagSignature.ifPresent(tag::addSignature);
|
||||
}
|
||||
// Ref ref =
|
||||
git.tag()
|
||||
.setObjectId(revObject)
|
||||
.setTagger(taggerIdent)
|
||||
.setName(name)
|
||||
.call();
|
||||
|
||||
// Uncomment lines once jgit added support for signing tags
|
||||
// try (RevWalk walk = new RevWalk(git.getRepository())) {
|
||||
// revObject = walk.parseTag(ref.getObjectId());
|
||||
// final Optional<Signature> tagSignature = GitUtil.getTagSignature(revObject, gpg, walk);
|
||||
// tagSignature.ifPresent(tag::addSignature);
|
||||
// }
|
||||
|
||||
eventBus.post(new PostReceiveRepositoryHookEvent(hookEvent));
|
||||
|
||||
return tag;
|
||||
} catch (IOException | GitAPIException ex) {
|
||||
throw new InternalRepositoryException(repository, "could not create tag " + request.getName(), ex);
|
||||
throw new InternalRepositoryException(repository, "could not create tag " + name + " for revision " + revision, ex);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete(TagDeleteRequest request) {
|
||||
String name = request.getName();
|
||||
|
||||
if (Strings.isNullOrEmpty(name)) {
|
||||
throw new IllegalArgumentException("Name is required");
|
||||
}
|
||||
|
||||
try (Git git = new Git(context.open())) {
|
||||
String name = request.getName();
|
||||
final Repository repository = git.getRepository();
|
||||
Optional<Ref> tagRef = findTagRef(git, name);
|
||||
Tag tag;
|
||||
@@ -135,7 +156,7 @@ public class GitTagCommand extends AbstractGitCommand implements TagCommand {
|
||||
|
||||
try (RevWalk walk = new RevWalk(repository)) {
|
||||
final RevCommit commit = GitUtil.getCommit(repository, walk, tagRef.get());
|
||||
Long tagTime = GitUtil.getTagTime(walk, commit.toObjectId());
|
||||
Long tagTime = GitUtil.getTagTime(walk, tagRef.get().getObjectId());
|
||||
tag = new Tag(name, commit.name(), tagTime);
|
||||
}
|
||||
|
||||
@@ -144,7 +165,7 @@ public class GitTagCommand extends AbstractGitCommand implements TagCommand {
|
||||
git.tagDelete().setTags(name).call();
|
||||
eventBus.post(new PostReceiveRepositoryHookEvent(hookEvent));
|
||||
} catch (GitAPIException | IOException e) {
|
||||
throw new InternalRepositoryException(repository, "could not delete tag", e);
|
||||
throw new InternalRepositoryException(repository, "could not delete tag " + name, e);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -158,46 +179,46 @@ public class GitTagCommand extends AbstractGitCommand implements TagCommand {
|
||||
return new RepositoryHookEvent(context, this.context.getRepository(), RepositoryHookType.PRE_RECEIVE);
|
||||
}
|
||||
|
||||
private static class TagHookContextProvider extends HookContextProvider {
|
||||
private final List<Tag> newTags;
|
||||
private final List<Tag> deletedTags;
|
||||
private static class TagHookContextProvider extends HookContextProvider {
|
||||
private final List<Tag> newTags;
|
||||
private final List<Tag> deletedTags;
|
||||
|
||||
private TagHookContextProvider(List<Tag> newTags, List<Tag> deletedTags) {
|
||||
this.newTags = newTags;
|
||||
this.deletedTags = deletedTags;
|
||||
}
|
||||
private TagHookContextProvider(List<Tag> newTags, List<Tag> deletedTags) {
|
||||
this.newTags = newTags;
|
||||
this.deletedTags = deletedTags;
|
||||
}
|
||||
|
||||
static TagHookContextProvider createHookEvent(Tag newTag) {
|
||||
return new TagHookContextProvider(singletonList(newTag), emptyList());
|
||||
}
|
||||
static TagHookContextProvider createHookEvent(Tag newTag) {
|
||||
return new TagHookContextProvider(singletonList(newTag), emptyList());
|
||||
}
|
||||
|
||||
static TagHookContextProvider deleteHookEvent(Tag deletedTag) {
|
||||
return new TagHookContextProvider(emptyList(), singletonList(deletedTag));
|
||||
}
|
||||
static TagHookContextProvider deleteHookEvent(Tag deletedTag) {
|
||||
return new TagHookContextProvider(emptyList(), singletonList(deletedTag));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<HookFeature> getSupportedFeatures() {
|
||||
return singleton(HookFeature.BRANCH_PROVIDER);
|
||||
}
|
||||
@Override
|
||||
public Set<HookFeature> getSupportedFeatures() {
|
||||
return singleton(HookFeature.TAG_PROVIDER);
|
||||
}
|
||||
|
||||
@Override
|
||||
public HookTagProvider getTagProvider() {
|
||||
return new HookTagProvider() {
|
||||
@Override
|
||||
public List<Tag> getCreatedTags() {
|
||||
return newTags;
|
||||
}
|
||||
@Override
|
||||
public HookTagProvider getTagProvider() {
|
||||
return new HookTagProvider() {
|
||||
@Override
|
||||
public List<Tag> getCreatedTags() {
|
||||
return newTags;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Tag> getDeletedTags() {
|
||||
return deletedTags;
|
||||
}
|
||||
};
|
||||
}
|
||||
@Override
|
||||
public List<Tag> getDeletedTags() {
|
||||
return deletedTags;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public HookChangesetProvider getChangesetProvider() {
|
||||
return r -> new HookChangesetResponse(emptyList());
|
||||
}
|
||||
@Override
|
||||
public HookChangesetProvider getChangesetProvider() {
|
||||
return r -> new HookChangesetResponse(emptyList());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -31,7 +31,10 @@ import com.google.common.collect.Lists;
|
||||
import org.eclipse.jgit.api.Git;
|
||||
import org.eclipse.jgit.api.errors.GitAPIException;
|
||||
import org.eclipse.jgit.errors.IncorrectObjectTypeException;
|
||||
import org.eclipse.jgit.lib.Constants;
|
||||
import org.eclipse.jgit.lib.Ref;
|
||||
import org.eclipse.jgit.lib.Repository;
|
||||
import org.eclipse.jgit.revwalk.RevCommit;
|
||||
import org.eclipse.jgit.revwalk.RevObject;
|
||||
import org.eclipse.jgit.revwalk.RevTag;
|
||||
import org.eclipse.jgit.revwalk.RevWalk;
|
||||
@@ -76,15 +79,13 @@ public class GitTagsCommand extends AbstractGitCommand implements TagsCommand {
|
||||
|
||||
RevWalk revWalk = null;
|
||||
|
||||
try {
|
||||
final Git git = new Git(open());
|
||||
|
||||
try (Git git = new Git(open())) {
|
||||
revWalk = new RevWalk(git.getRepository());
|
||||
|
||||
List<Ref> tagList = git.tagList().call();
|
||||
|
||||
tags = Lists.transform(tagList,
|
||||
new TransformFunction(git.getRepository(), revWalk, gpg, git));
|
||||
new TransformFunction(git.getRepository(), revWalk, gpg));
|
||||
} catch (GitAPIException ex) {
|
||||
throw new InternalRepositoryException(repository, "could not read tags from repository", ex);
|
||||
} finally {
|
||||
@@ -114,18 +115,15 @@ public class GitTagsCommand extends AbstractGitCommand implements TagsCommand {
|
||||
|
||||
/**
|
||||
* Constructs ...
|
||||
*
|
||||
* @param repository
|
||||
* @param repository
|
||||
* @param revWalk
|
||||
*/
|
||||
public TransformFunction(org.eclipse.jgit.lib.Repository repository,
|
||||
public TransformFunction(Repository repository,
|
||||
RevWalk revWalk,
|
||||
GPG gpg,
|
||||
Git git) {
|
||||
GPG gpg) {
|
||||
this.repository = repository;
|
||||
this.revWalk = revWalk;
|
||||
this.gpg = gpg;
|
||||
this.git = git;
|
||||
}
|
||||
|
||||
//~--- methods ------------------------------------------------------------
|
||||
@@ -141,26 +139,17 @@ public class GitTagsCommand extends AbstractGitCommand implements TagsCommand {
|
||||
Tag tag = null;
|
||||
|
||||
try {
|
||||
RevObject revObject = GitUtil.getCommit(repository, revWalk, ref);
|
||||
|
||||
if (revObject != null) {
|
||||
RevCommit revCommit = GitUtil.getCommit(repository, revWalk, ref);
|
||||
if (revCommit != null) {
|
||||
String name = GitUtil.getTagName(ref);
|
||||
|
||||
tag = new Tag(name, revObject.getId().name(), GitUtil.getTagTime(revWalk, ref.getObjectId()));
|
||||
|
||||
try {
|
||||
RevTag revTag = GitUtil.getTag(repository, revWalk, ref);
|
||||
|
||||
final Optional<Signature> tagSignature = GitUtil.getTagSignature(revTag, gpg, revWalk);
|
||||
if (tagSignature.isPresent()) {
|
||||
tag.addSignature(tagSignature.get());
|
||||
}
|
||||
} catch (IncorrectObjectTypeException e) {
|
||||
// Ignore because it is a lightweight tag which cannot have signatures
|
||||
tag = new Tag(name, revCommit.getId().name(), GitUtil.getTagTime(revWalk, ref.getObjectId()));
|
||||
RevObject revObject = revWalk.parseAny(ref.getObjectId());
|
||||
if (revObject.getType() == Constants.OBJ_TAG) {
|
||||
RevTag revTag = (RevTag) revObject;
|
||||
GitUtil.getTagSignature(revTag, gpg, revWalk)
|
||||
.ifPresent(tag::addSignature);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
} catch (IOException ex) {
|
||||
logger.error("could not get commit for tag", ex);
|
||||
}
|
||||
@@ -173,13 +162,12 @@ public class GitTagsCommand extends AbstractGitCommand implements TagsCommand {
|
||||
/**
|
||||
* Field description
|
||||
*/
|
||||
private org.eclipse.jgit.lib.Repository repository;
|
||||
private final org.eclipse.jgit.lib.Repository repository;
|
||||
|
||||
/**
|
||||
* Field description
|
||||
*/
|
||||
private RevWalk revWalk;
|
||||
private final RevWalk revWalk;
|
||||
private final GPG gpg;
|
||||
private final Git git;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -134,8 +134,8 @@ public class GitTagCommandTest extends AbstractGitCommandTestBase {
|
||||
}
|
||||
|
||||
private Optional<Tag> findTag(GitContext context, String name) throws IOException {
|
||||
List<Tag> branches = readTags(context);
|
||||
return branches.stream().filter(b -> name.equals(b.getName())).findFirst();
|
||||
List<Tag> tags = readTags(context);
|
||||
return tags.stream().filter(t -> name.equals(t.getName())).findFirst();
|
||||
}
|
||||
|
||||
private HookContext createMockedContext(InvocationOnMock invocation) {
|
||||
|
||||
Reference in New Issue
Block a user