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.update;
20  
21  import java.io.File;
22  
23  import org.apache.commons.lang3.StringUtils;
24  import org.apache.maven.scm.ScmBranch;
25  import org.apache.maven.scm.ScmException;
26  import org.apache.maven.scm.ScmFileSet;
27  import org.apache.maven.scm.ScmTag;
28  import org.apache.maven.scm.ScmVersion;
29  import org.apache.maven.scm.command.changelog.ChangeLogCommand;
30  import org.apache.maven.scm.command.update.AbstractUpdateCommand;
31  import org.apache.maven.scm.command.update.UpdateScmResult;
32  import org.apache.maven.scm.command.update.UpdateScmResultWithRevision;
33  import org.apache.maven.scm.provider.ScmProviderRepository;
34  import org.apache.maven.scm.provider.svn.SvnTagBranchUtils;
35  import org.apache.maven.scm.provider.svn.command.SvnCommand;
36  import org.apache.maven.scm.provider.svn.repository.SvnScmProviderRepository;
37  import org.apache.maven.scm.provider.svn.svnexe.command.SvnCommandLineUtils;
38  import org.apache.maven.scm.provider.svn.svnexe.command.changelog.SvnChangeLogCommand;
39  import org.apache.maven.scm.provider.svn.util.SvnUtil;
40  import org.apache.maven.scm.providers.svn.settings.Settings;
41  import org.codehaus.plexus.util.Os;
42  import org.codehaus.plexus.util.cli.CommandLineException;
43  import org.codehaus.plexus.util.cli.CommandLineUtils;
44  import org.codehaus.plexus.util.cli.Commandline;
45  
46  /**
47   * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
48   */
49  public class SvnUpdateCommand extends AbstractUpdateCommand implements SvnCommand {
50      private final boolean interactive;
51  
52      public SvnUpdateCommand(boolean interactive) {
53          this.interactive = interactive;
54      }
55  
56      /**
57       * {@inheritDoc}
58       */
59      protected UpdateScmResult executeUpdateCommand(ScmProviderRepository repo, ScmFileSet fileSet, ScmVersion version)
60              throws ScmException {
61          Commandline cl = createCommandLine((SvnScmProviderRepository) repo, fileSet.getBasedir(), version);
62  
63          SvnUpdateConsumer consumer = new SvnUpdateConsumer(fileSet.getBasedir());
64  
65          CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
66  
67          if (logger.isInfoEnabled()) {
68              logger.info("Executing: " + SvnCommandLineUtils.cryptPassword(cl));
69  
70              if (Os.isFamily(Os.FAMILY_WINDOWS)) {
71                  logger.info("Working directory: " + cl.getWorkingDirectory().getAbsolutePath());
72              }
73          }
74  
75          int exitCode;
76  
77          try {
78              exitCode = SvnCommandLineUtils.execute(cl, consumer, stderr);
79          } catch (CommandLineException ex) {
80              throw new ScmException("Error while executing command.", ex);
81          }
82  
83          if (exitCode != 0) {
84              return new UpdateScmResult(cl.toString(), "The svn command failed.", stderr.getOutput(), false);
85          }
86  
87          UpdateScmResultWithRevision result = new UpdateScmResultWithRevision(
88                  cl.toString(), consumer.getUpdatedFiles(), String.valueOf(consumer.getRevision()));
89  
90          result.setChanges(consumer.getChangeSets());
91  
92          if (logger.isDebugEnabled()) {
93              logger.debug("changeSets " + consumer.getChangeSets());
94          }
95  
96          return result;
97      }
98  
99      // ----------------------------------------------------------------------
100     //
101     // ----------------------------------------------------------------------
102     public Commandline createCommandLine(
103             SvnScmProviderRepository repository, File workingDirectory, ScmVersion version) {
104         return createCommandLine(repository, workingDirectory, version, true);
105     }
106 
107     public Commandline createCommandLine(
108             SvnScmProviderRepository repository, File workingDirectory, ScmVersion version, boolean interactive) {
109         Settings settings = SvnUtil.getSettings();
110 
111         String workingDir = workingDirectory.getAbsolutePath();
112 
113         if (settings.isUseCygwinPath()) {
114             workingDir = settings.getCygwinMountPath() + "/" + workingDir;
115             workingDir = StringUtils.replace(workingDir, ":", "");
116             workingDir = StringUtils.replace(workingDir, "\\", "/");
117         }
118 
119         if (version != null && StringUtils.isEmpty(version.getName())) {
120             version = null;
121         }
122 
123         Commandline cl = SvnCommandLineUtils.getBaseSvnCommandLine(workingDirectory, repository, interactive);
124 
125         if (version == null || SvnTagBranchUtils.isRevisionSpecifier(version)) {
126             cl.createArg().setValue("update");
127 
128             if (version != null && StringUtils.isNotEmpty(version.getName())) {
129                 cl.createArg().setValue("-r");
130                 cl.createArg().setValue(version.getName());
131             }
132 
133             cl.createArg().setValue(workingDir + "@");
134         } else {
135             if (version instanceof ScmBranch) {
136                 // The tag specified does not appear to be numeric, so assume it refers
137                 // to a branch/tag url and perform a switch operation rather than update
138                 cl.createArg().setValue("switch");
139                 if (version instanceof ScmTag) {
140                     String tagUrl = SvnTagBranchUtils.resolveTagUrl(repository, (ScmTag) version);
141                     cl.createArg().setValue(tagUrl + "@");
142                 } else {
143                     String branchUrl = SvnTagBranchUtils.resolveBranchUrl(repository, (ScmBranch) version);
144                     cl.createArg().setValue(branchUrl + "@");
145                 }
146                 cl.createArg().setValue(workingDir + "@");
147             }
148         }
149 
150         return cl;
151     }
152 
153     /**
154      * {@inheritDoc}
155      */
156     protected ChangeLogCommand getChangeLogCommand() {
157         return new SvnChangeLogCommand(interactive);
158     }
159 }