Recursive GIT Tree Traverser In Python

Here’s a little script I wrote which will traverse through an online git repo and hopefully display the file tree. There’s probably a command to do this but I thought I’d try to write it anyway to test my recursion skills.

#!/usr/bin/python

import re
import urllib

def mt(tn):
    o = ""
    
    for x in range(0, tn):
        o += "    "
    
    return o

def rr(pd, tn):
    webobj = urllib.urlopen("http://git.fedorahosted.org/git/?p=arm.git;a=tree;f=" + pd)
    webstr = webobj.read()
    
    webstr = webstr.replace("\t", "")
    webstr = webstr.replace("\r", "")
    webstr = webstr.replace("\n", "")
    
    webstr = re.sub("<tr", "\n<tr", webstr)
    
    weblist = webstr.split("\n")
    
    dirlist = pd.split("/")
    
    while (len(dirlist) > 1):
        dirlist.pop(0)
    
    print(mt(tn) + dirlist[0] + "/")
    
    for webitem in weblist:
        if (re.match("^<tr", webitem)):
            webitem = re.sub("<[^>]*>", " ", webitem)
            webitem = webitem.strip()
            webitem = re.sub("  [ ]*", " ", webitem)
            
            itemlist = webitem.split(" ")
            
            if (len(itemlist) < 3):
                continue
            
            if (itemlist[2] == ".."):
                continue
            
            if (not re.match("^d.*", itemlist[0])):
                print(mt(tn + 1) + itemlist[2])
            
            else:
                rr(pd + "/" + itemlist[2], tn + 1)

rr("styrene", 0)

also to get the last check-in name/time:

#!/bin/bash

webpoutp=`curl -s 'http://git.fedorahosted.org/git/?p=arm.git;a=shortlog'`
webpoutp=`echo "$webpoutp" | tr -d '\t\r\n'`
webpoutp=`echo "$webpoutp" | awk '{ gsub(/<td/, "\n<td"); gsub(/<\/td/, "\n</td"); print; }'`
webpoutp=`echo "$webpoutp" | grep -i '^<td'`
webpoutp=`echo "$webpoutp" | head -n 2`
webpoutp=`echo "$webpoutp" | sed -e 's/<[^>]*>//g'`

fnaloutp=""

echo "$webpoutp" | while read lineread
do
	if [ "$fnaloutp" != "" ]
	then
		fnaloutp="${fnaloutp} - "
	fi
	
	fnaloutp="${fnaloutp}${lineread}"
	echo "Last git push: [$fnaloutp]" > /tmp/git.log
done

cat /tmp/git.log

Leave a comment