diff options
Diffstat (limited to 'bn_django/git_wiki/models.py')
-rw-r--r-- | bn_django/git_wiki/models.py | 155 |
1 files changed, 154 insertions, 1 deletions
diff --git a/bn_django/git_wiki/models.py b/bn_django/git_wiki/models.py index 71a8362..4201a84 100644 --- a/bn_django/git_wiki/models.py +++ b/bn_django/git_wiki/models.py @@ -1,3 +1,156 @@ from django.db import models +from settings import * +from django.conf import settings -# Create your models here. +try: + ADMIN_URL = settings.ADMIN_URL +except AttributeError: + ADMIN_URL='/admin' +if ADMIN_URL[-1] == '/': + ADMIN_URL=ADMIN_URL[:-1] + +GITPREFIX = 'cd ' +str(GITWIKI_BASE) + '; ' + str(GITCOMMAND) + ' --git-dir=' \ + + str(GITWIKI_BASE) + '/.git ' + +def reposcan(): + import os + heads = dict() + for h in os.listdir(GITWIKI_BASE + self.slug + '/.git/refs/heads/'): + f = open(GITWIKI_BASE + self.slug + '/.git/refs/heads/' + h,'r') + heads[h.strip()] = f.readline().strip() + f.close() + tags = dict() + for t in os.listdir(GITWIKI_BASE + self.slug + '/.git/refs/tags/'): + f = open(GITWIKI_BASE + self.slug + '/.git/refs/tags/' + t,'r') + tags[t.strip()] = f.readline().strip() + f.close() + return (heads, tags) + +def shortlog(hash=None,tree=None): + import commands + + if tree: + hash=tree.id + logtxt = commands.getoutput(GITPREFIX \ + + ' log --relative-date --max-count=6 | cat') + print GITPREFIX + print logtxt + 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): + 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' + def slug(self): + #TODO: secure this + return self.name.strip().lower() + + class Admin: + ordering = ['path','name'] + + def __str__(self): + return self.name + def get_absolute_url(self): + return "/k/%s/" % self.slug() + def get_admin_url(self): + return "%s/k/%s/" % (ADMIN_URL, self.id) + +class Item(models.Model): + 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.IntegerField("filesize in byte", maxlength=128,blank=False) + contents = models.TextField("ASCII contents of the file") + type='blob' + + class Admin: + ordering = ['path','name'] + def __str__(self): + return self.name + def get_absolute_url(self): + return "/k/%s/" % self.slug() + def get_admin_url(self): + return "%s/k/%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 ' + self.id) + return + +class Commit(models.Model): + id = models.CharField("hash", maxlength=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") + author_email = models.DateField("Email address of commit author") + committer = models.CharField("Name of committer") + committer_email = models.DateField("Email address of committer") + comment = models.TextField("Notes on the commit") + parenthash = models.CharField("parent's hash", maxlength=40) + #TODO: parent = models.ForeignKey() + treehash = models.CharField("tree object's hash", maxlength=40) + tree = models.ForeignKey(Tree) + type='commit' + + class Admin: + ordering = ['commit_date','author_date','author'] + def __str__(self): + return self.id + def get_absolute_url(self): + return "/k/commit/%s/" % (self.id) + def get_admin_url(self): + return "%s/k/commit/%s/" % (ADMIN_URL, self.id) + + def update(self): + import commands,time + if (not self.id): return + + self.id = self.id.strip() + raw = commands.getoutput(GITPREFIX + ' cat-file -p ' + self.id) + self.rawdiff = commands.getoutput(GITPREFIX + ' diff ' + self.id \ + +' | cat') + + raw = raw.splitlines() + if len(raw) < 3: return + self.treehash = raw[0].split()[-1].strip() + self.parenthash = raw[1][6:].strip() + self.author = raw[2].split()[1] + self.author_date = time.ctime(int(raw[2].split()[-2])) + self.committer = raw[3].split()[1] + self.committer_date = time.ctime(int(raw[3].split()[-2])) + if len(raw) > 4: + for l in raw[4:]: + self.comment += str(l) + '\n' + else: + self.comment = '(none)' + return |