1#!/bin/bash
2
3# Generate old-style ChangeLog files from the GIT log. Ultimately we may
4# wish to insert this into ChangeLog
5
6footer=false
7printdate=true
8directory=.
9modules=true
10quiet=false
11moduleoptions=
12[ -z "$level" ] && level="="
13if [ $(uname) = Darwin ]; then
14   FMT="fmt"
15else
16   FMT="fmt -ut"
17fi
18
19export level
20
21help()
22{  echo "Usage: `basename $0` option ... [version][..version] [path ...]"
23   echo "Options:"
24   echo "    --footer"
25   echo "    --nodate"
26   echo "    --nomodules"
27   echo "    --dir=<dir>"
28}
29
30
31done=false
32while [ $done = false ]; do
33    case "$1" in
34        --help)
35		help
36		exit 0
37		;;
38	--footer)
39		footer=true
40		shift
41		;;
42	--nodate)
43		printdate=false
44		shift
45		moduleoptions+="--nodate"
46		;;
47	--quiet)
48		quiet=true
49		shift
50		;;
51	--wiki)
52		shift
53		;;
54	--nomodules)
55		modules=false
56		shift
57		;;
58	--dir=*)
59		directory="`echo $1 | sed 's/--dir=//'`"
60		shift
61		modules=false
62		;;
63	*)	done=true
64		;;
65    esac
66done
67
68tag="$1"
69
70if [ -z "$tag" ]; then
71   tag=$(cat VERSION)
72fi
73
74# Ensure case-sensitive matching
75LANG=C
76
77case "$tag" in
78   [0-9].[0-9]*.[0-9]*..[0-9].[0-9]*.[0-9]*)
79	vfrom="$(echo $tag | sed 's/\.\..*//')"
80	vto="$(echo $tag | sed 's/.*\.\.//')"
81	;;
82   V[0-9].[0-9]*.[0-9]*..V[0-9].[0-9]*.[0-9]*)
83	vfrom="$(echo $tag | sed -e s/V//g -e 's/\.\..*//')"
84	vto="$(echo $tag | sed -e s/V//g -e 's/.*\.\.//')"
85	;;
86   [0-9].[0-9]*.[0-9]*)
87        vfrom="$tag"
88	vto=
89	;;
90   V[0-9].[0-9]*.[0-9]*)
91        vfrom="$(echo $tag | sed s/V//)"
92	vto=
93	;;
94   *..*)
95	vfrom="$(echo $tag | sed 's/\.\..*//')"
96	vto="$(echo $tag | sed 's/.*\.\.//')"
97	;;
98esac
99shift
100
101vvtag()
102{ case $1 in
103    [0-9].[0-9]*.[0-9]*)
104      echo V$1
105      ;;
106    *)
107      echo $1
108      ;;
109  esac
110}
111
112if [ -z "$vto" ]; then
113  opt=$(vvtag $vfrom)..
114else
115  opt="$(vvtag $vfrom)..$(vvtag $vto)"
116fi
117
118cd $directory
119
120if [ "$quiet" = true ]; then
121  git log "$opt" --pretty=format:"PATCH[%ad]%n%s%n%b" $* | \
122	grep -q '^[A-Z][A-Z]*:'
123  exit $?
124fi
125
126header ()
127{ hdr="$*"
128
129  hdr="$*"
130  eq=`echo $hdr | sed "s/./$level/g"`
131  echo $hdr
132  echo $eq
133
134  echo
135}
136
137(
138if [ "$directory" = "." ]; then
139  if [ -z "$vto" ]; then
140    header "SWI-Prolog Changelog since version $vfrom"
141  else
142    header "SWI-Prolog Changelog from version $vfrom to $vto"
143  fi
144else
145  header "Package `basename $directory`"
146fi
147
148git log "$opt" --pretty=format:"PATCH[%ad]%n%s%n%b" --date-order --simplify-merges $* | awk '
149BEGIN		{ doprint="false";
150		  dateprinted="";
151		}
152/^PATCH[[]/	{ date="["  $2 " " $3 " " $5 "]";
153		  doprint="false";
154		  next;
155	        }
156/^[A-Z][A-Z]*:/	{ if ( dateprinted != date )
157		  { if ( "'$printdate'" == "true" )
158		    { printf("%s\n\n", date);
159		    }
160		    dateprinted = date;
161		  }
162		  printf(" * %s\n", $0);
163		  doprint="true";
164		  next;
165		}
166/.*/		{ if ( doprint == "true" )
167		  { if ( $0 != "<unknown>" )
168		    { printf("   %s\n", $0);
169		    } else
170		    { printf("\n");
171		    }
172		  }
173		  next;
174		}
175' ) | $FMT
176
177# Include submodules
178
179mtag()
180{ git ls-tree $1 $2 | awk '{print $3}'
181}
182
183if [ "$modules" = true ]; then
184  level="-"
185
186  echo
187
188  for d in `git submodule -q foreach pwd`; do
189    mfrom=$(mtag V$vfrom $d)
190    if [ ! -z "$vto" ]; then
191      mto=$(mtag V$vto $d)
192    else
193      mto=$(mtag HEAD $d)
194    fi
195    if [ ! -z "$mfrom" -a ! -z "$mto" ]; then
196      subopts=$mfrom..$mto
197
198      if $0 --dir="$d" --quiet "$subopts"; then
199	echo
200	$0 --dir="$d" $moduleoptions "$subopts"
201#     else
202#       echo "Package `basename $d`: no changes"
203      fi
204    fi
205  done
206fi
207
208if [ "$footer" = true ]; then
209   header VERSION $tag
210fi
211