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