From c27cc047ec9ade2b7e02004962d8d72b7f5cf96e Mon Sep 17 00:00:00 2001 From: "G.raud" Date: Sat, 28 Apr 2018 13:33:24 +0200 Subject: [PATCH] gitchangelog.sh: update to version 1.3 --- gitchangelog.sh | 180 +++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 134 insertions(+), 46 deletions(-) diff --git a/gitchangelog.sh b/gitchangelog.sh index c4f62d4..92dd6db 100755 --- a/gitchangelog.sh +++ b/gitchangelog.sh @@ -1,30 +1,93 @@ #!/bin/sh -# gitchangelog.sh - portable script generating a GNU-like changelog from a Git log -# Copyright © 2013 Géraud Meyer -# This program is free software; you can redistribute it and/or modify -# it under the terms of the GNU General Public License version 2 as -# published by the Free Software Foundation. +# Copyright © 2013,2014 Géraud Meyer # -# This program is distributed in the hope that it will be useful, but -# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY -# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License -# for more details. +# This program is free software; you can redistribute it and/or modify it under +# the terms of the GNU General Public License version 2 as published by the +# Free Software Foundation. # -# You should have received a copy of the GNU General Public License along -# with this program. If not, see . +# This program is distributed in the hope that it will be useful, but WITHOUT +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more +# details. +# +# You should have received a copy of the GNU General Public License along with +# this program. If not, see . PROGRAM_NAME="gitchangelog.sh" -PROGRAM_VERSION="1.1" -# Usage: -# gitchangelog.sh [] [--] [ {-|} [ {-|} ] ] +PROGRAM_VERSION="1.3" +help_usage () { + cat <<"HelpMessage" +Name + gitchangelog.sh - portable script generating a GNU-like changelog from a Git log +Usage + gitchangelog.sh [] [--] [ {-|} [] ] + gitchangelog.sh [] [--] {-|} - +Options + --tags + Prepend changelog entries by tag names in brackets. + --tag-pattern + Tags to consider for marking entries. The pattern should not match + across spaces; the default is "[^ ]\{1,\}". + --merge + Prepend changelog entries by merge marks. + --title-only + Only keep the title of a commit. This implies --title-star, that can + be disabled by a supplemental --git-body. + --title-star + Prepend the title by a star as is done with the other paragraphs. + --git-body + Do not reformat the commit message, only reindent it. + --no-blankline + Do not separate the title from the rest by a blank line. + --version + --help, --helpm +HelpMessage +} +help_message () { + help_usage + cat <<"HelpMessage" +Description + Each commit message is made into a changelog entry. Each paragraph of a + Git commit message body is made into a star item of the changelog. The + message title is kept on a separate line. Text lines are never broken nor + joined. + + By default git is run to get the log, with the required options. If a dash + is given in place of additional , then the log is read from + the standard input; in that case pass the option --date=short and possibly + also --decorate to git to get the appropriate log format. +Portability + The script is portable to POSIX sh and to other less comformant shells such + as pdksh. + + The sed code is portable to POSIX sed but requires only Simple Regular + Expressions (SRE) instead of Basic Regular Expressions (BRE) which makes it + prone to work under Solaris and old BSDs. git-log is required if the log + is not read from stdin. +Exit Status + The exit status of sed is returned if it fails; otherwise the one of the + command generating the git log (git or cat) is returned. +Examples + $ ./gitchangelog.sh --title-star --no-blankline GNUChangeLog + $ ./gitchangelog.sh --tags --tag-pattern 'release\/[^ ]*' -- - --date-order | + sed 's/^\[release/^L\[release/' > ChangeLog + $ git log --date=short --decorate --first-parent | + ./gitchangelog.sh --merge ChangeLog - +Author + gitchangelog.sh was written by G.raud Meyer. +See also + git-log(1) +HelpMessage +} # parameters GITBODY= NO_GITBODY=yes BLANKLINE=yes TAGS= -TAG_PATTERN='[^\n]\+' +TAG_PATTERN='[^ ]\+' MERGE= TITLE= NO_TITLE=yes +TITLESTAR= while [ $# -gt 0 ] do case $1 in @@ -35,7 +98,9 @@ do --merge) MERGE=yes ;; --title-only) - TITLE=yes NO_TITLE= ;; + TITLE=yes NO_TITLE= TITLESTAR=yes ;; + --title-star) + TITLESTAR=yes ;; --git-body) GITBODY=yes NO_GITBODY= ;; --no-blankline) @@ -43,6 +108,10 @@ do --version) echo "$PROGRAM_NAME version $PROGRAM_VERSION" exit ;; + --help) + help_usage; exit ;; + --helpm) + help_message; exit ;; --?*) echo "$0: unknown option $1" >&2 exit 255 ;; @@ -53,60 +122,74 @@ do esac shift done +ANNOTATE= +[ -n "$TAGS" ] || [ -n "$MERGE" ] && ANNOTATE=yes # git repository check test x"${2-}" != x"-" && -test ! -d .git && ! git rev-parse --git-dir >/dev/null && -{ - echo "$0: error not in a git repository" >&2 - exit 255 -} +if test -d .git || git rev-parse --git-dir >/dev/null +then : +else + echo >&2 "$0: ERROR not in a git repository" + exit 254 +fi # output file -test $# -gt 0 && -{ +if test $# -gt 0 +then test x"${1-}" != x"-" && exec >"$1" shift -} +fi # input source B='{' b='}' s='\' - # some shells, like the family of pdksh, behave differently regarding - # the backslash in variable substitutions inside a here document + # Some shells, like the family of pdksh, behave differently regarding + # the backslash in variable substitutions inside a here document. +NL=' +' + # pdksh and posh do not keep a newline preceded by a backslash inside a + # single quoted string inside two levels of command substitution (when the + # second level is double quoted). +exec 4>&1 # to pass stdout to the following process substitution +eval "$( exec 3>&1 1>&4 4>&- # eval will set $gitrc and $rc if test x"${1-}" = x"-" -then cat -else git log --date=short ${TAGS:+--decorate} ${1+"$@"} +then cat; echo >&3 gitrc=$? +else git log --date=short ${ANNOTATE:+--decorate} ${1+"$@"}; echo >&3 gitrc=$? fi | # processing LC_ALL=C sed -e "$(# Transform the GNU sed program into a more portable one LC_ALL=C sed -e 's/\\s/[ \\t]/g s/\\+/\\{1,\\}/g - s/; /\ -/g - s/@n/\\\ -/g - s/\\t/ /g' <\)\s*\nDate:\s*\([^\n]*\)\n\(.*\)/\4\3 \1 \2@n/ + ${ANNOTATE:+x; s/${s}s*\$/@n/; x; b s;} x; s/.*/@n/; x${ANNOTATE:+; :s} + N; G; s/^Author:\s*\(.*\) \s*\(<[^<>]*>\)\s*\nDate:\s*\(.*\)\n\(.*\n\)\$/\4\3 \1 \2@n/ + s/^\n// # Process title - n; N; s/[^\n]*\n /\t${TITLE:+${NO_GITBODY:+* }}/ + n; N; s/^.*\n /\t${TITLESTAR:+${NO_GITBODY:+* }}/ + t t; :t; n; s/^ \(.\+\)\$/\t${TITLESTAR:+${NO_GITBODY:+ }}\1/; t t # If non empty body, print an extra line - n; ${NO_TITLE:+${NO_GITBODY:+${BLANKLINE:+/^\$/! $B s/^ /${s}t/p; s/^${s}t/ /; $b}}} + ${NO_TITLE:+${NO_GITBODY:+${BLANKLINE:+/^\$/ !$B s/^ /${s}t/p; s/^${s}t/ /; $b}}} } ${TITLE:+/^ / d} # First line of the paragraph @@ -116,9 +199,14 @@ LC_ALL=C sed -e "$(# Transform the GNU sed program into a more portable one # Reset the hold space for the next entry /^\$/ h EOF -)" -rc=$? +)" # sed +echo >&3 rc=$? +)" # eval +exec 4>&- + # error check +test $gitrc -eq 0 || +echo >&2 "$0: ERROR git or cat failed with #$gitrc" test $rc -eq 0 || -echo "$0: ERROR sed failed with #$rc" >&2 -exit $rc +{ echo >&2 "$0: ERROR sed failed with #$rc"; exit $rc; } +exit $gitrc -- 2.11.4.GIT