Skip to content

Instantly share code, notes, and snippets.

@dlo
Created December 7, 2009 07:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dlo/250700 to your computer and use it in GitHub Desktop.
Save dlo/250700 to your computer and use it in GitHub Desktop.
A little online git viewer
#!/usr/bin/env python
from subprocess import *
import os
import re
import cgi
import cgitb
cgitb.enable()
GIT_DIR = "/Users/dloewenherz/gradifi/.git/"
GIT_TREE = "/Users/dloewenherz/gradifi/"
class Object():
def __init__(self, hash, type, repo, name = "", permissions = ""):
self.hash = hash
self.repo = repo
self.type = type
self.name = name
self.permissions = permissions
def __str__(self):
return self.name
def __link__(self):
if self.type == "tree":
return "<a href='?branch=%s'>[D] %s</a>" % (self.hash, self.name)
else:
return "<a href='?blob=%s'>[F] %s</a>" % (self.hash, self.name)
def raw(self):
return self.repo.git_cmd("cat-file -p %s" % self.hash)
class Repository():
def __init__(self, branch = "master", tree = GIT_TREE, dir = GIT_DIR):
self.dir = dir
self.tree = tree
self.branch = branch
def objects(self):
tree_contents = self.ls_tree()
r = re.compile(r"([0-9]{6}) ([a-z]{4}) ([a-z0-9]{40})\t(.*)")
objects = []
for item in r.finditer(tree_contents):
object=Object(repo=self, name=item.group(4), permissions=item.group(1), type=item.group(2), hash=item.group(3))
objects.append(object)
return objects
def status(self):
return self.git_cmd("status")
def ls_tree(self):
return self.git_cmd("ls-tree %s" % self.branch)
def git_cmd(self, cmd):
c = "git --work-tree='%s' --git-dir='%s' %s" % (self.tree, self.dir, cmd)
return Popen(c, shell=True, stdin=PIPE, stdout=PIPE).stdout.read()
form = cgi.FieldStorage()
if 'branch' in form:
branch = form['branch'].value
else:
branch = 'master'
repo = Repository(branch = branch)
if 'blob' in form:
print "Content-type: text/plain"
print ""
blob = form['blob'].value
object = Object(hash = blob, repo=repo, type = "blob")
print object.raw()
else:
print "Content-type: text/html"
print ""
objects = repo.objects()
for object in objects:
print "%s<br/>" % object.__link__()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment