View Javadoc
1   package org.apache.maven.scm.provider.git.jgit.command.untag;
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 java.util.Collection;
23  
24  import org.apache.maven.scm.ScmException;
25  import org.apache.maven.scm.ScmFileSet;
26  import org.apache.maven.scm.ScmResult;
27  import org.apache.maven.scm.ScmUntagParameters;
28  import org.apache.maven.scm.command.untag.AbstractUntagCommand;
29  import org.apache.maven.scm.command.untag.UntagScmResult;
30  import org.apache.maven.scm.log.ScmLogger;
31  import org.apache.maven.scm.provider.ScmProviderRepository;
32  import org.apache.maven.scm.provider.git.command.GitCommand;
33  import org.apache.maven.scm.provider.git.jgit.command.JGitUtils;
34  import org.apache.maven.scm.provider.git.repository.GitScmProviderRepository;
35  import org.codehaus.plexus.util.StringUtils;
36  import org.eclipse.jgit.api.Git;
37  import org.eclipse.jgit.lib.Constants;
38  import org.eclipse.jgit.transport.PushResult;
39  import org.eclipse.jgit.transport.RefSpec;
40  import org.eclipse.jgit.transport.RemoteRefUpdate;
41  
42  /** {@inheritDoc} */
43  public class JGitUntagCommand extends AbstractUntagCommand implements GitCommand
44  {
45  
46      @Override
47      protected ScmResult executeUntagCommand( ScmProviderRepository repository, ScmFileSet fileSet,
48                                               ScmUntagParameters scmUntagParameters )
49          throws ScmException
50      {
51          String tagName = scmUntagParameters.getTag();
52          if ( tagName == null || StringUtils.isEmpty( tagName.trim() ) )
53          {
54              throw new ScmException( "tag name must be specified" );
55          }
56          String escapedTagName = tagName.trim().replace( ' ', '_' );
57  
58          Git git = null;
59          try
60          {
61              git = JGitUtils.openRepo( fileSet.getBasedir() );
62  
63              // delete the tag
64              if ( git.tagDelete().setTags( escapedTagName ).call().isEmpty() )
65              {
66                  return new UntagScmResult( "JGit tagDelete", "Failed to delete tag", "", false );
67              }
68  
69              if ( repository.isPushChanges() )
70              {
71                  // From https://stackoverflow.com/q/11892766/696632
72                  RefSpec refSpec = new RefSpec()
73                      .setSource( null )
74                      .setDestination( Constants.R_TAGS + escapedTagName );
75  
76                  getLogger().info( "push delete tag [" + escapedTagName + "] to remote..." );
77                  ScmLogger logger = getLogger();
78  
79                  Iterable<PushResult> pushResultList
80                          = JGitUtils.push( logger, git, (GitScmProviderRepository) repository, refSpec );
81                  if ( logger.isInfoEnabled() )
82                  {
83                      for ( PushResult pushResult : pushResultList )
84                      {
85                          Collection<RemoteRefUpdate> ru = pushResult.getRemoteUpdates();
86                          for ( RemoteRefUpdate remoteRefUpdate : ru )
87                          {
88                              logger.info( remoteRefUpdate.getStatus() + " - " + remoteRefUpdate );
89                          }
90                      }
91                  }
92              }
93  
94              return new UntagScmResult( "JGit tagDelete" );
95          }
96          catch ( Exception e )
97          {
98              throw new ScmException( "JGit tagDelete failure!", e );
99          }
100         finally
101         {
102             JGitUtils.closeRepo( git );
103         }
104     }
105 }