forked from jenkinsci/subversion-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSubversionSCMSource.java
More file actions
1106 lines (1010 loc) · 45.2 KB
/
SubversionSCMSource.java
File metadata and controls
1106 lines (1010 loc) · 45.2 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
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* The MIT License
*
* Copyright (c) 2013, CloudBees, Inc., Stephen Connolly.
*
* 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 jenkins.scm.impl.subversion;
import com.cloudbees.jenkins.plugins.sshcredentials.SSHUserPrivateKey;
import com.cloudbees.plugins.credentials.Credentials;
import com.cloudbees.plugins.credentials.CredentialsMatchers;
import com.cloudbees.plugins.credentials.CredentialsNameProvider;
import com.cloudbees.plugins.credentials.CredentialsProvider;
import com.cloudbees.plugins.credentials.common.StandardCertificateCredentials;
import com.cloudbees.plugins.credentials.common.StandardCredentials;
import com.cloudbees.plugins.credentials.common.StandardListBoxModel;
import com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials;
import com.cloudbees.plugins.credentials.domains.DomainRequirement;
import com.cloudbees.plugins.credentials.domains.URIRequirementBuilder;
import edu.umd.cs.findbugs.annotations.CheckForNull;
import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.Extension;
import hudson.Functions;
import hudson.Util;
import hudson.model.Item;
import hudson.model.TaskListener;
import hudson.scm.CredentialsSVNAuthenticationProviderImpl;
import hudson.scm.FilterSVNAuthenticationManager;
import hudson.scm.SubversionRepositoryStatus;
import hudson.scm.SubversionSCM;
import hudson.scm.subversion.SvnHelper;
import hudson.security.ACL;
import hudson.util.EditDistance;
import hudson.util.FormValidation;
import hudson.util.ListBoxModel;
import jenkins.scm.api.SCMHead;
import jenkins.scm.api.SCMHeadObserver;
import jenkins.scm.api.SCMRevision;
import jenkins.scm.api.SCMSource;
import jenkins.scm.api.SCMSourceCriteria;
import jenkins.scm.api.SCMSourceDescriptor;
import jenkins.scm.api.SCMSourceOwner;
import jenkins.scm.api.SCMSourceOwners;
import net.jcip.annotations.GuardedBy;
import org.acegisecurity.Authentication;
import org.acegisecurity.context.SecurityContextHolder;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOCase;
import org.apache.commons.lang.StringUtils;
import org.kohsuke.stapler.AncestorInPath;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.QueryParameter;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;
import org.tmatesoft.svn.core.SVNDirEntry;
import org.tmatesoft.svn.core.SVNException;
import org.tmatesoft.svn.core.SVNNodeKind;
import org.tmatesoft.svn.core.SVNURL;
import org.tmatesoft.svn.core.auth.ISVNAuthenticationManager;
import org.tmatesoft.svn.core.internal.util.SVNPathUtil;
import org.tmatesoft.svn.core.io.ISVNSession;
import org.tmatesoft.svn.core.io.SVNRepository;
import org.tmatesoft.svn.core.io.SVNRepositoryFactory;
import org.tmatesoft.svn.core.wc.SVNRevision;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;
import java.util.SortedSet;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.UUID;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import jenkins.model.Jenkins;
import jenkins.scm.api.SCMHeadEvent;
import org.kohsuke.stapler.DataBoundSetter;
import org.kohsuke.stapler.interceptor.RequirePOST;
/**
* A {@link SCMSource} for Subversion.
*
* @author Stephen Connolly
*/
public class SubversionSCMSource extends SCMSource {
public static final StringListComparator COMPARATOR = new StringListComparator();
public static final Logger LOGGER = Logger.getLogger(SubversionSCMSource.class.getName());
private final String remoteBase;
private String credentialsId = ""; // TODO null would be a better default, but need to check null safety on usages
private String includes = DescriptorImpl.DEFAULT_INCLUDES;
private String excludes = DescriptorImpl.DEFAULT_EXCLUDES;
private boolean forceScanExternals = false;
@GuardedBy("this")
private transient String uuid;
@Deprecated
public SubversionSCMSource(String id, String remoteBase, String credentialsId, String includes, String excludes) {
super(id);
this.remoteBase = StringUtils.removeEnd(remoteBase, "/") + "/";
setCredentialsId(credentialsId);
setIncludes(StringUtils.defaultIfEmpty(includes, DescriptorImpl.DEFAULT_INCLUDES));
setExcludes(StringUtils.defaultIfEmpty(excludes, DescriptorImpl.DEFAULT_EXCLUDES));
}
@DataBoundConstructor
@SuppressWarnings("unused") // by stapler
public SubversionSCMSource(String id, String remoteBase) {
super(id);
this.remoteBase = StringUtils.removeEnd(remoteBase, "/") + "/";
}
/**
* Gets the credentials id.
*
* @return the credentials id.
*/
@SuppressWarnings("unused") // by stapler
public String getCredentialsId() {
return credentialsId;
}
@DataBoundSetter
public void setCredentialsId(String credentialsId) {
this.credentialsId = credentialsId;
}
/**
* Gets the comma separated list of exclusions.
*
* @return the comma separated list of exclusions.
*/
@SuppressWarnings("unused") // by stapler
public String getExcludes() {
return excludes;
}
@DataBoundSetter
public void setExcludes(String excludes) {
this.excludes = excludes;
}
/**
* Gets the comma separated list of inclusions.
*
* @return the comma separated list of inclusions.
*/
@SuppressWarnings("unused") // by stapler
public String getIncludes() {
return includes;
}
@DataBoundSetter
public void setIncludes(String includes) {
this.includes = includes;
}
/**
* Gets the force scan for externals flag
* Workaround for not handling revision numbers of externals.
*
* @return the force scan for externals flag.
*/
@Restricted(NoExternalUse.class)
public boolean getForceScanExternals() {
return forceScanExternals;
}
@DataBoundSetter
public void setForceScanExternals(boolean forceScanExternals) {
this.forceScanExternals = forceScanExternals;
}
/**
* Gets the base SVN URL of the project.
*
* @return the base SVN URL of the project.
*/
@SuppressWarnings("unused") // by stapler
public String getRemoteBase() {
return remoteBase;
}
@CheckForNull
public synchronized String getUuid() {
if (uuid == null) {
SVNRepositoryView repository = null;
try {
SVNURL repoURL = SVNURL.parseURIEncoded(remoteBase);
repository = openSession(repoURL);
uuid = repository.getUuid();
} catch (SVNException e) {
LOGGER.log(Level.WARNING, "Could not connect to remote repository " + remoteBase + " to determine UUID",
e);
} catch (IOException e) {
LOGGER.log(Level.WARNING, "Could not connect to remote repository " + remoteBase + " to determine UUID",
e);
} finally {
closeSession(repository);
}
}
return uuid;
}
/**
* {@inheritDoc}
*/
@NonNull
@Override
protected void retrieve(@CheckForNull SCMSourceCriteria criteria,
@NonNull final SCMHeadObserver observer,
@CheckForNull SCMHeadEvent<?> event,
@NonNull TaskListener listener)
throws IOException {
SVNRepositoryView repository = null;
try {
listener.getLogger().println("Opening conection to " + remoteBase);
SVNURL repoURL = SVNURL.parseURIEncoded(remoteBase);
repository = openSession(repoURL);
String repoPath = SubversionSCM.DescriptorImpl.getRelativePath(repoURL, repository.getRepository());
List<String> prefix = Collections.emptyList();
fetch(listener,
repository,
-1,
repoPath,
toPaths(splitCludes(includes)),
prefix,
prefix,
toPaths(splitCludes(excludes)),
criteria,
observer
);
} catch (SVNException e) {
e.printStackTrace(listener.error("Could not communicate with Subversion server"));
throw new IOException(e);
} finally {
closeSession(repository);
}
}
/**
* {@inheritDoc}
*/
@Override
protected SCMRevision retrieve(@NonNull SCMHead head, @NonNull TaskListener listener)
throws IOException {
SVNRepositoryView repository = null;
try {
listener.getLogger().println("Opening connection to " + remoteBase);
SVNURL repoURL = SVNURL.parseURIEncoded(remoteBase);
repository = openSession(repoURL);
String repoPath = SubversionSCM.DescriptorImpl.getRelativePath(repoURL, repository.getRepository());
String path = SVNPathUtil.append(repoPath, head.getName());
SVNRepositoryView.NodeEntry svnEntry = repository.getNode(path, -1);
SCMRevisionImpl revImpl = new SCMRevisionImpl(head, svnEntry.getRevision());
if(forceScanExternals) {
revImpl.setDeterministic(false);
}
return revImpl;
} catch (SVNException e) {
throw new IOException(e);
} finally {
closeSession(repository);
}
}
/**
* {@inheritDoc}
*/
@Override
protected SCMRevision retrieve(String unparsedRevision, TaskListener listener) throws IOException, InterruptedException {
SVNRepositoryView repository = null;
try {
listener.getLogger().println("Opening connection to " + remoteBase);
SVNURL repoURL = SVNURL.parseURIEncoded(remoteBase);
repository = openSession(repoURL);
String repoPath = SubversionSCM.DescriptorImpl.getRelativePath(repoURL, repository.getRepository());
String base;
long revision;
Matcher pathAtRev = Pattern.compile("(.+)@(\\d+)").matcher(unparsedRevision);
if (pathAtRev.matches()) {
base = pathAtRev.group(1);
revision = Long.parseLong(pathAtRev.group(2));
} else {
base = unparsedRevision;
revision = -1;
}
String path = SVNPathUtil.append(repoPath, base);
long resolvedRevision = repository.getNode(path, -1).getRevision();
if (resolvedRevision == -1) {
listener.getLogger().println("Could not find " + path);
return null;
}
SCMRevisionImpl revImpl = new SCMRevisionImpl(new SCMHead(base), revision == -1 ? resolvedRevision : revision);
if(forceScanExternals) {
revImpl.setDeterministic(false);
}
return revImpl;
} catch (SVNException e) {
throw new IOException(e);
} finally {
closeSession(repository);
}
}
/**
* {@inheritDoc}
*/
@Override
protected Set<String> retrieveRevisions(TaskListener listener) throws IOException, InterruptedException {
// Default implementation should do what we need: normally includes tags as well as branches.
return super.retrieveRevisions(listener);
}
private static void closeSession(@CheckForNull SVNRepositoryView repository) {
if (repository != null) {
repository.close();
}
}
private SVNRepositoryView openSession(SVNURL repoURL) throws SVNException, IOException {
return new SVNRepositoryView(repoURL, credentialsId == null ? null : CredentialsMatchers
.firstOrNull(CredentialsProvider.lookupCredentials(StandardCredentials.class, getOwner(),
ACL.SYSTEM, URIRequirementBuilder.fromUri(repoURL.toString()).build()),
CredentialsMatchers.allOf(CredentialsMatchers.withId(credentialsId),
CredentialsMatchers.anyOf(CredentialsMatchers.instanceOf(StandardCredentials.class),
CredentialsMatchers.instanceOf(SSHUserPrivateKey.class)))));
}
void fetch(@NonNull TaskListener listener,
@NonNull final SVNRepositoryView repository,
long rev,
@NonNull final String repoPath,
@NonNull SortedSet<List<String>> paths,
@NonNull List<String> prefix,
@NonNull List<String> realPath,
@NonNull SortedSet<List<String>> excludedPaths,
@CheckForNull SCMSourceCriteria branchCriteria,
@NonNull SCMHeadObserver observer)
throws IOException, SVNException {
String svnPath = SVNPathUtil.append(repoPath, StringUtils.join(realPath, '/'));
assert prefix.size() == realPath.size();
assert wildcardStartsWith(realPath, prefix);
SortedMap<List<String>, SortedSet<List<String>>> includePaths = groupPaths(paths, prefix);
listener.getLogger().println("Checking directory " + svnPath + (rev > -1 ? "@" + rev : "@HEAD"));
SVNRepositoryView.NodeEntry node = repository.getNode(svnPath, rev);
if (!SVNNodeKind.DIR.equals(node.getType()) || node.getChildren() == null) {
return;
}
for (Map.Entry<List<String>, SortedSet<List<String>>> entry : includePaths.entrySet()) {
for (List<String> path : entry.getValue()) {
String name = path.get(prefix.size());
SVNRepositoryView.ChildEntry[] children = node.getChildren().clone();
Arrays.sort(children, new Comparator<SVNRepositoryView.ChildEntry>() {
public int compare(SVNRepositoryView.ChildEntry o1, SVNRepositoryView.ChildEntry o2) {
long diff = o2.getRevision() - o1.getRevision();
return diff < 0 ? -1 : diff > 0 ? 1 : 0;
}
});
for (final SVNRepositoryView.ChildEntry svnEntry : children) {
if (svnEntry.getType() == SVNNodeKind.DIR && isMatch(svnEntry.getName(), name)) {
List<String> childPrefix = copyAndAppend(prefix, name);
List<String> childRealPath = copyAndAppend(realPath, svnEntry.getName());
if (wildcardStartsWith(childRealPath, excludedPaths)) {
continue;
}
if (path.equals(childPrefix)) {
final String childPath = StringUtils.join(childRealPath, '/');
final String candidateRootPath = SVNPathUtil.append(repoPath, childPath);
final long candidateRevision = svnEntry.getRevision();
final long lastModified = svnEntry.getLastModified();
listener.getLogger().println(
"Checking candidate branch " + candidateRootPath + "@" + candidateRevision);
if (branchCriteria == null || branchCriteria.isHead(
new SCMSourceCriteria.Probe() {
@Override
public String name() {
return childPath;
}
@Override
public long lastModified() {
return lastModified;
}
@Override
public boolean exists(@NonNull String path) throws IOException {
try {
return repository.checkPath(
SVNPathUtil.append(candidateRootPath, path),
candidateRevision) != SVNNodeKind.NONE;
} catch (SVNException e) {
throw new IOException(e);
}
}
}, listener)) {
listener.getLogger().println("Met criteria");
SCMHead head = new SCMHead(childPath);
SCMRevisionImpl revImpl = new SCMRevisionImpl(head, svnEntry.getRevision());
if(forceScanExternals) {
revImpl.setDeterministic(false);
}
observer.observe(head, revImpl);
if (!observer.isObserving()) {
return;
}
} else {
listener.getLogger().println("Does not meet criteria");
}
} else {
fetch(listener, repository, svnEntry.getRevision(), repoPath, paths,
childPrefix,
childRealPath, excludedPaths, branchCriteria, observer);
}
}
}
}
}
}
/**
* Copies a list and appends some more values.
*
* @param list the list.
* @param values the values to append.
* @param <T> the type of values to append.
* @return the list.
*/
@NonNull
private static <T> List<T> copyAndAppend(@NonNull List<T> list, T... values) {
List<T> childPrefix = new ArrayList<T>(list.size() + values.length);
childPrefix.addAll(list);
childPrefix.addAll(Arrays.asList(values));
return childPrefix;
}
/**
* Groups a set of path segments based on a supplied prefix.
*
* @param pathSegments the input path segments.
* @param prefix the prefix to group on.
* @return a map, all keys will {@link #startsWith(java.util.List, java.util.List)} the input prefix and be longer
* than the input prefix, all values will {@link #startsWith(java.util.List,
* java.util.List)} their corresponding key.
*/
@NonNull
static SortedMap<List<String>, SortedSet<List<String>>> groupPaths(@NonNull SortedSet<List<String>> pathSegments,
@NonNull List<String> prefix) {
// ensure pre-condition is valid and ensure we are using a copy
pathSegments = filterPaths(pathSegments, prefix);
SortedMap<List<String>, SortedSet<List<String>>> result =
new TreeMap<List<String>, SortedSet<List<String>>>(COMPARATOR);
while (!pathSegments.isEmpty()) {
List<String> longestPrefix = null;
int longestIndex = -1;
for (List<String> pathSegment : pathSegments) {
if (longestPrefix == null) {
longestPrefix = pathSegment;
longestIndex = indexOfNextWildcard(pathSegment, prefix.size());
} else {
int index = indexOfNextWildcard(pathSegment, prefix.size());
if (index > longestIndex) {
longestPrefix = pathSegment;
longestIndex = index;
}
}
}
assert longestPrefix != null;
longestPrefix = new ArrayList<String>(longestPrefix.subList(0, longestIndex));
SortedSet<List<String>> group = filterPaths(pathSegments, longestPrefix);
result.put(longestPrefix, group);
pathSegments.removeAll(group);
}
String optimization;
while (null != (optimization = getOptimizationPoint(result.keySet(), prefix.size()))) {
List<String> optimizedPrefix = copyAndAppend(prefix, optimization);
SortedSet<List<String>> optimizedGroup = new TreeSet<List<String>>(COMPARATOR);
for (Iterator<Map.Entry<List<String>, SortedSet<List<String>>>> iterator = result.entrySet().iterator();
iterator.hasNext(); ) {
Map.Entry<List<String>, SortedSet<List<String>>> entry = iterator.next();
if (startsWith(entry.getKey(), optimizedPrefix)) {
iterator.remove();
optimizedGroup.addAll(entry.getValue());
}
}
result.put(optimizedPrefix, optimizedGroup);
}
return result;
}
/**
* Returns the string that has multiple matches and can therefore be used to optimize the set of prefixes.
*
* @param newPrefixes the proposed set of prefixes.
* @param oldPrefixSize the length of the old prefix.
* @return either a string that when appended to the old prefix will give a prefix with multiple matches in the
* supplied set of new prefixes or {@code null} if there is no such string.
*/
@CheckForNull
static String getOptimizationPoint(@NonNull Set<List<String>> newPrefixes, int oldPrefixSize) {
Set<String> set = new HashSet<String>();
for (List<String> p : newPrefixes) {
if (p.size() <= oldPrefixSize) {
continue;
}
String value = p.get(oldPrefixSize);
if (set.contains(value)) {
return value;
}
set.add(value);
}
return null;
}
/**
* Returns the index of the next wildcard segment on or after the specified start index.
*
* @param pathSegment the path segments.
* @param startIndex the first index to check.
* @return the index with a wildcard or {@code -1} if none exist.
*/
static int indexOfNextWildcard(@NonNull List<String> pathSegment, int startIndex) {
int index = startIndex;
ListIterator<String> i = pathSegment.listIterator(index);
while (i.hasNext()) {
String segment = i.next();
if (segment.indexOf('*') != -1 || segment.indexOf('?') != -1) {
break;
}
index++;
}
return index;
}
/**
* Returns {@code true} if and only if the value starts with the supplied prefix.
*
* @param value the value.
* @param prefix the candidate prefix.
* @return {@code true} if and only if the value starts with the supplied prefix.
*/
static boolean startsWith(@NonNull List<String> value, @NonNull List<String> prefix) {
if (value.size() < prefix.size()) {
return false;
}
ListIterator<String> i1 = value.listIterator();
ListIterator<String> i2 = prefix.listIterator();
while (i1.hasNext() && i2.hasNext()) {
if (!i1.next().equals(i2.next())) {
return false;
}
}
return true;
}
/**
* Returns {@code true} if and only if the value starts with the supplied prefix.
*
* @param value the value.
* @param wildcardPrefix the candidate prefix.
* @return {@code true} if and only if the value starts with the supplied prefix.
*/
static boolean wildcardStartsWith(@NonNull List<String> value, @NonNull List<String> wildcardPrefix) {
if (value.size() < wildcardPrefix.size()) {
return false;
}
ListIterator<String> i1 = value.listIterator();
ListIterator<String> i2 = wildcardPrefix.listIterator();
while (i1.hasNext() && i2.hasNext()) {
if (!isMatch(i1.next(), i2.next())) {
return false;
}
}
return true;
}
/**
* Returns {@code true} if and only if the value starts with any of the supplied prefixes.
*
* @param value the value.
* @param wildcardPrefixes the candidate prefixes.
* @return {@code true} if and only if the value starts with any of the supplied prefixes.
*/
static boolean wildcardStartsWith(@NonNull List<String> value, @NonNull Collection<List<String>> wildcardPrefixes) {
for (List<String> wildcardPrefix : wildcardPrefixes) {
if (wildcardStartsWith(value, wildcardPrefix)) {
return true;
}
}
return false;
}
/**
* Filters the set of path segments, retaining only those that start with the supplied prefix.
*
* @param pathSegments the set of path segments.
* @param prefix the prefix.
* @return a new set of path segments.
*/
@NonNull
static SortedSet<List<String>> filterPaths(@NonNull SortedSet<List<String>> pathSegments,
@NonNull List<String> prefix) {
SortedSet<List<String>> result = new TreeSet<List<String>>(COMPARATOR);
for (List<String> pathSegment : pathSegments) {
if (startsWith(pathSegment, prefix)) {
result.add(pathSegment);
}
}
return result;
}
/**
* Splits a set of path strings into a set of lists of path segments.
*
* @param pathStrings the set of path strings.
* @return the set of lists of path segments.
*/
@NonNull
static SortedSet<List<String>> toPaths(@NonNull SortedSet<String> pathStrings) {
SortedSet<List<String>> result = new TreeSet<List<String>>(COMPARATOR);
for (String clude : pathStrings) {
result.add(Arrays.asList(clude.split("/")));
}
return result;
}
/**
* Split a comma separated set of includes/excludes into a set of strings.
*
* @param cludes a comma separated set of includes/excludes.
* @return a set of strings.
*/
@NonNull
static SortedSet<String> splitCludes(@CheckForNull String cludes) {
TreeSet<String> result = new TreeSet<String>();
StringTokenizer tokenizer = new StringTokenizer(StringUtils.defaultString(cludes), ",");
while (tokenizer.hasMoreTokens()) {
String clude = tokenizer.nextToken().trim();
if (StringUtils.isNotEmpty(clude)) {
result.add(clude.trim());
}
}
return result;
}
/**
* Checks if we have a match against a wildcard matcher.
*
* @param value the value
* @param wildcareMatcher the wildcard matcher
* @return {@code true} if and only if the value matches the wildcard matcher.
*/
static boolean isMatch(@NonNull String value, @NonNull String wildcareMatcher) {
return FilenameUtils.wildcardMatch(value, wildcareMatcher, IOCase.SENSITIVE);
}
/**
* {@inheritDoc}
*/
@NonNull
@Override
public SubversionSCM build(@NonNull SCMHead head,
@CheckForNull SCMRevision revision) {
if (revision != null && !head.equals(revision.getHead())) {
revision = null;
}
if (revision != null && !(revision instanceof SCMRevisionImpl)) {
revision = null;
}
StringBuilder remote = new StringBuilder(remoteBase);
if (!remoteBase.endsWith("/")) {
remote.append('/');
}
remote.append(head.getName());
if (revision != null) {
remote.append('@').append(((SCMRevisionImpl) revision).getRevision());
} else if (remote.indexOf("@") != -1) {
// name contains an @ so need to ensure there is an @ at the end of the name
remote.append('@');
}
return new SubversionSCM(remote.toString(), credentialsId, ".");
}
/**
* Our implementation.
*/
public static class SCMRevisionImpl extends SCMRevision {
/**
* The subversion revision.
*/
private long revision;
/**
* Whether or not the revision number is deterministic.
* Workaround for not handling revision numbers of externals.
*/
private boolean deterministic;
public SCMRevisionImpl(SCMHead head, long revision) {
super(head);
this.revision = revision;
deterministic = true;
}
public long getRevision() {
return revision;
}
public void setDeterministic(boolean deterministic) {
this.deterministic = deterministic;
}
public boolean isDeterministic() {
return deterministic;
}
/**
* {@inheritDoc}
*/
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
SCMRevisionImpl that = (SCMRevisionImpl) o;
return revision == that.revision && getHead().equals(that.getHead());
}
/**
* {@inheritDoc}
*/
@Override
public int hashCode() {
return (int) (revision ^ (revision >>> 32));
}
@Override
public String toString() {
return Long.toString(revision);
}
}
static class StringListComparator implements Comparator<List<String>> {
public int compare(List<String> o1, List<String> o2) {
ListIterator<String> e1 = o1.listIterator();
ListIterator<String> e2 = o2.listIterator();
while (e1.hasNext() && e2.hasNext()) {
String s1 = e1.next();
String s2 = e2.next();
int rv = s1.compareTo(s2);
if (rv != 0) {
return rv;
}
}
if (e1.hasNext()) {
return -1;
}
if (e2.hasNext()) {
return 1;
}
return 0;
}
}
@Extension
@SuppressWarnings("unused") // by jenkins
public static class DescriptorImpl extends SCMSourceDescriptor {
public static final String DEFAULT_INCLUDES = "trunk,branches/*,tags/*,sandbox/*";
public static final String DEFAULT_EXCLUDES = "";
static final Pattern URL_PATTERN = Pattern.compile("(https?|svn(\\+[a-z0-9]+)?|file)://.+");
/**
* {@inheritDoc}
*/
@Override
public String getDisplayName() {
return Messages.SubversionSCMSource_DisplayName();
}
/**
* Stapler helper method.
*
* @param context the context.
* @param remoteBase the remote base.
* @return list box model.
*/
@SuppressWarnings("unused") // by stapler
public ListBoxModel doFillCredentialsIdItems(@AncestorInPath SCMSourceOwner context,
@QueryParameter String remoteBase,
@QueryParameter String credentialsId) {
if (context == null && !Jenkins.getActiveInstance().hasPermission(Jenkins.ADMINISTER) ||
context != null && !context.hasPermission(Item.EXTENDED_READ)) {
return new StandardListBoxModel().includeCurrentValue(credentialsId);
}
List<DomainRequirement> domainRequirements;
domainRequirements = URIRequirementBuilder.fromUri(remoteBase.trim()).build();
return new StandardListBoxModel()
// TODO JENKINS-35553 update to newer APIs
.withEmptySelection()
.withMatching(
CredentialsMatchers.anyOf(
CredentialsMatchers.instanceOf(StandardUsernamePasswordCredentials.class),
CredentialsMatchers.instanceOf(StandardCertificateCredentials.class),
CredentialsMatchers.instanceOf(SSHUserPrivateKey.class)
),
CredentialsProvider.lookupCredentials(StandardCredentials.class,
context,
ACL.SYSTEM,
domainRequirements)
);
}
/**
* validate the value for a remote (repository) location.
*/
@RequirePOST
public FormValidation doCheckCredentialsId(StaplerRequest req, @AncestorInPath SCMSourceOwner context, @QueryParameter String remoteBase, @QueryParameter String value) {
// TODO suspiciously similar to SubversionSCM.ModuleLocation.DescriptorImpl.checkCredentialsId; refactor into shared method?
// Test the connection only if we may use the credentials
if (context == null && !Jenkins.getActiveInstance().hasPermission(Jenkins.ADMINISTER) ||
context != null && !context.hasPermission(CredentialsProvider.USE_ITEM)) {
return FormValidation.ok();
}
// if check remote is reporting an issue then we don't need to
String url = Util.fixEmptyAndTrim(remoteBase);
if (url == null)
return FormValidation.ok();
if(!URL_PATTERN.matcher(url).matches())
return FormValidation.ok();
try {
String urlWithoutRevision = SvnHelper.getUrlWithoutRevision(url);
SVNURL repoURL = SVNURL.parseURIDecoded(urlWithoutRevision);
StandardCredentials credentials = value == null ? null :
CredentialsMatchers.firstOrNull(CredentialsProvider
.lookupCredentials(StandardCredentials.class, context, ACL.SYSTEM,
URIRequirementBuilder.fromUri(repoURL.toString()).build()),
CredentialsMatchers.withId(value));
if (checkRepositoryPath(context, repoURL, credentials)!=SVNNodeKind.NONE) {
// something exists; now check revision if any
SVNRevision revision = getRevisionFromRemoteUrl(url);
if (revision != null && !revision.isValid()) {
return FormValidation.errorWithMarkup(
hudson.scm.subversion.Messages.SubversionSCM_doCheckRemote_invalidRevision());
}
return FormValidation.ok();
}
SVNRepository repository = null;
try {
repository = getRepository(context, repoURL, credentials,
Collections.<String, Credentials>emptyMap(), null);
long rev = repository.getLatestRevision();
// now go back the tree and find if there's anything that exists
String repoPath = getRelativePath(repoURL, repository);
String p = repoPath;
while(p.length()>0) {
p = SVNPathUtil.removeTail(p);
if(repository.checkPath(p,rev)==SVNNodeKind.DIR) {
// found a matching path
List<SVNDirEntry> entries = new ArrayList<SVNDirEntry>();
repository.getDir(p,rev,false,entries);
// build up the name list
List<String> paths = new ArrayList<String>();
for (SVNDirEntry e : entries)
if(e.getKind()==SVNNodeKind.DIR)
paths.add(e.getName());
String head = SVNPathUtil.head(repoPath.substring(p.length() + 1));
String candidate = EditDistance.findNearest(head, paths);
return FormValidation.error(
hudson.scm.subversion.Messages.SubversionSCM_doCheckRemote_badPathSuggest(p, head,
candidate != null ? "/" + candidate : ""));
}
}
return FormValidation.error(
hudson.scm.subversion.Messages.SubversionSCM_doCheckRemote_badPath(repoPath));
} finally {
if (repository != null)
repository.closeSession();
}
} catch (SVNException e) {
LOGGER.log(Level.INFO, "Failed to access subversion repository "+url,e);
String message = hudson.scm.subversion.Messages.SubversionSCM_doCheckRemote_exceptionMsg1(
Util.escape(url), Util.escape(e.getErrorMessage().getFullMessage()),
"javascript:document.getElementById('svnerror').style.display='block';"
+ "document.getElementById('svnerrorlink').style.display='none';"
+ "return false;")
+ "<br/><pre id=\"svnerror\" style=\"display:none\">"
+ Functions.printThrowable(e) + "</pre>";
return FormValidation.errorWithMarkup(message);
}
}
public SVNNodeKind checkRepositoryPath(SCMSourceOwner context, SVNURL repoURL, StandardCredentials credentials) throws SVNException {
SVNRepository repository = null;
try {
repository = getRepository(context,repoURL,credentials, Collections.<String, Credentials>emptyMap(), null);
repository.testConnection();
long rev = repository.getLatestRevision();
String repoPath = getRelativePath(repoURL, repository);
return repository.checkPath(repoPath, rev);
} catch (SVNException e) {
if (LOGGER.isLoggable(Level.FINE)) {
LogRecord lr = new LogRecord(Level.FINE,
"Could not check repository path {0} using credentials {1} ({2})");
lr.setThrown(e);
lr.setParameters(new Object[]{
repoURL,
credentials == null ? null : CredentialsNameProvider.name(credentials),
credentials
});
LOGGER.log(lr);
}
throw e;
} finally {
if (repository != null)
repository.closeSession();
}
}
protected SVNRepository getRepository(SCMSourceOwner context, SVNURL repoURL, StandardCredentials credentials,
Map<String, Credentials> additionalCredentials, ISVNSession session) throws SVNException {
SVNRepository repository = SVNRepositoryFactory.create(repoURL, session);
ISVNAuthenticationManager sam = SubversionSCM.createSvnAuthenticationManager(
new CredentialsSVNAuthenticationProviderImpl(credentials, additionalCredentials, /* TODO */ TaskListener.NULL)
);
sam = new FilterSVNAuthenticationManager(sam) {
// If there's no time out, the blocking read operation may hang forever, because TCP itself
// has no timeout. So always use some time out. If the underlying implementation gives us some
// value (which may come from ~/.subversion), honor that, as long as it sets some timeout value.
@Override
public int getReadTimeout(SVNRepository repository) {
int r = super.getReadTimeout(repository);