View Javadoc

1   /*
2   The MIT License
3   
4   Copyright (c) 2004, The Codehaus
5   
6   Permission is hereby granted, free of charge, to any person obtaining a copy of
7   this software and associated documentation files (the "Software"), to deal in
8   the Software without restriction, including without limitation the rights to
9   use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
10  of the Software, and to permit persons to whom the Software is furnished to do
11  so, subject to the following conditions:
12  
13  The above copyright notice and this permission notice shall be included in all
14  copies or substantial portions of the Software.
15  
16  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  SOFTWARE.
23   */
24  package org.codehaus.mojo.scmchangelog.scm.svn;
25  
26  import java.io.File;
27  import java.util.Iterator;
28  
29  import java.util.regex.Pattern;
30  import org.apache.maven.scm.ScmBranch;
31  import org.apache.maven.scm.ScmException;
32  import org.apache.maven.scm.ScmFileSet;
33  import org.apache.maven.scm.ScmRevision;
34  import org.apache.maven.scm.ScmTag;
35  import org.apache.maven.scm.ScmVersion;
36  import org.apache.maven.scm.command.list.AbstractListCommand;
37  import org.apache.maven.scm.command.list.ListScmResult;
38  import org.apache.maven.scm.provider.ScmProviderRepository;
39  import org.apache.maven.scm.provider.svn.command.SvnCommand;
40  import org.apache.maven.scm.provider.svn.repository.SvnScmProviderRepository;
41  import org.apache.maven.scm.provider.svn.svnexe.command.SvnCommandLineUtils;
42  import org.codehaus.plexus.util.StringUtils;
43  import org.codehaus.plexus.util.cli.CommandLineException;
44  import org.codehaus.plexus.util.cli.CommandLineUtils;
45  import org.codehaus.plexus.util.cli.Commandline;
46  
47  /**
48   * Command to list files in SVN ( <code>svn list --xml</code> command.
49   * @author ehsavoie
50   * @version $Id: SvnListCommand.java 8988 2009-02-05 08:48:09Z ehsavoie $
51   */
52  public class SvnListCommand
53      extends AbstractListCommand
54      implements SvnCommand
55  {
56    /**
57     * Temporary dir, where the command is executed.
58     */
59    private static final File TMP_DIR = new File( System.getProperty( "java.io.tmpdir" ) );
60  
61   /**
62     * The filter on the tag names to be used.
63     */
64    private Pattern filter;
65  
66    /**
67     * Creates a new instance of SvnListCommand.
68     * @param filter the filter on the tag names to be used.
69     */
70    public SvnListCommand( Pattern filter )
71    {
72      this.filter = filter;
73    }
74  
75    /**
76     * Executes a <code>svn list --xml repository_url</code> command.
77     * @param repository the Subversion repository.
78     * @param fileSet the list of files.
79     * @param recursive true if we want a recursive list command - false otherwise.
80     * @param version the version target (branch, tags, trunk).
81     * @return a ListScmResult containing a List&gt;Tag&lt;.
82     * @throws org.apache.maven.scm.ScmException in case of an error with the SCM.
83     */
84    protected ListScmResult executeListCommand( ScmProviderRepository repository, ScmFileSet fileSet,
85        boolean recursive, ScmVersion version )
86        throws ScmException
87    {
88      getLogger().info( "Executing our command " + version );
89      Commandline cl = createCommandLine( ( SvnScmProviderRepository ) repository, fileSet, recursive, version );
90      SvnListConsumer consumer = new SvnListConsumer( this.filter );
91      consumer.setLogger( getLogger() );
92  
93      CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
94      getLogger().info( "Executing: " + SvnCommandLineUtils.cryptPassword( cl ) );
95      getLogger().info( "Working directory: "  + cl.getWorkingDirectory().getAbsolutePath() );
96      int exitCode;
97      getLogger().info( "Executing this command " + cl.toString() );
98      try
99      {
100       exitCode = SvnCommandLineUtils.execute( cl, consumer, stderr, getLogger() );
101     }
102     catch ( CommandLineException ex )
103     {
104       getLogger().error( "Error while executing command.", ex );
105       throw new ScmException( "Error while executing command.", ex );
106     }
107 
108     if ( exitCode != 0 )
109     {
110       return new ListScmResult( cl.toString(), "The svn command failed.",
111           stderr.getOutput(), false );
112     }
113 
114     return new ListScmResult( cl.toString(), consumer.analyse() );
115   }
116 
117   /**
118    * Create the command line for svn list.
119    * @param repository the URL to the repository.
120    * @param fileSet the fileset.
121    * @param recursive true if we want the --recursive option - false otherwise.
122    * @param version the beginning and end revisions.
123    * @return the command line to be executed against the SCM.
124    */
125   static Commandline createCommandLine( SvnScmProviderRepository repository,
126       ScmFileSet fileSet, boolean recursive, ScmVersion version )
127   {
128     Commandline cl = SvnCommandLineUtils.getBaseSvnCommandLine( TMP_DIR,
129         repository );
130     cl.createArgument().setValue( "list" );
131 
132     if ( recursive )
133     {
134       cl.createArgument().setValue( "--recursive" );
135     }
136 
137     cl.createArgument().setValue( "--xml" );
138 
139     if ( ( version != null ) 
140         && StringUtils.isNotEmpty( version.getName() ) 
141         && version instanceof ScmRevision )
142     {
143       cl.createArgument().setValue( "-r" );
144       cl.createArgument().setValue( version.getName() );
145     }
146 
147     String baseUrl;
148 
149     if ( version instanceof ScmTag )
150     {
151       baseUrl = repository.getTagBase();
152     } 
153     else if ( version instanceof ScmBranch )
154     {
155       baseUrl = repository.getBranchBase();
156     }
157     else
158     {
159       baseUrl = repository.getUrl();
160     }
161 
162     Iterator it = fileSet.getFileList().iterator();
163 
164     while ( it.hasNext() )
165     {
166       File file = ( File ) it.next();
167       cl.createArgument().setValue( baseUrl + "/" + file.getPath().replace( '\\', '/' ) );
168     }
169 
170     return cl;
171   }
172 }