View Javadoc
1   package org.apache.maven.scm.provider.svn.svnexe.command.update;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   * http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.File;
23  
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.StringUtils;
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
51      extends AbstractUpdateCommand
52      implements SvnCommand
53  {
54      /** {@inheritDoc} */
55      protected UpdateScmResult executeUpdateCommand( ScmProviderRepository repo, ScmFileSet fileSet, ScmVersion version )
56          throws ScmException
57      {
58          Commandline cl = createCommandLine( (SvnScmProviderRepository) repo, fileSet.getBasedir(), version );
59  
60          SvnUpdateConsumer consumer = new SvnUpdateConsumer( getLogger(), fileSet.getBasedir() );
61  
62          CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
63  
64          if ( getLogger().isInfoEnabled() )
65          {
66              getLogger().info( "Executing: " + SvnCommandLineUtils.cryptPassword( cl ) );
67              getLogger().info( "Working directory: " + cl.getWorkingDirectory().getAbsolutePath() );
68          }
69  
70          int exitCode;
71  
72          try
73          {
74              exitCode = SvnCommandLineUtils.execute( cl, consumer, stderr, getLogger() );
75          }
76          catch ( CommandLineException ex )
77          {
78              throw new ScmException( "Error while executing command.", ex );
79          }
80  
81          if ( exitCode != 0 )
82          {
83              return new UpdateScmResult( cl.toString(), "The svn command failed.", stderr.getOutput(), false );
84          }
85  
86          UpdateScmResultWithRevision result = new UpdateScmResultWithRevision( cl.toString(), consumer.getUpdatedFiles(),
87                                                  String.valueOf( consumer.getRevision() ) );
88          
89          result.setChanges( consumer.getChangeSets() );
90  
91          if ( getLogger().isDebugEnabled() )
92          {
93              getLogger().debug( "changeSets " + consumer.getChangeSets() );
94          }
95          
96          return result;
97      }
98  
99      // ----------------------------------------------------------------------
100     //
101     // ----------------------------------------------------------------------
102 
103     public static Commandline createCommandLine( SvnScmProviderRepository repository, File workingDirectory,
104                                                  ScmVersion version )
105     {
106         Settings settings = SvnUtil.getSettings();
107 
108         String workingDir = workingDirectory.getAbsolutePath();
109 
110         if ( settings.isUseCygwinPath() )
111         {
112             workingDir = settings.getCygwinMountPath() + "/" + workingDir;
113             workingDir = StringUtils.replace( workingDir, ":", "" );
114             workingDir = StringUtils.replace( workingDir, "\\", "/" );
115         }
116 
117         if ( version != null && StringUtils.isEmpty( version.getName() ) )
118         {
119             version = null;
120         }
121 
122         Commandline cl = SvnCommandLineUtils.getBaseSvnCommandLine( workingDirectory, repository );
123 
124         if ( version == null || SvnTagBranchUtils.isRevisionSpecifier( version ) )
125         {
126             cl.createArg().setValue( "update" );
127 
128             if ( version != null && StringUtils.isNotEmpty( version.getName() ) )
129             {
130                 cl.createArg().setValue( "-r" );
131                 cl.createArg().setValue( version.getName() );
132             }
133 
134             cl.createArg().setValue( workingDir );
135         }
136         else
137         {
138             if ( version instanceof ScmBranch )
139             {
140                 // The tag specified does not appear to be numeric, so assume it refers
141                 // to a branch/tag url and perform a switch operation rather than update
142                 cl.createArg().setValue( "switch" );
143                 if ( version instanceof ScmTag )
144                 {
145                     cl.createArg().setValue( SvnTagBranchUtils.resolveTagUrl( repository, (ScmTag) version ) );
146                 }
147                 else
148                 {
149                     cl.createArg().setValue(
150                         SvnTagBranchUtils.resolveBranchUrl( repository, (ScmBranch) version ) );
151                 }
152                 cl.createArg().setValue( workingDir );
153             }
154         }
155 
156         return cl;
157     }
158 
159     /** {@inheritDoc} */
160     protected ChangeLogCommand getChangeLogCommand()
161     {
162         SvnChangeLogCommand command = new SvnChangeLogCommand();
163 
164         command.setLogger( getLogger() );
165 
166         return command;
167     }
168 
169 
170 }