Coverage Report - org.apache.maven.scm.provider.vss.commands.edit.VssEditCommand
 
Classes in this File Line Coverage Branch Coverage Complexity
VssEditCommand
55 %
45/81
30 %
8/26
8,333
 
 1  
 package org.apache.maven.scm.provider.vss.commands.edit;
 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 java.util.ArrayList;
 25  
 import java.util.List;
 26  
 
 27  
 import org.apache.maven.scm.ScmException;
 28  
 import org.apache.maven.scm.ScmFile;
 29  
 import org.apache.maven.scm.ScmFileSet;
 30  
 import org.apache.maven.scm.ScmResult;
 31  
 import org.apache.maven.scm.command.changelog.ChangeLogCommand;
 32  
 import org.apache.maven.scm.command.edit.AbstractEditCommand;
 33  
 import org.apache.maven.scm.command.edit.EditScmResult;
 34  
 import org.apache.maven.scm.provider.ScmProviderRepository;
 35  
 import org.apache.maven.scm.provider.vss.commands.VssCommandLineUtils;
 36  
 import org.apache.maven.scm.provider.vss.commands.VssConstants;
 37  
 import org.apache.maven.scm.provider.vss.commands.changelog.VssHistoryCommand;
 38  
 import org.apache.maven.scm.provider.vss.repository.VssScmProviderRepository;
 39  
 import org.codehaus.plexus.util.cli.CommandLineUtils;
 40  
 import org.codehaus.plexus.util.cli.Commandline;
 41  
 
 42  
 /**
 43  
  * @author <a href="mailto:triek@thrx.de">Thorsten Riek</a>
 44  
  * @version $Id: VssCheckOutCommand.java 02.06.2006 00:05:51
 45  
  */
 46  4
 public class VssEditCommand
 47  
     extends AbstractEditCommand
 48  
 {
 49  
 
 50  
     /** {@inheritDoc} */
 51  
     protected ScmResult executeEditCommand( ScmProviderRepository repository, ScmFileSet fileSet )
 52  
         throws ScmException
 53  
     {
 54  0
         if ( getLogger().isDebugEnabled() )
 55  
         {
 56  0
             getLogger().debug( "executing checkout command..." );
 57  
         }
 58  
 
 59  0
         VssScmProviderRepository repo = (VssScmProviderRepository) repository;
 60  
 
 61  0
         List<Commandline> commandLines = buildCmdLine( repo, fileSet );
 62  
 
 63  0
         VssEditConsumer consumer = new VssEditConsumer( repo, getLogger() );
 64  
 
 65  
         //      TODO handle deleted files from VSS
 66  0
         CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
 67  
 
 68  
         int exitCode;
 69  
 
 70  0
         StringBuilder sb = new StringBuilder();
 71  0
         List<ScmFile> updatedFiles = new ArrayList<ScmFile>();
 72  
 
 73  0
         for ( Commandline cl : commandLines )
 74  
         {
 75  
 
 76  0
             if ( getLogger().isDebugEnabled() )
 77  
             {
 78  0
                 getLogger().debug( "Executing: " + cl.getWorkingDirectory().getAbsolutePath() + ">>" + cl.toString() );
 79  
             }
 80  
 
 81  0
             exitCode = VssCommandLineUtils.executeCommandline( cl, consumer, stderr, getLogger() );
 82  
 
 83  0
             if ( exitCode != 0 )
 84  
             {
 85  0
                 String error = stderr.getOutput();
 86  
 
 87  0
                 if ( getLogger().isDebugEnabled() )
 88  
                 {
 89  0
                     getLogger().debug( "VSS returns error: [" + error + "] return code: [" + exitCode + "]" );
 90  
                 }
 91  0
                 if ( error.indexOf( "A writable copy of" ) < 0 )
 92  
                 {
 93  0
                     return new EditScmResult( cl.toString(), "The vss command failed.", error, false );
 94  
                 }
 95  
                 // print out the writable copy for manual handling
 96  0
                 if ( getLogger().isWarnEnabled() )
 97  
                 {
 98  0
                     getLogger().warn( error );
 99  
                 }
 100  
                 break;
 101  
             }
 102  
 
 103  0
             sb.append( cl.toString() + '\n' );
 104  0
             updatedFiles.addAll( consumer.getUpdatedFiles() );
 105  
 
 106  
         }
 107  0
         return new EditScmResult( sb.toString(), updatedFiles );
 108  
 
 109  
     }
 110  
 
 111  
     public List<Commandline> buildCmdLine( VssScmProviderRepository repo, ScmFileSet fileSet )
 112  
         throws ScmException
 113  
     {
 114  4
         List<File> files = fileSet.getFileList();
 115  4
         List<Commandline> commands = new ArrayList<Commandline>();
 116  
 
 117  4
         if ( files.size() > 0 )
 118  
         {
 119  
 
 120  
             String base;
 121  
             try
 122  
             {
 123  3
                 base = fileSet.getBasedir().getCanonicalPath();
 124  
             }
 125  0
             catch ( IOException e )
 126  
             {
 127  0
                 throw new ScmException( "Invalid canonical path", e );
 128  3
             }
 129  
 
 130  3
             for ( File file : files )
 131  
             {
 132  
 
 133  4
                 Commandline command = new Commandline();
 134  
 
 135  
                 try
 136  
                 {
 137  4
                     command.addSystemEnvironment();
 138  
                 }
 139  0
                 catch ( Exception e )
 140  
                 {
 141  0
                     throw new ScmException( "Can't add system environment.", e );
 142  4
                 }
 143  
 
 144  4
                 command.addEnvironment( "SSDIR", repo.getVssdir() );
 145  
 
 146  4
                 String ssDir = VssCommandLineUtils.getSsDir();
 147  
 
 148  4
                 command.setExecutable( ssDir + VssConstants.SS_EXE );
 149  
 
 150  4
                 command.createArg().setValue( VssConstants.COMMAND_CHECKOUT );
 151  
 
 152  
                 String absolute;
 153  
                 try
 154  
                 {
 155  4
                     absolute = file.getCanonicalPath();
 156  
                     String relative;
 157  4
                     int index = absolute.indexOf( base );
 158  4
                     if ( index >= 0 )
 159  
                     {
 160  4
                         relative = absolute.substring( index + base.length() );
 161  
                     }
 162  
                     else
 163  
                     {
 164  0
                         relative = file.getPath();
 165  
                     }
 166  
 
 167  4
                     relative = relative.replace( '\\', '/' );
 168  
 
 169  4
                     if ( !relative.startsWith( "/" ) )
 170  
                     {
 171  0
                         relative = '/' + relative;
 172  
                     }
 173  
 
 174  4
                     String relativeFolder = relative.substring( 0, relative.lastIndexOf( '/' ) );
 175  
 
 176  4
                     command.setWorkingDirectory( new File( fileSet.getBasedir().getAbsolutePath() + File.separatorChar
 177  
                         + relativeFolder ).getCanonicalPath() );
 178  
 
 179  4
                     command.createArg().setValue( VssConstants.PROJECT_PREFIX + repo.getProject() + relative );
 180  
                 }
 181  0
                 catch ( IOException e )
 182  
                 {
 183  0
                     throw new ScmException( "Invalid canonical path", e );
 184  4
                 }
 185  
 
 186  
                 //User identification to get access to vss repository
 187  4
                 if ( repo.getUserPassword() != null )
 188  
                 {
 189  4
                     command.createArg().setValue( VssConstants.FLAG_LOGIN + repo.getUserPassword() );
 190  
                 }
 191  
 
 192  
                 //Ignore: Do not ask for input under any circumstances.
 193  4
                 command.createArg().setValue( VssConstants.FLAG_AUTORESPONSE_DEF );
 194  
 
 195  4
                 commands.add( command );
 196  
 
 197  4
             }
 198  
 
 199  3
         }
 200  
         else
 201  
         {
 202  1
             Commandline command = new Commandline();
 203  
 
 204  1
             command.setWorkingDirectory( fileSet.getBasedir().getAbsolutePath() );
 205  
 
 206  
             try
 207  
             {
 208  1
                 command.addSystemEnvironment();
 209  
             }
 210  0
             catch ( Exception e )
 211  
             {
 212  0
                 throw new ScmException( "Can't add system environment.", e );
 213  1
             }
 214  
 
 215  1
             command.addEnvironment( "SSDIR", repo.getVssdir() );
 216  
 
 217  1
             String ssDir = VssCommandLineUtils.getSsDir();
 218  
 
 219  1
             command.setExecutable( ssDir + VssConstants.SS_EXE );
 220  
 
 221  1
             command.createArg().setValue( VssConstants.COMMAND_CHECKOUT );
 222  
 
 223  1
             command.createArg().setValue( VssConstants.PROJECT_PREFIX + repo.getProject() );
 224  
             //Display the history of an entire project list
 225  1
             command.createArg().setValue( VssConstants.FLAG_RECURSION );
 226  
 
 227  
             //User identification to get access to vss repository
 228  1
             if ( repo.getUserPassword() != null )
 229  
             {
 230  1
                 command.createArg().setValue( VssConstants.FLAG_LOGIN + repo.getUserPassword() );
 231  
             }
 232  
 
 233  
             //Ignore: Do not ask for input under any circumstances.
 234  1
             command.createArg().setValue( VssConstants.FLAG_AUTORESPONSE_DEF );
 235  
 
 236  1
             commands.add( command );
 237  
 
 238  
         }
 239  
 
 240  4
         return commands;
 241  
     }
 242  
 
 243  
     /**
 244  
      * @return
 245  
      */
 246  
     protected ChangeLogCommand getChangeLogCommand()
 247  
     {
 248  0
         VssHistoryCommand command = new VssHistoryCommand();
 249  
 
 250  0
         command.setLogger( getLogger() );
 251  
 
 252  0
         return command;
 253  
     }
 254  
 
 255  
 }