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   */
50  public class SvnUpdateCommand extends AbstractUpdateCommand implements SvnCommand {
51      /** {@inheritDoc} */
52      protected UpdateScmResult executeUpdateCommand(ScmProviderRepository repo, ScmFileSet fileSet, ScmVersion version)
53              throws ScmException {
54          Commandline cl = createCommandLine((SvnScmProviderRepository) repo, fileSet.getBasedir(), version);
55  
56          SvnUpdateConsumer consumer = new SvnUpdateConsumer(fileSet.getBasedir());
57  
58          CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
59  
60          if (logger.isInfoEnabled()) {
61              logger.info("Executing: " + SvnCommandLineUtils.cryptPassword(cl));
62  
63              if (Os.isFamily(Os.FAMILY_WINDOWS)) {
64                  logger.info("Working directory: " + cl.getWorkingDirectory().getAbsolutePath());
65              }
66          }
67  
68          int exitCode;
69  
70          try {
71              exitCode = SvnCommandLineUtils.execute(cl, consumer, stderr);
72          } catch (CommandLineException ex) {
73              throw new ScmException("Error while executing command.", ex);
74          }
75  
76          if (exitCode != 0) {
77              return new UpdateScmResult(cl.toString(), "The svn command failed.", stderr.getOutput(), false);
78          }
79  
80          UpdateScmResultWithRevision result = new UpdateScmResultWithRevision(
81                  cl.toString(), consumer.getUpdatedFiles(), String.valueOf(consumer.getRevision()));
82  
83          result.setChanges(consumer.getChangeSets());
84  
85          if (logger.isDebugEnabled()) {
86              logger.debug("changeSets " + consumer.getChangeSets());
87          }
88  
89          return result;
90      }
91  
92      // ----------------------------------------------------------------------
93      //
94      // ----------------------------------------------------------------------
95  
96      public static Commandline createCommandLine(
97              SvnScmProviderRepository repository, File workingDirectory, ScmVersion version) {
98          Settings settings = SvnUtil.getSettings();
99  
100         String workingDir = workingDirectory.getAbsolutePath();
101 
102         if (settings.isUseCygwinPath()) {
103             workingDir = settings.getCygwinMountPath() + "/" + workingDir;
104             workingDir = StringUtils.replace(workingDir, ":", "");
105             workingDir = StringUtils.replace(workingDir, "\\", "/");
106         }
107 
108         if (version != null && StringUtils.isEmpty(version.getName())) {
109             version = null;
110         }
111 
112         Commandline cl = SvnCommandLineUtils.getBaseSvnCommandLine(workingDirectory, repository);
113 
114         if (version == null || SvnTagBranchUtils.isRevisionSpecifier(version)) {
115             cl.createArg().setValue("update");
116 
117             if (version != null && StringUtils.isNotEmpty(version.getName())) {
118                 cl.createArg().setValue("-r");
119                 cl.createArg().setValue(version.getName());
120             }
121 
122             cl.createArg().setValue(workingDir + "@");
123         } else {
124             if (version instanceof ScmBranch) {
125                 // The tag specified does not appear to be numeric, so assume it refers
126                 // to a branch/tag url and perform a switch operation rather than update
127                 cl.createArg().setValue("switch");
128                 if (version instanceof ScmTag) {
129                     String tagUrl = SvnTagBranchUtils.resolveTagUrl(repository, (ScmTag) version);
130                     cl.createArg().setValue(tagUrl + "@");
131                 } else {
132                     String branchUrl = SvnTagBranchUtils.resolveBranchUrl(repository, (ScmBranch) version);
133                     cl.createArg().setValue(branchUrl + "@");
134                 }
135                 cl.createArg().setValue(workingDir + "@");
136             }
137         }
138 
139         return cl;
140     }
141 
142     /** {@inheritDoc} */
143     protected ChangeLogCommand getChangeLogCommand() {
144         return new SvnChangeLogCommand();
145     }
146 }