001package org.apache.maven.scm.provider.git.jgit.command.tag; 002 003/* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022import org.apache.maven.scm.ScmException; 023import org.apache.maven.scm.ScmFile; 024import org.apache.maven.scm.ScmFileSet; 025import org.apache.maven.scm.ScmFileStatus; 026import org.apache.maven.scm.ScmResult; 027import org.apache.maven.scm.ScmTagParameters; 028import org.apache.maven.scm.command.tag.AbstractTagCommand; 029import org.apache.maven.scm.command.tag.TagScmResult; 030import org.apache.maven.scm.provider.ScmProviderRepository; 031import org.apache.maven.scm.provider.git.command.GitCommand; 032import org.apache.maven.scm.provider.git.jgit.command.JGitUtils; 033import org.apache.maven.scm.provider.git.repository.GitScmProviderRepository; 034import org.codehaus.plexus.util.StringUtils; 035import org.eclipse.jgit.api.Git; 036import org.eclipse.jgit.lib.Constants; 037import org.eclipse.jgit.lib.Ref; 038import org.eclipse.jgit.revwalk.RevCommit; 039import org.eclipse.jgit.revwalk.RevWalk; 040import org.eclipse.jgit.transport.RefSpec; 041import org.eclipse.jgit.treewalk.TreeWalk; 042 043import java.util.ArrayList; 044import java.util.List; 045 046/** 047 * @author <a href="mailto:struberg@yahoo.de">Mark Struberg</a> 048 * @author Dominik Bartholdi (imod) 049 * @since 1.9 050 */ 051public class JGitTagCommand 052 extends AbstractTagCommand 053 implements GitCommand 054{ 055 056 public ScmResult executeTagCommand( ScmProviderRepository repo, ScmFileSet fileSet, String tag, String message ) 057 throws ScmException 058 { 059 return executeTagCommand( repo, fileSet, tag, new ScmTagParameters( message ) ); 060 } 061 062 /** 063 * {@inheritDoc} 064 */ 065 public ScmResult executeTagCommand( ScmProviderRepository repo, ScmFileSet fileSet, String tag, 066 ScmTagParameters scmTagParameters ) 067 throws ScmException 068 { 069 if ( tag == null || StringUtils.isEmpty( tag.trim() ) ) 070 { 071 throw new ScmException( "tag name must be specified" ); 072 } 073 074 if ( !fileSet.getFileList().isEmpty() ) 075 { 076 throw new ScmException( "This provider doesn't support tagging subsets of a directory" ); 077 } 078 079 String escapedTagName = tag.trim().replace( ' ', '_' ); 080 081 Git git = null; 082 try 083 { 084 git = JGitUtils.openRepo( fileSet.getBasedir() ); 085 086 // tag the revision 087 String tagMessage = scmTagParameters.getMessage(); 088 Ref tagRef = git.tag().setName( escapedTagName ).setMessage( tagMessage ).setForceUpdate( false ).call(); 089 090 if ( repo.isPushChanges() ) 091 { 092 getLogger().info( "push tag [" + escapedTagName + "] to remote..." ); 093 JGitUtils.push( getLogger(), git, (GitScmProviderRepository) repo, new RefSpec( Constants.R_TAGS 094 + escapedTagName ) ); 095 } 096 097 // search for the tagged files 098 RevWalk revWalk = new RevWalk( git.getRepository() ); 099 RevCommit commit = revWalk.parseCommit( tagRef.getObjectId() ); 100 revWalk.close(); 101 102 final TreeWalk walk = new TreeWalk( git.getRepository() ); 103 walk.reset(); // drop the first empty tree, which we do not need here 104 walk.setRecursive( true ); 105 walk.addTree( commit.getTree() ); 106 107 List<ScmFile> taggedFiles = new ArrayList<ScmFile>(); 108 while ( walk.next() ) 109 { 110 taggedFiles.add( new ScmFile( walk.getPathString(), ScmFileStatus.CHECKED_OUT ) ); 111 } 112 walk.close(); 113 114 return new TagScmResult( "JGit tag", taggedFiles ); 115 } 116 catch ( Exception e ) 117 { 118 throw new ScmException( "JGit tag failure!", e ); 119 } 120 finally 121 { 122 JGitUtils.closeRepo( git ); 123 } 124 } 125 126}