This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
regcomp.c: regpiece: Move chunk of code for clarity
[perl5.git] / make_patchnum.pl
CommitLineData
29299e47 1#!/usr/bin/perl
f22725be 2# These two should go upon release to make the script Perl 5.005 compatible
29299e47
YO
3use strict;
4use warnings;
5
786aaa25 6=head1 NAME
8ed12dca 7
786aaa25 8make_patchnum.pl - make patchnum
8ed12dca 9
786aaa25 10=head1 SYNOPSIS
8ed12dca 11
6033099b
MM
12 miniperl make_patchnum.pl
13
14 perl make_patchnum.pl
15
3f1788e1 16=head1 DESCRIPTION
505afc73 17
6033099b
MM
18This program creates the files holding the information
19about locally applied patches to the source code. The created
d9217264 20files are F<git_version.h> and F<lib/Config_git.pl>.
505afc73 21
f703fc96 22=head2 F<lib/Config_git.pl>
505afc73
YO
23
24Contains status information from git in a form meant to be processed
25by the tied hash logic of Config.pm. It is actually optional,
3f1788e1 26although -V:git.\* will be uninformative without it.
505afc73
YO
27
28C<git_version.h> contains similar information in a C header file
29format, designed to be used by patchlevel.h. This file is obtained
30from stock_git_version.h if miniperl is not available, and then
31later on replaced by the version created by this script.
32
33=head1 AUTHOR
6033099b 34
505afc73 35Yves Orton, Kenichi Ishigaki, Max Maischein
6033099b 36
505afc73 37=head1 COPYRIGHT
6033099b 38
505afc73 39Same terms as Perl itself.
8ed12dca 40
786aaa25
KI
41=cut
42
3f1788e1
JC
43# from a -Dmksymlink target dir, I need to cd to the git-src tree to
44# use git (like script does). Presuming that's not unique, one fix is
45# to follow Configure's symlink-path to run git. Maybe GIT_DIR or
46# path-args can solve it, if so we should advise here, I tried only
47# very briefly ('cd -' works too).
48
49my ($subcd, $srcdir);
50our $opt_v = scalar grep $_ eq '-v', @ARGV;
51
29299e47
YO
52BEGIN {
53 my $root=".";
3f1788e1
JC
54 # test 1st to see if we're a -Dmksymlinks target dir
55 $subcd = '';
56 $srcdir = $root;
57 if (-l "./Configure") {
58 $srcdir = readlink("./Configure");
59 $srcdir =~ s/Configure//;
60 $subcd = "cd $srcdir &&"; # activate backtick fragment
61 }
29299e47
YO
62 while (!-e "$root/perl.c" and length($root)<100) {
63 if ($root eq '.') {
91c3081c 64 $root="..";
505afc73 65 } else {
91c3081c
RGS
66 $root.="/..";
67 }
29299e47
YO
68 }
69 die "Can't find toplevel" if !-e "$root/perl.c";
70 sub path_to { "$root/$_[0]" } # use $_[0] if this'd be placed in toplevel.
71}
72
73sub read_file {
74 my $file = path_to(@_);
75 return "" unless -e $file;
76 open my $fh, '<', $file
91c3081c 77 or die "Failed to open for read '$file':$!";
29299e47
YO
78 return do { local $/; <$fh> };
79}
80
81sub write_file {
82 my ($file, $content) = @_;
83 $file= path_to($file);
84 open my $fh, '>', $file
91c3081c 85 or die "Failed to open for write '$file':$!";
29299e47
YO
86 print $fh $content;
87 close $fh;
88}
89
90sub backtick {
3f1788e1
JC
91 # only for git. If we're in a -Dmksymlinks build-dir, we need to
92 # cd to src so git will work . Probably a better way.
29299e47 93 my $command = shift;
eb5c076f 94 if (wantarray) {
3f1788e1 95 my @result= `$subcd $command`;
691ce773 96 #warn "$subcd $command: \$?=$?\n" if $?;
3f1788e1 97 print "#> $subcd $command ->\n @result\n" if !$? and $opt_v;
eb5c076f
YO
98 chomp @result;
99 return @result;
100 } else {
3f1788e1 101 my $result= `$subcd $command`;
eb5c076f 102 $result="" if ! defined $result;
31b42f2e 103 #warn "$subcd $command: \$?=$?\n" if $?;
3f1788e1 104 print "#> $subcd $command ->\n $result\n" if !$? and $opt_v;
eb5c076f
YO
105 chomp $result;
106 return $result;
107 }
29299e47 108}
8ed12dca 109
eb5c076f
YO
110sub write_files {
111 my %content= map { /WARNING: '([^']+)'/ || die "Bad mojo!"; $1 => $_ } @_;
112 my @files= sort keys %content;
113 my $files= join " and ", map { "'$_'" } @files;
505afc73 114 foreach my $file (@files) {
eb5c076f
YO
115 if (read_file($file) ne $content{$file}) {
116 print "Updating $files\n";
117 write_file($_,$content{$_}) for @files;
118 return 1;
505afc73 119 }
eb5c076f
YO
120 }
121 print "Reusing $files\n";
122 return 0;
123}
8ed12dca 124
691ce773 125my $unpushed_commits = ' ';
eb5c076f 126my ($read, $branch, $snapshot_created, $commit_id, $describe)= ("") x 5;
691ce773 127my ($changed, $extra_info, $commit_title)= ("") x 3;
3f1788e1 128
d42c7c41 129my $git_patch_file;
eb5c076f
YO
130if (my $patch_file= read_file(".patch")) {
131 ($branch, $snapshot_created, $commit_id, $describe) = split /\s+/, $patch_file;
786aaa25
KI
132 $extra_info = "git_snapshot_date='$snapshot_created'";
133 $commit_title = "Snapshot of:";
134}
d42c7c41
GK
135elsif ($git_patch_file = read_file(".git_patch") and $git_patch_file !~ /\A\$Format:%H/) {
136 chomp $git_patch_file;
137 ($commit_id, my $commit_date, my $names)
138 = split /\|/, $git_patch_file;
139
140 my @names = split /,\s*/, $names;
141
142 ($branch) = map m{^HEAD -> (.*)}, @names;
143 if (!$branch) {
144 ($branch) = map m{^(blead|maint/.*)}, @names;
145 }
146 if (!$branch) {
147 ($branch) = map m{^tag: (.*)}, @names;
148 $describe = $branch;
149 }
150 if (!$branch) {
151 my ($pr) = map m{^refs/pull/([0-9]+)/}, @names;
152 $branch = "pull-request-$pr";
153 }
154 if (!$branch) {
155 $branch = $names[0] || $commit_id;
156 }
157
158 $describe ||= $commit_id;
159 $extra_info = "git_commit_date='$commit_date'\n";
160 $extra_info .= "git_snapshot_date='$commit_date'\n";
161 $commit_title = "Snapshot of:";
162}
3f1788e1 163elsif (-d "$srcdir/.git") {
786aaa25 164 # git branch | awk 'BEGIN{ORS=""} /\*/ { print $2 }'
ee205375
DM
165 ($branch) = map { /\* ([^(]\S*)/ ? $1 : () } backtick("git branch");
166 $branch //= "";
eb5c076f 167 my ($remote,$merge);
786aaa25 168 if (length $branch) {
505afc73 169 $merge= backtick("git config branch.$branch.merge");
326df896 170 $merge = "" unless $? == 0;
eb5c076f
YO
171 $merge =~ s!^refs/heads/!!;
172 $remote= backtick("git config branch.$branch.remote");
326df896 173 $remote = "" unless $? == 0;
786aaa25
KI
174 }
175 $commit_id = backtick("git rev-parse HEAD");
b6194a9d 176 $describe = backtick("git describe");
786aaa25 177 my $commit_created = backtick(qq{git log -1 --pretty="format:%ci"});
786aaa25 178 $extra_info = "git_commit_date='$commit_created'";
691ce773
GA
179 backtick("git diff --no-ext-diff --quiet --exit-code");
180 $changed = $?;
181 unless ($changed) {
182 backtick("git diff-index --cached --quiet HEAD --");
183 $changed = $?;
184 }
185
786aaa25
KI
186 if (length $branch && length $remote) {
187 # git cherry $remote/$branch | awk 'BEGIN{ORS=","} /\+/ {print $2}' | sed -e 's/,$//'
188 my $unpushed_commit_list =
189 join ",", map { (split /\s/, $_)[1] }
eb5c076f 190 grep {/\+/} backtick("git cherry $remote/$merge");
786aaa25 191 # git cherry $remote/$branch | awk 'BEGIN{ORS="\t\\\\\n"} /\+/ {print ",\"" $2 "\""}'
29299e47 192 $unpushed_commits =
eb5c076f
YO
193 join "", map { ',"'.(split /\s/, $_)[1]."\"\t\\\n" }
194 grep {/\+/} backtick("git cherry $remote/$merge");
786aaa25
KI
195 if (length $unpushed_commits) {
196 $commit_title = "Local Commit:";
eb5c076f 197 my $ancestor = backtick("git rev-parse $remote/$merge");
786aaa25
KI
198 $extra_info = "$extra_info
199git_ancestor='$ancestor'
eb5c076f 200git_remote_branch='$remote/$merge'
786aaa25
KI
201git_unpushed='$unpushed_commit_list'";
202 }
203 }
691ce773 204 if ($changed) {
786aaa25 205 $commit_title = "Derived from:";
786aaa25 206 }
eb5c076f 207 $commit_title ||= "Commit id:";
786aaa25
KI
208}
209
2effe01f 210# we extract the filename out of the warning header, so don't mess with that
16ad9bfa 211write_files(<<"EOF_HEADER", <<"EOF_CONFIG");
eb5c076f
YO
212/**************************************************************************
213* WARNING: 'git_version.h' is automatically generated by make_patchnum.pl
214* DO NOT EDIT DIRECTLY - edit make_patchnum.pl instead
215***************************************************************************/
691ce773 216@{[$describe ? "#define PERL_PATCHNUM \"$describe\"" : ()]}
eb5c076f
YO
217#define PERL_GIT_UNPUSHED_COMMITS\t\t\\
218$unpushed_commits/*leave-this-comment*/
691ce773 219@{[$changed ? "#define PERL_GIT_UNCOMMITTED_CHANGES" : ()]}
eb5c076f
YO
220EOF_HEADER
221######################################################################
222# WARNING: 'lib/Config_git.pl' is generated by make_patchnum.pl
223# DO NOT EDIT DIRECTLY - edit make_patchnum.pl instead
224######################################################################
8ed12dca
YO
225\$Config::Git_Data=<<'ENDOFGIT';
226git_commit_id='$commit_id'
227git_describe='$describe'
228git_branch='$branch'
229git_uncommitted_changes='$changed'
230git_commit_id_title='$commit_title'
231$extra_info
232ENDOFGIT
eb5c076f 233EOF_CONFIG
91c3081c 234# ex: set ts=8 sts=4 sw=4 et ft=perl: