Login

Git recent commits template tag

Author:
david
Posted:
August 6, 2008
Language:
Python
Version:
.96
Score:
2 (after 2 ratings)

Requires GitPython.

Grabs the most recent commits to a Git repository, as defined in settings.py. Inspired by Simon Willison's about page.

Example:

{% get_recent_commits 10 as commits %}
<ul>
{% for commit in commits reversed %}
    <li>{{ commit.commited_date }} - {{ commit.message }}</li>
{% endfor %}
</ul>
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
    from git import *
    
    from django.conf import settings
    from django.template import Library, Node
    register = Library()
    
    class LatestCommitsNode(Node):
        def __init__(self, num, varname):
            self.num, self.varname = num, varname
    
        def render(self, context):
            repo = Repo(settings.REPO)
            context[self.varname] = repo.commits()[-self.num:]
            return ''
    
    @register.tag        
    def get_recent_commits(parser, token):
        bits = token.contents.split()
        if len(bits) != 4:
            raise TemplateSyntaxError, "get_recent_commits tag takes exactly three arguments"
        if bits[2] != 'as':
            raise TemplateSyntaxError, "second argument to get_recent_commits tag must be 'as'"
        return LatestCommitsNode(int(bits[1]), bits[3])

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 3 months, 1 week ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 3 months, 2 weeks ago
  3. Serializer factory with Django Rest Framework by julio 10 months, 2 weeks ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 11 months ago
  5. Help text hyperlinks by sa2812 12 months ago

Comments

Please login first before commenting.