-
-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathCVSSCM.java
More file actions
675 lines (555 loc) · 23.5 KB
/
CVSSCM.java
File metadata and controls
675 lines (555 loc) · 23.5 KB
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
/*
* The MIT License
*
* Copyright (c) 2004-2013, Sun Microsystems, Inc., Kohsuke Kawaguchi, Jene Jasper, Stephen Connolly, CloudBees, Inc., Michael Clarke
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package hudson.scm;
import hudson.AbortException;
import hudson.Extension;
import hudson.FilePath;
import hudson.Launcher;
import hudson.model.*;
import hudson.scm.browsers.CvsFacadeRepositoryBrowser;
import hudson.scm.cvstagging.LegacyTagAction;
import hudson.util.FormValidation;
import hudson.util.Secret;
import jenkins.model.Jenkins;
import jenkins.scm.cvs.QuietPeriodCompleted;
import net.sf.json.JSONObject;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.QueryParameter;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.export.Exported;
import org.netbeans.lib.cvsclient.CVSRoot;
import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import static hudson.Util.fixEmptyAndTrim;
/**
* CVS.
*
* <p>
* I couldn't call this class "CVS" because that would cause the view folder
* name to collide with CVS control files.
*
* <p>
* This object gets shipped to the remote machine to perform some of the work,
* so it implements {@link Serializable}.
*
* @author Kohsuke Kawaguchi
* @author Michael Clarke
*/
public class CVSSCM extends AbstractCvs implements Serializable {
private static final long serialVersionUID = -2175193493227149541L;
/**
* True to avoid creating a sub-directory inside the workspace. (Works only
* when there's just one repository containing one module.)
*/
private final boolean flatten;
private final CvsRepository[] repositories;
private final boolean canUseUpdate;
private final boolean skipChangeLog;
private boolean pruneEmptyDirectories;
private boolean disableCvsQuiet;
private boolean cleanOnFailedUpdate;
private boolean forceCleanCopy;
private transient CvsFacadeRepositoryBrowser facadeRepositoryBrowser;
/**
* If true, behave like pre-2.13 and use the timestamp of the build to check out the code.
* If false, prefer the timestamp when the quiet period has elapsed, so that we check out
* the code at a quiet stable state.
*
* @since 2.13
*/
private boolean checkoutCurrentTimestamp;
// start legacy fields
@Deprecated
private transient String module;
@Deprecated
private transient String cvsroot;
@Deprecated
private transient String branch;
@Deprecated
private transient boolean useHeadIfNotFound;
@Deprecated
private transient boolean isTag;
@Deprecated
private transient String excludedRegions;
@Deprecated
private transient String cvsRsh;
@Deprecated
private transient CVSRepositoryBrowser repositoryBrowser;
// end legacy fields
/**
* @deprecated we now use multiple repositories and don't use cvsRsh
*/
@Deprecated
public CVSSCM(final String cvsRoot, final String allModules, final String branch, final String cvsRsh,
final boolean canUseUpdate, final boolean useHeadIfNotFound, final boolean legacy,
final boolean isTag, final String excludedRegions) {
this(cvsRoot, allModules, branch, cvsRsh, canUseUpdate, useHeadIfNotFound, legacy, isTag , excludedRegions, null);
}
@Deprecated
public CVSSCM(final String cvsRoot, final String allModules, final String branch, final String cvsRsh,
final boolean canUseUpdate, final boolean useHeadIfNotFound, final boolean legacy,
final boolean isTag, final String excludedRegions, final CVSRepositoryBrowser browser) {
this(LegacyConvertor.getInstance().convertLegacyConfigToRepositoryStructure(cvsRoot, allModules, branch, isTag, excludedRegions,
useHeadIfNotFound, browser), canUseUpdate, legacy, Boolean.getBoolean(CVSSCM.class.getName() + ".skipChangeLog"), true, false, false, true);
}
@Deprecated
public CVSSCM(final List<CvsRepository> repositories, final boolean canUseUpdate, final boolean legacy, final boolean skipChangeLog, final boolean pruneEmptyDirectories,
final boolean disableCvsQuiet, final boolean cleanOnFailedUpdate, final boolean forceCleanCopy) {
this(repositories, canUseUpdate, legacy, skipChangeLog, pruneEmptyDirectories, disableCvsQuiet,
cleanOnFailedUpdate, forceCleanCopy, false);
}
@DataBoundConstructor
public CVSSCM(final List<CvsRepository> repositories, final boolean canUseUpdate, final boolean legacy, final boolean skipChangeLog, final boolean pruneEmptyDirectories,
final boolean disableCvsQuiet, final boolean cleanOnFailedUpdate, final boolean forceCleanCopy, boolean checkoutCurrentTimestamp) {
this.repositories = repositories.toArray(new CvsRepository[repositories.size()]);
this.canUseUpdate = canUseUpdate;
this.skipChangeLog = skipChangeLog;
flatten = !legacy && this.repositories.length == 1 && this.repositories[0].getRepositoryItems().length == 1 && this.repositories[0].getRepositoryItems()[0].getModules().length == 1;
this.pruneEmptyDirectories = pruneEmptyDirectories;
this.disableCvsQuiet = disableCvsQuiet;
this.cleanOnFailedUpdate = cleanOnFailedUpdate;
this.forceCleanCopy = forceCleanCopy;
this.checkoutCurrentTimestamp = checkoutCurrentTimestamp;
}
/**
* Convert legacy configuration into the new class structure.
*
* @return an instance of this class with all the new fields transferred
* from the old structure to the new one
*/
public final Object readResolve() {
if (repositoryBrowser != null) {
facadeRepositoryBrowser = new CvsFacadeRepositoryBrowser(repositoryBrowser);
if (repositories != null) {
List <CvsRepository> newRepositories = new ArrayList<CvsRepository>();
for (CvsRepository repository : repositories) {
newRepositories.add(new CvsRepository(repository.getCvsRoot(), repository.isPasswordRequired(), repository.getPassword().getPlainText(),
Arrays.asList(repository.getRepositoryItems()), Arrays.asList(repository.getExcludedRegions()), repository.getCompressionLevel(), repositoryBrowser));
}
return new CVSSCM(Arrays.asList(repositories), isCanUseUpdate(), isLegacy(), isSkipChangeLog(), isPruneEmptyDirectories(), isDisableCvsQuiet(), isCleanOnFailedUpdate(), isForceCleanCopy());
}
}
/*
* check if we're running a version of this class that uses multiple
* repositories. if we are then we can return it as it is
*/
if (null != repositories) {
return this;
}
/*
* if we've got to this point then we have to do some conversion. Do
* this by using the deprecated constructor to upgrade the model through
* constructor chaining
*/
return new CVSSCM(cvsroot, module, branch, cvsRsh, isCanUseUpdate(), useHeadIfNotFound, isLegacy(), isTag,
excludedRegions, repositoryBrowser);
}
/**
* Gets the repositories currently configured for this job.
*
* @return an array of {@link CvsRepository}
*/
@Exported
public CvsRepository[] getRepositories() {
return repositories;
}
@Override
public CvsFacadeRepositoryBrowser getBrowser() {
if (facadeRepositoryBrowser == null) {
return new CvsFacadeRepositoryBrowser();
}
return facadeRepositoryBrowser;
}
@Override
public boolean requiresWorkspaceForPolling() {
return false;
}
/**
* Checks for differences between the current workspace and the remote
* repository.
*
* @see SCM#compareRemoteRevisionWith(AbstractProject, Launcher, FilePath, TaskListener, SCMRevisionState)
*/
@Override
protected PollingResult compareRemoteRevisionWith(final AbstractProject<?, ?> project, final Launcher launcher,
final FilePath workspace, final TaskListener listener, final SCMRevisionState baseline)
throws IOException, InterruptedException {
return super.compareRemoteRevisionWith(project, launcher, workspace, listener, baseline, getRepositories());
}
/**
* Checks for differences between the current workspace and the remote
* repository.
*
* @see SCM#compareRemoteRevisionWith(Job, Launcher, FilePath, TaskListener, SCMRevisionState)
*/
@Override
public PollingResult compareRemoteRevisionWith(final Job<?, ?> project, final Launcher launcher,
final FilePath workspace, final TaskListener listener, final SCMRevisionState baseline)
throws IOException, InterruptedException {
return super.compareRemoteRevisionWith(project, launcher, workspace, listener, baseline, getRepositories());
}
/**
* If there are multiple modules, return the module directory of the first
* one.
*
* @param workspace
*/
@Override
public FilePath getModuleRoot(final FilePath workspace, @SuppressWarnings("rawtypes") final AbstractBuild build) {
if (flatten) {
return workspace;
}
return workspace.child(getRepositories()[0].getRepositoryItems()[0].getModules()[0].getCheckoutName());
}
@Override
public FilePath[] getModuleRoots(final FilePath workspace, @SuppressWarnings("rawtypes") final AbstractBuild build) {
if (!flatten) {
List<FilePath> moduleRoots = new ArrayList<FilePath>();
for (CvsRepository repository : getRepositories()) {
for (CvsRepositoryItem item : repository.getRepositoryItems()) {
for (CvsModule module : item.getModules()) {
moduleRoots.add(workspace.child(module.getCheckoutName()));
}
}
}
return moduleRoots.toArray(new FilePath[moduleRoots.size()]);
}
return new FilePath[] {getModuleRoot(workspace, build)};
}
@Exported
public boolean isCanUseUpdate() {
return canUseUpdate;
}
@Exported
public boolean isSkipChangeLog() {
return skipChangeLog;
}
@Exported
public boolean isPruneEmptyDirectories() {
return pruneEmptyDirectories;
}
@Exported
public boolean isFlatten() {
return flatten;
}
@Exported
public boolean isDisableCvsQuiet() {
return disableCvsQuiet;
}
@Exported
public boolean isCleanOnFailedUpdate() {
return cleanOnFailedUpdate;
}
@Exported
public boolean isForceCleanCopy() {
return forceCleanCopy;
}
public boolean isLegacy() {
return !flatten;
}
@Exported
public boolean isCheckoutCurrentTimestamp() {
return checkoutCurrentTimestamp;
}
@Override
public void buildEnvVars(AbstractBuild<?,?> build, Map<String, String> env) {
String branchName = getBranchName();
if (branchName != null) {
env.put("CVS_BRANCH", branchName);
}
}
/**
* Used to support legacy setup - if we have one repository with all modules on the
* same tag/branch then this will return that tag/branch name, otherwise it will return null.
* @return a name of a tag or branch, or null if we have more than one repository or modules on different branches
*/
private String getBranchName() {
if (getRepositories()[0].getRepositoryItems()[0].getLocation().getLocationType() == CvsRepositoryLocationType.HEAD) {
return null;
}
String locationName = getRepositories()[0].getRepositoryItems()[0].getLocation().getLocationName();
if (null == locationName) {
return null;
}
for (CvsRepository repository : getRepositories()) {
for (CvsRepositoryItem item : repository.getRepositoryItems()) {
if (!locationName.equals(item.getLocation().getLocationName())) {
return null;
}
}
}
return locationName;
}
@Override
public boolean checkout(final AbstractBuild<?, ?> build, final Launcher launcher, final FilePath workspace,
final BuildListener listener, final File changelogFile) throws IOException, InterruptedException {
try {
checkout(build, launcher, workspace, listener, changelogFile, null);
}
catch (AbortException e) {
return false;
}
return true;
}
@Override
public void checkout(final @Nonnull Run<?,?> build, final @Nonnull Launcher launcher, final @Nonnull FilePath workspace,
final @Nonnull TaskListener listener, final @CheckForNull File changelogFile,
final @CheckForNull SCMRevisionState baseline) throws IOException, InterruptedException {
if (!canUseUpdate) {
workspace.deleteContents();
}
final String dateStamp;
Date checkoutDate = getCheckoutDate(build);
synchronized (DATE_FORMATTER) {
dateStamp = DATE_FORMATTER.format(checkoutDate);
}
if (!checkout(repositories, flatten, workspace, canUseUpdate,
build, dateStamp, pruneEmptyDirectories, cleanOnFailedUpdate, listener)) {
throw new AbortException();
}
postCheckout(build, changelogFile, getRepositories(), workspace, listener, isFlatten(), build.getEnvironment(listener));
}
@Override
public DescriptorImpl getDescriptor() {
return (DescriptorImpl) super.getDescriptor();
}
@Extension
public static class DescriptorImpl extends AbstractCvsDescriptor<CVSSCM> implements ModelObject {
// start legacy fields
private transient String cvsPassFile;
@SuppressWarnings("unused")
private transient String cvsExe;
@SuppressWarnings("unused")
private transient boolean noCompression;
private class RepositoryBrowser {
@SuppressWarnings("unused")
transient String diffURL;
@SuppressWarnings("unused")
transient String browseURL;
}
@SuppressWarnings("unused")
private transient Map<String, RepositoryBrowser> browsers;
// end legacy fields
/**
* CVS compression level if individual repositories don't specifically
* set it.
*/
private int compressionLevel = 3;
private String privateKeyLocation = System.getProperty("user.home") + "/.ssh/id_rsa";
private Secret privateKeyPassword = null;
private String knownHostsLocation = System.getProperty("user.home") + "/.ssh/known_hosts";
private CvsAuthentication[] authTokens = new CvsAuthentication[]{};
// we don't provide a way for users to edit this, other than by manually editing their XML config
private String changelogEncoding = "UTF-8";
public DescriptorImpl() {
super(CVSRepositoryBrowser.class);
load();
}
public List<Descriptor<hudson.scm.RepositoryBrowser<?>>> getBrowserDescriptors() {
List<Descriptor<hudson.scm.RepositoryBrowser<?>>> browsers = super.getBrowserDescriptors();
for (Iterator<Descriptor<hudson.scm.RepositoryBrowser<?>>> itr = browsers.iterator(); itr.hasNext();) {
if (itr.next().getClass() == CvsFacadeRepositoryBrowser.CvsFacadeRepositoryBrowserDescriptor.class) {
itr.remove();
}
}
return browsers;
}
@Override
public String getDisplayName() {
return "CVS";
}
@Override
public SCM newInstance(final StaplerRequest req, final JSONObject formData) throws FormException {
return req.bindJSON(CVSSCM.class, formData);
}
@Exported
public String getPrivateKeyLocation() {
return privateKeyLocation;
}
@Exported
public Secret getPrivateKeyPassword() {
return privateKeyPassword;
}
@Exported
public String getKnownHostsLocation() {
return knownHostsLocation;
}
@Exported
public int getCompressionLevel() {
return compressionLevel;
}
@Exported
public CvsAuthentication[] getAuthentication() {
return authTokens;
}
@Override
@Exported
public String getChangelogEncoding() {
return changelogEncoding;
}
@Override
public void load() {
super.load();
// used to move to the new data structure
if (noCompression) {
compressionLevel = 0;
}
}
@Override
public boolean configure(final StaplerRequest req, final JSONObject o) {
String compressionLevel = fixEmptyAndTrim(o.getString("cvsCompression"));
try {
this.compressionLevel = Integer.parseInt(compressionLevel);
} catch (final NumberFormatException ex) {
this.compressionLevel = 0;
}
final String knownHostsLocation = fixEmptyAndTrim(o.getString("knownHostsLocation"));
if (knownHostsLocation == null) {
this.knownHostsLocation = System.getProperty("user.home") + "/.ssh/known_hosts";
} else {
this.knownHostsLocation = knownHostsLocation;
}
final String privateKeyLocation = fixEmptyAndTrim(o.getString("privateKeyLocation"));
if (privateKeyLocation == null) {
this.privateKeyLocation = System.getProperty("user.home") + "/.ssh/id_rsa";
} else {
this.privateKeyLocation = privateKeyLocation;
}
privateKeyPassword = Secret.fromString(fixEmptyAndTrim(o.getString("privateKeyPassword")));
List<CvsAuthentication> authTokens = req.bindParametersToList(CvsAuthentication.class, "cvsAuthentication.");
this.authTokens = authTokens.toArray(new CvsAuthentication[authTokens.size()]);
save();
return true;
}
/**
* Returns all {@code CVSROOT} strings used in the current Jenkins
* installation.
*/
public Set<String> getAllCvsRoots() {
Set<String> r = new TreeSet<String>();
for (AbstractProject<?, ?> p : Jenkins.getActiveInstance().getAllItems(AbstractProject.class)) {
SCM scm = p.getScm();
if (scm instanceof CVSSCM) {
CVSSCM cvsscm = (CVSSCM) scm;
for (CvsRepository repository : cvsscm.getRepositories()) {
r.add(repository.getCvsRoot());
}
}
}
return r;
}
@Deprecated
public String getCvsPassFile() {
return cvsPassFile;
}
public FormValidation doCheckAuthenticationCvsRoot(@QueryParameter final String value) {
try {
CVSRoot cvsRoot = CVSRoot.parse(value);
if (cvsRoot.getUserName() != null) {
return FormValidation.error("Do not specify a username in the CVS root; use the username field.");
}
if (cvsRoot.getPassword() != null) {
return FormValidation.error("Do not specify a password in the CVS root; use the password field.");
}
if (cvsRoot.getMethod().equals(CVSRoot.METHOD_FORK)
|| cvsRoot.getMethod().equals(CVSRoot.METHOD_LOCAL)) {
return FormValidation.error(cvsRoot.getMethod() +
" does not support authentication so should not be specified");
}
return FormValidation.ok();
} catch(IllegalArgumentException ex) {
return FormValidation.error(value +
" does not seem to be a valid CVS root so would not match any repositories.");
}
}
@Restricted(NoExternalUse.class)
@Nonnull
public static DescriptorImpl getOrDie() {
return (CVSSCM.DescriptorImpl) Jenkins.getActiveInstance().getDescriptorOrDie(CVSSCM.class);
}
//
// web methods
//
}
/**
* Action for a build that performs the tagging.
*
* @deprecated we now use CvsTagAction but have to keep this class around
* for old builds that have a serialized version of this class
* and use the old archive method of tagging a build
*/
@Deprecated
public final class TagAction extends AbstractScmTagAction implements Describable<TagAction> {
private String tagName;
public TagAction(final Run<?, ?> build) {
super(build);
}
@Override
public String getIconFileName() {
return null;
}
@Override
public String getDisplayName() {
return null;
}
@Override
public Descriptor<TagAction> getDescriptor() {
return null;
}
@Override
public boolean isTagged() {
return false;
}
/**
* Convert the old TagAction structure into the new (legacy) structure
*
* @return an instance of LegacyTagAction
* @throws NoSuchFieldException
* @throws IllegalAccessException
* @throws SecurityException
* @throws IllegalArgumentException
*/
public Object readResolve() throws IllegalArgumentException, SecurityException, IllegalAccessException, NoSuchFieldException {
LegacyTagAction legacyTagAction = new LegacyTagAction(super.build, CVSSCM.this);
Field tagNameField = legacyTagAction.getClass().getDeclaredField("tagName");
tagNameField.setAccessible(true);
tagNameField.set(legacyTagAction, tagName);
return legacyTagAction;
}
}
}