RepoBrowser App for Browsing Source Code

Posted by: thogan 7 years, 3 months ago

Lately I have been working on the writeup about my automated restore testing project. I have quite a bit of shell and python code to go over and adding it all to the single blog post was impractical. I wanted to be able to use snippets, but easily link to the full source in case the reader was interested. There is also a lot of structure there, so being able to see the directory layout would also be helpful to the reader.

To provide this, I sat down this weekend and wrote a DJango app to plug into my Mezzanine based site. The app is RepoBrowser, a simple filesystem browser that renders text files with syntaxhighlighter.

UPDATE: I took down RepoBrowser and will not be using that code any longer. I have moved most of my projects to BitBucket in an effort to shed workload related to operating my own services for code hosting. This post is being preserved, but broken links have been removed. There is no live version of RepoBrowser at this time.

My source code is all filed neatly away in Atlassian Stash, which has a very nice web-based repository browser built-in. Unfortunately, anonymous access for Stash was requested over a year ago, but hasn't yet come to fruition. While the status was recently updated to "In Progress", I had a blog post to write and couldn't wait.

All I needed was something simple that provided access to a directory structure somewhere on disk. All of the fancy Git integration, as in branches, prior commits, comments, etc, could be done without or added later. For this first run, RepoBrowser actually has nothing to do with "repositories", it just provides prettied-up access to a directory. I am running the git clones and pulls on this directory manually for now.

Mezzanine Integration

I felt like this was also a good opportunity to play with extending Mezzanine by building a generic DJango app and wiring it into my main site. The steps required to integrate my new app with Mezzanine were as follows:

1. Use the render function from mezzanine.utils.views

from mezzanine.utils.views import render, paginate
def directory(request, path = ""):
    templates = [ "repobrowser_directory.html" ]
    context = { "dir_links": dir_links, "path": path, "file_list": sorted(file_list) }
    return render(request, templates, context)

2. Include my app's URLs in the project's

urlpatterns = patterns("",
    url(r'^' + REPOBROWSER_URL + '/', include('thoganmez.repobrowser.urls')),


Configured to point to a particular location on the disk, RepoBrowser will list the contents of that directory. Navigation is supported into sub-folders and a bread-crumb back up to the root of the repository is provided. The FileMeta class I wrote does some rudimentary file type evaluation based on file extension.

I wanted a bunch of pretty file-type icons, so I got them from the Open Icon Library. The Gnome themed icons fit my site's theme pretty well, and a little hue shifting fixed the ones that didn't.

When files are rendered, the type information provided by FileMeta is used to select a syntaxhighlighter brush and render the file contents with appropriate coloring. Unfortunately, if a file has no extension, it is rendered without syntax highlighting. I added improvement THOG-8 with the aim of doing some file magic to identify the file type if it does not have an extension.

There is also room to support viewing certain binary file types in the future. The FileMeta class provides "viewer type" information to the renderer, but it is ignored right now (bug THOG-7).


And now for a very meta demonstration: Browse RepoBrowser Source in RepoBrowser


I have discontinued my use of RepoBrowser and have removed broken links from this post.  The original content is otherwise unchanged.

Posted by: thogan