Create an RSS feed of your git commits

It’s sometimes hard to stay up-to-date on changes your development team makes to projects. Especially when there’s a flurry of activity. To deal with this, I’ve written a ruby script that takes output from git log and transforms it into a valid RSS feed. It includes name, commit notes, and affected files but it can be easily modified to include more (or less).

Because git’s built-in hooks system is only available client-side, this is set to run via a cron task. One could implement some sort of push system pretty easily with client-side hooks, but the hook scripts would have to be distributed to each of the developers. That seems like a pain. This script is meant to be executed on the server that holds your git repositories.

gitrss.rb

gitrss.rb takes one argument: the path to the repository.

repository_path = $*[0]

git_history = `cd #{repository_path} && git log --max-count=10 --name-status`
entries = git_history.split("\ncommit ")

rss = "<?xml version="1.0" encoding="UTF-8" ?>
  <rss version="2.0">

  <channel>
  <title>your_repository commits</title>
  <description>Git commits to your_repository</description>
  <link>http://example.com/your_respository.xml</link>
  <lastBuildDate>#{Time.now}</lastBuildDate>
  <pubDate>#{Time.now}</pubDate>
"

entries.each do |entry|
  guid = entry.gsub(/^.*commit /ms, '').gsub(/\n.*$/ms, '')
  author_name = entry.gsub(/^.*Author: /ms, '').gsub(/ <.*$/ms, '')
  date = entry.gsub(/^.*Date: +/ms, '').gsub(/\n.*$/ms, '')
  comments = entry.gsub(/^.*Date[^\n]*/ms, '')

  rss += "
    <item>
      <title>Commit by #{author_name}</title>
      <description>#{author_name} made a commit on #{date}</description>
      <content><![CDATA[
        <h2>#{author_name}</h2>
        <p>#{date}</p>
        <pre>#{comments}</pre>
      ]]></content>
      <link></link>
      <guid isPermaLink="false">#{guid}</guid>
      <pubDate>#{date}</pubDate>
    </item>"
end 

rss += "
  </channel>
</rss>"

puts rss

cron

The gitrss.rb script only outputs the RSS XML; we’ll have to redirect the output in cron. Here’s an example cron task that runs this every 30 minutes:

*/30 * * * * root ruby /var/git/gitrss.rb /var/git/your_repository.git > /var/www/your_repository.xml

This cron task will run the gitrss.rb script every 30 minutes and redirect its output to a file called your_repository.xml

Comment on this post

You may use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>