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.svn.svnexe.command.untag;
20  
21  import java.io.File;
22  import java.io.IOException;
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.ScmTag;
28  import org.apache.maven.scm.ScmUntagParameters;
29  import org.apache.maven.scm.command.untag.AbstractUntagCommand;
30  import org.apache.maven.scm.command.untag.UntagScmResult;
31  import org.apache.maven.scm.provider.ScmProviderRepository;
32  import org.apache.maven.scm.provider.svn.SvnCommandUtils;
33  import org.apache.maven.scm.provider.svn.SvnTagBranchUtils;
34  import org.apache.maven.scm.provider.svn.command.SvnCommand;
35  import org.apache.maven.scm.provider.svn.repository.SvnScmProviderRepository;
36  import org.apache.maven.scm.provider.svn.svnexe.command.SvnCommandLineUtils;
37  import org.codehaus.plexus.util.FileUtils;
38  import org.codehaus.plexus.util.Os;
39  import org.codehaus.plexus.util.cli.CommandLineException;
40  import org.codehaus.plexus.util.cli.CommandLineUtils;
41  import org.codehaus.plexus.util.cli.Commandline;
42  
43  /**
44   * Scm:untag for provider svn is done by removing the tag dir.
45   *
46   * @since 1.11.2
47   */
48  public class SvnUntagCommand extends AbstractUntagCommand implements SvnCommand {
49  
50      private final boolean interactive;
51  
52      public SvnUntagCommand(boolean interactive) {
53          this.interactive = interactive;
54      }
55  
56      /**
57       * {@inheritDoc}
58       */
59      @Override
60      public ScmResult executeUntagCommand(
61              ScmProviderRepository repo, ScmFileSet fileSet, ScmUntagParameters scmUntagParameters) throws ScmException {
62          String tag = scmUntagParameters.getTag();
63          if (tag == null || tag.trim().isEmpty()) {
64              throw new ScmException("tag must be specified");
65          }
66  
67          SvnScmProviderRepository repository = (SvnScmProviderRepository) repo;
68  
69          File messageFile = FileUtils.createTempFile("maven-scm-", ".commit", null);
70  
71          String message = scmUntagParameters.getMessage();
72          try {
73              FileUtils.fileWrite(messageFile.getAbsolutePath(), "UTF-8", message);
74          } catch (IOException ex) {
75              return new UntagScmResult(
76                      null,
77                      "Error while making a temporary file for the commit message: " + ex.getMessage(),
78                      null,
79                      false);
80          }
81  
82          Commandline cl = createCommandline(repository, fileSet, tag, messageFile);
83  
84          CommandLineUtils.StringStreamConsumer stdout = new CommandLineUtils.StringStreamConsumer();
85  
86          CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
87  
88          if (logger.isDebugEnabled()) {
89              logger.debug("Executing: " + SvnCommandLineUtils.cryptPassword(cl));
90  
91              if (Os.isFamily(Os.FAMILY_WINDOWS)) {
92                  logger.debug("Working directory: " + cl.getWorkingDirectory().getAbsolutePath());
93              }
94          }
95  
96          int exitCode;
97  
98          try {
99              exitCode = SvnCommandLineUtils.execute(cl, stdout, stderr);
100         } catch (CommandLineException ex) {
101             throw new ScmException("Error while executing svn remove command.", ex);
102         } finally {
103             try {
104                 FileUtils.forceDelete(messageFile);
105             } catch (IOException ex) {
106                 // ignore
107             }
108         }
109 
110         if (exitCode == 0) {
111             return new UntagScmResult(
112                     cl.toString(), "The svn remove command was successful.", stderr.getOutput(), true);
113         } else {
114             return new UntagScmResult(cl.toString(), "The svn remove command failed.", stderr.getOutput(), false);
115         }
116     }
117 
118     /**
119      * Create command line from parameters.
120      *
121      * @param repo        svn repo tu delete tag from
122      * @param fileSet     file set containing base dir
123      * @param tag         tag to delete
124      * @param messageFile file containing commit message
125      * @return  command line instance
126      */
127     Commandline createCommandline(SvnScmProviderRepository repo, ScmFileSet fileSet, String tag, File messageFile) {
128         Commandline cl = SvnCommandLineUtils.getBaseSvnCommandLine(fileSet.getBasedir(), repo, interactive);
129 
130         cl.createArg().setValue("--file");
131 
132         cl.createArg().setValue(messageFile.getAbsolutePath());
133 
134         cl.createArg().setValue("remove");
135 
136         String tagUrl = SvnTagBranchUtils.resolveTagUrl(repo, new ScmTag(tag));
137         tagUrl = SvnCommandUtils.fixUrl(tagUrl, repo.getUser());
138         cl.createArg().setValue(tagUrl + "@");
139 
140         return cl;
141     }
142 }