-use strict;
-use warnings;
-no warnings 'uninitialized';
-
-my $existing_patchnum = read_file('.patchnum');
-my $existing_config = read_file('lib/Config_git.pl');
-my $existing_unpushed = read_file('unpushed.h');
-
-my $unpushed_commits = '/*no-op*/';
-my ($read, $branch, $snapshot_created, $commit_id, $describe);
-my ($changed, $extra_info, $commit_title, $new_patchnum);
-if (-s path_to('.patch')) {
- open my $fh, '<', path_to('.patch') or die "Failed to read .patch:$!";
- ($read, $branch, $snapshot_created, $commit_id, $describe) = map { chomp $_; $_ } <$fh>;
- $changed = '';
+# from a -Dmksymlink target dir, I need to cd to the git-src tree to
+# use git (like script does). Presuming that's not unique, one fix is
+# to follow Configure's symlink-path to run git. Maybe GIT_DIR or
+# path-args can solve it, if so we should advise here, I tried only
+# very briefly ('cd -' works too).
+
+my ($subcd, $srcdir);
+our $opt_v = scalar grep $_ eq '-v', @ARGV;
+
+BEGIN {
+ my $root=".";
+ # test 1st to see if we're a -Dmksymlinks target dir
+ $subcd = '';
+ $srcdir = $root;
+ if (-l "./Configure") {
+ $srcdir = readlink("./Configure");
+ $srcdir =~ s/Configure//;
+ $subcd = "cd $srcdir &&"; # activate backtick fragment
+ }
+ while (!-e "$root/perl.c" and length($root)<100) {
+ if ($root eq '.') {
+ $root="..";
+ } else {
+ $root.="/..";
+ }
+ }
+ die "Can't find toplevel" if !-e "$root/perl.c";
+ sub path_to { "$root/$_[0]" } # use $_[0] if this'd be placed in toplevel.
+}
+
+sub read_file {
+ my $file = path_to(@_);
+ return "" unless -e $file;
+ open my $fh, '<', $file
+ or die "Failed to open for read '$file':$!";
+ return do { local $/; <$fh> };
+}
+
+sub write_file {
+ my ($file, $content) = @_;
+ $file= path_to($file);
+ open my $fh, '>', $file
+ or die "Failed to open for write '$file':$!";
+ print $fh $content;
+ close $fh;
+}
+
+sub backtick {
+ # only for git. If we're in a -Dmksymlinks build-dir, we need to
+ # cd to src so git will work . Probably a better way.
+ my $command = shift;
+ if (wantarray) {
+ my @result= `$subcd $command`;
+ #warn "$subcd $command: \$?=$?\n" if $?;
+ print "#> $subcd $command ->\n @result\n" if !$? and $opt_v;
+ chomp @result;
+ return @result;
+ } else {
+ my $result= `$subcd $command`;
+ $result="" if ! defined $result;
+ #warn "$subcd $command: \$?=$?\n" if $?;
+ print "#> $subcd $command ->\n $result\n" if !$? and $opt_v;
+ chomp $result;
+ return $result;
+ }
+}
+
+sub write_files {
+ my %content= map { /WARNING: '([^']+)'/ || die "Bad mojo!"; $1 => $_ } @_;
+ my @files= sort keys %content;
+ my $files= join " and ", map { "'$_'" } @files;
+ foreach my $file (@files) {
+ if (read_file($file) ne $content{$file}) {
+ print "Updating $files\n";
+ write_file($_,$content{$_}) for @files;
+ return 1;
+ }
+ }
+ print "Reusing $files\n";
+ return 0;
+}
+
+my $unpushed_commits = ' ';
+my ($read, $branch, $snapshot_created, $commit_id, $describe)= ("") x 5;
+my ($changed, $extra_info, $commit_title)= ("") x 3;
+
+my $git_patch_file;
+if (my $patch_file= read_file(".patch")) {
+ ($branch, $snapshot_created, $commit_id, $describe) = split /\s+/, $patch_file;