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