A Git frontend with Flask part 3
In this article, I'll be going over the commit and inspect pages. The commit page is responsible for showing the changes made in a commit, and the inspect page is responsible for showing the contents of a file.
Keep in mind that this is a very simple web app by design - I wanted to get it stable, then write about it, then update it, then write about the updates.
Commit page
Backend method
This time, I'll start with the backend method as it's not as simple as the one for browsing the repository. The method decoration and definition is:
1 | @app.route('/repo/<repo_name>/<commit_sha>') |
Coming in, I get the name of the repository so I can get the DB model Repository, and the SHA of the commit to get the commit object from GitPython.
First, I'll check the repo_name
argument:
1 | repo = Repository.query.filter_by(name=repo_name).first() |
Pretty simple, and pretty jarring to the user (should definitely be updated later, probably with a flash
of a message).
Next, I'll grab some information from GitPython:
1 | commit = repo.git_repo.commit(commit_sha) |
Here, I grab the commit object, the list of all commits, and the index of the commit that the URL supplied. Next, I need to look forward and backward for the next and previous commits, respectively, for navigation:
1 | previous_commit, next_commit = None, None |
I'm just taking the index that I got before and getting the next and previous items in the list. Next, get the data from the commit:
1 | diff_raw = Markup.escape(repo.git_repo.git.diff(previous_commit, commit)) |
I'm calling GitPython's repo.git.diff
, passing the previous_commit and the commit that the user is actually attempting to see in order to get the changes made by that commit. This data comes as a string, then passed to Flask's Markup (from flask import Markup) to escape any HTML, as I'll be adding HTML to the string before passing it to the template to be rendered (and making use of the |safe template filter).
Here's that "markup":
1 | diff, color = '', None |
I'm wrapping each of the lines in the output with a div with a class dependent on the contents of the line. If the line starts with a +, denoting that it was added in the commit, then I color the line green. Reversely, if the line starts with a -, denoting removal, then the line becomes red. "Information" lines, lines that denote where and which file was changed, are in gray. All other lines are not colored.
Finally, the call to render:
1 | return render_template('commit.html', repo=repo, commit=commit, |
This is just passing the data the method has collected to render_template.
The template
The template, though not as large as the repo.html
template, is still a bit lengthy. Here is that file in full.
The first item of note is the title:
1 | <h3><span title="{{ commit.hexsha }}">{{ commit.hexsha|sha }}</span> by {{ commit.author }} on {{ commit.authored_date|date }}</h3> |
This uses two custom template filters. The first to condense the full SHA into 7 characters, and the second to parse the timestamp into a date:
1 | @app.template_filter('sha') |
Next, there're the previous and next commit links that were found in the backend method:
1 | <div class="row"> |
Note the if checks - if the variable is None, then the link isn't shown at all. The next bit is a table of the commit data which is pretty simple: just getting the data from the Commit object from GitPython.
Yes, that's it. A pre block (which is formatted by Bootstrap) and the data passed through Jinja's |safe filter. The classes added in the backend are defined in a CSS file:
1 | div.code_row { |
Inspect page
Backend method
The backend method here is a bit shorter:
1 | @app.route('/repo/<repo_name>/inspect/<path:code_path>') |
First up, there's the check for the valid DB model entry. Next, I get the blob for the file that the user is trying to see with the call to repo.git_repo.head.commit.tree[code_path]. This f variable now points to a Blob object. I make use if the .data_stream.read().decode method to get the contents of the file. up_directory is used for navigating back to the user's previous place in the repository. The data is passed to the render call, and off to the template.
The template
The inspect.html template is pretty small; it's only a few elements. First, I grab the Code-Prettify JavaScript file from a CDN to add syntax highlighting to the code:
1 | {% block head %} |
This isn't in the content block like all the other templates (except base.html) have been - I'm adding this script to the HTML head.
Next, the Bootstrap breadcrumb so the user can go back:
1 | <ol class="breadcrumb"> |
Then I show the path to the current file and its contents:
1 | <h3>{{ code_path }}</h3> |
Code-Prettify requires the prettyprint class to be added to the pre element, and the code_block class to the parent is defined in a custom CSS file:
1 | div.code_block > pre { |
The formatting of the pre
block here, without the padding added through the code_block class, is very tight and the edges of the code are a bit difficult to see. A little padding solves this problem.
Conclusion
That's the bulk of the app! I've noted several places where improvements need to be made, but it's stable as-is. For a bit of a "Yo, dawg", I'm currently running the app at https://git.celeodor.com and you can browse it's source from the app itself.
Running
If you want to run the app, just do the following:
1 | $ git clone http://bitbucket.org/Celeo/CodeWall |
This will run the app through gunicorn on port 5000. Production/public running should include a reverse proxy to serve the static files. I use Nginx.