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