intertwingly

It’s just data

Monitoring Working Directories


As git gives me no excuse not to, pretty much everything I do is now under version control, be it scripts, web pages, configuration files, personal applications or clones of projects that are hosted elsewhere.  This gives me the freedom to experiment at will, confident that anything that doesn’t work out can be backed out.

As a side effect of gitweb, I have a symbolic link to each project from /var/cache/git, and ready access to history.

What I have found I’m missing is ready access to information about whether my working directories are clean, i.e., whether or not I have uncommitted changes or changes that need to be pushed.

This is easily rectified with a simple script:

#!/usr/bin/ruby
require 'rubygems'
require 'cgi-spa'
require 'pathname'

$cgi.html do |x|
  x.head do
    x.title 'Git status'
    x.meta :charset => "utf-8"

    x.style! %{
      body {margin:0}
       h1 {
         background: #F2E5E5; color: #828; text-align: center;
         font-weight: normal; padding: 10px 0; margin: 0;
       }
       h2 {
         background: #E5E5F2; font: 20px normal; clear: both;
         padding: 12px 0; margin: 0 0 0 1em; float:left; width: 8em;
       }
       pre {
         background: #F2F2E5; padding: 1em 2em; margin: 0; clear: both;
         border-left: 6em solid #E5E5F2
       }
       p {
         top: 50%; height: 20px; overflow: hidden; background: #E5F2E5;
         border: solid 16px #E5F2E5; margin: 0
       }
       a {text-decoration: none}
       a:hover {text-decoration: underline}
       h1 a:visited {color: #828}
       h2 a:visited {color: blue}
       hr {clear: both; margin: 0; padding: 0}
       body {background-color: #E5E5F2}
    }
  end

  x.body do
    x.h1 do
     x.a 'Git Project Status', :href => 'gitweb.cgi'
    end

    Dir.chdir '/var/cache/git' do
      Dir['*'].sort.each do |path|
        x.h2 do
          x.a path, :href => "gitweb.cgi?p=#{path}"
        end
        x.p open("#{path}/description").read.strip
        Dir.chdir Pathname.new(path).realpath.parent do
          status = `git status`.gsub(/.*use "git.*\n/,'')
          unless status =~ /\A# On branch.*\n.*\(working directory clean\)\Z/
            x.pre status.gsub(/^#/,'').gsub(/\s+\Z/,'')
          end
        end
      end
    end
  end
end

At the moment, this script only accesses the repositories in a read-only manner.