This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Typo fix in stock PERL_PATCHNUM definition
[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
505afc73
YO
16=head1 DESCRITPTION
17
6033099b
MM
18This program creates the files holding the information
19about locally applied patches to the source code. The created
505afc73
YO
20files are C<git_version.h> and C<lib/Config_git.pl>.
21
22=item C<lib/Config_git.pl>
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,
26although -V will look strange without it.
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
29299e47
YO
43BEGIN {
44 my $root=".";
45 while (!-e "$root/perl.c" and length($root)<100) {
46 if ($root eq '.') {
47 $root="..";
505afc73 48 } else {
29299e47
YO
49 $root.="/..";
50 }
51 }
52 die "Can't find toplevel" if !-e "$root/perl.c";
53 sub path_to { "$root/$_[0]" } # use $_[0] if this'd be placed in toplevel.
54}
55
56sub read_file {
57 my $file = path_to(@_);
58 return "" unless -e $file;
59 open my $fh, '<', $file
60 or die "Failed to open for read '$file':$!";
61 return do { local $/; <$fh> };
62}
63
64sub write_file {
65 my ($file, $content) = @_;
66 $file= path_to($file);
67 open my $fh, '>', $file
68 or die "Failed to open for write '$file':$!";
69 print $fh $content;
70 close $fh;
71}
72
73sub backtick {
74 my $command = shift;
eb5c076f
YO
75 if (wantarray) {
76 my @result= `$command`;
77 chomp @result;
78 return @result;
79 } else {
80 my $result= `$command`;
81 $result="" if ! defined $result;
82 chomp $result;
83 return $result;
84 }
29299e47 85}
8ed12dca 86
eb5c076f
YO
87sub write_files {
88 my %content= map { /WARNING: '([^']+)'/ || die "Bad mojo!"; $1 => $_ } @_;
89 my @files= sort keys %content;
90 my $files= join " and ", map { "'$_'" } @files;
505afc73 91 foreach my $file (@files) {
eb5c076f
YO
92 if (read_file($file) ne $content{$file}) {
93 print "Updating $files\n";
94 write_file($_,$content{$_}) for @files;
95 return 1;
505afc73 96 }
eb5c076f
YO
97 }
98 print "Reusing $files\n";
99 return 0;
100}
8ed12dca 101
786aaa25 102my $unpushed_commits = '/*no-op*/';
eb5c076f
YO
103my ($read, $branch, $snapshot_created, $commit_id, $describe)= ("") x 5;
104my ($changed, $extra_info, $commit_title, $new_patchnum, $status)= ("") x 5;
105if (my $patch_file= read_file(".patch")) {
106 ($branch, $snapshot_created, $commit_id, $describe) = split /\s+/, $patch_file;
786aaa25
KI
107 $extra_info = "git_snapshot_date='$snapshot_created'";
108 $commit_title = "Snapshot of:";
109}
110elsif (-d path_to('.git')) {
111 # git branch | awk 'BEGIN{ORS=""} /\*/ { print $2 }'
eb5c076f
YO
112 ($branch) = map { /\* ([^(]\S*)/ ? $1 : () } backtick('git branch');
113 my ($remote,$merge);
786aaa25 114 if (length $branch) {
505afc73 115 $merge= backtick("git config branch.$branch.merge");
eb5c076f
YO
116 $merge =~ s!^refs/heads/!!;
117 $remote= backtick("git config branch.$branch.remote");
786aaa25
KI
118 }
119 $commit_id = backtick("git rev-parse HEAD");
b6194a9d 120 $describe = backtick("git describe");
786aaa25
KI
121 my $commit_created = backtick(qq{git log -1 --pretty="format:%ci"});
122 $new_patchnum = "describe: $describe";
123 $extra_info = "git_commit_date='$commit_created'";
124 if (length $branch && length $remote) {
125 # git cherry $remote/$branch | awk 'BEGIN{ORS=","} /\+/ {print $2}' | sed -e 's/,$//'
126 my $unpushed_commit_list =
127 join ",", map { (split /\s/, $_)[1] }
eb5c076f 128 grep {/\+/} backtick("git cherry $remote/$merge");
786aaa25 129 # git cherry $remote/$branch | awk 'BEGIN{ORS="\t\\\\\n"} /\+/ {print ",\"" $2 "\""}'
29299e47 130 $unpushed_commits =
eb5c076f
YO
131 join "", map { ',"'.(split /\s/, $_)[1]."\"\t\\\n" }
132 grep {/\+/} backtick("git cherry $remote/$merge");
786aaa25
KI
133 if (length $unpushed_commits) {
134 $commit_title = "Local Commit:";
eb5c076f 135 my $ancestor = backtick("git rev-parse $remote/$merge");
786aaa25
KI
136 $extra_info = "$extra_info
137git_ancestor='$ancestor'
eb5c076f 138git_remote_branch='$remote/$merge'
786aaa25
KI
139git_unpushed='$unpushed_commit_list'";
140 }
141 }
eb5c076f 142 if ($changed) {
786aaa25
KI
143 $changed = 'true';
144 $commit_title = "Derived from:";
eb5c076f
YO
145 $status='"uncommitted-changes"'
146 } else {
147 $status='/*clean-working-directory*/'
786aaa25 148 }
eb5c076f 149 $commit_title ||= "Commit id:";
786aaa25
KI
150}
151
eb5c076f
YO
152# we extract the filename out of the warning header, so dont mess with that
153exit(write_files(<<"EOF_HEADER", <<"EOF_CONFIG"));
154/**************************************************************************
155* WARNING: 'git_version.h' is automatically generated by make_patchnum.pl
156* DO NOT EDIT DIRECTLY - edit make_patchnum.pl instead
157***************************************************************************/
158#define PERL_GIT_UNCOMMITTED_CHANGES $status
159#define PERL_PATCHNUM "$describe"
160#define PERL_GIT_UNPUSHED_COMMITS\t\t\\
161$unpushed_commits/*leave-this-comment*/
162EOF_HEADER
163######################################################################
164# WARNING: 'lib/Config_git.pl' is generated by make_patchnum.pl
165# DO NOT EDIT DIRECTLY - edit make_patchnum.pl instead
166######################################################################
8ed12dca
YO
167\$Config::Git_Data=<<'ENDOFGIT';
168git_commit_id='$commit_id'
169git_describe='$describe'
170git_branch='$branch'
171git_uncommitted_changes='$changed'
172git_commit_id_title='$commit_title'
173$extra_info
174ENDOFGIT
eb5c076f 175EOF_CONFIG
29299e47 176# ex: set ts=4 sts=4 et ft=perl: