001package org.apache.maven.scm.provider.svn.svnexe.command.branch; 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.List; 026 027import org.apache.maven.scm.ScmBranch; 028import org.apache.maven.scm.ScmBranchParameters; 029import org.apache.maven.scm.ScmException; 030import org.apache.maven.scm.ScmFile; 031import org.apache.maven.scm.ScmFileSet; 032import org.apache.maven.scm.ScmFileStatus; 033import org.apache.maven.scm.ScmResult; 034import org.apache.maven.scm.command.branch.AbstractBranchCommand; 035import org.apache.maven.scm.command.branch.BranchScmResult; 036import org.apache.maven.scm.provider.ScmProviderRepository; 037import org.apache.maven.scm.provider.svn.SvnCommandUtils; 038import org.apache.maven.scm.provider.svn.SvnTagBranchUtils; 039import org.apache.maven.scm.provider.svn.command.SvnCommand; 040import org.apache.maven.scm.provider.svn.repository.SvnScmProviderRepository; 041import org.apache.maven.scm.provider.svn.svnexe.command.SvnCommandLineUtils; 042import org.codehaus.plexus.util.FileUtils; 043import org.codehaus.plexus.util.Os; 044import org.codehaus.plexus.util.StringUtils; 045import org.codehaus.plexus.util.cli.CommandLineException; 046import org.codehaus.plexus.util.cli.CommandLineUtils; 047import org.codehaus.plexus.util.cli.Commandline; 048 049/** 050 * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a> 051 * @author Olivier Lamy 052 * 053 * TODO since this is just a copy, use that instead. 054 */ 055public class SvnBranchCommand 056 extends AbstractBranchCommand 057 implements SvnCommand 058{ 059 060 public ScmResult executeBranchCommand( ScmProviderRepository repo, ScmFileSet fileSet, String branch, 061 ScmBranchParameters scmBranchParameters ) 062 throws ScmException 063 { 064 if ( branch == null || StringUtils.isEmpty( branch.trim() ) ) 065 { 066 throw new ScmException( "branch name must be specified" ); 067 } 068 069 if ( !fileSet.getFileList().isEmpty() ) 070 { 071 throw new ScmException( "This provider doesn't support branching subsets of a directory" ); 072 } 073 074 SvnScmProviderRepository repository = (SvnScmProviderRepository) repo; 075 076 File messageFile = FileUtils.createTempFile( "maven-scm-", ".commit", null ); 077 078 try 079 { 080 FileUtils.fileWrite( messageFile.getAbsolutePath(), "UTF-8", scmBranchParameters.getMessage() ); 081 } 082 catch ( IOException ex ) 083 { 084 return new BranchScmResult( null, "Error while making a temporary file for the commit message: " 085 + ex.getMessage(), null, false ); 086 } 087 088 Commandline cl = createCommandLine( repository, fileSet.getBasedir(), branch, messageFile, 089 scmBranchParameters ); 090 091 CommandLineUtils.StringStreamConsumer stdout = new CommandLineUtils.StringStreamConsumer(); 092 093 CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer(); 094 095 if ( getLogger().isInfoEnabled() ) 096 { 097 getLogger().info( "Executing: " + SvnCommandLineUtils.cryptPassword( cl ) ); 098 099 if ( Os.isFamily( Os.FAMILY_WINDOWS ) ) 100 { 101 getLogger().info( "Working directory: " + cl.getWorkingDirectory().getAbsolutePath() ); 102 } 103 } 104 105 int exitCode; 106 107 try 108 { 109 exitCode = SvnCommandLineUtils.execute( cl, stdout, stderr, getLogger() ); 110 } 111 catch ( CommandLineException ex ) 112 { 113 throw new ScmException( "Error while executing command.", ex ); 114 } 115 finally 116 { 117 try 118 { 119 FileUtils.forceDelete( messageFile ); 120 } 121 catch ( IOException ex ) 122 { 123 // ignore 124 } 125 } 126 127 if ( exitCode != 0 ) 128 { 129 return new BranchScmResult( cl.toString(), "The svn branch command failed.", stderr.getOutput(), false ); 130 } 131 132 List<ScmFile> fileList = new ArrayList<ScmFile>(); 133 134 List<File> files = null; 135 136 try 137 { 138 files = FileUtils.getFiles( fileSet.getBasedir(), "**", "**/.svn/**", false ); 139 } 140 catch ( IOException e ) 141 { 142 throw new ScmException( "Error while executing command.", e ); 143 } 144 145 for ( File f : files ) 146 { 147 fileList.add( new ScmFile( f.getPath(), ScmFileStatus.TAGGED ) ); 148 } 149 150 return new BranchScmResult( cl.toString(), fileList ); 151 } 152 153 /** {@inheritDoc} */ 154 public ScmResult executeBranchCommand( ScmProviderRepository repo, ScmFileSet fileSet, String branch, 155 String message ) 156 throws ScmException 157 { 158 ScmBranchParameters scmBranchParameters = new ScmBranchParameters( message ); 159 return executeBranchCommand( repo, fileSet, branch, scmBranchParameters ); 160 } 161 162 // ---------------------------------------------------------------------- 163 // 164 // ---------------------------------------------------------------------- 165 166 public static Commandline createCommandLine( SvnScmProviderRepository repository, File workingDirectory, 167 String branch, File messageFile ) 168 { 169 ScmBranchParameters scmBranchParameters = new ScmBranchParameters(); 170 scmBranchParameters.setRemoteBranching( false ); 171 scmBranchParameters.setPinExternals( false ); 172 return createCommandLine( repository, workingDirectory, branch, messageFile, scmBranchParameters ); 173 } 174 175 public static Commandline createCommandLine( SvnScmProviderRepository repository, File workingDirectory, 176 String branch, File messageFile, 177 ScmBranchParameters scmBranchParameters ) 178 { 179 Commandline cl = SvnCommandLineUtils.getBaseSvnCommandLine( workingDirectory, repository ); 180 181 cl.createArg().setValue( "copy" ); 182 183 cl.createArg().setValue( "--parents" ); 184 185 cl.createArg().setValue( "--file" ); 186 187 cl.createArg().setValue( messageFile.getAbsolutePath() ); 188 189 cl.createArg().setValue( "--encoding" ); 190 191 cl.createArg().setValue( "UTF-8" ); 192 193 if ( scmBranchParameters != null && scmBranchParameters.isPinExternals() ) 194 { 195 cl.createArg().setValue( "--pin-externals" ); 196 } 197 198 if ( scmBranchParameters != null && scmBranchParameters.isRemoteBranching() ) 199 { 200 if ( StringUtils.isNotBlank( scmBranchParameters.getScmRevision() ) ) 201 { 202 cl.createArg().setValue( "--revision" ); 203 cl.createArg().setValue( scmBranchParameters.getScmRevision() ); 204 } 205 String url = SvnCommandUtils.fixUrl( repository.getUrl(), repository.getUser() ); 206 cl.createArg().setValue( url + "@" ); 207 } 208 else 209 { 210 cl.createArg().setValue( "." ); 211 } 212 // Note: this currently assumes you have the branch base checked out too 213 String branchUrl = SvnTagBranchUtils.resolveBranchUrl( repository, new ScmBranch( branch ) ); 214 branchUrl = SvnCommandUtils.fixUrl( branchUrl, repository.getUser() ); 215 cl.createArg().setValue( branchUrl + "@" ); 216 217 return cl; 218 } 219}