View Javadoc
1   package org.apache.maven.scm.provider.git.gitexe.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.io.File;
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.provider.ScmProviderRepository;
31  import org.apache.maven.scm.provider.git.command.GitCommand;
32  import org.apache.maven.scm.provider.git.gitexe.command.GitCommandLineUtils;
33  import org.apache.maven.scm.provider.git.repository.GitScmProviderRepository;
34  import org.codehaus.plexus.util.StringUtils;
35  import org.codehaus.plexus.util.cli.CommandLineUtils;
36  import org.codehaus.plexus.util.cli.Commandline;
37  
38  /** {@inheritDoc} */
39  public class GitUntagCommand
40      extends AbstractUntagCommand
41      implements GitCommand
42  {
43  
44      /** {@inheritDoc} */
45      public ScmResult executeUntagCommand( ScmProviderRepository repo, ScmFileSet fileSet,
46                                            ScmUntagParameters scmUntagParameters )
47          throws ScmException
48      {
49          String tag = scmUntagParameters.getTag();
50          if ( tag == null || StringUtils.isEmpty( tag.trim() ) )
51          {
52              throw new ScmException( "tag name must be specified" );
53          }
54  
55          GitScmProviderRepository repository = (GitScmProviderRepository) repo;
56  
57          CommandLineUtils.StringStreamConsumer stdout = new CommandLineUtils.StringStreamConsumer();
58          CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
59  
60          int exitCode;
61  
62          Commandline clTag = createCommandLine( repository, fileSet.getBasedir(), tag );
63  
64          exitCode = GitCommandLineUtils.execute( clTag, stdout, stderr, getLogger() );
65          if ( exitCode != 0 )
66          {
67              return new UntagScmResult( clTag.toString(), "The git-tag command failed.", stderr.getOutput(), false );
68          }
69  
70          if ( repo.isPushChanges() )
71          {
72              // and now push the tag to the configured upstream repository
73              Commandline clPush = createPushCommandLine( repository, fileSet, tag );
74  
75              exitCode = GitCommandLineUtils.execute( clPush, stdout, stderr, getLogger() );
76              if ( exitCode != 0 )
77              {
78                  return new UntagScmResult( clPush.toString(), "The git-push command failed.", stderr.getOutput(),
79                                           false );
80              }
81          }
82  
83          return new UntagScmResult( clTag.toString() );
84      }
85  
86      // ----------------------------------------------------------------------
87      //
88      // ----------------------------------------------------------------------
89  
90      public static Commandline createCommandLine( GitScmProviderRepository repository, File workingDirectory,
91                                                   String tag )
92      {
93          Commandline cl = GitCommandLineUtils.getBaseGitCommandLine( workingDirectory, "tag" );
94  
95          cl.createArg().setValue( "-d" );
96          cl.createArg().setValue( tag );
97  
98          return cl;
99      }
100 
101     public static Commandline createPushCommandLine( GitScmProviderRepository repository, ScmFileSet fileSet,
102                                                      String tag )
103     {
104         Commandline cl = GitCommandLineUtils.getBaseGitCommandLine( fileSet.getBasedir(), "push" );
105 
106         cl.createArg().setValue( "--delete" );
107         cl.createArg().setValue( repository.getPushUrl() );
108         cl.createArg().setValue( "refs/tags/" + tag );
109 
110         return cl;
111     }
112 
113 }