1 package org.apache.maven.scm.provider.svn.svnexe.command.remoteinfo;
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.CommandParameters;
23 import org.apache.maven.scm.ScmException;
24 import org.apache.maven.scm.ScmFileSet;
25 import org.apache.maven.scm.command.remoteinfo.AbstractRemoteInfoCommand;
26 import org.apache.maven.scm.command.remoteinfo.RemoteInfoScmResult;
27 import org.apache.maven.scm.provider.ScmProviderRepository;
28 import org.apache.maven.scm.provider.svn.command.SvnCommand;
29 import org.apache.maven.scm.provider.svn.repository.SvnScmProviderRepository;
30 import org.apache.maven.scm.provider.svn.svnexe.command.SvnCommandLineUtils;
31 import org.apache.maven.scm.util.AbstractConsumer;
32 import org.codehaus.plexus.util.StringUtils;
33 import org.codehaus.plexus.util.cli.CommandLineException;
34 import org.codehaus.plexus.util.cli.CommandLineUtils;
35 import org.codehaus.plexus.util.cli.Commandline;
36
37 import java.util.HashMap;
38 import java.util.Map;
39
40
41
42
43
44 public class SvnRemoteInfoCommand
45 extends AbstractRemoteInfoCommand
46 implements SvnCommand
47 {
48 @Override
49 public RemoteInfoScmResult executeRemoteInfoCommand( ScmProviderRepository repository, ScmFileSet fileSet,
50 CommandParameters parameters )
51 throws ScmException
52 {
53
54 String url = ( (SvnScmProviderRepository) repository ).getUrl();
55
56
57 url = StringUtils.stripEnd( url, "/" );
58 int idx = url.lastIndexOf( "/" );
59 String baseUrl = url.substring( 0, idx );
60
61 Commandline cl = SvnCommandLineUtils.getBaseSvnCommandLine( fileSet == null ? null : fileSet.getBasedir(),
62 (SvnScmProviderRepository) repository );
63
64 cl.createArg().setValue( "ls" );
65
66 cl.createArg().setValue( baseUrl + "/tags" + "@" );
67
68 CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
69
70 LsConsumer consumer = new LsConsumer( baseUrl + "/tags" );
71
72 int exitCode = 0;
73
74 Map<String, String> tagsInfos = null;
75
76 try
77 {
78 exitCode = SvnCommandLineUtils.execute( cl, consumer, stderr );
79 tagsInfos = consumer.infos;
80
81 }
82 catch ( CommandLineException ex )
83 {
84 throw new ScmException( "Error while executing svn command.", ex );
85 }
86
87 if ( exitCode != 0 )
88 {
89 return new RemoteInfoScmResult( cl.toString(), "The svn command failed.", stderr.getOutput(), false );
90 }
91
92 cl = SvnCommandLineUtils.getBaseSvnCommandLine( fileSet == null ? null : fileSet.getBasedir(),
93 (SvnScmProviderRepository) repository );
94
95 cl.createArg().setValue( "ls" );
96
97 cl.createArg().setValue( baseUrl + "/branches" + "@" );
98
99 stderr = new CommandLineUtils.StringStreamConsumer();
100
101 consumer = new LsConsumer( baseUrl + "/branches" );
102
103 Map<String, String> branchesInfos = null;
104
105 try
106 {
107 exitCode = SvnCommandLineUtils.execute( cl, consumer, stderr );
108 branchesInfos = consumer.infos;
109
110 }
111 catch ( CommandLineException ex )
112 {
113 throw new ScmException( "Error while executing svn command.", ex );
114 }
115
116 if ( exitCode != 0 )
117 {
118 return new RemoteInfoScmResult( cl.toString(), "The svn command failed.", stderr.getOutput(), false );
119 }
120
121 return new RemoteInfoScmResult( cl.toString(), branchesInfos, tagsInfos );
122 }
123
124 public boolean remoteUrlExist( ScmProviderRepository repository, CommandParameters parameters )
125 throws ScmException
126 {
127 String url = ( (SvnScmProviderRepository) repository ).getUrl();
128
129 Commandline cl = SvnCommandLineUtils.getBaseSvnCommandLine( null, (SvnScmProviderRepository) repository );
130
131 cl.createArg().setValue( "ls" );
132
133 cl.createArg().setValue( url + "@" );
134
135 CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
136
137 LsConsumer consumer = new LsConsumer( url );
138
139 int exitCode = 0;
140
141 try
142 {
143 exitCode = SvnCommandLineUtils.execute( cl, consumer, stderr );
144 }
145 catch ( CommandLineException ex )
146 {
147 throw new ScmException( "Error while executing svn command.", ex );
148 }
149
150 if ( exitCode != 0 )
151 {
152 String output = stderr.getOutput();
153
154
155 if ( output.contains( "W160013" ) || output.contains( "svn: URL" ) )
156 {
157 return false;
158 }
159 throw new ScmException( cl + ".The svn command failed:" + stderr.getOutput() );
160 }
161
162 return true;
163 }
164
165 private static class LsConsumer
166 extends AbstractConsumer
167 {
168 Map<String, String> infos = new HashMap<>();
169
170 String url;
171
172 LsConsumer( String url )
173 {
174 this.url = url;
175 }
176
177 public void consumeLine( String s )
178 {
179 infos.put( StringUtils.stripEnd( s, "/" ), url + "/" + s );
180 }
181
182 Map<String, String> getInfos()
183 {
184 return infos;
185 }
186 }
187 }