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
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"
19 # gitchangelog.sh [<options>] [--] [ {-|<outfile>} [ {-|<git_log_args>} ] ]
22 GITBODY
= NO_GITBODY
=yes
34 shift; TAG_PATTERN
=${1-} ;;
38 TITLE
=yes NO_TITLE
= ;;
40 GITBODY
=yes NO_GITBODY
= ;;
44 echo "$PROGRAM_NAME version $PROGRAM_VERSION"
47 echo "$0: unknown option $1" >&2
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
66 test x
"${1-}" != x
"-" &&
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"-"
78 else git log --date=short ${TAGS:+--decorate} ${1+"$@"}
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
89 # Put the tags in the hold space
91 s/^commit [a-zA-Z0-9]\+//
92 s/^ \+(\(.*\))\$/, \1/; s/, /@n/g
93 # conditionnal branch to reset
96 s/\(.*\)\ntag: \($TAG_PATTERN\)\$/[\2]@n\1/; t a
97 s/\(.*\)\n.*\$/\1/; t a
100 # Add the merge marker to the hold space
101 /^Merge: / { ${MERGE:+x; s/\$/${B}M$b /; x;} d; }
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/
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}}}
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
123 echo "$0: ERROR sed failed with #$rc" >&2