1 from django.db import models
2 from django.contrib.auth.models import User
3 from django.utils.translation import ugettext_lazy as _
4 from mercurial import mdiff, simplemerge
7 class Change(models.Model):
9 Single document change related to previous change. The "parent"
10 argument points to the version against which this change has been
11 recorded. Initial text will have a null parent.
13 Data contains a reverse diff needed to reproduce the initial document.
15 author = models.ForeignKey(User)
16 patch = models.TextField(blank=True)
17 tree = models.ForeignKey('Document')
19 parent = models.ForeignKey('self',
20 null=True, blank=True, default=None,
21 related_name="children")
23 merge_parent = models.ForeignKey('self',
24 null=True, blank=True, default=None,
25 related_name="merge_children")
27 description = models.TextField(blank=True, default='')
28 created_at = models.DateTimeField(auto_now_add=True)
31 ordering = ('created_at',)
33 def __unicode__(self):
34 return "Id: %r, Tree %r, Parent %r, Patch '''\n%s'''" % (self.id, self.tree_id, self.parent_id, self.patch)
37 def make_patch(src, dst):
38 return pickle.dumps(mdiff.textdiff(src, dst))
40 def materialize(self):
41 changes = Change.objects.exclude(parent=None).filter(
43 created_at__lte=self.created_at).order_by('created_at')
45 for change in changes:
46 text = change.apply_to(text)
49 def make_child(self, patch, description):
50 return self.children.create(patch=patch,
52 description=description)
54 def make_merge_child(self, patch, description):
55 return self.merge_children.create(patch=patch,
57 description=description)
59 def apply_to(self, text):
60 return mdiff.patch(text, pickle.loads(self.patch.encode('ascii')))
63 def merge_with(self, other):
64 assert self.tree_id == other.tree_id # same tree
65 if other.parent_id == self.pk:
69 local = self.materialize()
70 base = other.merge_parent.materialize()
71 remote = other.apply_to(base)
73 merge = simplemerge.Merge3Text(base, local, remote)
74 result = ''.join(merge.merge_lines())
75 patch = self.make_patch(local, result)
76 return self.children.create(
78 merge_parent=other, tree=self.tree, description=u"Automatic merge")
81 class Document(models.Model):
86 creator = models.ForeignKey(User)
87 head = models.ForeignKey(Change,
88 null=True, blank=True, default=None,
89 help_text=_("This document's current head."))
92 name = models.CharField(max_length=200,
93 help_text=_("Name for this file to display."))
95 def __unicode__(self):
96 return "{0}, HEAD: {1}".format(self.name, self.head_id)
98 def materialize(self, version=None):
103 elif not isinstance(version, Change):
104 version = self.change_set.get(pk=version)
105 return version.materialize()
107 def commit(self, **kwargs):
108 if 'parent' not in kwargs:
111 parent = kwargs['parent']
112 if not isinstance(parent, Change):
113 parent = Change.objects.get(pk=kwargs['parent'])
115 if 'patch' not in kwargs:
116 if 'text' not in kwargs:
117 raise ValueError("You must provide either patch or target document.")
118 patch = Change.make_patch(self.materialize(version=parent), kwargs['text'])
121 raise ValueError("You can provide only text or patch - not both")
122 patch = kwargs['patch']
125 if parent != old_head:
126 change = parent.make_merge_child(patch, kwargs.get('description', ''))
127 # not Fast-Forward - perform a merge
128 self.head = old_head.merge_with(change)
130 self.head = parent.make_child(patch, kwargs.get('description', ''))
135 return self.changes.all()
138 def listener_initial_commit(sender, instance, created, **kwargs):
140 instance.head = Change.objects.create(
141 author=instance.creator,
142 patch=pickle.dumps(mdiff.textdiff('', '')),
146 models.signals.post_save.connect(Document.listener_initial_commit, sender=Document)