forked from bklarson/Jenkins-repo-plugin
-
Notifications
You must be signed in to change notification settings - Fork 84
/
RepoScm.java
1389 lines (1266 loc) · 40.1 KB
/
RepoScm.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
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) 2010, Brad Larson
*
* 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.plugins.repo;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.io.Serializable;
import java.net.URL;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.InvalidPathException;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import hudson.AbortException;
import hudson.EnvVars;
import hudson.Extension;
import hudson.FilePath;
import hudson.Launcher;
import hudson.Util;
import hudson.model.Job;
import hudson.model.ParameterDefinition;
import hudson.model.ParametersDefinitionProperty;
import hudson.model.Run;
import hudson.model.StringParameterDefinition;
import hudson.model.TaskListener;
import hudson.scm.ChangeLogParser;
import hudson.scm.PollingResult;
import hudson.scm.SCM;
import hudson.scm.SCMDescriptor;
import hudson.scm.SCMRevisionState;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;
import org.kohsuke.stapler.QueryParameter;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.export.Exported;
import org.kohsuke.stapler.export.ExportedBean;
import hudson.scm.PollingResult.Change;
import hudson.util.FormValidation;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
/**
* The main entrypoint of the plugin. This class contains code to store user
* configuration and to check out the code using a repo binary.
*/
@ExportedBean
public class RepoScm extends SCM implements Serializable {
private static Logger debug = Logger
.getLogger("hudson.plugins.repo.RepoScm");
/**
* escape hatch name.
*/
static final String ALLOW_LOCAL_CHECKOUT_PROPERTY =
RepoScm.class.getName() + ".ALLOW_LOCAL_CHECKOUT";
/**
* escape hatch.
*/
//CS IGNORE VisibilityModifier FOR NEXT 1 LINES. REASON: escape hatch property.
static /* not final */ boolean ALLOW_LOCAL_CHECKOUT = Boolean.parseBoolean(System.getProperty(ALLOW_LOCAL_CHECKOUT_PROPERTY, "false"));
private final String manifestRepositoryUrl;
// Advanced Fields:
@CheckForNull private String manifestFile;
@CheckForNull private String manifestGroup;
@CheckForNull private String manifestPlatform;
@CheckForNull private String repoUrl;
@CheckForNull private String repoBranch;
@CheckForNull private String mirrorDir;
@CheckForNull private String manifestBranch;
@CheckForNull private int jobs;
@CheckForNull private int depth;
@CheckForNull private String localManifest;
@CheckForNull private String destinationDir;
@CheckForNull private boolean currentBranch;
@CheckForNull private boolean resetFirst;
@CheckForNull private boolean cleanFirst;
@CheckForNull private boolean quiet;
@CheckForNull private boolean forceSync;
@CheckForNull private boolean trace;
@CheckForNull private boolean showAllChanges;
@CheckForNull private boolean noTags;
@CheckForNull private boolean manifestSubmodules;
@CheckForNull private boolean fetchSubmodules;
@CheckForNull private Set<String> ignoreProjects;
@CheckForNull private EnvVars extraEnvVars;
@CheckForNull private boolean noCloneBundle;
@CheckForNull private boolean worktree;
/**
* Returns the manifest repository URL.
*/
@Exported
public String getManifestRepositoryUrl() {
return manifestRepositoryUrl;
}
/**
* Returns the manifest branch name. By default, this is null and repo
* defaults to "master".
*/
@Exported
public String getManifestBranch() {
return manifestBranch;
}
/**
* Merge the provided environment with the <em>default</em> values of
* the project parameters. The values from the provided environment
* take precedence.
* @param environment an existing environment, which contains already
* properties from the current build
* @param project the project that is being built
*/
private EnvVars getEnvVars(final EnvVars environment,
final Job<?, ?> project) {
// create an empty vars map
final EnvVars finalEnv = new EnvVars();
final ParametersDefinitionProperty params = project.getProperty(
ParametersDefinitionProperty.class);
if (params != null) {
for (ParameterDefinition param
: params.getParameterDefinitions()) {
if (param instanceof StringParameterDefinition) {
final StringParameterDefinition stpd =
(StringParameterDefinition) param;
final String dflt = stpd.getDefaultValue();
if (dflt != null) {
finalEnv.put(param.getName(), dflt);
}
}
}
}
// now merge the settings from the last build environment
if (environment != null) {
finalEnv.overrideAll(environment);
}
// merge extra env vars, if specified
if (extraEnvVars != null) {
finalEnv.overrideAll(extraEnvVars);
}
EnvVars.resolve(finalEnv);
return finalEnv;
}
/**
* Returns the initial manifest file name. By default, this is null and repo
* defaults to "default.xml"
*/
@Exported
public String getManifestFile() {
return manifestFile;
}
/**
* Returns the group of projects to fetch. By default, this is null and
* repo will fetch the default group.
*/
@Exported
public String getManifestGroup() {
return manifestGroup;
}
/**
* Returns the platform of projects to fetch. By default, this is null and
* repo will automatically fetch the appropriate platform.
*/
@CheckForNull
public String getManifestPlatform() {
return manifestPlatform;
}
/**
* Returns the repo url. by default, this is null and
* repo is fetched from aosp
*/
@Exported
public String getRepoUrl() {
return repoUrl;
}
/**
* Returns the repo branch. by default, this is null and
* repo is used from the default branch
*/
@Exported
public String getRepoBranch() {
return repoBranch;
}
/**
* Returns the name of the mirror directory. By default, this is null and
* repo does not use a mirror.
*/
@Exported
public String getMirrorDir() {
return mirrorDir;
}
/**
* Returns the number of jobs used for sync. By default, this is null and
* repo does not use concurrent jobs.
*/
@Exported
public int getJobs() {
return jobs;
}
/**
* Returns the depth used for sync. By default, this is null and repo
* will sync the entire history.
*/
@Exported
public int getDepth() {
return depth;
}
/**
* Returns the contents of the local_manifests/local.xml. By default, this is null
* and a local_manifests/local.xml is neither created nor modified.
*/
@Exported
public String getLocalManifest() {
return localManifest;
}
/**
* Returns the destination directory. By default, this is null and the
* source is synced to the root of the workspace.
*/
@Exported
public String getDestinationDir() {
return destinationDir;
}
/**
* returns list of ignore projects.
*/
@Exported
public String getIgnoreProjects() {
return StringUtils.join(ignoreProjects, '\n');
}
/**
* Returns the value of currentBranch.
*/
@Exported
public boolean isCurrentBranch() {
return currentBranch;
}
/**
* Returns the value of resetFirst.
*/
@Exported
public boolean isResetFirst() {
return resetFirst;
}
/**
* Returns the value of cleanFirst.
*/
@Exported
public boolean isCleanFirst() {
return cleanFirst;
}
/**
* Returns the value of showAllChanges.
*/
@Exported
public boolean isShowAllChanges() {
return showAllChanges;
}
/**
* Returns the value of quiet.
*/
@Exported
public boolean isQuiet() {
return quiet;
}
/**
* Returns the value of forceSync.
*/
@Exported
public boolean isForceSync() {
return forceSync;
}
/**
* Returns the value of trace.
*/
@Exported
public boolean isTrace() {
return trace;
}
/**
* Returns the value of noTags.
*/
@Exported
public boolean isNoTags() {
return noTags;
}
/**
* Returns the value of noCloneBundle.
*/
@Exported
public boolean isNoCloneBundle() {
return noCloneBundle;
}
/**
* Returns the value of isWorktree.
*/
@Exported
public boolean isWorktree() {
return worktree;
}
/**
* Returns the value of manifestSubmodules.
*/
@Exported
public boolean isManifestSubmodules() {
return manifestSubmodules;
}
/**
* Returns the value of fetchSubmodules.
*/
public boolean isFetchSubmodules() {
return fetchSubmodules;
}
/**
* Returns the value of extraEnvVars.
*/
@Exported
public Map<String, String> getExtraEnvVars() {
return extraEnvVars;
}
/**
* The constructor takes in user parameters and sets them. Each job using
* the RepoSCM will call this constructor.
*
* @param manifestRepositoryUrl The URL for the manifest repository.
* @param manifestBranch The branch of the manifest repository. Typically this is null
* or the empty string, which will cause repo to default to
* "master".
* @param manifestFile The file to use as the repository manifest. Typically this is
* null which will cause repo to use the default of "default.xml"
* @param manifestGroup The group name for the projects that need to be fetched.
* Typically, this is null and all projects tagged 'default' will
* be fetched.
* @param mirrorDir The path of the mirror directory to reference when
* initializing repo.
* @param jobs The number of concurrent jobs to use for the sync command. If
* this is 0 or negative the jobs parameter is not specified.
* @param depth This is the depth to use when syncing. By default this is 0
* and the full history is synced.
* @param localManifest May be null, a string containing XML, or an URL.
* If XML, this string is written to
* .repo/local_manifests/local.xml
* If an URL, the URL is fetched and the content is written
* to .repo/local_manifests/local.xml
* @param destinationDir If not null then the source is synced to the destinationDir
* subdirectory of the workspace.
* @param repoUrl If not null then use this url as repo base,
* instead of the default.
* @param currentBranch If this value is true, add the "-c" option when executing
* "repo sync".
* @param resetFirst If this value is true, do "repo forall -c 'git reset --hard'"
* before syncing.
* @param quiet If this value is true, add the "-q" option when executing
* "repo sync".
* @param trace If this value is true, add the "--trace" option when
* executing "repo init" and "repo sync".
* @param showAllChanges If this value is true, add the "--first-parent" option to
* "git log" when determining changesets.
*
*/
@Deprecated
public RepoScm(final String manifestRepositoryUrl,
final String manifestBranch, final String manifestFile,
final String manifestGroup, final String mirrorDir, final int jobs,
final int depth,
final String localManifest, final String destinationDir,
final String repoUrl,
final boolean currentBranch,
final boolean resetFirst,
final boolean quiet,
final boolean trace,
final boolean showAllChanges) {
this(manifestRepositoryUrl);
setManifestBranch(manifestBranch);
setManifestGroup(manifestGroup);
setManifestFile(manifestFile);
setMirrorDir(mirrorDir);
setJobs(jobs);
setDepth(depth);
setLocalManifest(localManifest);
setDestinationDir(destinationDir);
setCurrentBranch(currentBranch);
setResetFirst(resetFirst);
setCleanFirst(false);
setQuiet(quiet);
setTrace(trace);
setShowAllChanges(showAllChanges);
setRepoUrl(repoUrl);
ignoreProjects = Collections.<String>emptySet();
setWorktree(false);
}
/**
* The constructor takes in user parameters and sets them. Each job using
* the RepoSCM will call this constructor.
*
* @param manifestRepositoryUrl The URL for the manifest repository.
*/
@DataBoundConstructor
public RepoScm(final String manifestRepositoryUrl) {
this.manifestRepositoryUrl = manifestRepositoryUrl;
manifestFile = null;
manifestGroup = null;
repoUrl = null;
repoBranch = null;
mirrorDir = null;
manifestBranch = null;
jobs = 0;
depth = 0;
localManifest = null;
destinationDir = null;
currentBranch = false;
resetFirst = false;
cleanFirst = false;
quiet = false;
forceSync = false;
trace = false;
showAllChanges = false;
noTags = false;
manifestSubmodules = false;
fetchSubmodules = false;
ignoreProjects = Collections.<String>emptySet();
noCloneBundle = false;
worktree = false;
}
/**
* Set the manifest branch name.
*
* @param manifestBranch
* The branch of the manifest repository. Typically this is null
* or the empty string, which will cause repo to default to
* "master".
*/
@DataBoundSetter
public void setManifestBranch(@CheckForNull final String manifestBranch) {
this.manifestBranch = Util.fixEmptyAndTrim(manifestBranch);
}
/**
* Set the initial manifest file name.
*
* @param manifestFile
* The file to use as the repository manifest. Typically this is
* null which will cause repo to use the default of "default.xml"
*/
@DataBoundSetter
public void setManifestFile(@CheckForNull final String manifestFile) {
this.manifestFile = Util.fixEmptyAndTrim(manifestFile);
}
/**
* Set the group of projects to fetch.
*
* @param manifestGroup
* The group name for the projects that need to be fetched.
* Typically, this is null and all projects tagged 'default' will
* be fetched.
*/
@DataBoundSetter
public void setManifestGroup(@CheckForNull final String manifestGroup) {
this.manifestGroup = Util.fixEmptyAndTrim(manifestGroup);
}
/**
* Set the platform of projects to fetch.
*
* @param manifestPlatform
* The platform for the projects that need to be fetched.
* Typically, this is null and only projects for the current platform
* will be fetched.
*/
@DataBoundSetter
public void setManifestPlatform(@CheckForNull final String manifestPlatform) {
this.manifestPlatform = Util.fixEmptyAndTrim(manifestPlatform);
}
/**
* Set the name of the mirror directory.
*
* @param mirrorDir
* The path of the mirror directory to reference when
* initializing repo.
*/
@DataBoundSetter
public void setMirrorDir(@CheckForNull final String mirrorDir) {
this.mirrorDir = Util.fixEmptyAndTrim(mirrorDir);
}
/**
* Set the number of jobs used for sync.
*
* @param jobs
* The number of concurrent jobs to use for the sync command. If
* this is 0 or negative the jobs parameter is not specified.
*/
@DataBoundSetter
public void setJobs(final int jobs) {
this.jobs = jobs;
}
/**
* Set the depth used for sync.
*
* @param depth
* This is the depth to use when syncing. By default this is 0
* and the full history is synced.
*/
@DataBoundSetter
public void setDepth(final int depth) {
this.depth = depth;
}
/**
* Set the content of the local manifest.
*
* @param localManifest
* May be null, a string containing XML, or an URL.
* If XML, this string is written to .repo/local_manifests/local.xml
* If an URL, the URL is fetched and the content is written
* to .repo/local_manifests/local.xml
*/
@DataBoundSetter
public void setLocalManifest(@CheckForNull final String localManifest) {
this.localManifest = Util.fixEmptyAndTrim(localManifest);
}
/**
* Set the destination directory.
*
* @param destinationDir
* If not null then the source is synced to the destinationDir
* subdirectory of the workspace.
*/
@DataBoundSetter
public void setDestinationDir(@CheckForNull final String destinationDir) {
this.destinationDir = Util.fixEmptyAndTrim(destinationDir);
}
/**
* Set currentBranch.
*
* @param currentBranch
* If this value is true, add the "-c" option when executing
* "repo sync".
*/
@DataBoundSetter
public void setCurrentBranch(final boolean currentBranch) {
this.currentBranch = currentBranch;
}
/**
* Set resetFirst.
*
* @param resetFirst
* If this value is true, do "repo forall -c 'git reset --hard'"
* before syncing.
*/
@DataBoundSetter
public void setResetFirst(final boolean resetFirst) {
this.resetFirst = resetFirst;
}
/**
* Set cleanFirst.
*
* @param cleanFirst
* If this value is true, do "repo forall -c 'git clean -fdx'"
* before syncing.
*/
@DataBoundSetter
public void setCleanFirst(final boolean cleanFirst) {
this.cleanFirst = cleanFirst;
}
/**
* Set quiet.
*
* @param quiet
* * If this value is true, add the "-q" option when executing
* "repo sync".
*/
@DataBoundSetter
public void setQuiet(final boolean quiet) {
this.quiet = quiet;
}
/**
* Set trace.
*
* @param trace
* If this value is true, add the "--trace" option when
* executing "repo init" and "repo sync".
*/
@DataBoundSetter
public void setTrace(final boolean trace) {
this.trace = trace;
}
/**
* Set showAllChanges.
*
* @param showAllChanges
* If this value is true, add the "--first-parent" option to
* "git log" when determining changesets.
*/
@DataBoundSetter
public void setShowAllChanges(final boolean showAllChanges) {
this.showAllChanges = showAllChanges;
}
/**
* Set noCloneBundle.
*
* @param noCloneBundle
* If this value is true, add the "--no-clone-bundle" option when
* running the "repo init" and "repo sync" commands.
*/
@DataBoundSetter
public void setNoCloneBundle(final boolean noCloneBundle) {
this.noCloneBundle = noCloneBundle;
}
/**
* Set worktree.
*
* @param worktree
* If this value is true, add the "--worktree" option when
* running the "repo init" command.
*/
@DataBoundSetter
public void setWorktree(final boolean worktree) {
this.worktree = worktree;
}
/**
* Set the repo url.
*
* @param repoUrl
* If not null then use this url as repo base,
* instead of the default
*/
@DataBoundSetter
public void setRepoUrl(@CheckForNull final String repoUrl) {
this.repoUrl = Util.fixEmptyAndTrim(repoUrl);
}
/**
* Set the repo branch.
*
* @param repoBranch
* If not null then use this as branch for repo itself
* instead of the default.
*/
@DataBoundSetter
public void setRepoBranch(@CheckForNull final String repoBranch) {
this.repoBranch = Util.fixEmptyAndTrim(repoBranch);
}
/**
* Enables --force-sync option on repo sync command.
* @param forceSync
* If this value is true, add the "--force-sync" option when
* executing "repo sync".
*/
@DataBoundSetter
public void setForceSync(final boolean forceSync) {
this.forceSync = forceSync;
}
/**
* Set noTags.
*
* @param noTags
* If this value is true, add the "--no-tags" option when
* executing "repo sync".
*/
@DataBoundSetter
public final void setNoTags(final boolean noTags) {
this.noTags = noTags;
}
/**
* Set manifestSubmodules.
*
* @param manifestSubmodules
* If this value is true, add the "--submodules" option when
* executing "repo init".
*/
@DataBoundSetter
public void setManifestSubmodules(final boolean manifestSubmodules) {
this.manifestSubmodules = manifestSubmodules;
}
/**
* Set fetchSubmodules.
*
* @param fetchSubmodules
* If this value is true, add the "--fetch-submodules" option when
* executing "repo sync".
*/
@DataBoundSetter
public void setFetchSubmodules(final boolean fetchSubmodules) {
this.fetchSubmodules = fetchSubmodules;
}
/**
* Sets list of projects which changes will be ignored when
* calculating whether job needs to be rebuild. This field corresponds
* to serverpath i.e. "name" section of the manifest.
* @param ignoreProjects
* String representing project names separated by " ".
*/
@DataBoundSetter
public final void setIgnoreProjects(final String ignoreProjects) {
if (ignoreProjects == null) {
this.ignoreProjects = Collections.<String>emptySet();
return;
}
this.ignoreProjects = new LinkedHashSet<String>(
Arrays.asList(ignoreProjects.split("\\s+")));
}
/**
* Set additional environment variables to use. These variables will override
* any parameter from the project or variable set in environment already.
* @param extraEnvVars
* Additional environment variables to set.
*/
@DataBoundSetter
public void setExtraEnvVars(@CheckForNull final Map<String, String> extraEnvVars) {
this.extraEnvVars = extraEnvVars != null ? new EnvVars(extraEnvVars) : null;
}
@Override
public SCMRevisionState calcRevisionsFromBuild(
@Nonnull final Run<?, ?> build, @Nullable final FilePath workspace,
@Nullable final Launcher launcher, @Nonnull final TaskListener listener
) throws IOException, InterruptedException {
// We add our SCMRevisionState from within checkout, so this shouldn't
// be called often. However it will be called if this is the first
// build, if a build was aborted before it reported the repository
// state, etc.
return SCMRevisionState.NONE;
}
private boolean shouldIgnoreChanges(final RevisionState current, final RevisionState baseline) {
List<ProjectState> changedProjects = current.whatChanged(baseline);
if ((changedProjects == null) || (ignoreProjects == null)) {
return false;
}
if (ignoreProjects.isEmpty()) {
return false;
}
// Check for every changed item if it is not contained in the
// ignored setting .. project must be rebuilt
for (ProjectState changed : changedProjects) {
if (!ignoreProjects.contains(changed.getServerPath())) {
return false;
}
}
return true;
}
@Override
public PollingResult compareRemoteRevisionWith(
@Nonnull final Job<?, ?> job, @Nullable final Launcher launcher,
@Nullable final FilePath workspace, @Nonnull final TaskListener listener,
@Nonnull final SCMRevisionState baseline) throws IOException,
InterruptedException {
SCMRevisionState myBaseline = baseline;
final EnvVars env = getEnvVars(null, job);
final String expandedManifestUrl = env.expand(manifestRepositoryUrl);
final String expandedManifestBranch = env.expand(manifestBranch);
final String expandedManifestFile = env.expand(manifestFile);
final Run<?, ?> lastRun = job.getLastBuild();
if (myBaseline == SCMRevisionState.NONE) {
// Probably the first build, or possibly an aborted build.
myBaseline = getLastState(lastRun, expandedManifestUrl,
expandedManifestBranch, expandedManifestFile);
if (myBaseline == SCMRevisionState.NONE) {
return PollingResult.BUILD_NOW;
}
}
FilePath repoDir;
if (destinationDir != null) {
repoDir = workspace.child(env.expand(destinationDir));
} else {
repoDir = workspace;
}
if (!repoDir.isDirectory()) {
repoDir.mkdirs();
}
if (!checkoutCode(launcher, repoDir, env, listener.getLogger())) {
// Some error occurred, try a build now so it gets logged.
return new PollingResult(myBaseline, myBaseline,
Change.INCOMPARABLE);
}
final RevisionState currentState = new RevisionState(
getStaticManifest(launcher, repoDir, listener.getLogger(), env),
getManifestRevision(launcher, repoDir, listener.getLogger(), env),
expandedManifestUrl, expandedManifestBranch, expandedManifestFile,
listener.getLogger());
final Change change;
if (currentState.equals(myBaseline)) {
change = Change.NONE;
} else {
if (shouldIgnoreChanges(currentState,
myBaseline instanceof RevisionState ? (RevisionState) myBaseline : null)) {
change = Change.NONE;
} else {
change = Change.SIGNIFICANT;
}
}
return new PollingResult(myBaseline, currentState, change);
}
@Override
public void checkout(
@Nonnull final Run<?, ?> build, @Nonnull final Launcher launcher,
@Nonnull final FilePath workspace, @Nonnull final TaskListener listener,
@CheckForNull final File changelogFile, @CheckForNull final SCMRevisionState baseline)
throws IOException, InterruptedException {
if (!ALLOW_LOCAL_CHECKOUT && !workspace.isRemote()) {
abortIfUrlLocal();
}
Job<?, ?> job = build.getParent();
EnvVars env = build.getEnvironment(listener);
env = getEnvVars(env, job);
FilePath repoDir;
if (destinationDir != null) {
repoDir = workspace.child(env.expand(destinationDir));
} else {
repoDir = workspace;
}
if (!repoDir.isDirectory()) {
repoDir.mkdirs();
}
if (!checkoutCode(launcher, repoDir, env, listener.getLogger())) {
throw new IOException("Could not checkout");
}
final String manifest =
getStaticManifest(launcher, repoDir, listener.getLogger(), env);
final String manifestRevision =
getManifestRevision(launcher, repoDir, listener.getLogger(), env);
final String expandedUrl = env.expand(manifestRepositoryUrl);
final String expandedBranch = env.expand(manifestBranch);
final String expandedFile = env.expand(manifestFile);
final RevisionState currentState =
new RevisionState(manifest, manifestRevision, expandedUrl,
expandedBranch, expandedFile, listener.getLogger());
build.addAction(currentState);
final Run previousBuild = build.getPreviousBuild();
final SCMRevisionState previousState =
getLastState(previousBuild, expandedUrl, expandedBranch, expandedFile);
if (changelogFile != null) {
ChangeLog.saveChangeLog(
currentState,
previousState == SCMRevisionState.NONE ? null : (RevisionState) previousState,
changelogFile,
launcher,
repoDir,
showAllChanges);
}
// TODO: create a single action displaying all manifests?
ManifestAction manifestAction = new ManifestAction(build);
int revisionStateCount = build.getActions(RevisionState.class).size();
manifestAction.setIndex(revisionStateCount);
build.addAction(manifestAction);
}
/**
* Throws an {@link AbortException} if {@link #manifestRepositoryUrl} references a local file.
*
* @throws AbortException if {@link #manifestRepositoryUrl} references a local file
*/
void abortIfUrlLocal() throws AbortException {
if (!isValidRepositoryUrl(manifestRepositoryUrl)) {
throw new AbortException("Checkout of Repo url '" + manifestRepositoryUrl
+ "' aborted because it references a local directory, "
+ "which may be insecure. "
+ "You can allow local checkouts anyway by setting the system property '"
+ ALLOW_LOCAL_CHECKOUT_PROPERTY + "' to true.");
}
}
private static boolean isValidRepositoryUrl(final String url) {
if (StringUtils.isEmpty(url)) {
return true;
} else if (url.toLowerCase(Locale.ENGLISH).startsWith("file://")) {
return false;
}
try {
// Check for local URLs with no protocol like /path/to/repo
return !Files.exists(Paths.get(url));
} catch (InvalidPathException e) {
return true;
}
}
private int doSync(final Launcher launcher, @Nonnull final FilePath workspace,
final OutputStream logger, final EnvVars env)
throws IOException, InterruptedException {
final List<String> commands = new ArrayList<String>(4);
debug.log(Level.FINE, "Syncing out code in: " + workspace.getName());
commands.clear();
if (resetFirst) {
commands.add(getDescriptor().getExecutable());
commands.add("forall");
if (jobs > 0) {
commands.add("--jobs=" + jobs);
}
commands.add("-c");
commands.add("git reset --hard");
int resetCode = launcher.launch().stdout(logger)
.stderr(logger).pwd(workspace).cmds(commands).envs(env).join();
if (resetCode != 0) {