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

last_version=$(grep -A 2 "%changelog" *.spec | grep "^\*" | awk '{print $NF}')

current_version=$(rpmspec -P *.spec | grep "^Version:" | awk '{print $2}')-$(rpmspec -P *.spec | grep "^Release:" | awk '{print $2}')

if [ "$last_version" != "$current_version" ]; then
   echo "No changelog for current release"
   userstring=$(git log --pretty='format:%an <%ae>' -1)
   since_hash=$(git blame *.spec | grep "\*" | grep "$last_version$" | awk '{print $1}')
   echo -e "* $(date +"%a %b %d %Y") $userstring - $current_version\n$(git log --pretty='format:- %s' $since_hash..HEAD)\n" > ./change
   sed -i "/%changelog/ r ./change" *.spec
   echo "Added change to spec-file:"
   cat ./change
   git add *.spec
   git commit -m 'Updated changelog'
   git push origin master
fi

Post-commit hook

#!/bin/bash

last_version=$(grep -A 2 "%changelog" *.spec | grep "^\*" | awk '{print $NF}')

current_version=$(rpmspec -P *.spec | grep "^Version:" | awk '{print $2}')-$(rpmspec -P *.spec | grep "^Release:" | awk '{print $2}')

if [ "$last_version" != "$current_version" ]; then
   echo "No changelog for current release"
   userstring=$(git log --pretty='format:%an <%ae>' -1)
   since_hash=$(git blame *.spec | grep "\*" | grep "$last_version$" | awk '{print $1}')
   echo -e "* $(date +"%a %b %d %Y") $userstring - $current_version\n$(git log --pretty='format:- %s' $since_hash..HEAD)\n" > /tmp/change
   sed -i "/%changelog/ r /tmp/change" *.spec
   echo "Added change to spec-file:"
   cat /tmp/change
   git add *.spec
   git commit --amend
fi

Tags: ,
Category: Linux | No comments »

Comments

Leave a comment

 Comment Form