-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathSCM.pm
More file actions
1414 lines (1216 loc) · 33 KB
/
SCM.pm
File metadata and controls
1414 lines (1216 loc) · 33 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
=comment
Copyright (c) 2003-2024, Andrew Dunstan
See accompanying License file for license details
=cut
##########################################################################
#
# SCM Class and subclasses for specific SCMs (currently CVS and git).
#
#########################################################################
package PGBuild::SCM;
use strict;
use warnings;
our ($VERSION); $VERSION = 'REL_20';
# factory function to return the right subclass
sub new
{
my $class = shift;
my $conf = shift;
my $target = shift || 'pgsql';
if (defined($conf->{scm}) && $conf->{scm} =~ /^git$/i)
{
$conf->{scm} = 'git';
return PGBuild::SCM::Git->new($conf, $target);
}
elsif ((defined($conf->{scm}) && $conf->{scm} =~ /^cvs$/i)
|| $conf->{csvrepo}
|| $conf->{cvsmethod})
{
$conf->{scm} = 'cvs';
return PGBuild::SCM::CVS->new($conf, $target);
}
die "only CVS and Git currently supported";
}
# common routine use for copying the source, called by the
# SCM objects (directly, not as class methods)
sub copy_source
{
my $using_msvc = shift;
my $target = shift;
my $build_path = shift;
# annoyingly, there isn't a standard perl module to do a recursive copy
# and I don't want to require use of the non-standard File::Copy::Recursive
if ($using_msvc)
{
system("xcopy /I /Q /E $target $build_path 2>&1");
}
else
{
system("cp -R -p $target $build_path 2>&1");
}
my $status = $? >> 8;
die "copying directories: $status" if $status;
return;
}
# required operations in each subclass:
# new()
# copy_source_required()
# copy_source()
# check_access()
# get_build_path()
# checkout()
# cleanup()
# find_changed()
# get_versions()
# log_id()
# rm_worktree()
# get_branches()
##################################
#
# SCM for CVS
#
##################################
package PGBuild::SCM::CVS; ## no critic (ProhibitMultiplePackages)
use strict;
use warnings;
use File::Find;
use File::Basename;
use PGBuild::Options;
use PGBuild::Utils;
sub new
{
my $class = shift;
my $conf = shift;
my $target = shift;
my $self = {};
$self->{cvsrepo} =
$conf->{cvsrepo}
|| $conf->{scmrepo}
|| ":pserver:anoncvs\@anoncvs.postgresql.org:/projects/cvsroot";
$self->{cvsmethod} = $conf->{cvsmethod} || 'update';
$self->{use_git_cvsserver} = $conf->{use_git_cvsserver};
$self->{ignore_files} = {};
$self->{target} = $target;
die "can't use export cvs method with git-cvsserver"
if $self->{use_git_cvsserver} && ($self->{cvsmethod} eq 'export');
return bless $self, $class;
}
sub copy_source_required
{
my $self = shift;
return $self->{cvsmethod} ne 'export';
}
sub copy_source
{
my $self = shift;
my $using_msvc = shift;
my $target = $self->{target};
my $build_path = $self->{build_path};
die "no build path" unless $build_path;
PGBuild::SCM::copy_source($using_msvc, $target, $build_path);
return;
}
sub check_access
{
my $self = shift;
my $using_msvc = shift;
return unless ($self->{cvsrepo} =~ /^:pserver:/ && !$using_msvc);
# we can't do this when using cvsnt (for msvc) because it
# stores the passwords in the registry, damn it
# this is NOT a perfect check, because we don't want to
# catch the port which might or might not be there
# but it will warn most people if necessary, and it's not
# worth any extra work.
my $cvspass;
my $loginfound = 0;
my $srvr;
(undef,, undef, $srvr, undef) = split(/:/, $self->{cvsrepo});
my $qsrvr = quotemeta($srvr);
if (open($cvspass, '<', glob("~/.cvspass")))
{
while (my $line = <$cvspass>)
{
if ($line =~ /:pserver:$qsrvr:/)
{
$loginfound = 1;
last;
}
}
close($cvspass);
}
die "Need to login to :pserver:$srvr first"
unless $loginfound;
return;
}
sub get_build_path
{
my $self = shift;
my $use_vpath = shift;
my $target = $self->{target};
$self->{build_path} =
($self->{cvsmethod} eq 'export' && (!$use_vpath))
? "$target"
: "$target.build";
return $self->{build_path};
}
sub log_id
{
# CVS doesn't have a concept of a tree id.
return;
}
sub checkout
{
my $self = shift;
my $branch = shift;
$self->{branch} = $branch;
my $cvsmethod = $self->{cvsmethod};
my $cvsserver = $self->{cvsrepo};
my $target = $self->{target};
my @cvslog;
if ($self->{use_git_cvsserver})
{
# git-cvsserver treats a branch as a module, so we have to do things
# a bit differently from the old CVS server
my $module = $branch eq 'HEAD' ? 'master' : $branch;
if (-d $target)
{
chdir $target;
@cvslog = `cvs -d $cvsserver update -d 2>&1`;
chdir '..';
find_ignore($self);
}
else
{
@cvslog = `cvs -d $cvsserver co -d $target $module 2>&1`;
find_ignore($self);
}
}
else
{
# old style CVS repo where the module name is 'pgsql' and we
# check out branches
# cvs occasionally does weird things when given an explicit HEAD
# especially on checkout or update.
# since it's the default anyway, we omit it.
my $rtag = $branch eq 'HEAD' ? "" : "-r $branch";
if ($cvsmethod eq 'export')
{
# but you have to have a tag for export
@cvslog = `cvs -d $cvsserver export -r $branch $target 2>&1`;
}
elsif (-d $target)
{
chdir $target;
@cvslog = `cvs -d $cvsserver update -d $rtag 2>&1`;
chdir '..';
find_ignore($self);
}
else
{
@cvslog = `cvs -d $cvsserver co $rtag $target 2>&1`;
find_ignore($self);
}
}
my $status = $? >> 8;
print "======== cvs $cvsmethod log ===========\n", @cvslog
if ($verbose > 1);
# can't call writelog here because we call cleanlogs after the
# scm stage, since we only clear out the logs if we find we need to
# do a build run.
# consequence - we don't save the cvs log if we don't do a run
# doesn't matter too much because if CVS fails we exit anyway.
my $merge_conflicts = grep { /^C/ } @cvslog;
my $mod_files = grep { /^M/ } @cvslog;
my $unknown_files = grep { /^\?/ } @cvslog;
my @bad_ignore = ();
foreach my $ignore (keys %{ $self->{ignore_files} })
{
push(@bad_ignore, "X $ignore\n")
if -e $ignore;
}
if ( $cvsmethod ne 'export'
&& $unknown_files
&& !($nosend && $nostatus))
{
sleep 20;
my @statout = `cd $target && cvs -d $cvsserver status 2>&1`;
$unknown_files = grep { /^\?/ } @statout;
}
send_result("$target-CVS", $status, \@cvslog) if ($status);
send_result("$target-CVS-Merge", $merge_conflicts, \@cvslog)
if ($merge_conflicts);
unless ($nosend && $nostatus)
{
send_result("$target-CVS-Dirty", $mod_files, \@cvslog)
if ($mod_files);
send_result("$target-CVS-Extraneous-Files", $unknown_files, \@cvslog)
if ($unknown_files);
send_result("$target-CVS-Extraneous-Ignore",
scalar(@bad_ignore), \@bad_ignore)
if (@bad_ignore);
}
# if we were successful, however, we return the info so that
# we can put it in the newly cleaned logdir later on.
return \@cvslog;
}
sub cleanup
{
my $self = shift;
unlink keys %{ $self->{ignore_files} };
return;
}
sub rm_worktree
{
# noop for cvs
}
# find_ignore is now a private method of the subclass.
sub find_ignore
{
my $self = shift;
my $target = $self->{target};
my $ignore_file = $self->{ignore_files};
my $cvsmethod = $self->{cvsmethod};
my $wanted = sub {
# skip CVS dirs if using update
if ($cvsmethod eq 'update' && $_ eq 'CVS' && -d $_)
{
$File::Find::prune = 1;
}
elsif (-f $_ && $_ eq '.cvsignore')
{
my $fh;
open($fh, '<', $_)
|| die "cannot open $File::Find::name for reading";
my @names = (<$fh>);
close($fh);
chomp @names;
my $found_dir = $File::Find::dir;
do { s!^!$found_dir/!; }
foreach @names;
@{$ignore_file}{@names} = (1) x @names;
}
};
File::Find::find({ wanted => $wanted }, $target);
return;
}
sub find_changed ## no critic (Subroutines::ProhibitManyArgs)
{
my $self = shift;
my $current_snap = shift;
my $last_run_snap = shift;
my $last_success_snap = shift;
my $changed_files = shift;
my $changed_since_success = shift;
my $cvsmethod = $self->{cvsmethod};
my $target = $self->{target};
my $wanted = sub {
# skip CVS dirs if using update
if ($cvsmethod eq 'update' && $_ eq 'CVS' && -d $_)
{
$File::Find::prune = 1;
}
else
{
my (
$dev, $ino, $mode, $nlink, $uid,
$gid, $rdev, $size, $atime, $mtime,
$ctime, $blksize, $blocks
) = lstat($_);
if (-f _ )
{
$$current_snap = $mtime if ($mtime > $$current_snap);
my $sname = $File::Find::name;
if ($last_run_snap && ($mtime > $last_run_snap))
{
$sname =~ s!^$target/!!;
push(@$changed_files, $sname);
}
elsif ($last_success_snap && ($mtime > $last_success_snap))
{
$sname =~ s!^$target/!!;
push(@$changed_since_success, $sname);
}
}
}
};
File::Find::find({ wanted => $wanted }, $target);
return;
}
sub get_branches
{
# noop for cvs
return ();
}
sub get_versions
{
my $self = shift;
return if $self->{cvsmethod} eq "export";
my $flist = shift;
return unless @$flist;
my $target = $self->{target};
my @cvs_status;
# some shells (e.g cygwin::bash ) choke on very long command lines
# so do this in batches.
while (@$flist)
{
my @chunk = splice(@$flist, 0, 200);
my @res = `cd $target && cvs status @chunk 2>&1`;
push(@cvs_status, @res);
my $status = $? >> 8;
print "======== $target-cvs status log ===========\n", @cvs_status
if ($verbose > 1);
send_result("$target-CVS-status", $status, \@cvs_status)
if ($status);
}
my @fchunks = split(/File:/, join("", @cvs_status));
my @repolines;
foreach (@fchunks)
{
## no critic (RegularExpressions::ProhibitComplexRegexes)
# we need to report the working revision rather than the
# repository revision version in case the file has been
# updated between the time we did the checkout/update and now.
my $module = $target; # XXX is it??
$module = ($self->{branch} eq 'HEAD' ? 'master' : $self->{branch})
if $self->{use_git_cvsserver};
next
unless m!
Working\srevision:\s+
(\d+(\.\d+)+)
.*Repository.revision:.
(\d+(\.\d+)+)
.*
/($module/.*)
,v
!sx;
push(@repolines, "$5 $1");
}
@$flist = (@repolines);
return;
}
##################################
#
# SCM for git
#
##################################
package PGBuild::SCM::Git; ## no critic (ProhibitMultiplePackages)
use strict;
use warnings;
use Cwd qw(getcwd abs_path);
use File::Copy;
use File::Path 'mkpath';
use Fcntl qw(:flock);
use File::Find;
use File::Basename;
use PGBuild::Utils qw(:DEFAULT $devnull);
use PGBuild::Options;
sub new
{
my $class = shift;
my $conf = shift;
my $target = shift;
my $self = {};
$self->{gitrepo} = $conf->{scmrepo}
|| "https://git.postgresql.org/git/postgresql.git";
$self->{reference} = $conf->{git_reference}
if defined($conf->{git_reference});
# need to use abs_path here to avoid some idiocy in msysGit.
$self->{mirror} = (
$target eq 'pgsql'
? abs_path("$conf->{build_root}") . "/pgmirror.git"
: abs_path("$conf->{build_root}") . "/$target-mirror.git"
) if $conf->{git_keep_mirror};
$self->{ignore_mirror_failure} = $conf->{git_ignore_mirror_failure};
$self->{use_workdirs} = $conf->{git_use_workdirs};
$self->{build_root} = $conf->{build_root};
$self->{gchours} = 7 * 24; # default 1 week.
if (exists($conf->{git_gc_hours}))
{
$self->{gchours} = $conf->{git_gc_hours};
}
$self->{target} = $target;
$self->{skip_git_default_check} = $conf->{skip_git_default_check} || 0;
if (!$self->{skip_git_default_check} && !$from_source)
{
# check if we can run "git ls-remote --symref" If not, we can't run
# the default branch name update code.
# try to test against a known local git repo.
my $repo = $self->{gitrepo};
if (exists $self->{mirror} && -d $self->{mirror})
{
$repo = $self->{mirror};
}
elsif (-d "$self->{build_root}/HEAD/pgsql/.git")
{
$repo = "$self->{build_root}/HEAD/pgsql";
}
system(qq{git ls-remote --symref "$repo" HEAD > $devnull 2>&1});
if ($?)
{
my $gversion = `git --version`;
chomp $gversion;
print "$gversion too old for automatic default branch update\n"
if $verbose;
$self->{skip_git_default_check} = "detected by SCM module";
}
}
return bless $self, $class;
}
sub copy_source_required
{
my $self = shift;
# always copy git
return 1;
}
sub copy_source
{
my $self = shift;
my $using_msvc = shift;
my $target = $self->{target};
my $build_path = $self->{build_path};
die "no build path" unless $build_path;
# we don't want to copy the (very large) .git directory
# If we can, use git's archive facility, which won't copy the .git dir
# Otherwise we just move it out of the way during the copy,
# which is a bit less robust.
# Have to copy in the from-source case, though.
system("tar --version > $devnull 2>&1");
my $have_tar = $? == 0;
if ($have_tar && !$from_source)
{
mkdir($build_path);
system("git -C $target archive HEAD | tar -C $build_path -xf -");
die "copying $target" if $?;
}
else
{
move "$target/.git", "./git-save";
PGBuild::SCM::copy_source($using_msvc, $target, $build_path);
move "./git-save", "$target/.git";
}
return;
}
sub get_build_path
{
my $self = shift;
my $use_vpath = shift; # irrelevant for git
my $target = $self->{target};
$self->{build_path} = "$target.build";
return $self->{build_path};
}
sub check_access
{
# no login required?
return;
}
sub log_id
{
my $self = shift;
my $name = shift || 'githead';
writelog($name, [ $self->{headref} ])
if $self->{headref};
return;
}
# test if symlinks are available:
# For directories we could use junctions via "mklink /j"
# (c.f. TextUpgradeXVersion.pm) but there is no equivalent for plain files
# which we need here. Windows currently forbids using non-junction mklink
# except by administrative users, which is both stupid and sad.
# It can be fixed by setting a security policy for the user.
# Local Group Policy Editor: Launch gpedit.msc, navigate to
# Computer Configuration - Windows Settings - Security Settings -
# Local Policies - User Rights Assignment
# and add the account(s) to the list named Create symbolic links.
# See https://github.com/git-for-windows/git/wiki/Symbolic-Links
sub have_symlink
{
my $self = shift;
$self->{have_symlink} = 1 unless $^O eq 'msys' || $^O eq 'MSWin32';
return $self->{have_symlink} if exists $self->{have_symlink};
if ($^O eq 'msys')
{
my $symlink_exists = eval { symlink("", ""); 1 };
unless ($symlink_exists)
{
$self->{have_symlink} = 0;
return 0;
}
open(my $tg, ">", "tg.txt") || die "opening tg.txt: $1";
print $tg "boo!\n";
close $tg;
unless (symlink "tg.txt", "lnk.txt")
{
unlink "lnk.txt", "tg.txt";
$self->{have_symlink} = 0;
return 0;
}
my $ok = -l "lnk.txt" && -f "lnk.txt";
my $txt = $ok ? file_contents("lnk.txt") : "";
$ok &&= $txt eq "boo!\n";
unlink "lnk.txt", "tg.txt";
$self->{have_symlink} = $ok;
return $ok;
}
else
{
# MSWin32
# even if symlinks are working it appears that even recent versions
# of windows git are unable to handle them, so just don't use them here
$self->{have_symlink} = 0;
return 0;
# if the above were not true we'd do this:
## no critic (ControlStructures::ProhibitUnreachableCode)
open(my $tg, ">", "tg.txt") || return 0;
print $tg "boo!\n";
close $tg;
system(qq{mklink "lnk.txt" "tg.txt" >nul 2>&1});
my $txt = -e "lnk.txt" ? file_contents("lnk.txt") : "";
my $ok = $txt eq "boo!\n";
unlink "lnk.txt", "tg.txt";
$self->{have_symlink} = $ok;
return $ok;
}
return 0; # keep perl critic happy
}
# the argument here should be a symbolic link and point to a plain file
# returns:
# nosuchfile - link is missing
# notsym - it's not a symlink
# dangling - pointed to file is missing or not a plain file
# ok - all good
sub _test_file_symlink
{
my $file = shift;
return 'nosuchfile' unless -e $file;
if ($^O eq 'MSWin32')
{
$file =~ s!/!\\!g; # `dir` doesn't like forward slash paths
my $dirout = `dir "$file"`;
return 'notsym' unless $dirout =~ /<SYMLINK>.*\[(.*)\]/;
$file = $1;
}
else
{
return 'notsym' unless -l $file;
}
return 'dangling' unless -f $file;
return 'ok';
}
sub _make_symlink
{
# assumes we have a working symlink (see above)
# note: unix and windows do link/target in the opposite order
my $target = shift;
my $link = shift;
if ($^O eq 'MSWin32')
{
my $dirswitch = -d $target ? "/d" : "";
system(qq{mklink $dirswitch "$link" "$target" >nul 2>&1});
}
else
{
# msys2 perl is smart enough to check if the target is a directory
# and set up the right type of symlink
symlink $target, $link;
}
return;
}
sub _check_default_branch
{
my $self = shift;
my $target = shift;
return if $self->{skip_git_default_check};
my $upstream = $self->{mirror} || $self->{gitrepo};
my @remote = `git ls-remote --symref $upstream HEAD`;
chomp(@remote);
my $remote_def = (grep { /^ref:/ } @remote)[0];
die "no remote default: @remote" unless $remote_def;
$remote_def =~ s!.*/([a-zA-Z0-9_-]+)\s.*!$1!;
my $local = `git symbolic-ref refs/remotes/origin/HEAD`;
chomp $local;
return () if $local eq "refs/remotes/origin/$remote_def";
$local =~ s!.*/!!;
my $this_branch = `git rev-parse --abbrev-ref HEAD`;
chomp $this_branch;
# ok, here we go
my (@lines, @log);
# check out the local idea of the upstream default
@lines = run_log("git checkout $local");
push(@log, @lines);
return @log if $?;
# bring it up to date
@lines = run_log("git merge -q --ff-only origin/$local");
push(@log, @lines);
return @log if $?;
# delete the bf_HEAD branch if it exists
my ($hasbfhead) = grep { /bf_HEAD/ } split(/\n/, `git branch`);
if ($hasbfhead)
{
my $here = getcwd();
# clean out the bf_HEAD working files before we mangle things
chdir "../../HEAD";
$self->rm_worktree($target);
chdir $here;
@lines = run_log("git branch -d bf_HEAD");
push(@log, @lines);
return @log if $?;
}
# rename the branch to align with upstream
@lines = run_log("git branch -m $remote_def");
push(@log, @lines);
return @log if $?;
# fetch the upstream
@lines = run_log("git fetch");
push(@log, @lines);
return @log if $?;
# realign the branch to fetch from the right upstream branch
@lines = run_log("git branch --unset-upstream");
push(@log, @lines);
return @log if $?;
@lines = run_log("git branch -u origin/$remote_def");
push(@log, @lines);
return @log if $?;
# bring it up to date
@lines = run_log("git merge -q --ff-only origin/$remote_def");
push(@log, @lines);
return @log if $?;
# realign the local version of the remote HEAD ref
@lines = run_log("git symbolic-ref refs/remotes/origin/HEAD "
. "refs/remotes/origin/$remote_def");
push(@log, @lines);
return @log if $?;
# now run a pruning fetch, which will remove the last vestiges of the
# old branch name
@lines = run_log("git fetch -p");
push(@log, @lines);
return @log if $?;
# recreate the bf_HEAD branch if we deleted it above
if ($hasbfhead)
{
my $here = getcwd;
chdir "../../HEAD/$target";
@lines = run_log("git checkout -b bf_HEAD --track origin/$remote_def");
push(@log, @lines);
return @log if $?;
# fix the git index for bf_HEAD
@lines = run_log("git reset --hard origin/$remote_def");
push(@log, @lines);
return @log if $?;
chdir $here;
}
# go back to the branch we were on if we're not on it already
unless ($this_branch eq 'bf_HEAD')
{
@lines = run_log("git checkout $this_branch");
push(@log, @lines);
return @log if $?;
}
return @log;
}
sub _create_or_update_mirror
{
my $self = shift;
my $target = shift;
my $branch = shift;
my $gitserver = $self->{gitrepo};
my $skip_default_name_check = $self->{skip_git_default_check};
my @gitlog;
my $status;
if (-d $self->{mirror})
{
# do we need --prune-tags here? I'm not sure. Only very modern versions
# of git have --prune-tags, so for now we'll leave it out.
# see https://git-scm.com/docs/git-fetch/2.25.1 for a discussion
# of different ways of saying it
@gitlog = run_log(qq{git --git-dir="$self->{mirror}" fetch --prune});
if ($self->{ignore_mirror_failure})
{
print "Git mirror failure (ignored)\n", @gitlog if $? && $verbose;
$status = 0;
}
else
{
$status = $? >> 8;
}
if (!$status && !$skip_default_name_check)
{
# make sure we have the same idea of the default branch name
# as upstream
my @remote_def = run_log(
qq{git --git-dir="$self->{mirror}" ls-remote --symref origin HEAD}
);
$status = $? >> 8;
if (!$status)
{
my $ref = (grep { m!ref: .*\s+HEAD! } @remote_def)[0];
$ref =~ s/.*?ref: //;
$ref =~ s/\s+HEAD.*//;
system(
qq{git --git-dir="$self->{mirror}" symbolic-ref HEAD $ref});
# failure here is local, thus not an ignore-mirror-failure
$status = $? >> 8;
}
elsif ($self->{ignore_mirror_failure})
{
print "Git mirror ls-remote failure (ignored)\n", @remote_def
if $verbose;
$status = 0;
}
}
my $last_gc = find_last("$target.mirror.gc") || 0;
if ( !$status
&& $branch eq 'HEAD'
&& $self->{gchours}
&& time - $last_gc > $self->{gchours} * 3600)
{
my @gclog = run_log(qq{git --git-dir="$self->{mirror}" gc});
push(@gitlog, "----- mirror garbage collection -----\n", @gclog);
set_last("$target.mirror.gc");
# this is also local, so not covered by ignore-mirror-failure
$status = $? >> 8;
}
}
else # mirror does not exist
{
$gitserver = abs_path($gitserver) if $gitserver =~ m!^[/\\]!;
# this will fail on older git versions
# workaround is to do this manually in the buildroot:
# git clone --bare $gitserver pgmirror.git
# (cd pgmirror.git && git remote add --mirror origin $gitserver)
# or equivalent for other targets
@gitlog = run_log("git clone --mirror $gitserver $self->{mirror}");
$status = $? >> 8;
}
if ($status)
{
unshift(@gitlog, "Git mirror failure:\n");
print @gitlog if ($verbose);
send_result('Git-mirror', $status, \@gitlog);
}
return @gitlog;
}
sub _setup_new_head
{
# only called when HEAD has disappeared from under a workdir (or it never
# existed)
my $self = shift;
my $target = shift;
my $gitserver = $self->{gitrepo};
my $base = $self->{mirror} || $gitserver;
my $head = $self->{build_root} . '/HEAD';
my @gitlog;
my $status;
$base = abs_path($base) if $base =~ m!^[/\\]!;
mkdir $head;
print "running ", qq{git clone -q $base "$head/$target"}, "\n";
my @clonelog = run_log(qq{git clone -q $base "$head/$target"});
push(@gitlog, @clonelog);
$status = $? >> 8;
if (!$status)
{
my $savedir = getcwd();
chdir "$head/$target";
# we're on a fresh clone so the current branch should be the
# upstream default
my $defbranch = `git rev-parse --abbrev-ref HEAD`;
chomp($defbranch);
# make sure we don't name the new branch HEAD
my @colog =
run_log("git checkout -b bf_HEAD --track origin/$defbranch");
push(@gitlog, @colog);
chdir $savedir;
}
else
{
die "clone status: $status";
}
return @gitlog;
}
sub _setup_new_workdir
{
my $self = shift;
my $target = shift;
my $branch = shift;
my @gitlog;
my $head = $self->{build_root} . '/HEAD';
unless (-d "$head/$target/.git")
{
# clone HEAD even if not (yet) needed for a run, as it will be the
# non-symlinked repo linkd to by all the others.
@gitlog = $self->_setup_new_head($target);
}
# now we can set up the git dir symlinks like git-new-workdir does
mkdir $target;
chdir $target;
mkdir ".git";
mkdir ".git/logs";
# skip qw(remotes rr-cache svn). They shouldn't exist on the linked-to
# directory, regardless of what git-new-workdir thinks
my @links = qw (config refs logs/refs objects info hooks packed-refs);
foreach my $link (@links)
{
_make_symlink("$head/$target/.git/$link", ".git/$link");
}
copy("$head/$target/.git/HEAD", ".git/HEAD");
my @checklog = $self->_check_default_branch($target);
# run git fetch in case there are new branches the local repo
# doesn't yet know about
my @fetchlog = run_log('git fetch --prune');