[meld] vcview: Go to parent directory until existing one found (bgo#770076)



commit 4ff3f5ac5cc998a47d43e9b18375a489079f002a
Author: Vasily Galkin <galkin-vv yandex ru>
Date:   Thu Aug 18 11:56:28 2016 +0300

    vcview: Go to parent directory until existing one found (bgo#770076)
    
    After this change when meld is passed non-existing path it opens the first
    existing parent.

 meld/vcview.py |   52 +++++++++++++++++++++++++++++-----------------------
 1 files changed, 29 insertions(+), 23 deletions(-)
---
diff --git a/meld/vcview.py b/meld/vcview.py
index cbc827a..0476d5b 100644
--- a/meld/vcview.py
+++ b/meld/vcview.py
@@ -229,30 +229,36 @@ class VcView(melddoc.MeldDoc, gnomeglade.Component):
         vcs_model = self.combobox_vcs.get_model()
         vcs_model.clear()
 
-        # VC systems work at the directory level, so make sure we're checking
-        # for VC support there instead of on a specific file.
+        # VC systems can be executed at the directory level, so make sure
+        # we're checking for VC support there instead of
+        # on a specific file or on deleted/unexisting path inside vc
         location = os.path.abspath(location or ".")
-        if os.path.isfile(location):
-            location = os.path.dirname(location)
-
-        for avc in vc.get_vcs(location):
-            err_str = ''
-            vc_details = {'name': avc.NAME, 'cmd': avc.CMD}
-
-            if not avc.is_installed():
-                # Translators: This error message is shown when a version
-                # control binary isn't installed.
-                err_str = _("%(name)s (%(cmd)s not installed)")
-            elif not avc.valid_repo(location):
-                # Translators: This error message is shown when a version
-                # controlled repository is invalid.
-                err_str = _("%(name)s (Invalid repository)")
-
-            if err_str:
-                vcs_model.append([err_str % vc_details, avc, False])
-                continue
-
-            vcs_model.append([avc.NAME, avc(location), True])
+        while not os.path.isdir(location):
+            parent_location = os.path.dirname(location)
+            if len(parent_location) >= len(location):
+                # no existing parent: for example unexisting drive on Windows
+                break
+            location = parent_location
+        else:
+            # existing parent directory was found
+            for avc in vc.get_vcs(location):
+                err_str = ''
+                vc_details = {'name': avc.NAME, 'cmd': avc.CMD}
+
+                if not avc.is_installed():
+                    # Translators: This error message is shown when a version
+                    # control binary isn't installed.
+                    err_str = _("%(name)s (%(cmd)s not installed)")
+                elif not avc.valid_repo(location):
+                    # Translators: This error message is shown when a version
+                    # controlled repository is invalid.
+                    err_str = _("%(name)s (Invalid repository)")
+
+                if err_str:
+                    vcs_model.append([err_str % vc_details, avc, False])
+                    continue
+
+                vcs_model.append([avc.NAME, avc(location), True])
 
         valid_vcs = [(i, r[1].NAME) for i, r in enumerate(vcs_model) if r[2]]
         default_active = min(valid_vcs)[0] if valid_vcs else 0


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]