View Javadoc

1   package org.apache.maven.scm.provider.svn.svnexe.command.checkout;
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 org.apache.maven.scm.ScmBranch;
23  import org.apache.maven.scm.ScmException;
24  import org.apache.maven.scm.ScmFileSet;
25  import org.apache.maven.scm.ScmRevision;
26  import org.apache.maven.scm.ScmTag;
27  import org.apache.maven.scm.ScmVersion;
28  import org.apache.maven.scm.command.checkout.AbstractCheckOutCommand;
29  import org.apache.maven.scm.command.checkout.CheckOutScmResult;
30  import org.apache.maven.scm.provider.ScmProviderRepository;
31  import org.apache.maven.scm.provider.svn.SvnCommandUtils;
32  import org.apache.maven.scm.provider.svn.SvnTagBranchUtils;
33  import org.apache.maven.scm.provider.svn.command.SvnCommand;
34  import org.apache.maven.scm.provider.svn.repository.SvnScmProviderRepository;
35  import org.apache.maven.scm.provider.svn.svnexe.command.SvnCommandLineUtils;
36  import org.codehaus.plexus.util.StringUtils;
37  import org.codehaus.plexus.util.cli.CommandLineException;
38  import org.codehaus.plexus.util.cli.CommandLineUtils;
39  import org.codehaus.plexus.util.cli.Commandline;
40  
41  import java.io.File;
42  
43  /**
44   * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
45   * @version $Id: SvnCheckOutCommand.java 524909 2007-04-02 20:02:44Z evenisse $
46   */
47  public class SvnCheckOutCommand
48      extends AbstractCheckOutCommand
49      implements SvnCommand
50  {
51      protected CheckOutScmResult executeCheckOutCommand( ScmProviderRepository repo, ScmFileSet fileSet,
52                                                          ScmVersion version )
53          throws ScmException
54      {
55          SvnScmProviderRepository repository = (SvnScmProviderRepository) repo;
56  
57          String url = repository.getUrl();
58  
59          if ( version != null && StringUtils.isNotEmpty( version.getName() ) )
60          {
61              if ( version instanceof ScmTag )
62              {
63                  url = SvnTagBranchUtils.resolveTagUrl( repository, (ScmTag) version );
64              }
65              else if ( version instanceof ScmBranch )
66              {
67                  url = SvnTagBranchUtils.resolveBranchUrl( repository, (ScmBranch) version );
68              }
69          }
70  
71          url = SvnCommandUtils.fixUrl( url, repository.getUser() );
72  
73          Commandline cl = createCommandLine( repository, fileSet.getBasedir(), version, url );
74  
75          SvnCheckOutConsumer consumer = new SvnCheckOutConsumer( getLogger(), fileSet.getBasedir().getParentFile() );
76  
77          CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
78  
79          int exitCode;
80  
81          getLogger().info( "Executing: " + SvnCommandLineUtils.cryptPassword( cl ) );
82          getLogger().info( "Working directory: " + cl.getWorkingDirectory().getAbsolutePath() );
83  
84          try
85          {
86              exitCode = SvnCommandLineUtils.execute( cl, consumer, stderr, getLogger() );
87          }
88          catch ( CommandLineException ex )
89          {
90              throw new ScmException( "Error while executing command.", ex );
91          }
92  
93          if ( exitCode != 0 )
94          {
95              return new CheckOutScmResult( cl.toString(), "The svn command failed.", stderr.getOutput(), false );
96          }
97  
98          return new CheckOutScmResult( cl.toString(), consumer.getCheckedOutFiles() );
99      }
100 
101     // ----------------------------------------------------------------------
102     //
103     // ----------------------------------------------------------------------
104 
105     public static Commandline createCommandLine( SvnScmProviderRepository repository, File workingDirectory,
106                                                  ScmVersion version, String url )
107     {
108         Commandline cl = SvnCommandLineUtils.getBaseSvnCommandLine( workingDirectory.getParentFile(), repository );
109 
110         cl.createArgument().setValue( "checkout" );
111 
112         if ( version != null && StringUtils.isNotEmpty( version.getName() ) )
113         {
114             if ( version instanceof ScmRevision )
115             {
116                 cl.createArgument().setValue( "-r" );
117 
118                 cl.createArgument().setValue( version.getName() );
119             }
120         }
121 
122         cl.createArgument().setValue( url );
123 
124         cl.createArgument().setValue( workingDirectory.getName() );
125 
126         return cl;
127     }
128 }