1 package org.apache.maven.scm.provider.svn.svnexe.command.update;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
46
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
113
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
133
134 protected ChangeLogCommand getChangeLogCommand()
135 {
136 SvnChangeLogCommand command = new SvnChangeLogCommand();
137
138 command.setLogger( getLogger() );
139
140 return command;
141 }
142
143
144 }