View Javadoc

1   package org.apache.maven.scm.provider.git.jgit.command.tag;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   * http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.scm.ScmException;
23  import org.apache.maven.scm.ScmFile;
24  import org.apache.maven.scm.ScmFileSet;
25  import org.apache.maven.scm.ScmFileStatus;
26  import org.apache.maven.scm.ScmResult;
27  import org.apache.maven.scm.ScmTagParameters;
28  import org.apache.maven.scm.command.tag.AbstractTagCommand;
29  import org.apache.maven.scm.command.tag.TagScmResult;
30  import org.apache.maven.scm.provider.ScmProviderRepository;
31  import org.apache.maven.scm.provider.git.command.GitCommand;
32  import org.apache.maven.scm.provider.git.jgit.command.JGitUtils;
33  import org.apache.maven.scm.provider.git.repository.GitScmProviderRepository;
34  import org.codehaus.plexus.util.StringUtils;
35  import org.eclipse.jgit.api.Git;
36  import org.eclipse.jgit.lib.Constants;
37  import org.eclipse.jgit.lib.Ref;
38  import org.eclipse.jgit.revwalk.RevCommit;
39  import org.eclipse.jgit.revwalk.RevWalk;
40  import org.eclipse.jgit.transport.RefSpec;
41  import org.eclipse.jgit.treewalk.TreeWalk;
42  
43  import java.util.ArrayList;
44  import java.util.List;
45  
46  /**
47   * @author <a href="mailto:struberg@yahoo.de">Mark Struberg</a>
48   * @author Dominik Bartholdi (imod)
49   * @since 1.9
50   */
51  public class JGitTagCommand
52      extends AbstractTagCommand
53      implements GitCommand
54  {
55  
56      public ScmResult executeTagCommand( ScmProviderRepository repo, ScmFileSet fileSet, String tag, String message )
57          throws ScmException
58      {
59          return executeTagCommand( repo, fileSet, tag, new ScmTagParameters( message ) );
60      }
61  
62      /**
63       * {@inheritDoc}
64       */
65      public ScmResult executeTagCommand( ScmProviderRepository repo, ScmFileSet fileSet, String tag,
66                                          ScmTagParameters scmTagParameters )
67          throws ScmException
68      {
69          if ( tag == null || StringUtils.isEmpty( tag.trim() ) )
70          {
71              throw new ScmException( "tag name must be specified" );
72          }
73  
74          if ( !fileSet.getFileList().isEmpty() )
75          {
76              throw new ScmException( "This provider doesn't support tagging subsets of a directory" );
77          }
78  
79          String escapedTagName = tag.trim().replace( ' ', '_' );
80  
81          Git git = null;
82          try
83          {
84              git = Git.open( fileSet.getBasedir() );
85  
86              // tag the revision
87              String tagMessage = scmTagParameters.getMessage();
88              Ref tagRef = git.tag().setName( escapedTagName ).setMessage( tagMessage ).setForceUpdate( false ).call();
89  
90              if ( repo.isPushChanges() )
91              {
92                  getLogger().info( "push tag [" + escapedTagName + "] to remote..." );
93                  JGitUtils.push( getLogger(), git, (GitScmProviderRepository) repo, new RefSpec( Constants.R_TAGS
94                      + escapedTagName ) );
95              }
96  
97              // search for the tagged files
98              RevWalk revWalk = new RevWalk( git.getRepository() );
99              RevCommit commit = revWalk.parseCommit( tagRef.getObjectId() );
100             revWalk.release();
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.release();
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 }