Login

Git recent commits template tag

Author:
david
Posted:
August 6, 2008
Language:
Python
Version:
.96
Tags:
templatetag git
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 filter that divides a list into exact columns by davmuz 3 years, 5 months ago
  2. Git Revision Template Tag by sbaechler 2 years, 7 months ago
  3. Paginator TemplateTag by trbs 7 years, 3 months ago
  4. simple text image view by derelm 8 years, 3 months ago
  5. Pagination Alphabetically compatible with paginator_class by vascop 3 years, 2 months ago

Comments

Please login first before commenting.