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 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
48
49
50 public class SvnUpdateCommand
51 extends AbstractUpdateCommand
52 implements SvnCommand
53 {
54
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
141
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
160 protected ChangeLogCommand getChangeLogCommand()
161 {
162 SvnChangeLogCommand command = new SvnChangeLogCommand();
163
164 command.setLogger( getLogger() );
165
166 return command;
167 }
168
169
170 }