change the package "pretty name" to CDimg|tools
[cdimgtools.git] / gitchangelog.sh
blobc4f62d4e568908ceba48dc5c150674b887945e31
1 #!/bin/sh
2 # gitchangelog.sh - portable script generating a GNU-like changelog from a Git log
3 # Copyright © 2013 Géraud Meyer <graud@gmx.com>
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License version 2 as
6 # published by the Free Software Foundation.
8 # This program is distributed in the hope that it will be useful, but
9 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
10 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
11 # for more details.
13 # You should have received a copy of the GNU General Public License along
14 # with this program. If not, see <http://www.gnu.org/licenses/>.
16 PROGRAM_NAME="gitchangelog.sh"
17 PROGRAM_VERSION="1.1"
18 # Usage:
19 # gitchangelog.sh [<options>] [--] [ {-|<outfile>} [ {-|<git_log_args>} ] ]
21 # parameters
22 GITBODY= NO_GITBODY=yes
23 BLANKLINE=yes
24 TAGS=
25 TAG_PATTERN='[^\n]\+'
26 MERGE=
27 TITLE= NO_TITLE=yes
28 while [ $# -gt 0 ]
30 case $1 in
31 --tags)
32 TAGS=yes ;;
33 --tag-pattern)
34 shift; TAG_PATTERN=${1-} ;;
35 --merge)
36 MERGE=yes ;;
37 --title-only)
38 TITLE=yes NO_TITLE= ;;
39 --git-body)
40 GITBODY=yes NO_GITBODY= ;;
41 --no-blankline)
42 BLANKLINE= ;;
43 --version)
44 echo "$PROGRAM_NAME version $PROGRAM_VERSION"
45 exit ;;
46 --?*)
47 echo "$0: unknown option $1" >&2
48 exit 255 ;;
49 --)
50 shift; break ;;
52 break ;;
53 esac
54 shift
55 done
56 # git repository check
57 test x"${2-}" != x"-" &&
58 test ! -d .git && ! git rev-parse --git-dir >/dev/null &&
60 echo "$0: error not in a git repository" >&2
61 exit 255
63 # output file
64 test $# -gt 0 &&
66 test x"${1-}" != x"-" &&
67 exec >"$1"
68 shift
71 # input source
72 B='{' b='}'
73 s='\'
74 # some shells, like the family of pdksh, behave differently regarding
75 # the backslash in variable substitutions inside a here document
76 if test x"${1-}" = x"-"
77 then cat
78 else git log --date=short ${TAGS:+--decorate} ${1+"$@"}
79 fi |
80 # processing
81 LC_ALL=C sed -e "$(# Transform the GNU sed program into a more portable one
82 LC_ALL=C sed -e 's/\\s/[ \\t]/g
83 s/\\+/\\{1,\\}/g
84 s/; /\
86 s/@n/\\\
88 s/\\t/ /g' <<EOF
89 # Put the tags in the hold space
90 /^commit / {
91 s/^commit [a-zA-Z0-9]\+//
92 s/^ \+(\(.*\))\$/, \1/; s/, /@n/g
93 # conditionnal branch to reset
94 s/^/@n/; t a
95 :a; s/\n\$//; t e
96 s/\(.*\)\ntag: \($TAG_PATTERN\)\$/[\2]@n\1/; t a
97 s/\(.*\)\n.*\$/\1/; t a
98 :e; s/\n/ /g; h; d
100 # Add the merge marker to the hold space
101 /^Merge: / { ${MERGE:+x; s/\$/${B}M$b /; x;} d; }
102 /^Author:/ {
103 # Process author, date, tag
104 ${TAGS:+x; /^\$/! $B s/${s}s*\$//; s/\$/@n/; $b; x; b s;} x; s/.*//; x ${TAGS:+; :s}
105 N; G; s/^Author:\s*\([^\n]*\) \s*\(<[^ \n]*>\)\s*\nDate:\s*\([^\n]*\)\n\(.*\)/\4\3 \1 \2@n/
106 # Process title
107 n; N; s/[^\n]*\n /\t${TITLE:+${NO_GITBODY:+* }}/
108 # If non empty body, print an extra line
109 n; ${NO_TITLE:+${NO_GITBODY:+${BLANKLINE:+/^\$/! $B s/^ /${s}t/p; s/^${s}t/ /; $b}}}
111 ${TITLE:+/^ / d}
112 # First line of the paragraph
113 :b; /^ \$/ { N; s/^ \n//; s/^ \(.\)/${GITBODY:+${s}t@n}\t${NO_GITBODY:+* }\1/; b b; }
114 # Rest of the paragraph
115 s/^ /\t${NO_GITBODY:+ }/
116 # Reset the hold space for the next entry
117 /^\$/ h
120 rc=$?
121 # error check
122 test $rc -eq 0 ||
123 echo "$0: ERROR sed failed with #$rc" >&2
124 exit $rc