Merge branch 'master' into lqc-trunk
[redakcja.git] / lib / wlrepo / mercurial_backend / document.py
old mode 100644 (file)
new mode 100755 (executable)
index 0883d99..77b8ead
@@ -103,91 +103,130 @@ class MercurialDocument(wlrepo.Document):
             self.invoke_and_commit(take_action, \
                 lambda d: ("$AUTO$ File checkout.", user) )
 
-        return self._library.document_for_rev(fullid)
+        return self._library.document_for_revision(fullid)
 
     def up_to_date(self):
-        return self.ismain() or (\
-            self.shared().ancestorof(self) )
+        if self.ismain():
+            return True
+
+        shared = self.shared()
         
-            
+        if shared.ancestorof(self):
+            return True
+
+        if shared.has_parent_from(self):
+            return True
+
+        return False
+                    
     def update(self, user):
         """Update parts of the document."""
         lock = self.library.lock()
         try:
             if self.ismain():
                 # main revision of the document
-                return (True, False)
-            
-            if self._revision.has_children():
-                log.info('Update failed: has children.')
-                # can't update non-latest revision
-                return (False, False)
+                return self
 
-            sv = self.shared()
+            # check for children in this branch
+            if self._revision.has_children(limit_branch=True):
+                raise wlrepo.UpdateException("Revision %s has children." % self.revision)
 
-            if self.parentof(sv):
-                return (True, False)
-
-            if sv.ancestorof(self):
-                return (True, False)
+            shared = self.shared()
+            #     *
+            #    /|
+            #   * |
+            #   | |
+            #
+            # we carry the latest version
+            if shared.ancestorof(self):
+               return self
+
+            #   *
+            #   |\
+            #   | *
+            #   | |
+            #
+            # We just shared
+            if self.parentof(shared):
+                return self
+
+            #  s     s  S
+            #  |     |  |
+            #  *<-S  *<-*
+            #  |  |  |  .
+            #
+            #  This is ok (s - shared, S - self)
 
-            return self._revision.merge_with(sv._revision, user=user,
-                message="$AUTO$ Personal branch update.")
+            if self._revision.merge_with(shared._revision, user=user,\
+                message="$AUTO$ Personal branch update."):
+                return self.latest()
+            else:
+                raise wlrepo.UpdateException("Merge failed.")
         finally:
-            lock.release()  
+            lock.release()
+
+
+    def would_share(self):
+        if self.ismain():
+            return False, "Main version is always shared"
+
+        shared = self.shared()
+
+        # we just did this - move on
+        if self.parentof(shared):
+            return False, "Document has been recetly shared - no changes"
+
+        #     *
+        #    /|
+        #   * *
+        #   |\|
+        #   | *
+        #   | |
+        # Situation above is ok - what we don't want, is:
+        #     *
+        #    /|
+        #   * |
+        #   |\|
+        #   | *
+        #   | |
+        # We want to prevent stuff like this.
+        if self.parent().parentof(shared) and shared.parentof(self):
+            return False, "Preventing zig-zag"
+
+        return True, "All ok"
 
     def share(self, message):
         lock = self.library.lock()
-        try:            
-            if self.ismain():
-                return False # always shared
-
-            user = self._revision.user_name
-            main = self.shared()._revision
-            local = self._revision            
+        try:
+            # check if the document is in "updated" state
+            if not self.up_to_date():
+                raise wlrepo.OutdatedException("You must update your document before share.")
 
-            # Case 1:
+            # now check if there is anything to do
+            need_work, info = self.would_share()
+            
+            if not need_work:
+                return self.shared()          
+      
+            # The good situation
+            #
             #         * local
             #         |
-            #         * <- can also be here!
-            #        /|
+            #        >* 
+            #        ||
             #       / |
             # main *  *
             #      |  |
-            # The local branch has been recently updated,
-            # so we don't need to update yet again, but we need to
-            # merge down to default branch, even if there was
-            # no commit's since last update
-            #
-            # This is actually the only good case!
-            if main.ancestorof(local):
-                success, changed = main.merge_with(local, user=user, message=message)
+            shared = self.shared()
 
+            try:
+                success = shared._revision.merge_with(self._revision, user=self.owner, message=message)
                 if not success:
-                    raise LibraryException("Merge failed.")
-
-                return changed
-            
-            # Case 2:
-            # main *
-            #      |
-            #      * <- this case overlaps with previos one
-            #      |\
-            #      | \
-            #      |  * local
-            #      |  |
-            #
-            # There was a recent merge to the defaul branch and
-            # no changes to local branch recently.
-            #
-            # Nothing to do
-            elif local.ancestorof(main):                
-                return False
-
-            # In all other cases, the local needs an update
-            # and possibly conflict resolution, so fail
-            raise LibraryExcepton("Document not prepared for sharing.")
-        
+                    raise wlrepo.LibraryException("Merge failed.")
+                
+                return shared.latest()
+            except Abort, e:
+                raise wlrepo.LibraryException( repr(e) )
         finally:
             lock.release()