001package org.apache.maven.scm.provider.svn.svnexe.command.tag; 002 003/* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022import java.io.File; 023import java.io.IOException; 024import java.util.ArrayList; 025import java.util.Iterator; 026import java.util.List; 027 028import org.apache.maven.scm.ScmException; 029import org.apache.maven.scm.ScmFile; 030import org.apache.maven.scm.ScmFileSet; 031import org.apache.maven.scm.ScmFileStatus; 032import org.apache.maven.scm.ScmResult; 033import org.apache.maven.scm.ScmTag; 034import org.apache.maven.scm.ScmTagParameters; 035import org.apache.maven.scm.command.tag.AbstractTagCommand; 036import org.apache.maven.scm.command.tag.TagScmResult; 037import org.apache.maven.scm.provider.ScmProviderRepository; 038import org.apache.maven.scm.provider.svn.SvnCommandUtils; 039import org.apache.maven.scm.provider.svn.SvnTagBranchUtils; 040import org.apache.maven.scm.provider.svn.command.SvnCommand; 041import org.apache.maven.scm.provider.svn.repository.SvnScmProviderRepository; 042import org.apache.maven.scm.provider.svn.svnexe.command.SvnCommandLineUtils; 043import org.codehaus.plexus.util.FileUtils; 044import org.codehaus.plexus.util.Os; 045import org.codehaus.plexus.util.StringUtils; 046import org.codehaus.plexus.util.cli.CommandLineException; 047import org.codehaus.plexus.util.cli.CommandLineUtils; 048import org.codehaus.plexus.util.cli.Commandline; 049 050/** 051 * @author <a href="mailto:brett@apache.org">Brett Porter</a> 052 * @author Olivier Lamy 053 * 054 * @todo since this is just a copy, use that instead. 055 */ 056public class SvnTagCommand 057 extends AbstractTagCommand 058 implements SvnCommand 059{ 060 061 public ScmResult executeTagCommand( ScmProviderRepository repo, ScmFileSet fileSet, String tag, String message ) 062 throws ScmException 063 { 064 ScmTagParameters scmTagParameters = new ScmTagParameters( message ); 065 // force false to preserve backward comp 066 scmTagParameters.setRemoteTagging( false ); 067 scmTagParameters.setPinExternals( false ); 068 return executeTagCommand( repo, fileSet, tag, scmTagParameters ); 069 } 070 071 /** {@inheritDoc} */ 072 public ScmResult executeTagCommand( ScmProviderRepository repo, ScmFileSet fileSet, String tag, 073 ScmTagParameters scmTagParameters ) 074 throws ScmException 075 { 076 // NPE free 077 if ( scmTagParameters == null ) 078 { 079 getLogger().debug( "SvnTagCommand :: scmTagParameters is null create an empty one" ); 080 scmTagParameters = new ScmTagParameters(); 081 scmTagParameters.setRemoteTagging( false ); 082 scmTagParameters.setPinExternals( false ); 083 } 084 else 085 { 086 getLogger().debug( 087 "SvnTagCommand :: scmTagParameters.remoteTagging : " 088 + scmTagParameters.isRemoteTagging() ); 089 } 090 if ( tag == null || StringUtils.isEmpty( tag.trim() ) ) 091 { 092 throw new ScmException( "tag must be specified" ); 093 } 094 095 if ( !fileSet.getFileList().isEmpty() ) 096 { 097 throw new ScmException( "This provider doesn't support tagging subsets of a directory" ); 098 } 099 100 SvnScmProviderRepository repository = (SvnScmProviderRepository) repo; 101 102 File messageFile = FileUtils.createTempFile( "maven-scm-", ".commit", null ); 103 104 try 105 { 106 FileUtils.fileWrite( messageFile.getAbsolutePath(), "UTF-8", scmTagParameters.getMessage() ); 107 } 108 catch ( IOException ex ) 109 { 110 return new TagScmResult( null, "Error while making a temporary file for the commit message: " 111 + ex.getMessage(), null, false ); 112 } 113 114 Commandline cl = createCommandLine( repository, fileSet.getBasedir(), tag, messageFile, scmTagParameters ); 115 116 CommandLineUtils.StringStreamConsumer stdout = new CommandLineUtils.StringStreamConsumer(); 117 118 CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer(); 119 120 if ( getLogger().isInfoEnabled() ) 121 { 122 getLogger().info( "Executing: " + SvnCommandLineUtils.cryptPassword( cl ) ); 123 124 if ( Os.isFamily( Os.FAMILY_WINDOWS ) ) 125 { 126 getLogger().info( "Working directory: " + cl.getWorkingDirectory().getAbsolutePath() ); 127 } 128 } 129 130 int exitCode; 131 132 try 133 { 134 exitCode = SvnCommandLineUtils.execute( cl, stdout, stderr, getLogger() ); 135 } 136 catch ( CommandLineException ex ) 137 { 138 throw new ScmException( "Error while executing command.", ex ); 139 } 140 finally 141 { 142 try 143 { 144 FileUtils.forceDelete( messageFile ); 145 } 146 catch ( IOException ex ) 147 { 148 // ignore 149 } 150 } 151 152 if ( exitCode != 0 ) 153 { 154 // TODO: Improve this error message 155 return new TagScmResult( cl.toString(), "The svn tag command failed.", stderr.getOutput(), false ); 156 } 157 158 List<ScmFile> fileList = new ArrayList<ScmFile>(); 159 160 List<File> files = null; 161 162 try 163 { 164 if ( StringUtils.isNotEmpty( fileSet.getExcludes() ) ) 165 { 166 List<File> list = 167 FileUtils.getFiles( fileSet.getBasedir(), 168 ( StringUtils.isEmpty( fileSet.getIncludes() ) ? "**" 169 : fileSet.getIncludes() ), fileSet.getExcludes() 170 + ",**/.svn/**", false ); 171 files = list; 172 } 173 else 174 { 175 List<File> list = 176 FileUtils.getFiles( fileSet.getBasedir(), 177 ( StringUtils.isEmpty( fileSet.getIncludes() ) ? "**" 178 : fileSet.getIncludes() ), "**/.svn/**", false ); 179 files = list; 180 } 181 } 182 catch ( IOException e ) 183 { 184 throw new ScmException( "Error while executing command.", e ); 185 } 186 187 for ( Iterator<File> i = files.iterator(); i.hasNext(); ) 188 { 189 File f = i.next(); 190 191 fileList.add( new ScmFile( f.getPath(), ScmFileStatus.TAGGED ) ); 192 } 193 194 return new TagScmResult( cl.toString(), fileList ); 195 } 196 197 // ---------------------------------------------------------------------- 198 // 199 // ---------------------------------------------------------------------- 200 201 /** 202 * @deprecated 203 * @param repository 204 * @param workingDirectory 205 * @param tag 206 * @param messageFile 207 * @return 208 */ 209 public static Commandline createCommandLine( SvnScmProviderRepository repository, File workingDirectory, String tag, 210 File messageFile ) 211 { 212 Commandline cl = SvnCommandLineUtils.getBaseSvnCommandLine( workingDirectory, repository ); 213 214 cl.createArg().setValue( "copy" ); 215 216 cl.createArg().setValue( "--parents" ); 217 218 cl.createArg().setValue( "--file" ); 219 220 cl.createArg().setValue( messageFile.getAbsolutePath() ); 221 222 cl.createArg().setValue( "." ); 223 224 // Note: this currently assumes you have the tag base checked out too 225 String tagUrl = SvnTagBranchUtils.resolveTagUrl( repository, new ScmTag( tag ) ); 226 tagUrl = SvnCommandUtils.fixUrl( tagUrl, repository.getUser() ); 227 cl.createArg().setValue( tagUrl + "@" ); 228 229 return cl; 230 } 231 232 233 public static Commandline createCommandLine( SvnScmProviderRepository repository, File workingDirectory, 234 String tag, File messageFile, ScmTagParameters scmTagParameters ) 235 { 236 Commandline cl = SvnCommandLineUtils.getBaseSvnCommandLine( workingDirectory, repository ); 237 238 cl.createArg().setValue( "copy" ); 239 240 cl.createArg().setValue( "--file" ); 241 242 cl.createArg().setValue( messageFile.getAbsolutePath() ); 243 244 cl.createArg().setValue( "--encoding" ); 245 246 cl.createArg().setValue( "UTF-8" ); 247 248 cl.createArg().setValue( "--parents" ); 249 250 if ( scmTagParameters != null && scmTagParameters.getScmRevision() != null ) 251 { 252 cl.createArg().setValue( "--revision" ); 253 254 cl.createArg().setValue( scmTagParameters.getScmRevision() ); 255 256 } 257 258 if ( scmTagParameters != null && scmTagParameters.isPinExternals() ) 259 { 260 cl.createArg().setValue( "--pin-externals" ); 261 } 262 263 if ( scmTagParameters != null && scmTagParameters.isRemoteTagging() ) 264 { 265 String url = SvnCommandUtils.fixUrl( repository.getUrl(), repository.getUser() ); 266 cl.createArg().setValue( url + "@" ); 267 } 268 else 269 { 270 cl.createArg().setValue( "." ); 271 } 272 273 // Note: this currently assumes you have the tag base checked out too 274 String tagUrl = SvnTagBranchUtils.resolveTagUrl( repository, new ScmTag( tag ) ); 275 tagUrl = SvnCommandUtils.fixUrl( tagUrl, repository.getUser() ); 276 cl.createArg().setValue( tagUrl + "@" ); 277 278 return cl; 279 } 280}