View Javadoc

1   package org.apache.maven.scm.provider.svn.svnexe.command.info;
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.util.Iterator;
24  
25  import org.apache.maven.scm.CommandParameters;
26  import org.apache.maven.scm.ScmException;
27  import org.apache.maven.scm.ScmFileSet;
28  import org.apache.maven.scm.ScmResult;
29  import org.apache.maven.scm.command.AbstractCommand;
30  import org.apache.maven.scm.command.info.InfoScmResult;
31  import org.apache.maven.scm.provider.ScmProviderRepository;
32  import org.apache.maven.scm.provider.svn.command.SvnCommand;
33  import org.apache.maven.scm.provider.svn.repository.SvnScmProviderRepository;
34  import org.apache.maven.scm.provider.svn.svnexe.command.SvnCommandLineUtils;
35  import org.codehaus.plexus.util.StringUtils;
36  import org.codehaus.plexus.util.cli.CommandLineException;
37  import org.codehaus.plexus.util.cli.CommandLineUtils;
38  import org.codehaus.plexus.util.cli.Commandline;
39  
40  /**
41   * @author <a href="mailto:kenney@apache.org">Kenney Westerhof</a>
42   *
43   */
44  public class SvnInfoCommand
45      extends AbstractCommand
46      implements SvnCommand
47  {
48  
49      /** {@inheritDoc} */
50      protected ScmResult executeCommand( ScmProviderRepository repository, ScmFileSet fileSet,
51                                          CommandParameters parameters )
52          throws ScmException
53      {
54          return executeInfoCommand( (SvnScmProviderRepository) repository, fileSet, parameters, false, null );
55      }
56  
57      public InfoScmResult executeInfoCommand( SvnScmProviderRepository repository, ScmFileSet fileSet,
58                                                  CommandParameters parameters, boolean recursive, String revision )
59          throws ScmException
60      {
61          Commandline cl = createCommandLine( repository, fileSet, recursive, revision );
62  
63          SvnInfoConsumer consumer = new SvnInfoConsumer();
64  
65          CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
66  
67          if ( getLogger().isInfoEnabled() )
68          {
69              getLogger().info( "Executing: " + SvnCommandLineUtils.cryptPassword( cl ) );
70              getLogger().info( "Working directory: " + cl.getWorkingDirectory().getAbsolutePath() );
71          }
72  
73          int exitCode;
74  
75          try
76          {
77              exitCode = SvnCommandLineUtils.execute( cl, consumer, stderr, getLogger() );
78          }
79          catch ( CommandLineException ex )
80          {
81              throw new ScmException( "Error while executing command.", ex );
82          }
83  
84          if ( exitCode != 0 )
85          {
86              return new InfoScmResult( cl.toString(), "The svn command failed.", stderr.getOutput(), false );
87          }
88  
89          return new InfoScmResult( cl.toString(), consumer.getInfoItems() );
90      }
91  
92      //set scope to protected to allow test to call it directly
93      protected static Commandline createCommandLine( SvnScmProviderRepository repository, ScmFileSet fileSet,
94                                                    boolean recursive, String revision )
95      {
96          Commandline cl = SvnCommandLineUtils.getBaseSvnCommandLine( fileSet.getBasedir(), repository );
97  
98          cl.createArg().setValue( "info" );
99  
100         if ( recursive )
101         {
102             cl.createArg().setValue( "--recursive" );
103         }
104 
105         if ( StringUtils.isNotEmpty( revision ) )
106         {
107             cl.createArg().setValue( "-r" );
108 
109             cl.createArg().setValue( revision );
110         }
111 
112         Iterator<File> it = fileSet.getFileList().iterator();
113 
114         while ( it.hasNext() )
115         {
116             File file = (File) it.next();
117 
118             if ( repository == null )
119             {
120                 cl.createArg().setValue( file.getPath() );
121             }
122             else
123             {
124                 cl.createArg().setValue( repository.getUrl() + "/" + file.getPath().replace( '\\', '/' ) );
125             }
126         }
127 
128         return cl;
129     }
130 
131 }