View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.scm.provider.svn.svnexe.command.info;
20  
21  import java.io.File;
22  import java.util.Iterator;
23  
24  import org.apache.maven.scm.CommandParameters;
25  import org.apache.maven.scm.ScmException;
26  import org.apache.maven.scm.ScmFileSet;
27  import org.apache.maven.scm.ScmResult;
28  import org.apache.maven.scm.command.AbstractCommand;
29  import org.apache.maven.scm.command.info.InfoScmResult;
30  import org.apache.maven.scm.provider.ScmProviderRepository;
31  import org.apache.maven.scm.provider.svn.command.SvnCommand;
32  import org.apache.maven.scm.provider.svn.repository.SvnScmProviderRepository;
33  import org.apache.maven.scm.provider.svn.svnexe.command.SvnCommandLineUtils;
34  import org.codehaus.plexus.util.Os;
35  import org.codehaus.plexus.util.cli.CommandLineException;
36  import org.codehaus.plexus.util.cli.CommandLineUtils;
37  import org.codehaus.plexus.util.cli.Commandline;
38  
39  /**
40   * @author <a href="mailto:kenney@apache.org">Kenney Westerhof</a>
41   *
42   */
43  public class SvnInfoCommand extends AbstractCommand implements SvnCommand {
44  
45      /** {@inheritDoc} */
46      protected ScmResult executeCommand(
47              ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters parameters) throws ScmException {
48          return executeInfoCommand((SvnScmProviderRepository) repository, fileSet, parameters, false, null);
49      }
50  
51      public InfoScmResult executeInfoCommand(
52              SvnScmProviderRepository repository,
53              ScmFileSet fileSet,
54              CommandParameters parameters,
55              boolean recursive,
56              String revision)
57              throws ScmException {
58          Commandline cl = createCommandLine(repository, fileSet, recursive, revision);
59  
60          SvnInfoConsumer consumer = new SvnInfoConsumer();
61  
62          CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
63  
64          if (logger.isInfoEnabled()) {
65              logger.info("Executing: " + SvnCommandLineUtils.cryptPassword(cl));
66  
67              if (Os.isFamily(Os.FAMILY_WINDOWS)) {
68                  logger.info("Working directory: " + cl.getWorkingDirectory().getAbsolutePath());
69              }
70          }
71  
72          int exitCode;
73  
74          try {
75              exitCode = SvnCommandLineUtils.execute(cl, consumer, stderr);
76          } catch (CommandLineException ex) {
77              throw new ScmException("Error while executing command.", ex);
78          }
79  
80          if (exitCode != 0) {
81              return new InfoScmResult(cl.toString(), "The svn command failed.", stderr.getOutput(), false);
82          }
83  
84          return new InfoScmResult(cl.toString(), consumer.getInfoItems());
85      }
86  
87      // set scope to protected to allow test to call it directly
88      protected static Commandline createCommandLine(
89              SvnScmProviderRepository repository, ScmFileSet fileSet, boolean recursive, String revision) {
90          Commandline cl = SvnCommandLineUtils.getBaseSvnCommandLine(fileSet.getBasedir(), repository);
91  
92          cl.createArg().setValue("info");
93  
94          if (recursive) {
95              cl.createArg().setValue("--recursive");
96          }
97  
98          if (revision != null && !revision.isEmpty()) {
99              cl.createArg().setValue("-r");
100 
101             cl.createArg().setValue(revision);
102         }
103 
104         Iterator<File> it = fileSet.getFileList().iterator();
105 
106         while (it.hasNext()) {
107             File file = (File) it.next();
108 
109             if (repository == null) {
110                 cl.createArg().setValue(file.getPath());
111             } else {
112                 cl.createArg()
113                         .setValue(repository.getUrl() + "/" + file.getPath().replace('\\', '/') + "@");
114             }
115         }
116 
117         return cl;
118     }
119 }