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