eliminate .patchnum and related infrastrcuture from *nix based build process
[p5sagit/p5-mst-13.2.git] / make_patchnum.sh
1 #!/bin/sh
2
3 # this script is used to regenerate a number of special build files
4 # based on either information contained in a file called .patch or
5 # directly from git.
6 # The files involved are:
7 #   .patchnum         # information about the current checkout
8 #   lib/Config_git.pl # holds some special configure settings related to git
9 #   unpushed.h        # header file used by patchlevel.h to store unpushed commits
10
11 config_file="lib/Config_git.pl"
12 header_file="git_version.h"
13 patch_file=".patch"
14
15 git_dir=`git rev-parse --git-dir 2>/dev/null`
16 existing_config=`cat $config_file 2>/dev/null`
17 existing_header=`cat $header_file 2>/dev/null`
18
19 unpushed_commits='/*no-op*/'
20 if [ -s $patch_file ] ; then
21         # this is the minimal expectation for the 
22         read branch snapshot_created commit_id describe < .patch
23         changed=""
24         extra_info="git_snapshot_date='$snapshot_created'"
25         commit_title='Snapshot of:'
26 elif [ -n "$git_dir" ]; then
27         branch=`git branch | awk 'BEGIN{ORS=""} /^\* [^(]/ { print $2 }'`
28         test -n "$branch" && remote=`git config branch.$branch.remote`
29         test -n "$branch" && merge=`git config branch.$branch.merge | sed s,refs/heads/,,`
30         commit_id=`git rev-parse HEAD`
31         changed=`git diff-index --name-only HEAD`
32         describe=`git describe --tags`
33         commit_created=`git log -1 --pretty='format:%ci'`
34         extra_info="git_commit_date='$commit_created'"
35         if [ -n "$merge" ] && [ -n "$remote" ]; then
36                 unpushed_commit_list=`git cherry $remote/$merge | awk 'BEGIN{ORS=","} /\+/ {print $2}' | sed -e 's/,$//'`
37                 unpushed_commits=`git cherry $remote/$merge | awk 'BEGIN{ORS="\t\\\\\n"} /\+/ {print ",\"" $2 "\""}'`
38
39                 if [ -n "$unpushed_commits" ]; then
40                         commit_title="Local Commit:"
41                         ancestor=`git rev-parse $remote/$merge`
42                         extra_info="$extra_info
43 git_ancestor='$ancestor'
44 git_remote_branch='$remote/$merge'
45 git_unpushed='$unpushed_commit_list'"
46                 fi
47                         
48         fi
49 else
50         cat <<SNDOGS
51 Something is wrong with your source tree. You should 
52 either have a .git directory and a functional git toolset
53 OR should have a .patch file in the source tree. Please
54 report the particulars of this situation to 
55 perl5-porters@perl.org.
56 SNDOGS
57         exit 2
58 fi
59
60 # Set up defaults for various values
61 if [ -n "$changed" ]; then
62         changed="true"
63         commit_title="Derived from:"
64         status="#define PERL_GIT_UNCOMMITTED_CHANGES uncommitted-changes"
65 else
66         status="/* clean working directory */"
67 fi
68 test -z "$commit_title" && commit_title='Commit id:'
69
70 new_header=`cat <<EOFTEXT
71 /***************************************************************************
72 * WARNING: $header_file is automatically generated by make_patchnum.sh 
73 *          DO NOT EDIT DIRECTLY - edit make_patchnum.sh instead      
74 ****************************************************************************/
75 #define PERL_PATCHNUM $describe
76 $status
77 #define PERL_GIT_UNPUSHED_COMMITS       $unpushed_commits
78 /*leave-this-comment*/
79 EOFTEXT
80 `
81 new_config=`cat <<EOFDATA
82 #######################################################################
83 # WARNING: $config_file is generated by make_patchnum.sh   
84 #          DO NOT EDIT DIRECTLY - edit make_patchnum.sh instead 
85 #######################################################################
86 \\$Config::Git_Data=<<'ENDOFGIT';
87 git_commit_id='$commit_id'
88 git_describe='$describe'
89 git_branch='$branch'
90 git_uncommitted_changes='$changed'
91 git_commit_id_title='$commit_title'
92 $extra_info
93 ENDOFGIT
94 EOFDATA
95 `
96 # only update the files if necessary, other build product depends on these files
97 if [ "$new_config" != "$existing_config" ] || [ "$existing_header" != "$new_header" ]; then
98         echo "Updating $header_file and $config_file"
99         echo "$new_config" > $config_file
100         echo "$new_header" > $header_file
101 else
102         echo "Reusing $header_file and $config_file"
103 fi
104