- local = self.materialize()
- base = other.merge_parent.materialize()
- remote = other.apply_to(base)
-
- merge = simplemerge.Merge3Text(base, local, remote)
- result = ''.join(merge.merge_lines())
- patch = self.make_patch(local, result)
- return self.children.create(
- patch=patch, merge_parent=other, tree=self.tree,
- author=author,
- author_name=author_name,
- author_email=author_email,
- description=description)
-
- def revert(self, **kwargs):
- """ commit this version of a doc as new head """
- self.tree.commit(text=self.materialize(), **kwargs)
-
-
-def create_tag_model(model):
- name = model.__name__ + 'Tag'
- attrs = {
- '__module__': model.__module__,
- }
- return type(name, (Tag,), attrs)
-
-
-def create_change_model(model):
- name = model.__name__ + 'Change'
-
- attrs = {
- '__module__': model.__module__,
- 'tree': models.ForeignKey(model, related_name='change_set'),
- 'tags': models.ManyToManyField(model.tag_model, related_name='change_set'),
- }
- return type(name, (Change,), attrs)
-
-
-
-class DocumentMeta(ModelBase):
- "Metaclass for Document models."
- def __new__(cls, name, bases, attrs):
- model = super(DocumentMeta, cls).__new__(cls, name, bases, attrs)
- if not model._meta.abstract:
- # create a real Tag object and `stage' fk
- model.tag_model = create_tag_model(model)
- models.ForeignKey(model.tag_model,
- null=True, blank=True).contribute_to_class(model, 'stage')
-
- # create real Change model and `head' fk
- model.change_model = create_change_model(model)
- models.ForeignKey(model.change_model,
- null=True, blank=True, default=None,
- help_text=_("This document's current head."),
- editable=False).contribute_to_class(model, 'head')
-
- return model
-
-
-
-class Document(models.Model):
- """
- File in repository.
- """
- __metaclass__ = DocumentMeta
-
- creator = models.ForeignKey(User, null=True, blank=True, editable=False,
- related_name="created_documents")
-
- user = models.ForeignKey(User, null=True, blank=True)
-
- class Meta:
- abstract = True
-
- def __unicode__(self):
- return u"{0}, HEAD: {1}".format(self.id, self.head_id)
-
- @models.permalink
- def get_absolute_url(self):
- return ('dvcs.views.document_data', (), {
- 'document_id': self.id,
- 'version': self.head_id,
- })
-
- def materialize(self, change=None):
- if self.head is None:
- return u''
- if change is None:
- change = self.head
- elif not isinstance(change, Change):
- change = self.change_set.get(pk=change)
- return change.materialize()
-
- def commit(self, **kwargs):
- if 'parent' not in kwargs:
- parent = self.head