Fixed the print view.
[redakcja.git] / lib / wlrepo / mercurial_backend / document.py
old mode 100644 (file)
new mode 100755 (executable)
index f7da32d..77b8ead
@@ -103,7 +103,7 @@ class MercurialDocument(wlrepo.Document):
             self.invoke_and_commit(take_action, \
                 lambda d: ("$AUTO$ File checkout.", user) )
 
             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):
         if self.ismain():
 
     def up_to_date(self):
         if self.ismain():
@@ -111,7 +111,7 @@ class MercurialDocument(wlrepo.Document):
 
         shared = self.shared()
         
 
         shared = self.shared()
         
-        if shared.parentof(self):
+        if shared.ancestorof(self):
             return True
 
         if shared.has_parent_from(self):
             return True
 
         if shared.has_parent_from(self):
@@ -132,45 +132,82 @@ class MercurialDocument(wlrepo.Document):
                 raise wlrepo.UpdateException("Revision %s has children." % self.revision)
 
             shared = self.shared()
                 raise wlrepo.UpdateException("Revision %s has children." % self.revision)
 
             shared = self.shared()
-
-            # the shared version comes from our version
-            if self.parentof(self.shared()):
-                return self
-
-            # no changes since last update
+            #     *
+            #    /|
+            #   * |
+            #   | |
+            #
+            # we carry the latest version
             if shared.ancestorof(self):
             if shared.ancestorof(self):
-                return self
+               return self
 
 
-            # last share was from this branch
-            if shared.has_parent_from(self):
+            #   *
+            #   |\
+            #   | *
+            #   | |
+            #
+            # We just shared
+            if self.parentof(shared):
                 return self
 
                 return self
 
-            if self._revision.merge_with(sv._revision, user=user,\
+            #  s     s  S
+            #  |     |  |
+            #  *<-S  *<-*
+            #  |  |  |  .
+            #
+            #  This is ok (s - shared, S - self)
+
+            if self._revision.merge_with(shared._revision, user=user,\
                 message="$AUTO$ Personal branch update."):
                 return self.latest()
             else:
                 raise wlrepo.UpdateException("Merge failed.")
         finally:
                 message="$AUTO$ Personal branch update."):
                 return self.latest()
             else:
                 raise wlrepo.UpdateException("Merge failed.")
         finally:
-            lock.release()  
+            lock.release()
 
 
-    def share(self, message):
-        lock = self.library.lock()
-        try:            
 
 
-            # nothing to do
-            if self.ismain():
-                return self
+    def would_share(self):
+        if self.ismain():
+            return False, "Main version is always shared"
 
 
-            shared = self.shared()
+        shared = self.shared()
 
 
-            # we just did this - move on
-            if self.parentof(shared):
-                return 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"
 
 
-            # No changes since update
-            if shared.parentof(self):
-                return shared
+    def share(self, message):
+        lock = self.library.lock()
+        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.")
 
 
+            # 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
             # The good situation
             #
             #         * local
@@ -180,15 +217,16 @@ class MercurialDocument(wlrepo.Document):
             #       / |
             # main *  *
             #      |  |
             #       / |
             # main *  *
             #      |  |
-            if shared.ancestorof(self):               
-                success = shared._revision.merge_with(self._revision, user=self.owner, message=message)
+            shared = self.shared()
 
 
+            try:
+                success = shared._revision.merge_with(self._revision, user=self.owner, message=message)
                 if not success:
                     raise wlrepo.LibraryException("Merge failed.")
                 
                 return shared.latest()
                 if not success:
                     raise wlrepo.LibraryException("Merge failed.")
                 
                 return shared.latest()
-
-            raise wlrepo.LibraryException("Unrecognized share-state.")
+            except Abort, e:
+                raise wlrepo.LibraryException( repr(e) )
         finally:
             lock.release()
 
         finally:
             lock.release()