001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019package org.apache.maven.scm.provider.svn.svnexe.command.branch; 020 021import java.io.File; 022import java.io.IOException; 023import java.util.ArrayList; 024import java.util.List; 025 026import org.apache.commons.lang3.StringUtils; 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.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 extends AbstractBranchCommand implements SvnCommand { 055 056 public ScmResult executeBranchCommand( 057 ScmProviderRepository repo, ScmFileSet fileSet, String branch, ScmBranchParameters scmBranchParameters) 058 throws ScmException { 059 if (branch == null || branch.trim().isEmpty()) { 060 throw new ScmException("branch name must be specified"); 061 } 062 063 if (!fileSet.getFileList().isEmpty()) { 064 throw new ScmException("This provider doesn't support branching subsets of a directory"); 065 } 066 067 SvnScmProviderRepository repository = (SvnScmProviderRepository) repo; 068 069 File messageFile = FileUtils.createTempFile("maven-scm-", ".commit", null); 070 071 try { 072 FileUtils.fileWrite(messageFile.getAbsolutePath(), "UTF-8", scmBranchParameters.getMessage()); 073 } catch (IOException ex) { 074 return new BranchScmResult( 075 null, 076 "Error while making a temporary file for the commit message: " + ex.getMessage(), 077 null, 078 false); 079 } 080 081 Commandline cl = createCommandLine(repository, fileSet.getBasedir(), branch, messageFile, scmBranchParameters); 082 083 CommandLineUtils.StringStreamConsumer stdout = new CommandLineUtils.StringStreamConsumer(); 084 085 CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer(); 086 087 if (logger.isInfoEnabled()) { 088 logger.info("Executing: " + SvnCommandLineUtils.cryptPassword(cl)); 089 090 if (Os.isFamily(Os.FAMILY_WINDOWS)) { 091 logger.info("Working directory: " + cl.getWorkingDirectory().getAbsolutePath()); 092 } 093 } 094 095 int exitCode; 096 097 try { 098 exitCode = SvnCommandLineUtils.execute(cl, stdout, stderr); 099 } catch (CommandLineException ex) { 100 throw new ScmException("Error while executing command.", ex); 101 } finally { 102 try { 103 FileUtils.forceDelete(messageFile); 104 } catch (IOException ex) { 105 // ignore 106 } 107 } 108 109 if (exitCode != 0) { 110 return new BranchScmResult(cl.toString(), "The svn branch command failed.", stderr.getOutput(), false); 111 } 112 113 List<ScmFile> fileList = new ArrayList<>(); 114 115 List<File> files = null; 116 117 try { 118 files = FileUtils.getFiles(fileSet.getBasedir(), "**", "**/.svn/**", false); 119 } catch (IOException e) { 120 throw new ScmException("Error while executing command.", e); 121 } 122 123 for (File f : files) { 124 fileList.add(new ScmFile(f.getPath(), ScmFileStatus.TAGGED)); 125 } 126 127 return new BranchScmResult(cl.toString(), fileList); 128 } 129 130 /** {@inheritDoc} */ 131 public ScmResult executeBranchCommand(ScmProviderRepository repo, ScmFileSet fileSet, String branch, String message) 132 throws ScmException { 133 ScmBranchParameters scmBranchParameters = new ScmBranchParameters(message); 134 return executeBranchCommand(repo, fileSet, branch, scmBranchParameters); 135 } 136 137 // ---------------------------------------------------------------------- 138 // 139 // ---------------------------------------------------------------------- 140 141 public static Commandline createCommandLine( 142 SvnScmProviderRepository repository, File workingDirectory, String branch, File messageFile) { 143 ScmBranchParameters scmBranchParameters = new ScmBranchParameters(); 144 scmBranchParameters.setRemoteBranching(false); 145 scmBranchParameters.setPinExternals(false); 146 return createCommandLine(repository, workingDirectory, branch, messageFile, scmBranchParameters); 147 } 148 149 public static Commandline createCommandLine( 150 SvnScmProviderRepository repository, 151 File workingDirectory, 152 String branch, 153 File messageFile, 154 ScmBranchParameters scmBranchParameters) { 155 Commandline cl = SvnCommandLineUtils.getBaseSvnCommandLine(workingDirectory, repository); 156 157 cl.createArg().setValue("copy"); 158 159 cl.createArg().setValue("--parents"); 160 161 cl.createArg().setValue("--file"); 162 163 cl.createArg().setValue(messageFile.getAbsolutePath()); 164 165 cl.createArg().setValue("--encoding"); 166 167 cl.createArg().setValue("UTF-8"); 168 169 if (scmBranchParameters != null && scmBranchParameters.isPinExternals()) { 170 cl.createArg().setValue("--pin-externals"); 171 } 172 173 if (scmBranchParameters != null && scmBranchParameters.isRemoteBranching()) { 174 if (StringUtils.isNotBlank(scmBranchParameters.getScmRevision())) { 175 cl.createArg().setValue("--revision"); 176 cl.createArg().setValue(scmBranchParameters.getScmRevision()); 177 } 178 String url = SvnCommandUtils.fixUrl(repository.getUrl(), repository.getUser()); 179 cl.createArg().setValue(url + "@"); 180 } else { 181 cl.createArg().setValue("."); 182 } 183 // Note: this currently assumes you have the branch base checked out too 184 String branchUrl = SvnTagBranchUtils.resolveBranchUrl(repository, new ScmBranch(branch)); 185 branchUrl = SvnCommandUtils.fixUrl(branchUrl, repository.getUser()); 186 cl.createArg().setValue(branchUrl + "@"); 187 188 return cl; 189 } 190}