2 # These two should go upon release to make the script Perl 5.005 compatible
8 make_patchnum.pl - make patchnum
12 miniperl make_patchnum.pl
18 This program creates the files holding the information
19 about locally applied patches to the source code. The created
20 files are C<git_version.h> and C<lib/Config_git.pl>.
22 =item C<lib/Config_git.pl>
24 Contains status information from git in a form meant to be processed
25 by the tied hash logic of Config.pm. It is actually optional,
26 although -V:git.\* will be uninformative without it.
28 C<git_version.h> contains similar information in a C header file
29 format, designed to be used by patchlevel.h. This file is obtained
30 from stock_git_version.h if miniperl is not available, and then
31 later on replaced by the version created by this script.
35 Yves Orton, Kenichi Ishigaki, Max Maischein
39 Same terms as Perl itself.
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).
50 our $opt_v = scalar grep $_ eq '-v', @ARGV;
54 # test 1st to see if we're a -Dmksymlinks target dir
57 if (-l "./Configure") {
58 $srcdir = readlink("./Configure");
59 $srcdir =~ s/Configure//;
60 $subcd = "cd $srcdir &&"; # activate backtick fragment
62 while (!-e "$root/perl.c" and length($root)<100) {
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.
74 my $file = path_to(@_);
75 return "" unless -e $file;
76 open my $fh, '<', $file
77 or die "Failed to open for read '$file':$!";
78 return do { local $/; <$fh> };
82 my ($file, $content) = @_;
83 $file= path_to($file);
84 open my $fh, '>', $file
85 or die "Failed to open for write '$file':$!";
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.
95 my @result= `$subcd $command`;
96 warn "$subcd $command: \$?=$?\n" if $?;
97 print "#> $subcd $command ->\n @result\n" if !$? and $opt_v;
101 my $result= `$subcd $command`;
102 $result="" if ! defined $result;
103 warn "$subcd $command: \$?=$?\n" if $?;
104 print "#> $subcd $command ->\n $result\n" if !$? and $opt_v;
111 my %content= map { /WARNING: '([^']+)'/ || die "Bad mojo!"; $1 => $_ } @_;
112 my @files= sort keys %content;
113 my $files= join " and ", map { "'$_'" } @files;
114 foreach my $file (@files) {
115 if (read_file($file) ne $content{$file}) {
116 print "Updating $files\n";
117 write_file($_,$content{$_}) for @files;
121 print "Reusing $files\n";
125 my $unpushed_commits = '/*no-op*/';
126 my ($read, $branch, $snapshot_created, $commit_id, $describe)= ("") x 5;
127 my ($changed, $extra_info, $commit_title, $new_patchnum, $status)= ("") x 5;
129 if (my $patch_file= read_file(".patch")) {
130 ($branch, $snapshot_created, $commit_id, $describe) = split /\s+/, $patch_file;
131 $extra_info = "git_snapshot_date='$snapshot_created'";
132 $commit_title = "Snapshot of:";
134 elsif (-d "$srcdir/.git") {
135 # git branch | awk 'BEGIN{ORS=""} /\*/ { print $2 }'
136 ($branch) = map { /\* ([^(]\S*)/ ? $1 : () } backtick("git branch");
138 if (length $branch) {
139 $merge= backtick("git config branch.$branch.merge");
140 $merge = "" unless $? == 0;
141 $merge =~ s!^refs/heads/!!;
142 $remote= backtick("git config branch.$branch.remote");
143 $remote = "" unless $? == 0;
145 $commit_id = backtick("git rev-parse HEAD");
146 $describe = backtick("git describe");
147 my $commit_created = backtick(qq{git log -1 --pretty="format:%ci"});
148 $new_patchnum = "describe: $describe";
149 $extra_info = "git_commit_date='$commit_created'";
150 if (length $branch && length $remote) {
151 # git cherry $remote/$branch | awk 'BEGIN{ORS=","} /\+/ {print $2}' | sed -e 's/,$//'
152 my $unpushed_commit_list =
153 join ",", map { (split /\s/, $_)[1] }
154 grep {/\+/} backtick("git cherry $remote/$merge");
155 # git cherry $remote/$branch | awk 'BEGIN{ORS="\t\\\\\n"} /\+/ {print ",\"" $2 "\""}'
157 join "", map { ',"'.(split /\s/, $_)[1]."\"\t\\\n" }
158 grep {/\+/} backtick("git cherry $remote/$merge");
159 if (length $unpushed_commits) {
160 $commit_title = "Local Commit:";
161 my $ancestor = backtick("git rev-parse $remote/$merge");
162 $extra_info = "$extra_info
163 git_ancestor='$ancestor'
164 git_remote_branch='$remote/$merge'
165 git_unpushed='$unpushed_commit_list'";
168 if ($changed) { # not touched since init'd. never true.
170 $commit_title = "Derived from:";
171 $status='"uncommitted-changes"'
173 $status='/*clean-working-directory-maybe*/'
175 $commit_title ||= "Commit id:";
178 # we extract the filename out of the warning header, so dont mess with that
179 write_files(<<"EOF_HEADER", <<"EOF_CONFIG");
180 /**************************************************************************
181 * WARNING: 'git_version.h' is automatically generated by make_patchnum.pl
182 * DO NOT EDIT DIRECTLY - edit make_patchnum.pl instead
183 ***************************************************************************/
184 #define PERL_GIT_UNCOMMITTED_CHANGES $status
185 #define PERL_PATCHNUM "$describe"
186 #define PERL_GIT_UNPUSHED_COMMITS\t\t\\
187 $unpushed_commits/*leave-this-comment*/
189 ######################################################################
190 # WARNING: 'lib/Config_git.pl' is generated by make_patchnum.pl
191 # DO NOT EDIT DIRECTLY - edit make_patchnum.pl instead
192 ######################################################################
193 \$Config::Git_Data=<<'ENDOFGIT';
194 git_commit_id='$commit_id'
195 git_describe='$describe'
197 git_uncommitted_changes='$changed'
198 git_commit_id_title='$commit_title'
202 # ex: set ts=8 sts=4 sw=4 et ft=perl: