View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.scm.provider.git.gitexe.command.untag;
20  
21  import java.io.File;
22  
23  import org.apache.commons.lang3.StringUtils;
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.cli.CommandLineUtils;
35  import org.codehaus.plexus.util.cli.Commandline;
36  
37  /** {@inheritDoc} */
38  public class GitUntagCommand extends AbstractUntagCommand implements GitCommand {
39  
40      /** {@inheritDoc} */
41      public ScmResult executeUntagCommand(
42              ScmProviderRepository repo, ScmFileSet fileSet, ScmUntagParameters scmUntagParameters) throws ScmException {
43          String tag = scmUntagParameters.getTag();
44          if (tag == null || StringUtils.isEmpty(tag.trim())) {
45              throw new ScmException("tag name must be specified");
46          }
47  
48          GitScmProviderRepository repository = (GitScmProviderRepository) repo;
49  
50          CommandLineUtils.StringStreamConsumer stdout = new CommandLineUtils.StringStreamConsumer();
51          CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
52  
53          int exitCode;
54  
55          Commandline clTag = createCommandLine(repository, fileSet.getBasedir(), tag);
56  
57          exitCode = GitCommandLineUtils.execute(clTag, stdout, stderr);
58          if (exitCode != 0) {
59              return new UntagScmResult(clTag.toString(), "The git-tag command failed.", stderr.getOutput(), false);
60          }
61  
62          if (repo.isPushChanges()) {
63              // and now push the tag to the configured upstream repository
64              Commandline clPush = createPushCommandLine(repository, fileSet, tag);
65  
66              exitCode = GitCommandLineUtils.execute(clPush, stdout, stderr);
67              if (exitCode != 0) {
68                  return new UntagScmResult(clPush.toString(), "The git-push command failed.", stderr.getOutput(), false);
69              }
70          }
71  
72          return new UntagScmResult(clTag.toString());
73      }
74  
75      // ----------------------------------------------------------------------
76      //
77      // ----------------------------------------------------------------------
78  
79      public static Commandline createCommandLine(
80              GitScmProviderRepository repository, File workingDirectory, String tag) {
81          Commandline cl = GitCommandLineUtils.getBaseGitCommandLine(workingDirectory, "tag");
82  
83          cl.createArg().setValue("-d");
84          cl.createArg().setValue(tag);
85  
86          return cl;
87      }
88  
89      public static Commandline createPushCommandLine(
90              GitScmProviderRepository repository, ScmFileSet fileSet, String tag) {
91          Commandline cl = GitCommandLineUtils.getBaseGitCommandLine(fileSet.getBasedir(), "push");
92  
93          cl.createArg().setValue("--delete");
94          cl.createArg().setValue(repository.getPushUrl());
95          cl.createArg().setValue("refs/tags/" + tag);
96  
97          return cl;
98      }
99  }