/
CheckoutProjectFromScm.java
293 lines (247 loc) · 11.3 KB
/
CheckoutProjectFromScm.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
package org.apache.maven.shared.release.phase;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;
import java.io.File;
import java.io.IOException;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import org.apache.maven.project.MavenProject;
import org.apache.maven.scm.CommandParameter;
import org.apache.maven.scm.CommandParameters;
import org.apache.maven.scm.ScmException;
import org.apache.maven.scm.ScmFileSet;
import org.apache.maven.scm.ScmTag;
import org.apache.maven.scm.command.checkout.CheckOutScmResult;
import org.apache.maven.scm.manager.NoSuchScmProviderException;
import org.apache.maven.scm.provider.ScmProvider;
import org.apache.maven.scm.repository.ScmRepository;
import org.apache.maven.scm.repository.ScmRepositoryException;
import org.apache.maven.shared.release.ReleaseExecutionException;
import org.apache.maven.shared.release.ReleaseFailureException;
import org.apache.maven.shared.release.ReleaseResult;
import org.apache.maven.shared.release.config.ReleaseDescriptor;
import org.apache.maven.shared.release.env.ReleaseEnvironment;
import org.apache.maven.shared.release.scm.ReleaseScmCommandException;
import org.apache.maven.shared.release.scm.ReleaseScmRepositoryException;
import org.apache.maven.shared.release.scm.ScmRepositoryConfigurator;
import org.apache.maven.shared.release.util.ReleaseUtil;
import org.codehaus.plexus.util.FileUtils;
import org.codehaus.plexus.util.StringUtils;
import static java.util.Objects.requireNonNull;
import static org.apache.maven.shared.utils.logging.MessageUtils.buffer;
/**
* @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
*/
@Singleton
@Named( "checkout-project-from-scm" )
public class CheckoutProjectFromScm
extends AbstractReleasePhase
{
/**
* Tool that gets a configured SCM repository from release configuration.
*/
private final ScmRepositoryConfigurator scmRepositoryConfigurator;
@Inject
public CheckoutProjectFromScm( ScmRepositoryConfigurator scmRepositoryConfigurator )
{
this.scmRepositoryConfigurator = requireNonNull( scmRepositoryConfigurator );
}
@Override
public ReleaseResult execute( ReleaseDescriptor releaseDescriptor, ReleaseEnvironment releaseEnvironment,
List<MavenProject> reactorProjects )
throws ReleaseExecutionException, ReleaseFailureException
{
ReleaseResult releaseResult;
if ( releaseDescriptor.isLocalCheckout() )
{
// in the release phase we have to change the checkout URL
// to do a local checkout instead of going over the network.
// the first step is a bit tricky, we need to know which provider! like e.g. "scm:jgit:http://"
// the offset of 4 is because 'scm:' has 4 characters...
String providerPart = releaseDescriptor.getScmSourceUrl()
.substring( 0, releaseDescriptor.getScmSourceUrl().indexOf( ':', 4 ) );
String scmPath = releaseDescriptor.getWorkingDirectory();
// now we iteratively try to checkout.
// if the local checkout fails, then we might be in a subdirectory
// and need to walk a few directories up.
do
{
try
{
if ( scmPath.startsWith( "/" ) )
{
// cut off the first '/'
scmPath = scmPath.substring( 1 );
}
String scmUrl = providerPart + ":file:///" + scmPath;
releaseDescriptor.setScmSourceUrl( scmUrl );
getLogger().info( "Performing a LOCAL checkout from " + releaseDescriptor.getScmSourceUrl() );
releaseResult = performCheckout( releaseDescriptor, releaseEnvironment, reactorProjects );
}
catch ( ScmException scmEx )
{
// the checkout from _this_ directory failed
releaseResult = null;
}
if ( releaseResult == null || releaseResult.getResultCode() == ReleaseResult.ERROR )
{
// this means that there is no SCM repo in this directory
// thus we try to step one directory up
releaseResult = null;
// remove last sub-directory path
int lastSlashPos = scmPath.lastIndexOf( File.separator );
if ( lastSlashPos > 0 )
{
scmPath = scmPath.substring( 0, lastSlashPos );
}
else
{
throw new ReleaseExecutionException( "could not perform a local checkout" );
}
}
}
while ( releaseResult == null );
}
else
{
// when there is no localCheckout, then we just do a standard SCM checkout.
try
{
releaseResult = performCheckout( releaseDescriptor, releaseEnvironment, reactorProjects );
}
catch ( ScmException e )
{
releaseResult = new ReleaseResult();
releaseResult.setResultCode( ReleaseResult.ERROR );
logError( releaseResult, e.getMessage() );
throw new ReleaseExecutionException( "An error is occurred in the checkout process: "
+ e.getMessage(), e );
}
}
return releaseResult;
}
private ReleaseResult performCheckout( ReleaseDescriptor releaseDescriptor, ReleaseEnvironment releaseEnvironment,
List<MavenProject> reactorProjects )
throws ReleaseExecutionException, ReleaseFailureException, ScmException
{
ReleaseResult result = new ReleaseResult();
logInfo( result, "Checking out the project to perform the release ..." );
ScmRepository repository;
ScmProvider provider;
try
{
repository = scmRepositoryConfigurator.getConfiguredRepository( releaseDescriptor,
releaseEnvironment.getSettings() );
provider = scmRepositoryConfigurator.getRepositoryProvider( repository );
}
catch ( ScmRepositoryException e )
{
result.setResultCode( ReleaseResult.ERROR );
logError( result, e.getMessage() );
throw new ReleaseScmRepositoryException( e.getMessage(), e.getValidationMessages() );
}
catch ( NoSuchScmProviderException e )
{
result.setResultCode( ReleaseResult.ERROR );
logError( result, e.getMessage() );
throw new ReleaseExecutionException( "Unable to configure SCM repository: " + e.getMessage(), e );
}
MavenProject rootProject = ReleaseUtil.getRootProject( reactorProjects );
// TODO: sanity check that it is not . or .. or lower
File checkoutDirectory =
FileUtils.resolveFile( rootProject.getBasedir(), releaseDescriptor.getCheckoutDirectory() );
if ( checkoutDirectory.exists() )
{
try
{
FileUtils.deleteDirectory( checkoutDirectory );
}
catch ( IOException e )
{
result.setResultCode( ReleaseResult.ERROR );
logError( result, e.getMessage() );
throw new ReleaseExecutionException( "Unable to remove old checkout directory: " + e.getMessage(), e );
}
}
checkoutDirectory.mkdirs();
CommandParameters commandParameters = new CommandParameters();
commandParameters.setString( CommandParameter.SHALLOW,
Boolean.valueOf( releaseDescriptor.isScmShallowClone() ).toString() );
CheckOutScmResult scmResult = provider.checkOut( repository, new ScmFileSet( checkoutDirectory ),
new ScmTag( releaseDescriptor.getScmReleaseLabel() ), commandParameters );
if ( releaseDescriptor.isLocalCheckout() && !scmResult.isSuccess() )
{
// this is not beautiful but needed to indicate that the execute() method
// should continue in the parent directory
return null;
}
String scmRelativePathProjectDirectory = scmResult.getRelativePathProjectDirectory();
if ( StringUtils.isEmpty( scmRelativePathProjectDirectory ) )
{
Path workingDirectory = Paths.get( releaseDescriptor.getWorkingDirectory() );
Path rootProjectBasedir;
try
{
rootProjectBasedir = rootProject.getBasedir().toPath().toRealPath( LinkOption.NOFOLLOW_LINKS );
}
catch ( IOException e )
{
throw new ReleaseExecutionException( e.getMessage(), e );
}
scmRelativePathProjectDirectory = workingDirectory.relativize( rootProjectBasedir ).toString();
}
releaseDescriptor.setScmRelativePathProjectDirectory( scmRelativePathProjectDirectory );
if ( !scmResult.isSuccess() )
{
result.setResultCode( ReleaseResult.ERROR );
logError( result, scmResult.getProviderMessage() );
throw new ReleaseScmCommandException( "Unable to checkout from SCM", scmResult );
}
result.setResultCode( ReleaseResult.SUCCESS );
return result;
}
@Override
public ReleaseResult simulate( ReleaseDescriptor releaseDescriptor, ReleaseEnvironment releaseEnvironment,
List<MavenProject> reactorProjects )
throws ReleaseExecutionException, ReleaseFailureException
{
ReleaseResult result = new ReleaseResult();
MavenProject rootProject = ReleaseUtil.getRootProject( reactorProjects );
File checkoutDirectory =
FileUtils.resolveFile( rootProject.getBasedir(), releaseDescriptor.getCheckoutDirectory() );
if ( releaseDescriptor.isLocalCheckout() )
{
logInfo( result,
"The project would have a " + buffer().strong( "local" )
+ " check out to perform the release from " + checkoutDirectory + "..." );
}
else
{
logInfo( result,
"The project would be checked out to perform the release from " + checkoutDirectory + "..." );
}
result.setResultCode( ReleaseResult.SUCCESS );
return result;
}
}