Generate RPM SPEC changelogs in Git

Date November 8th, 2016 Author Vitaly Agapov

You have to be a bit of a liar to tell a story the right way.

Patrick Rothfuss «The Name of the Wind»

rpm-gitThis little note will be useful for those who is lazy enough for dealing with changelogs in RPM spec-files and lazy enough to implement own solution. I will present the script to work from inside the git repository. It can check if the changelog complies the current version and update the changelog accordingly if needed. No git tags are needed.

There are two variants of the script. One is for running on the build server (Jenkins/Bamboo/TeamCity…) right before building the RPM. And the second one is the post-commit hook. Second option can be useful if your build server has no write access to the repository or if you'd like to keep git history cleaner (this option doesn't create extra commits).

The script needs rpmspec utility to be installed. So if you are using some non-RH distro just look for the needed package. In Ubuntu you need the package named 'rpm'.

So here are the scripts. Find 10 differences and choose / modify the one you need.

Script for build server

01.last_version=$(grep -A 2 "%changelog" *.spec | grep "^\*" | awk '{print $NF}')
02. 
03.current_version=$(rpmspec -P *.spec | grep "^Version:" | awk '{print $2}')-$(rpmspec -P *.spec | grep "^Release:" | awk '{print $2}')
04. 
05.if [ "$last_version" != "$current_version" ]; then
06.   echo "No changelog for current release"
07.   userstring=$(git log --pretty='format:%an <%ae>' -1)
08.   since_hash=$(git blame *.spec | grep "\*" | grep "$last_version$" | awk '{print $1}')
09.   echo -e "* $(date +"%a %b %d %Y") $userstring - $current_version\n$(git log --pretty='format:- %s' $since_hash..HEAD)\n" > ./change
10.   sed -i "/%changelog/ r ./change" *.spec
11.   echo "Added change to spec-file:"
12.   cat ./change
13.   git add *.spec
14.   git commit -m 'Updated changelog'
15.   git push origin master
16.fi

Post-commit hook

01.#!/bin/bash
02. 
03.last_version=$(grep -A 2 "%changelog" *.spec | grep "^\*" | awk '{print $NF}')
04. 
05.current_version=$(rpmspec -P *.spec | grep "^Version:" | awk '{print $2}')-$(rpmspec -P *.spec | grep "^Release:" | awk '{print $2}')
06. 
07.if [ "$last_version" != "$current_version" ]; then
08.   echo "No changelog for current release"
09.   userstring=$(git log --pretty='format:%an <%ae>' -1)
10.   since_hash=$(git blame *.spec | grep "\*" | grep "$last_version$" | awk '{print $1}')
11.   echo -e "* $(date +"%a %b %d %Y") $userstring - $current_version\n$(git log --pretty='format:- %s' $since_hash..HEAD)\n" > /tmp/change
12.   sed -i "/%changelog/ r /tmp/change" *.spec
13.   echo "Added change to spec-file:"
14.   cat /tmp/change
15.   git add *.spec
16.   git commit --amend
17.fi

Tags: ,
Category: Linux | No comments »

Comments

Leave a comment

 Comment Form