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.tag;
20  
21  import java.io.File;
22  import java.io.IOException;
23  import java.util.Map;
24  
25  import org.apache.maven.scm.CommandParameters.SignOption;
26  import org.apache.maven.scm.ScmException;
27  import org.apache.maven.scm.ScmFileSet;
28  import org.apache.maven.scm.ScmFileStatus;
29  import org.apache.maven.scm.ScmResult;
30  import org.apache.maven.scm.ScmTagParameters;
31  import org.apache.maven.scm.command.checkout.CheckOutScmResult;
32  import org.apache.maven.scm.command.tag.AbstractTagCommand;
33  import org.apache.maven.scm.command.tag.TagScmResult;
34  import org.apache.maven.scm.provider.ScmProviderRepository;
35  import org.apache.maven.scm.provider.git.command.GitCommand;
36  import org.apache.maven.scm.provider.git.gitexe.command.GitCommandLineUtils;
37  import org.apache.maven.scm.provider.git.gitexe.command.list.GitListCommand;
38  import org.apache.maven.scm.provider.git.gitexe.command.list.GitListConsumer;
39  import org.apache.maven.scm.provider.git.repository.GitScmProviderRepository;
40  import org.codehaus.plexus.util.FileUtils;
41  import org.codehaus.plexus.util.cli.CommandLineUtils;
42  import org.codehaus.plexus.util.cli.Commandline;
43  
44  /**
45   * @author <a href="mailto:struberg@yahoo.de">Mark Struberg</a>
46   */
47  public class GitTagCommand extends AbstractTagCommand implements GitCommand {
48      private final Map<String, String> environmentVariables;
49  
50      public GitTagCommand(Map<String, String> environmentVariables) {
51          this.environmentVariables = environmentVariables;
52      }
53  
54      public ScmResult executeTagCommand(ScmProviderRepository repo, ScmFileSet fileSet, String tag, String message)
55              throws ScmException {
56          return executeTagCommand(repo, fileSet, tag, new ScmTagParameters(message));
57      }
58  
59      /**
60       * {@inheritDoc}
61       */
62      public ScmResult executeTagCommand(
63              ScmProviderRepository repo, ScmFileSet fileSet, String tag, ScmTagParameters scmTagParameters)
64              throws ScmException {
65          if (tag == null || tag.trim().isEmpty()) {
66              throw new ScmException("tag name must be specified");
67          }
68  
69          if (!fileSet.getFileList().isEmpty()) {
70              throw new ScmException("This provider doesn't support tagging subsets of a directory");
71          }
72  
73          GitScmProviderRepository repository = (GitScmProviderRepository) repo;
74  
75          File messageFile = FileUtils.createTempFile("maven-scm-", ".commit", null);
76  
77          try {
78              FileUtils.fileWrite(messageFile.getAbsolutePath(), "UTF-8", scmTagParameters.getMessage());
79          } catch (IOException ex) {
80              return new TagScmResult(
81                      null,
82                      "Error while making a temporary file for the commit message: " + ex.getMessage(),
83                      null,
84                      false);
85          }
86  
87          try {
88              CommandLineUtils.StringStreamConsumer stdout = new CommandLineUtils.StringStreamConsumer();
89              CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
90  
91              int exitCode;
92  
93              Commandline clTag = createCommandLine(
94                      repository, fileSet.getBasedir(), tag, messageFile, scmTagParameters.getSignOption());
95  
96              exitCode = GitCommandLineUtils.execute(clTag, stdout, stderr);
97              if (exitCode != 0) {
98                  return new TagScmResult(clTag.toString(), "The git-tag command failed.", stderr.getOutput(), false);
99              }
100 
101             if (repo.isPushChanges()) {
102                 // and now push the tag to the configured upstream repository
103                 Commandline clPush = createPushCommandLine(repository, fileSet, tag);
104 
105                 exitCode = GitCommandLineUtils.execute(clPush, stdout, stderr);
106                 if (exitCode != 0) {
107                     return new TagScmResult(
108                             clPush.toString(), "The git-push command failed.", stderr.getOutput(), false);
109                 }
110             }
111 
112             // plus search for the tagged files
113             GitListConsumer listConsumer = new GitListConsumer(fileSet.getBasedir(), ScmFileStatus.TAGGED);
114 
115             Commandline clList = GitListCommand.createCommandLine(repository, fileSet.getBasedir());
116 
117             exitCode = GitCommandLineUtils.execute(clList, listConsumer, stderr);
118             if (exitCode != 0) {
119                 return new CheckOutScmResult(
120                         clList.toString(), "The git-ls-files command failed.", stderr.getOutput(), false);
121             }
122 
123             return new TagScmResult(clTag.toString(), listConsumer.getListedFiles());
124         } finally {
125             try {
126                 FileUtils.forceDelete(messageFile);
127             } catch (IOException ex) {
128                 // ignore
129             }
130         }
131     }
132 
133     // ----------------------------------------------------------------------
134     //
135     // ----------------------------------------------------------------------
136 
137     static Commandline createCommandLine(
138             GitScmProviderRepository repository,
139             File workingDirectory,
140             String tag,
141             File messageFile,
142             SignOption signOption) {
143         Commandline cl = GitCommandLineUtils.getBaseGitCommandLine(workingDirectory, "tag");
144 
145         switch (signOption) {
146             case FORCE_SIGN:
147                 cl.createArg().setValue("-s");
148                 break;
149             case FORCE_NO_SIGN:
150                 cl.createArg().setValue("--no-sign");
151                 break;
152             default:
153                 // no CLI argument needed when default option from git config should be used
154                 break;
155         }
156 
157         cl.createArg().setValue("-F");
158         cl.createArg().setValue(messageFile.getAbsolutePath());
159 
160         // Note: this currently assumes you have the tag base checked out too
161         cl.createArg().setValue(tag);
162 
163         return cl;
164     }
165 
166     public Commandline createPushCommandLine(GitScmProviderRepository repository, ScmFileSet fileSet, String tag) {
167         Commandline cl = GitCommandLineUtils.getBaseGitCommandLine(
168                 fileSet.getBasedir(), "push", repository, environmentVariables);
169 
170         cl.createArg().setValue(repository.getPushUrl());
171         cl.createArg().setValue("refs/tags/" + tag);
172 
173         return cl;
174     }
175 }