This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
PATCH: [perl #121292] wrong perlunicode BOM claims
[perl5.git] / Porting / bisect.pl
CommitLineData
6a8dbfd7
NC
1#!/usr/bin/perl -w
2use strict;
3
9b404864
NC
4=for comment
5
6Documentation for this is in bisect-runner.pl
7
8=cut
9
e295b7be
NC
10# The default, auto_abbrev will treat -e as an abbreviation of --end
11# Which isn't what we want.
12use Getopt::Long qw(:config pass_through no_auto_abbrev);
fbaf6793
MH
13use File::Spec;
14use File::Path qw(mkpath);
15
16my ($start, $end, $validate, $usage, $bad, $jobs, $make, $gold,
17 $module, $with_module);
18
19my $need_cpan_config;
c5134773 20my $cpan_config_dir;
6a8dbfd7 21
d3232d34 22$bad = !GetOptions('start=s' => \$start, 'end=s' => \$end,
02b83d1d 23 'jobs|j=i' => \$jobs, 'make=s' => \$make, 'gold=s' => \$gold,
fbaf6793 24 validate => \$validate, 'usage|help|?' => \$usage,
c5134773
MH
25 'module=s' => \$module, 'with-module=s' => \$with_module,
26 'cpan-config-dir=s' => \$cpan_config_dir);
0ed3b851 27unshift @ARGV, '--help' if $bad || $usage;
b826a648 28unshift @ARGV, '--validate' if $validate;
e2760528 29
fbaf6793 30if ($module || $with_module) {
fbaf6793
MH
31 unshift @ARGV, '--module', $module if defined $module;
32 unshift @ARGV, '--with-module', $with_module if defined $with_module;
c5134773
MH
33
34 if ($cpan_config_dir) {
35 my $c = File::Spec->catfile($cpan_config_dir, 'CPAN', 'MyConfig.pm');
36 die "--cpan-config-dir: $c does not exist\n" unless -e $c;
37
38 unshift @ARGV, '--cpan-config-dir', $cpan_config_dir;
39 } else {
40 $need_cpan_config = 1;
41 }
fbaf6793
MH
42}
43
77ae6092
NC
44my $runner = $0;
45$runner =~ s/bisect\.pl/bisect-runner.pl/;
46
47die "Can't find bisect runner $runner" unless -f $runner;
48
0ed3b851
NC
49system $^X, $runner, '--check-args', '--check-shebang', @ARGV and exit 255;
50exit 255 if $bad;
51exit 0 if $usage;
52
0ed3b851 53my $start_time = time;
6a8dbfd7 54
d3232d34
NC
55if (!defined $jobs &&
56 !($^O eq 'hpux' && system((defined $make ? $make : 'make')
57 . ' --version >/dev/null 2>&1'))) {
0bc550ba
NC
58 # Try to default to (ab)use all the CPUs:
59 my $cpus;
60 if (open my $fh, '<', '/proc/cpuinfo') {
61 while (<$fh>) {
62 ++$cpus if /^processor\s+:\s+\d+$/;
63 }
f16e7fa3
KW
64 } elsif (-x '/sbin/sysctl' || -x '/usr/sbin/sysctl') {
65 my $sysctl = '/sbin/sysctl';
66 $sysctl = "/usr$sysctl" unless -x $sysctl;
67 $cpus = $1 if `$sysctl hw.ncpu` =~ /^hw\.ncpu: (\d+)$/;
0bc550ba
NC
68 } elsif (-x '/usr/bin/getconf') {
69 $cpus = $1 if `/usr/bin/getconf _NPROCESSORS_ONLN` =~ /^(\d+)$/;
70 }
71 $jobs = defined $cpus ? $cpus + 1 : 2;
72}
73
d3232d34
NC
74unshift @ARGV, '--jobs', $jobs if defined $jobs;
75unshift @ARGV, '--make', $make if defined $make;
0bc550ba 76
fbaf6793
MH
77if ($need_cpan_config) {
78 # Make sure we have a CPAN::MyConfig so if we start at an old
79 # revision CPAN doesn't ask for user input to configure itself
80
81 my $cdir = File::Spec->catdir($ENV{HOME},".cpan","CPAN");
82 my $cfile = File::Spec->catfile($cdir, "MyConfig.pm");
83
84 unless (-e $cfile) {
85 printf <<EOF;
86I could not find a CPAN::MyConfig. We need to create one now so that
87you can bisect with --module or --with-module. I'll boot up the CPAN
88shell for you. Feel free to use defaults or change things as needed.
c5134773
MH
89We recommend using 'manual' over 'local::lib' if it asks.
90
fbaf6793 91Type 'quit' when finished.
c5134773 92
fbaf6793
MH
93EOF
94 system("$^X -MCPAN -e shell");
fbaf6793
MH
95 }
96}
97
4819dd2a 98# We try these in this order for the start revision if none is specified.
f66c64b1
NC
99my @stable = map {chomp $_; $_} grep {/v5\.[0-9]+[02468]\.0$/} `git tag -l`;
100die "git tag -l didn't seem to return any tags for stable releases"
101 unless @stable;
102unshift @stable, qw(perl-5.005 perl-5.6.0 perl-5.8.0);
3ffe2687
NC
103
104{
105 my ($dev_C, $ino_C) = stat 'Configure';
106 my ($dev_c, $ino_c) = stat 'configure';
107 if (defined $dev_C && defined $dev_c
108 && $dev_C == $dev_c && $ino_C == $ino_c) {
eef6290d 109 print "You seem to be on a case-insensitive file system.\n\n";
3ffe2687
NC
110 } else {
111 unshift @stable, qw(perl-5.002 perl-5.003 perl-5.004)
112 }
113}
e295b7be 114
02b83d1d
NC
115unshift @ARGV, '--gold', defined $gold ? $gold : $stable[-1];
116
7f39e2df
NC
117if (!defined $end) {
118 # If we have a branch blead, use that as the end
119 $end = `git rev-parse --verify --quiet blead`;
120 die unless defined $end;
121 if (!length $end) {
122 # Else use whichever is newer - HEAD, or the most recent stable tag.
123 if (`git rev-list -n1 HEAD ^$stable[-1]` eq "") {
124 $end = pop @stable;
125 } else {
126 $end = 'HEAD';
127 }
128 }
129}
c382b335
NC
130
131# Canonicalising branches to revisions before moving the checkout permits one
132# to use revisions such as 'HEAD' for --start or --end
133foreach ($start, $end) {
134 next unless $_;
135 $_ = `git rev-parse $_`;
136 die unless defined $_;
137 chomp;
138}
6a8dbfd7 139
c8fde7fa
NC
140{
141 my $modified = my @modified = `git ls-files --modified --deleted --others`;
6a8dbfd7 142
c8fde7fa
NC
143 my ($dev0, $ino0) = stat $0;
144 die "Can't stat $0: $!" unless defined $ino0;
145 my ($dev1, $ino1) = stat 'Porting/bisect.pl';
146
147 my $inplace = defined $dev1 && $dev0 == $dev1 && $ino0 == $ino1;
148
149 if ($modified) {
150 my $final = $inplace
151 ? "Can't run a bisect using a dirty directory containing $runner"
152 : "You can use 'git clean -Xdf' to cleanup the ignored files";
153
154 die "This checkout is not clean, found file(s):\n",
155 join("\t","",@modified),
156 "$modified modified, untracked, or other file(s)\n",
157 "These files may not show in git status as they may be ignored.\n",
158 "$final.\n";
159 }
160
161 if ($inplace) {
162 # We assume that it's safe to copy the runner to the temporary
163 # directory and run it from there, because a shared /tmp should be +t
164 # and hence others are not be able to delete or rename our file.
165 require File::Temp;
166 my ($to, $toname) = File::Temp::tempfile();
167 die "Can't create tempfile"
168 unless $to;
169 open my $from, '<', $runner
170 or die "Can't open '$runner': $!";
171 local $/;
172 print {$to} <$from>
173 or die "Can't copy from '$runner' to '$toname': $!";
174 close $from
175 or die "Can't close '$runner': $!";
176 close $to
177 or die "Can't close '$toname': $!";
178 chmod 0500, $toname
179 or die "Can't chmod 0500, '$toname': $!";
180 $runner = $toname;
181 system $^X, $runner, '--check-args', @ARGV
182 and die "Can't run inplace for some reason. :-(";
183 }
184}
6a8dbfd7 185
195ed8b1
NC
186sub validate {
187 my $commit = shift;
3f14869b
NC
188 if (defined $start && `git rev-list -n1 $commit ^$start^` eq "") {
189 print "Skipping $commit, as it is earlier than $start\n";
190 return;
191 }
192 if (defined $end && `git rev-list -n1 $end ^$commit^` eq "") {
193 print "Skipping $commit, as it is more recent than $end\n";
194 return;
195 }
195ed8b1
NC
196 print "Testing $commit...\n";
197 system "git checkout $commit </dev/null" and die;
198 my $ret = system $^X, $runner, '--no-clean', @ARGV;
199 die "Runner returned $ret, not 0 for revision $commit" if $ret;
200 system 'git clean -dxf </dev/null' and die;
201 system 'git reset --hard HEAD </dev/null' and die;
e2760528 202 return $commit;
195ed8b1
NC
203}
204
205if ($validate) {
e2760528
NC
206 require Text::Wrap;
207 my @built = map {validate $_} 'blead', reverse @stable;
208 if (@built) {
209 print Text::Wrap::wrap("", "", "Successfully validated @built\n");
210 exit 0;
211 }
212 print "Did not validate anything\n";
213 exit 1;
195ed8b1
NC
214}
215
c382b335
NC
216my $git_version = `git --version`;
217if (defined $git_version
218 && $git_version =~ /\Agit version (\d+\.\d+\.\d+)(.*)/) {
219 $git_version = eval "v$1";
220} else {
221 $git_version = v0.0.0;
222}
223
224if ($git_version ge v1.6.6) {
225 system "git bisect reset HEAD" and die;
226} else {
227 system "git bisect reset" and die;
228}
6a8dbfd7 229
6a8dbfd7 230# Sanity check the first and last revisions:
c382b335
NC
231system "git checkout $end" and die;
232my $ret = system $^X, $runner, @ARGV;
233die "Runner returned $ret for end revision" unless $ret;
a84ce349
NC
234die "Runner returned $ret for end revision, which is a skip"
235 if $ret == 125 * 256;
c382b335 236
4819dd2a
NC
237if (defined $start) {
238 system "git checkout $start" and die;
e295b7be 239 my $ret = system $^X, $runner, @ARGV;
4819dd2a
NC
240 die "Runner returned $ret, not 0 for start revision" if $ret;
241} else {
242 # Try to find the earliest version for which the test works
03cc0342 243 my @tried;
4819dd2a 244 foreach my $try (@stable) {
03cc0342
NC
245 if (`git rev-list -n1 $end ^$try^` eq "") {
246 print "Skipping $try, as it is more recent than end commit "
247 . (substr $end, 0, 16) . "\n";
248 # As @stable is supposed to be in age order, arguably we should
249 # last; here.
250 next;
251 }
4819dd2a 252 system "git checkout $try" and die;
e295b7be 253 my $ret = system $^X, $runner, @ARGV;
4819dd2a
NC
254 if (!$ret) {
255 $start = $try;
256 last;
257 }
03cc0342 258 push @tried, $try;
4819dd2a 259 }
03cc0342 260 die "Can't find a suitable start revision to default to.\nTried @tried"
4819dd2a
NC
261 unless defined $start;
262}
6a8dbfd7
NC
263
264system "git bisect start" and die;
265system "git bisect good $start" and die;
266system "git bisect bad $end" and die;
267
268# And now get git bisect to do the hard work:
e295b7be 269system 'git', 'bisect', 'run', $^X, $runner, @ARGV and die;
6a8dbfd7 270
c3d98a71
NC
271END {
272 my $end_time = time;
6a8dbfd7 273
625770c2 274 printf "That took %d seconds.\n", $end_time - $start_time
c3d98a71
NC
275 if defined $start_time;
276}
277
9b404864
NC
278=for comment
279
280Documentation for this is in bisect-runner.pl
281
282=cut
283
c3d98a71 284# ex: set ts=8 sts=4 sw=4 et: