aboutsummaryrefslogtreecommitdiffstats
path: root/bn_django/git_browse/models.py
blob: edddf601dafd3214c123a91a7144b4c80341b4e0 (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
from django.db import models
from django.conf import settings

try:
    GITBROWSE_BASE = settings.GITBROWSE_BASE
except AttributeError:
    GITBROWSE_BASE='/home'
if GITBROWSE_BASE[-1] != '/':
    GITBROWSE_BASE += '/'

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

try:
    ADMIN_URL = settings.ADMIN_URL
except AttributeError:
    ADMIN_URL='/admin'
if ADMIN_URL[-1] == '/':
    ADMIN_URL=ADMIN_URL[:-1]


# Create your models here.
class Repository(models.Model):
    path = models.FilePathField("relative path to repository", path=GITBROWSE_BASE,recursive=True,match="^.*\.git$",unique=True,blank=False)
    #path = models.FilePathField("relative path to repository", path='/home/bnewbold/code/',recursive=True,match=".*\.git$",unique=True,blank=False)
    name = models.CharField(_("name"), maxlength=80)
    slug = models.SlugField(prepopulate_from=("path",),unique=True)
    git_version = models.CharField(_("git version"), maxlength=100,default="git version 1.4.4", blank=True, help_text="Output of \'git --version\'")

    description = models.TextField("description of repo",blank=True)

    class Admin: 
        ordering = ['slug']

    def __str__(self):
        return self.name
    def get_absolute_url(self):
        return "/code/%s/" % self.slug
    def get_admin_url(self):
        return "%s/code/repository/%s/" % (ADMIN_URL, self.slug)

    def scan(self):
        import os

        GITPREFIX = 'cd ' + GITBROWSE_BASE + self.slug + '; ' + GITCOMMAND \
                + ' --git-dir=' + GITBROWSE_BASE + self.slug + '/.git '
        heads = dict()
        for h in os.listdir(GITBROWSE_BASE + self.slug + '/.git/refs/heads/'):
            f = open(GITBROWSE_BASE + self.slug + '/.git/refs/heads/' + h,'r')
            heads[h.strip()] = f.readline().strip()
            f.close()
        tags = dict()
        for t in os.listdir(GITBROWSE_BASE + self.slug + '/.git/refs/tags/'):
            f = open(GITBROWSE_BASE + self.slug + '/.git/refs/tags/' + t,'r')
            tags[t.strip()] = f.readline().strip()
            f.close()
        return (GITPREFIX, heads, tags)


    def shortlog(self):
        import commands

        GITPREFIX = 'cd ' + GITBROWSE_BASE + self.slug + '; ' + GITCOMMAND \
                + ' --git-dir=' + GITBROWSE_BASE + self.slug + '/.git '
        logtxt = commands.getoutput(GITPREFIX + ' log --relative-date --max-count=6 | 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:]
        shortlog = list()
        for li in log_items:
            logobj = dict()
            lines = li.splitlines()
            if len(lines) < 3: continue
            logobj['hash'] = lines[0].strip()
            logobj['shorthash'] = lines[0].strip()[:5]
            logobj['author'] = lines[1][8:]
            logobj['date'] = lines[2][8:]
            if len(lines) > 4:
                logobj['description'] = lines[4][4:]
            else:
                logobj['description'] = '(none)'
            # here we truncate commit comments for shortlogs
            logobj['shortdescription'] = logobj['description'][:128]
            shortlog.append(logobj)
        return shortlog
        


class Tree(models.Model):
    repo = models.ForeignKey(Repository)
    mode = models.CharField("file mode/permissions", blank=False,maxlength=4)
    path = models.CharField("relative path from repo base", maxlength=512)
    id = models.CharField("hash", maxlength=40,blank=False,primary_key=True)
    name = models.CharField("name of dir", maxlength=128,blank=False)
    type = 'tree'

    class Admin: 
        ordering = ['repo','path']

    def __str__(self):
        return self.name
    def get_absolute_url(self):
        return "/code/%s/%s" % (self.repo.slug, self.hash)
    def get_admin_url(self):
        return "%s/code/tree/%s/" % (ADMIN_URL, self.id)

def tree_from_str(s):
    s = s.split();
    if len(s) != 4: return 
    return Tree(mode=s[0],id=s[2],name=s[3])

class Blob(models.Model):
    repo = models.ForeignKey(Repository)
    mode = models.CharField("file mode/permissions", blank=False,maxlength=4)
    path = models.CharField("relative path from repo base", maxlength=512)
    id = models.CharField("hash", maxlength=40,blank=False,primary_key=True)
    name = models.CharField("name of dir", maxlength=128,blank=False)
    size = models.IntField("filesize in byte", maxlength=128,blank=False)
    contents = models.TextField("ASCII contents of the file")
    type='blob'

    class Admin: 
        ordering = ['repo','path']

    def __str__(self):
        return self.name
    def get_absolute_url(self):
        return "/code/%s/%s" % (self.repo.slug, self.hash)
    def get_admin_url(self):
        return "%s/code/tree/%s/" % (ADMIN_URL, self.id)
    
    def update(self):
        import commands

        if not self.id: return
        self.id = self.id.strip()
        self.contents = commands.getoutput(GITPREFIX + ' cat-file -p  ' + self.id) 
        self.size = commands.getoutput(GITPREFIX + ' cat-file -s  ' + hash)

        

def blob_from_str(s):
    s = s.split();
    if len(s) != 4: return
    return Blob(mode=s[0],id=s[2],name=s[3])