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