Resources‎ > ‎Standard Procedures‎ > ‎Git‎ > ‎

Generating RSS Feed

If you are working on a collaborative project, you often would like to keep track on what others done to the project. The following explain how to automatically generate a RSS feed upon someone put new changes into the repository.


Gitrss.rb

First, the following Ruby script will generates a RSS feed file from the git repository.  This script is hosted here

require 'digest/md5'
  
def git_rss(repository_path, repository_title)
  git_history = `cd #{repository_path} && git log --max-count=20 --name-status`

  entries = git_history.split("\ncommit ")

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

<channel>
<title>#{repository_title} Git 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, '')
    guid = guid.gsub(/\n.*$/ms, '')
    author_name = entry.gsub(/^.*Author: /ms, '').gsub(/ <.*$/ms, '')
    author_email = entry.gsub(/^.* </ms, '').gsub(/>.*$/ms, '')
    gravatar_hash = Digest::MD5.hexdigest(author_email)
    gravatar = "http://www.gravatar.com/avatar/#{gravatar_hash}"
    date = entry.gsub(/^.*Date: +/ms, '').gsub(/\n.*$/ms, '')
    comments = entry.gsub(/^.*Date[^\n]*/ms, '')
    comment_lines = comments.split("\n")
    comment_lines = comment_lines.map {|line| line.strip}
    first_line_of_comments = comment_lines.find {|line| line =~ /\S/}
    first_line_of_comments ||= " "
  
    rss += "
<item>
<title>#{first_line_of_comments} - #{author_name}</title>
<description>#{author_name} made a commit on #{date}</description>
<content><![CDATA[
<h2>#{author_name}</h2>
<p><a href='http://gravatar.com'><img src='#{gravatar}'> #{author_email}</a></p>
<p>#{date}</p>
<pre>#{comments}</pre>
]]></content>
<link></link>
<guid isPermaLink='false'>#{guid}</guid>
<pubDate>#{date}</pubDate>
</item>"
  end

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


# Command line argument 1: Path to git repository
repository_path = $*[0]
# Command line argument 2: Repository title to be displayed in RSS feed
repository_title = $*[1]
puts git_rss(repository_path, repository_title)

Put this gitrss.rb in /Groups/git folder on project.vislab.usyd.edu.au (vislab's project server).  Then in each project's git repository, put the following file (post-receive) in hooks folder.
You need to change 

post-receive 

#!/bin/sh                                                                       
#                                                                               
# An example hook script for the "post-receive" event.                          
#                                                                               
# The "post-receive" script is run after receive-pack has accepted a pack       
# and the repository has been updated.  It is passed arguments in through       
# stdin in the form                                                             
#  <oldrev> <newrev> <refname>                                                  
# For example:                                                                  
#  aa453216d1b3e49e7f6f98441fa56946ddcd6a20 68f7abf4e6f922807889f52bc043ecd31b7\
9f814 refs/heads/master                                                         
#                                                                               
# see contrib/hooks/ for a sample, or uncomment the next line and               
# rename the file to "post-receive".                                            

#. /usr/share/doc/git-core/contrib/hooks/post-receive-email                     
ruby /Groups/git/gitrss.rb /Groups/GROUPNAME/project_name.git project_name.git > /Library/WebServer/Documents/git_rss/project_name.git.rss


Then, you can subscribe the RSS feed:
http://project.vislab.usyd.edu.au/git_rss/project_name.git.rss

or you can put the RSS feed widget on project's home page.
Comments