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