mirror of
				https://github.com/scm-manager/scm-manager.git
				synced 2025-10-31 18:46:07 +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; | ||||
|   } | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user