aboutsummaryrefslogtreecommitdiffstats
path: root/bn_django/git_browse/views.py
blob: 23b01919765f930f6b092285ffc46d2f244cc07e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import settings
from django import forms, http, template
from django.contrib.auth.decorators import login_required
from django.shortcuts import get_object_or_404, render_to_response
from django.http import HttpResponse

import os, commands

from models import *

try:
    GITCOMMAND = settings.GITCOMMAND
except AttributeError:
    GITCOMMAND='git'


# Create your views here.

def repo_info(request, repo,branch=None):
    therepo = get_object_or_404(Repository, slug=repo)
    (GITPREFIX, heads, tags) = therepo.scan()
    
    branches = commands.getoutput(GITPREFIX + ' branch')
    filelist = commands.getoutput(GITPREFIX + ' ls-files')
    shortlog = therepo.shortlog()

    return render_to_response('git_browse/repository_info.html', \
                      dict(object=therepo, \
                            filelist=filelist, \
                            tags=tags, \
                            heads=heads, \
                            shortlog=shortlog,))

def view_tree(request, repo, hash=None,branch=None):
    therepo = get_object_or_404(Repository, slug=repo)
    (GITPREFIX, heads, tags) = therepo.scan()
    
    if(hash == None): 
        head_ref = commands.getoutput('cd ' + therepo.path + '; cat HEAD')
        head_ref = head_ref.split()[1]
        hash = commands.getoutput('cd ' +  therepo.path + '; cat ' + head_ref)
    
    tree_ls = commands.getoutput(GITPREFIX + ' ls-tree ' + hash)
    tree_objs = list()
    blob_objs = list()
    for line in tree_ls.splitlines():
        l = line.split()
        if len(l) < 4:
            continue
        if l[1] == 'tree':
            t = Tree(id=l[2])
            t.path = ' '.join(l[3:])
            #if self.path and self.path != '/':
            #    t.path = self.path + '/' + t.path
            t.name = t.path
            t.type = 'tree'
            tree_objs.append(t)
        if l[1] == 'blob':
            i = Blob(id=l[2])
            i.path = ' '.join(l[3:])
            #if self.path and self.path != '/':
            #    i.path = self.path + '/' + i.path
            i.name=i.path
            i.type = 'blob'
            blob_objs.append(i)

    return render_to_response('git_browse/tree.html',
                      dict(object=therepo,
                            blob_objs=blob_objs,
                            tree_objs=tree_objs,
                            hash=hash,
                            all_objs=tree_objs+blob_objs,))

def view_log(request, repo, hash=None):
    therepo = get_object_or_404(Repository, slug=repo)
    (GITPREFIX, heads, tags) = therepo.scan()
    
    logtxt = commands.getoutput(GITPREFIX + ' log | cat')
    log_items = logtxt.split('\ncommit ')
    if (log_items[0] == ''):
        log_items.pop(0)
    if (log_items[0].startswith('commit ')):
        log_items[0] = log_items[0][7:]
    log_objs = list()
    for li in log_items:
        logobj = dict()
        lines = li.splitlines()
        if len(lines) < 3: continue
        logobj['hash'] = lines[0]
        logobj['author'] = lines[1][8:]
        logobj['date'] = lines[2][8:]
        logobj['comment'] = ''
        for l in lines[4:]:
            if l.startswith('    '):
                logobj['comment'] += l[4:] + '\n'
        log_objs.append(logobj)
    
    return render_to_response('git_browse/full_log.html',
                      dict(object=therepo,
                            log_objs=log_objs,))

def view_blob(request, repo, hash):
    therepo = get_object_or_404(Repository, slug=repo)
    (GITPREFIX, heads, tags) = therepo.scan()
    b = Blob(id=hash,repo=therepo)
    b.update()
    try:
        from pygments import highlight
        from pygments.lexers import guess_lexer
        from pygments.formatters import HtmlFormatter
        b.pretty_contents = highlight(b.contents, guess_lexer(b.contents), HtmlFormatter())
    except: 
        b.pretty_contents = None

    return render_to_response('git_browse/blob.html',
                      dict(object=therepo,
                        heads=heads,
                        tags=tags,
                        hash=b.id,
                        size=b.size,
                        contents=b.contents,
                        pretty_contents=b.pretty_contents))

def view_commit(request, repo, hash):
    therepo = get_object_or_404(Repository, slug=repo)
    (GITPREFIX, heads, tags) = therepo.scan()
    c = Commit(id=hash,repo=therepo)
    c.update()

    try:
        from pygments import highlight
        from pygments.lexers import DiffLexer
        from pygments.formatters import HtmlFormatter
        c.pretty_diff = highlight(c.rawdiff, DiffLexer(), HtmlFormatter())
    except: pass

    return render_to_response('git_browse/commit.html',
                      dict(object=therepo, heads=heads, tags=tags,
                      commit=c))

def view_obj(request, repo, hash, branch=None):
    therepo = get_object_or_404(Repository, slug=repo)
    (GITPREFIX, heads, tags) = therepo.scan()
    
    obj_type = commands.getoutput(GITPREFIX + ' cat-file -t  ' + hash)
    obj_contents = commands.getoutput(GITPREFIX + ' cat-file -p  ' + hash)
    obj_size = commands.getoutput(GITPREFIX + ' cat-file -s  ' + hash)

    return render_to_response('git_browse/obj.html',
                      dict(object=therepo,
                        hash=hash,
                        type=obj_type,
                        size=float(obj_size),
                        contents=obj_contents))

def zip(request, repo, hash):
    therepo = get_object_or_404(Repository, slug=repo)
    (GITPREFIX, heads, tags) = therepo.scan()
    t = Tree(id=hash,repo=therepo)
    t.update()

    hr = HttpResponse(mimetype="application/zip")
    hr['Content-Disposition'] = 'filename=%s.zip' % t.id
    archive = commands.getoutput(GITPREFIX + \
        ' archive --format=zip ' + hash + ' | cat')
    hr.write(archive)
    return hr