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