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