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.plugins.changelog.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.ScmTag;
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.SvnCommandUtils;
33  import org.apache.maven.scm.provider.svn.SvnTagBranchUtils;
34  import org.apache.maven.scm.provider.svn.command.SvnCommand;
35  import org.apache.maven.scm.provider.svn.repository.SvnScmProviderRepository;
36  import org.apache.maven.scm.provider.svn.svnexe.command.SvnCommandLineUtils;
37  import org.apache.maven.scm.provider.svn.svnexe.command.info.SvnInfoConsumer;
38  import org.codehaus.plexus.util.cli.CommandLineException;
39  import org.codehaus.plexus.util.cli.CommandLineUtils;
40  import org.codehaus.plexus.util.cli.Commandline;
41  
42  /**
43   * variation of SvnInfoCommand to work for branches. Taken from 1.7 release of maven-scm-providers
44   *
45   * @author <a href="mailto:kenney@apache.org">Kenney Westerhof</a>
46   */
47  public class SvnInfoCommandExpanded extends AbstractCommand implements SvnCommand {
48  
49      /** {@inheritDoc} */
50      @Override
51      protected ScmResult executeCommand(
52              final ScmProviderRepository repository, final ScmFileSet fileSet, final CommandParameters parameters)
53              throws ScmException {
54          return executeInfoCommand((SvnScmProviderRepository) repository, fileSet, parameters, false, null);
55      }
56  
57      public InfoScmResult executeInfoCommand(
58              final SvnScmProviderRepository repository,
59              final ScmFileSet fileSet,
60              final CommandParameters parameters,
61              final boolean recursive,
62              final String revision)
63              throws ScmException {
64          Commandline cl = createCommandLine(repository, fileSet, recursive, revision);
65          return executeInfoCommand(cl);
66      }
67  
68      public InfoScmResult executeInfoTagCommand(
69              final SvnScmProviderRepository repository,
70              final ScmFileSet fileSet,
71              final String tag,
72              final CommandParameters parameters,
73              final boolean recursive,
74              final String revision)
75              throws ScmException {
76          Commandline cl = createTagCommandLine(repository, fileSet, tag, recursive, revision);
77          return executeInfoCommand(cl);
78      }
79  
80      private InfoScmResult executeInfoCommand(final Commandline cl) throws ScmException {
81  
82          SvnInfoConsumer consumer = new SvnInfoConsumer();
83  
84          CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
85          if (logger.isInfoEnabled()) {
86              logger.info("Executing: " + SvnCommandLineUtils.cryptPassword(cl));
87              logger.info("Working directory: " + cl.getWorkingDirectory().getAbsolutePath());
88          }
89  
90          int exitCode;
91          try {
92              exitCode = SvnCommandLineUtils.execute(cl, consumer, stderr);
93          } catch (CommandLineException ex) {
94              throw new ScmException("Error while executing command.", ex);
95          }
96  
97          if (exitCode != 0) {
98              return new InfoScmResult(cl.toString(), "The svn command failed.", stderr.getOutput(), false);
99          }
100 
101         return new InfoScmResult(cl.toString(), consumer.getInfoItems());
102     }
103 
104     // set scope to protected to allow test to call it directly
105     protected static Commandline createCommandLine(
106             final SvnScmProviderRepository repository,
107             final ScmFileSet fileSet,
108             final boolean recursive,
109             final String revision) {
110         Commandline cl = SvnCommandLineUtils.getBaseSvnCommandLine(fileSet.getBasedir(), repository);
111 
112         cl.createArg().setValue("info");
113 
114         if (recursive) {
115             cl.createArg().setValue("--recursive");
116         }
117 
118         if (revision != null && !revision.isEmpty()) {
119             cl.createArg().setValue("-r");
120             cl.createArg().setValue(revision);
121         }
122 
123         for (File file : fileSet.getFileList()) {
124             if (repository == null) {
125                 cl.createArg().setValue(file.getPath());
126             } else {
127                 cl.createArg()
128                         .setValue(repository.getUrl() + "/" + file.getPath().replace('\\', '/'));
129             }
130         }
131 
132         return cl;
133     }
134 
135     // set scope to protected to allow test to call it directly
136     protected static Commandline createTagCommandLine(
137             final SvnScmProviderRepository repository,
138             final ScmFileSet fileSet,
139             final String tag,
140             final boolean recursive,
141             final String revision) {
142         Commandline cl = SvnCommandLineUtils.getBaseSvnCommandLine(fileSet.getBasedir(), repository);
143 
144         cl.createArg().setValue("info");
145 
146         if (recursive) {
147             cl.createArg().setValue("--recursive");
148         }
149 
150         if (revision != null && !revision.isEmpty()) {
151             cl.createArg().setValue("-r");
152             cl.createArg().setValue(revision);
153         }
154 
155         Iterator<File> it = fileSet.getFileList().iterator();
156 
157         if (!it.hasNext()) {
158             String tagUrl = SvnTagBranchUtils.resolveTagUrl(repository, new ScmTag(tag));
159             cl.createArg().setValue(SvnCommandUtils.fixUrl(tagUrl, repository.getUser()));
160         } else {
161             while (it.hasNext()) {
162                 File file = it.next();
163 
164                 if (repository == null) {
165                     cl.createArg().setValue(file.getPath());
166                 } else {
167                     // Note: this currently assumes you have the tag base checked out too
168                     String tagUrl = SvnTagBranchUtils.resolveTagUrl(repository, new ScmTag(tag)) + "/"
169                             + file.getPath().replace('\\', '/');
170                     cl.createArg().setValue(SvnCommandUtils.fixUrl(tagUrl, repository.getUser()));
171                 }
172             }
173         }
174 
175         return cl;
176     }
177 }