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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
|
from django.db import models
from django.conf import settings
from django.utils.translation import ugettext as _
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.CharField("path to git dir", max_length=386, unique=True,\
blank=False, default="/srv/git/")
name = models.CharField(_("name"), max_length=80, unique=True)
slug = models.SlugField("short description of repo", unique=True,\
blank=False)
git_version = models.CharField(_("git version"), max_length=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:
list_display = ['name', 'slug', 'path']
ordering = ['name']
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 getGITPREFIX(self):
"""returns the glued together combination of GITCOMMAND and
GITBROWSE_BASE needed to call git commands on this repository"""
return 'cd ' + str(self.path) + '; ' + str(GITCOMMAND) + ' --git-dir='\
+ str(self.path)
def scan(self):
import os
GITPREFIX = self.getGITPREFIX()
heads = dict()
for h in os.listdir(self.path + '/refs/heads/'):
f = open(self.path + '/refs/heads/' + h,'r')
heads[h.strip()] = f.readline().strip()
f.close()
tags = dict()
for t in os.listdir(self.path + '/refs/tags/'):
f = open(self.path + '/refs/tags/' + t,'r')
tags[t.strip()] = f.readline().strip()
f.close()
return (GITPREFIX, heads, tags)
def shortlog(self):
import commands
GITPREFIX = self.getGITPREFIX()
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,max_length=4)
path = models.CharField("relative path from repo base", max_length=512)
id = models.CharField("hash", max_length=40,blank=False,primary_key=True)
name = models.CharField("name of dir", max_length=128,blank=False)
type = 'tree'
class Admin:
list_display = ['path','name','repo', 'id']
ordering = ['repo','path']
list_filter = ['repo','path']
def __str__(self):
return self.name
def get_absolute_url(self):
return "/code/%s/tree/%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
GITPREFIX = self.repo.getGITPREFIX()
self.id = self.id.strip()
tree_ls = commands.getoutput(GITPREFIX + ' ls-tree --full-name ' \
+ self.id)
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
tree_objs.append(t)
if l[1] == 'blob':
b = Blob(id=l[2])
b.path = ' '.join(l[3:])
if self.path and self.path != '/':
b.path = self.path + '/' + b.path
b.name=b.path
blob_objs.append(b)
self.tree_objs = tree_objs
self.blob_objs = blob_objs
self.all_objs = tree_objs + blob_objs
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,max_length=4)
path = models.CharField("relative path from repo base", max_length=512)
id = models.CharField("hash", max_length=40,blank=False,primary_key=True)
name = models.CharField("name of dir", max_length=128,blank=False)
size = models.IntegerField("filesize in byte", max_length=128,blank=False)
contents = models.TextField("ASCII contents of the file")
type='blob'
class Admin:
list_filter = ['repo','path']
list_display = ['name','path','id','size','repo']
search_fields = ['contents']
ordering = ['repo','path','name']
def __str__(self):
return self.name
def get_absolute_url(self):
return "/code/%s/blob/%s/" % (self.repo.slug, self.hash)
def get_admin_url(self):
return "%s/code/blob/%s/" % (ADMIN_URL, self.id)
def update(self):
import commands
if (not self.id) or (not self.repo): return
GITPREFIX = self.repo.getGITPREFIX()
self.id = self.id.strip()
self.contents = commands.getoutput(GITPREFIX + ' cat-file -p ' \
+ self.id)
self.size = commands.getoutput(GITPREFIX + ' cat-file -s ' + self.id)
return
def blob_from_str(s):
s = s.split();
if len(s) != 4: return
return Blob(mode=s[0],id=s[2],name=s[3])
class Commit(models.Model):
repo = models.ForeignKey(Repository)
id = models.CharField("hash", max_length=40,blank=False,primary_key=True)
rawdiff = models.TextField("ASCII contents of full commit diff")
commit_date = models.DateField("Date of commit to repository")
author_date = models.DateField("Date commit was writen/created")
author = models.CharField("Name of commit author", max_length=96)
author_email = models.CharField("Email address of commit author", \
max_length=196)
committer = models.CharField("Name of committer", max_length=96)
committer_email = models.CharField("Email address of committer", \
max_length=196)
comment = models.TextField("Notes on the commit")
parenthash = models.CharField("parent's hash", max_length=40)
#TODO: parent = models.ForeignKey()
treehash = models.CharField("tree object's hash", max_length=40)
tree = models.ForeignKey(Tree)
type='commit'
class Admin:
list_filter = ['repo']
list_display = ['id', 'commit_date', 'author', 'repo']
ordering = ['repo','commit_date','author_date','author']
def __str__(self):
return self.id
def get_absolute_url(self):
return "/code/%s/commit/%s/" % (self.repo.slug, self.hash)
def get_admin_url(self):
return "%s/code/commit/%s/" % (ADMIN_URL, self.id)
def update(self):
import commands,time
if (not self.id) or (not self.repo): return
GITPREFIX = self.repo.getGITPREFIX()
self.id = self.id.strip()
raw = commands.getoutput(GITPREFIX + ' cat-file -p ' + self.id)
raw = raw.splitlines()
if len(raw) < 3: return
self.treehash = raw[0].split()[-1].strip()
if not raw[1].startswith('parent'):
raw.insert(1, 'parent ')
self.parenthash = raw[1][6:].strip()
if raw[2].startswith('author'):
self.author = raw[2].split()[1]
self.author_date = time.ctime(int(raw[2].split()[-2]))
elif raw[3].startswith('author'):
self.author = raw[3].split()[1]
self.author_date = time.ctime(int(raw[3].split()[-2]))
elif raw[4].startswith('author'):
self.author = raw[4].split()[1]
self.author_date = time.ctime(int(raw[4].split()[-2]))
else: return
self.committer = raw[3].split()[1]
self.committer_date = time.ctime(int(raw[3].split()[-2]))
self.rawdiff = commands.getoutput(GITPREFIX + ' diff ' \
+ self.parenthash + ' ' + self.id + ' | cat')
if len(raw) > 4:
for l in raw[4:]:
self.comment += str(l) + '\n'
else:
self.comment = '(none)'
return
from django.contrib import admin
admin.site.register(Repository)
admin.site.register(Tree)
admin.site.register(Blob)
admin.site.register(Commit)
|