Project

General

Profile

Action Item #294 ยป MakeChangelog.sh

Changelog generator script for PiBox and opkgs. - Hammel, 16 Mar 2014 21:20

 
1
#!/bin/bash
2
# Generate changelogs for any GIT project repositories:
3
# git log v0.5...v0.6 --pretty=format:'%s'
4
# Note: this script requires the use of the cdtools functions.
5
# -------------------------------------------------------------------------------------------
6

    
7
function doHelp
8
{
9
    echo ""
10
    echo "Generate a Changelog for a collection of repositories."
11
    echo ""
12
    echo "$0 [-d | -t <msg> ] -r <repofile> -s <version> -e <version>"
13
    echo "where"
14
    echo "-r <repofile>    File containing cdtools projects for which Changelogs should be generated"
15
    echo "-s <version>     Start tag, such as v0.5."
16
    echo "-e <version>     End tag, such as v0.6."
17
    echo "-t <msg>         Apply end tag (with specified message) before generating Changelog"
18
    echo "-d               Dry run (print but don't perform)"
19
}
20

    
21
# Test a return code.  Write code to RCFILE if not 0.
22
function rcCheck
23
{
24
    rc=$1
25
    shift
26
    msg=$@
27
    if [ $rc -ne 0 ]
28
    then 
29
        echo "$msg"
30
        echo $rc>${RCFILE}
31
    fi
32
    return $rc
33
}
34

    
35
# Enable debug output (aka "dry run")
36
function debug
37
{
38
    if [ $doDebug -eq 1 ]
39
    then
40
        echo $*
41
    fi
42
}
43

    
44
#--------------------------------------------------------------
45
# Read command line arguments
46
#--------------------------------------------------------------
47
CWD=`pwd`
48
ECHO=
49
RCFILE=${CWD}/clrc.$$
50
ERRFILE=${CWD}/clerr.$$
51
REPOS=
52
START=
53
END=
54
MSG=
55
doTag=0
56
doDebug=0
57
while getopts ":de:r:s:t:" Option
58
do
59
    case $Option in
60
    d) doDebug=1;;
61
    r) REPOS=$OPTARG;;
62
    e) END=$OPTARG;;
63
    s) START=$OPTARG;;
64
    t) doTag=1; MSG=$OPTARG;;
65
    *) doHelp; exit 0;;
66
    esac
67
done
68

    
69
#--------------------------------------------------------------
70
# Validate configuration
71
#--------------------------------------------------------------
72
if [ "$REPOS" = "" ]
73
then
74
    doHelp
75
    exit 0
76
fi
77

    
78
if [ ! -f $REPOS ]
79
then
80
    echo "No such file: $REPOS"
81
    doHelp
82
    exit 0
83
fi
84

    
85
if [ "$START" = "" ] || [ "$END" = "" ]
86
then
87
    doHelp
88
    exit 0
89
fi
90

    
91
if [ $doTag -eq 1 ] && [ "$MSG" = "" ]
92
then
93
    doHelp
94
    exit 0
95
fi
96

    
97
if [ $doDebug -eq 1 ]
98
then
99
    ECHO=echo
100
fi
101

    
102
#--------------------------------------------------------------
103
# Build command strings
104
#--------------------------------------------------------------
105
TAGCMD="git tag -a ${END} -m\"${MSG}\" >${ERRFILE} 2>&1"
106
PUSHCMD="git push origin --tags >${ERRFILE} 2>&1"
107
CLCMD="git log ${START}...${END} --pretty=format:'%s'"
108

    
109
# Setup
110
rm -rf logs
111
mkdir logs
112
cd logs
113
LOGDIR=`pwd`
114
cd $CWD
115

    
116
#--------------------------------------------------------------
117
# Process repo list
118
# Format:
119
# repoFile repo [repo ...]
120
#--------------------------------------------------------------
121
cat ${REPOS} | while read line
122
do
123
    # skip comments and blank lines
124
    if [ "`echo $line|cut -c1`" = "#" ] || [ "$line" = "" ]
125
    then
126
        continue;
127
    fi
128

    
129
    echo "------------------------------------"
130

    
131
    # Parse line for repoFile and repo command
132
    # "name" is used as suffix for Changelog
133
    repoFile=`echo ${line}|cut -f1 -d" "`
134
    repo=`echo ${line}|sed 's%'${repoFile}'%%'|sed 's% %%'`
135
    name=`echo ${repo}|cut -f1 -d" "`
136

    
137
    # Validate this line
138
    if [ "$repoFile" = "" ] || [ ! -f `expr $repoFile` ]
139
    then
140
        debug "No such file, skipping: $repoFile"
141
        continue
142
    fi
143
    if [ "$repo" = "" ] 
144
    then
145
        debug "Missing repo, skipping: $repoFile"
146
        continue
147
    fi
148

    
149
    # Change to repo location and generate log file
150
    # Then save log file and cleanup
151
    echo "Generating changelog for: $repo"
152
    . $repoFile
153
    $repo
154
    cd $GM_SRC
155
    debug "PWD=`pwd`"
156
    if [ $doTag -eq 1 ]
157
    then
158
        ${ECHO} eval $TAGCMD
159
        rcCheck $? "Failed: Tag $repo" || break
160
        ${ECHO} eval $PUSHCMD
161
        rcCheck $? "Failed: Push tags for $repo" || break
162
    fi
163
    ${ECHO} eval $CLCMD > ${LOGDIR}/Changelog.${name} 2>${ERRFILE}
164
    rcCheck $? "Failed: Changelog gen for $repo" || break
165
done
166

    
167
if [ -f ${RCFILE} ]
168
then
169
    rc=`cat ${RCFILE}`
170
    err=`cat ${ERRFILE}`
171
    if [ "$err" != "" ]
172
    then
173
        echo "Error: $err"
174
    fi
175
    rm -f ${RCFILE} ${ERRFILE}
176
    exit $rc
177
fi
178
rm -f ${RCFILE} ${ERRFILE}
179

    
180
# Don't do the rest if we're just doing a dry run
181
if [ $doDebug -eq 1 ]
182
then
183
    exit 0
184
fi
185

    
186
#--------------------------------------------------------------
187
# Consilidate changelogs
188
#--------------------------------------------------------------
189
cd $LOGDIR
190
for file in `ls -1`
191
do
192
    if [ -s $file ]
193
    then
194
        repo=`echo $file | cut -f2 -d"."`
195
        hdr="Repository: $repo"
196

    
197
        echo "-----------------------------------------------------" >> Changelog
198
        echo "$hdr" >> Changelog
199
        echo "-----------------------------------------------------" >> Changelog
200

    
201
        # cat $file | sort >> Changelog
202
        cat $file >> Changelog
203
        echo "" >> Changelog
204
    fi
205
done
206

    
207
echo "Changelog generated: "
208
ls -1 $LOGDIR/Changelog
209

    
    (1-1/1)