View Javadoc
1   package org.apache.maven.scm.provider.svn.svnexe.command.list;
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.ScmException;
23  import org.apache.maven.scm.ScmFileSet;
24  import org.apache.maven.scm.ScmRevision;
25  import org.apache.maven.scm.ScmVersion;
26  import org.apache.maven.scm.command.list.AbstractListCommand;
27  import org.apache.maven.scm.command.list.ListScmResult;
28  import org.apache.maven.scm.provider.ScmProviderRepository;
29  import org.apache.maven.scm.provider.svn.command.SvnCommand;
30  import org.apache.maven.scm.provider.svn.repository.SvnScmProviderRepository;
31  import org.apache.maven.scm.provider.svn.svnexe.command.SvnCommandLineUtils;
32  import org.codehaus.plexus.util.StringUtils;
33  import org.codehaus.plexus.util.cli.CommandLineException;
34  import org.codehaus.plexus.util.cli.CommandLineUtils;
35  import org.codehaus.plexus.util.cli.Commandline;
36  
37  import java.io.File;
38  import java.util.Iterator;
39  
40  /**
41   * Command to list files in SVN ( <code>svn list</code> command )
42   *
43   * @author <a href="mailto:carlos@apache.org">Carlos Sanchez</a>
44   *
45   */
46  public class SvnListCommand
47      extends AbstractListCommand
48      implements SvnCommand
49  {
50      private static final File TMP_DIR = new File( System.getProperty( "java.io.tmpdir" ) );
51  
52      /** {@inheritDoc} */
53      protected ListScmResult executeListCommand( ScmProviderRepository repository, ScmFileSet fileSet, boolean recursive,
54                                                  ScmVersion version )
55          throws ScmException
56      {
57          Commandline cl = createCommandLine( (SvnScmProviderRepository) repository, fileSet, recursive, version );
58  
59          SvnListConsumer consumer = new SvnListConsumer();
60  
61          CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
62  
63          if ( getLogger().isInfoEnabled() )
64          {
65              getLogger().info( "Executing: " + SvnCommandLineUtils.cryptPassword( cl ) );
66              getLogger().info( "Working directory: " + cl.getWorkingDirectory().getAbsolutePath() );
67          }
68  
69          int exitCode;
70  
71          try
72          {
73              exitCode = SvnCommandLineUtils.execute( cl, consumer, stderr, getLogger() );
74          }
75          catch ( CommandLineException ex )
76          {
77              throw new ScmException( "Error while executing command.", ex );
78          }
79  
80          if ( exitCode != 0 )
81          {
82              return new ListScmResult( cl.toString(), "The svn command failed.", stderr.getOutput(), false );
83          }
84  
85          return new ListScmResult( cl.toString(), consumer.getFiles() );
86      }
87  
88      static Commandline createCommandLine( SvnScmProviderRepository repository, ScmFileSet fileSet, boolean recursive,
89                                            ScmVersion version )
90      {
91          Commandline cl = SvnCommandLineUtils.getBaseSvnCommandLine( TMP_DIR, repository );
92  
93          cl.createArg().setValue( "list" );
94  
95          if ( recursive )
96          {
97              cl.createArg().setValue( "--recursive" );
98          }
99  
100         if ( version != null && StringUtils.isNotEmpty( version.getName() ) )
101         {
102             if ( version instanceof ScmRevision )
103             {
104                 cl.createArg().setValue( "-r" );
105 
106                 cl.createArg().setValue( version.getName() );
107             }
108         }
109 
110         Iterator<File> it = fileSet.getFileList().iterator();
111 
112         while ( it.hasNext() )
113         {
114             File file = it.next();
115 
116             cl.createArg().setValue( repository.getUrl() + "/" + file.getPath().replace( '\\', '/' ) );
117         }
118 
119         return cl;
120     }
121 
122 }