Almost a literal conversion of make_patchnum.sh
authorKenichi Ishigaki <ishigaki@cpan.org>
Thu, 1 Jan 2009 16:41:06 +0000 (17:41 +0100)
committerYves Orton <demerphq@gmail.com>
Sun, 4 Jan 2009 22:32:39 +0000 (23:32 +0100)
make_patchnum.pl

index 7eb6515..5b85b51 100644 (file)
@@ -1,79 +1,85 @@
-#!/bin/sh
+=head1 NAME
 
-# this script is used to regenerate a number of special build files
-# based on either information contained in a file called .patch or
-# directly from git.
-# The files involved are:
-#   .patchnum         # information about the current checkout
-#   lib/Config_git.pl # holds some special configure settings related to git
-#   unpushed.h        # header file used by patchlevel.h to store unpushed commits
+make_patchnum.pl - make patchnum
 
-existing_patchnum=$(cat .patchnum 2>/dev/null)
-existing_config=$(cat lib/Config_git.pl 2>/dev/null)
-existing_unpushed=$(cat unpushed.h 2>/dev/null)
+=head1 SYNOPSIS
 
-unpushed_commits='/*no-op*/'
-if [ -s ".patch" ] ; then
-       # this is the minimal expectation for the 
-       read branch snapshot_created commit_id describe < .patch
-       changed=""
-       extra_info="git_snapshot_date='$snapshot_created'"
-       commit_title='Snapshot of:'
-elif [ -d ".git" ]; then
-       branch=$(git branch | awk 'BEGIN{ORS=""} /\*/ { print $2 }')
-       test -n "$branch" && remote=$(git config branch.$branch.remote)
-       commit_id=$(git rev-parse HEAD)
-       changed=$(git diff-index --name-only HEAD)
-       describe=$(git describe --tags)
-       commit_created=$(git log -1 --pretty='format:%ci')
-       extra_info="git_commit_date='$commit_created'"
-       if [ -n "$branch" ] && [ -n "$remote" ]; then
-               unpushed_commit_list=$(git cherry $remote/$branch | awk 'BEGIN{ORS=","} /+/ {print $2}' | sed -e 's/,$//')
-               unpushed_commits=$(git cherry $remote/$branch | awk 'BEGIN{ORS="\t\\\n"} /+/ {print ",\"" $2 "\""}')
+...
 
-               if [ -n "$unpushed_commits" ]; then
-                       commit_title="Local Commit:"
-                       ancestor=`git rev-parse $remote/$branch`
-                       extra_info="$extra_info
-git_ancestor='$ancestor'
-git_unpushed='$unpushed_commit_list'"
-               fi
-                       
-       fi
-else
-       cat <<SNDOGS
-Something is wrong with your source tree. You should 
-either have a .git directory and a functional git toolset
-OR should have a .patch file in the source tree. Please
-report the particulars of this situation to 
-perl5-porters@perl.org.
-SNDOGS
-       exit 2
-fi
+=cut
+
+use strict;
+use warnings;
+no warnings 'uninitialized';
 
-# Set up defaults for various values
-new_patchnum="describe: $describe"
-if [ -n "$changed" ]; then
-       changed="true"
-       commit_title="Derived from:"
-       new_patchnum="$new_patchnum
-status: uncommitted-changes"
-fi
-test -z "$commit_title" && commit_title='Commit id:'
+my $existing_patchnum = read_file('.patchnum');
+my $existing_config   = read_file('lib/Config_git.pl');
+my $existing_unpushed = read_file('unpushed.h');
 
-new_unpushed=$(cat <<EOFTEXT
+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 = '';
+    $extra_info = "git_snapshot_date='$snapshot_created'";
+    $commit_title = "Snapshot of:";
+}
+elsif (-d path_to('.git')) {
+    # git branch | awk 'BEGIN{ORS=""} /\*/ { print $2 }'
+    $branch = join "", map { (split /\s/, $_)[1] }
+              grep {/\*/} split /\n/, backtick('git branch');
+    my $remote;
+    if (length $branch) {
+        $remote = backtick("git config branch.$branch.remote");
+    }
+    $commit_id = backtick("git rev-parse HEAD");
+    $describe = backtick("git describe --tags");
+    my $commit_created = backtick(qq{git log -1 --pretty="format:%ci"});
+    $new_patchnum = "describe: $describe";
+    $extra_info = "git_commit_date='$commit_created'";
+    if (length $branch && length $remote) {
+        # git cherry $remote/$branch | awk 'BEGIN{ORS=","} /\+/ {print $2}' | sed -e 's/,$//'
+        my $unpushed_commit_list =
+            join ",", map { (split /\s/, $_)[1] }
+            grep {/\+/} split /\n/, backtick("git cherry $remote/$branch");
+        # git cherry $remote/$branch | awk 'BEGIN{ORS="\t\\\\\n"} /\+/ {print ",\"" $2 "\""}'
+        $unpushed_commits = 
+            join "", map { ',"'.(split /\s/, $_)[1].'"'."\t\\\n" }
+            grep {/\+/} split /\n/, backtick("git cherry $remote/$branch");
+        if (length $unpushed_commits) {
+            $commit_title = "Local Commit:";
+            my $ancestor = backtick("git rev-parse $remote/$branch");
+            $extra_info = "$extra_info
+git_ancestor='$ancestor'
+git_unpushed='$unpushed_commit_list'";
+        }
+    }
+    if (length $changed) {
+        $changed = 'true';
+        $commit_title =  "Derived from:";
+        $new_patchnum = "$new_patchnum
+status: uncommitted-changes";
+    }
+    if (not length $commit_title) {
+        $commit_title = "Commit id:";
+    }
+}
+
+my $new_unpushed =<<"EOFTEXT";
 /*********************************************************************
-* WARNING: unpushed.h is automatically generated by make_patchnum.sh *
-*          DO NOT EDIT DIRECTLY - edit make_patchnum.sh instead      *
+* WARNING: unpushed.h is automatically generated by make_patchnum.pl *
+*          DO NOT EDIT DIRECTLY - edit make_patchnum.pl instead      *
 *********************************************************************/
 #define PERL_GIT_UNPUSHED_COMMITS       $unpushed_commits
 /*leave-this-comment*/
 EOFTEXT
-)
-new_config=$(cat <<EOFDATA
+
+my $new_config =<<"EOFDATA";
 #################################################################
-# WARNING: lib/Config_git.pl is generated by make_patchnum.sh   #
-#          DO NOT EDIT DIRECTLY - edit make_patchnum.sh instead #
+# WARNING: lib/Config_git.pl is generated by make_patchnum.pl   #
+#          DO NOT EDIT DIRECTLY - edit make_patchnum.pl instead #
 #################################################################
 \$Config::Git_Data=<<'ENDOFGIT';
 git_commit_id='$commit_id'
@@ -84,14 +90,38 @@ git_commit_id_title='$commit_title'
 $extra_info
 ENDOFGIT
 EOFDATA
-)
+
 # only update the files if necessary, other build product depends on these files
-if [ "$existing_patchnum" != "$new_patchnum" ] || [ "$new_config" != "$existing_config" ] || [ "$existing_unpushed" != "$new_unpushed" ]; then
-       echo "Updating .patchnum and lib/Config_git.pl"
-       echo "$new_patchnum" > .patchnum
-       echo "$new_config" > lib/Config_git.pl
-       echo "$new_unpushed" > unpushed.h
-else
-       echo "Reusing .patchnum and lib/Config_git.pl"
-fi
+if (( $existing_patchnum ne $new_patchnum ) || ( $existing_config ne $new_config ) || ( $existing_unpushed ne $new_unpushed )) {
+    print "Updating .patchnum and lib/Config_git.pl\n";
+    write_file('.patchnum', $new_patchnum);
+    write_file('lib/Config_git.pl', $new_config);
+    write_file('unpushed.h', $new_unpushed);
+}
+else {
+    print "Reusing .patchnum and lib/Config_git.pl\n"
+}
+
+sub path_to { "../$_[0]" } # use $_[0] if this'd be placed in toplevel.
+
+sub read_file {
+    my $file = shift;
+    return unless -f path_to($file);
+    open my $fh, '<', path_to($file) or die "Failed to open $file:$!";
+    return do { local $/; <$fh> };
+}
+
+sub write_file {
+    my ($file, $content) = @_;
+    open my $fh, '>', path_to($file) or die "Failed to open $file:$!";
+    print $fh $content;
+    close $fh;
+}
+
+sub backtick {
+    my $command = shift;
+    my $result = `$command`;
+    chomp $result;
+    return $result;
+}