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   * @version $Id: SvnUpdateCommand.java 1054126 2010-12-31 15:18:11Z olamy $
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          getLogger().info( "changeSets " + consumer.getChangeSets());
91          
92          return result;
93      }
94  
95      // ----------------------------------------------------------------------
96      //
97      // ----------------------------------------------------------------------
98  
99      public static Commandline createCommandLine( SvnScmProviderRepository repository, File workingDirectory,
100                                                  ScmVersion version )
101     {
102         Settings settings = SvnUtil.getSettings();
103 
104         String workingDir = workingDirectory.getAbsolutePath();
105 
106         if ( settings.isUseCygwinPath() )
107         {
108             workingDir = settings.getCygwinMountPath() + "/" + workingDir;
109             workingDir = StringUtils.replace( workingDir, ":", "" );
110             workingDir = StringUtils.replace( workingDir, "\\", "/" );
111         }
112 
113         if ( version != null && StringUtils.isEmpty( version.getName() ) )
114         {
115             version = null;
116         }
117 
118         Commandline cl = SvnCommandLineUtils.getBaseSvnCommandLine( workingDirectory, repository );
119 
120         if ( version == null || SvnTagBranchUtils.isRevisionSpecifier( version ) )
121         {
122             cl.createArg().setValue( "update" );
123 
124             if ( version != null && StringUtils.isNotEmpty( version.getName() ) )
125             {
126                 cl.createArg().setValue( "-r" );
127                 cl.createArg().setValue( version.getName() );
128             }
129 
130             cl.createArg().setValue( workingDir );
131         }
132         else
133         {
134             if ( version instanceof ScmBranch )
135             {
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                 {
141                     cl.createArg().setValue( SvnTagBranchUtils.resolveTagUrl( repository, (ScmTag) version ) );
142                 }
143                 else
144                 {
145                     cl.createArg().setValue(
146                         SvnTagBranchUtils.resolveBranchUrl( repository, (ScmBranch) version ) );
147                 }
148                 cl.createArg().setValue( workingDir );
149             }
150         }
151 
152         return cl;
153     }
154 
155     /** {@inheritDoc} */
156     protected ChangeLogCommand getChangeLogCommand()
157     {
158         SvnChangeLogCommand command = new SvnChangeLogCommand();
159 
160         command.setLogger( getLogger() );
161 
162         return command;
163     }
164 
165 
166 }