View Javadoc
1   package org.apache.maven.scm.provider.svn.svnexe.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  import java.io.IOException;
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  
51      /** {@inheritDoc} */
52      @Override
53      public ScmResult executeUntagCommand( ScmProviderRepository repo, ScmFileSet fileSet,
54              ScmUntagParameters scmUntagParameters ) throws ScmException
55      {
56          String tag = scmUntagParameters.getTag();
57          if ( tag == null || tag.trim().isEmpty() )
58          {
59              throw new ScmException( "tag must be specified" );
60          }
61  
62          SvnScmProviderRepository repository = (SvnScmProviderRepository) repo;
63  
64          File messageFile = FileUtils.createTempFile( "maven-scm-", ".commit", null );
65  
66          String message = scmUntagParameters.getMessage();
67          try
68          {
69              FileUtils.fileWrite( messageFile.getAbsolutePath(), "UTF-8", message );
70          }
71          catch ( IOException ex )
72          {
73              return new UntagScmResult( null, "Error while making a temporary file for the commit message: "
74                  + ex.getMessage(), null, false );
75          }
76  
77          Commandline cl = createCommandline( repository, fileSet, tag, messageFile );
78  
79          CommandLineUtils.StringStreamConsumer stdout = new CommandLineUtils.StringStreamConsumer();
80  
81          CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
82  
83          if ( getLogger().isInfoEnabled() )
84          {
85              getLogger().info( "Executing: " + SvnCommandLineUtils.cryptPassword( cl ) );
86  
87              if ( Os.isFamily( Os.FAMILY_WINDOWS ) )
88              {
89                  getLogger().info( "Working directory: " + cl.getWorkingDirectory().getAbsolutePath() );
90              }
91          }
92  
93          int exitCode;
94  
95          try
96          {
97              exitCode = SvnCommandLineUtils.execute( cl, stdout, stderr, getLogger() );
98          }
99          catch ( CommandLineException ex )
100         {
101             throw new ScmException( "Error while executing svn remove command.", ex );
102         }
103         finally
104         {
105             try
106             {
107                 FileUtils.forceDelete( messageFile );
108             }
109             catch ( IOException ex )
110             {
111                 // ignore
112             }
113         }
114 
115         if ( exitCode == 0 )
116         {
117             return new UntagScmResult( cl.toString(), "The svn remove command was successful.",
118                     stderr.getOutput(), true );
119         }
120         else
121         {
122             return new UntagScmResult( cl.toString(), "The svn remove command failed.", stderr.getOutput(), false );
123         }
124 
125     }
126 
127     /**
128      * create command line from parameters
129      *
130      * @param repo        svn repo tu delete tag from
131      * @param fileSet     file set containing base dir
132      * @param tag         tag to delete
133      * @param messageFile file containing commit message
134      * @return            command line instance
135      */
136     Commandline createCommandline( SvnScmProviderRepository repo, ScmFileSet fileSet, String tag, File messageFile )
137     {
138         Commandline cl = SvnCommandLineUtils.getBaseSvnCommandLine( fileSet.getBasedir(), repo );
139 
140         cl.createArg().setValue( "--file" );
141 
142         cl.createArg().setValue( messageFile.getAbsolutePath() );
143 
144         cl.createArg().setValue( "remove" );
145 
146         String tagUrl = SvnTagBranchUtils.resolveTagUrl( repo, new ScmTag( tag ) );
147         tagUrl = SvnCommandUtils.fixUrl( tagUrl, repo.getUser() );
148         cl.createArg().setValue( tagUrl + "@" );
149 
150         return cl;
151     }
152 
153 }