Create an RSS feed of your git commits

It’s some­times hard to stay up-to-date on changes your devel­op­ment team makes to projects. Espe­cially when there’s a flurry of activ­ity. To deal with this, I’ve writ­ten a ruby script that takes out­put from git log and trans­forms it into a valid RSS feed. It includes name, com­mit notes, and affected files but it can be eas­ily mod­i­fied to include more (or less).

Because git’s built-in hooks sys­tem is only avail­able client-side, this is set to run via a cron task. One could imple­ment some sort of push sys­tem pretty eas­ily with client-side hooks, but the hook scripts would have to be dis­trib­uted to each of the devel­op­ers. That seems like a pain. This script is meant to be exe­cuted on the server that holds your git repositories.

gitrss.rb

gitrss.rb takes one argu­ment: 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 out­puts the RSS XML; we’ll have to redi­rect the out­put in cron. Here’s an exam­ple 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 min­utes and redi­rect its out­put 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>