| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
"""Universal tree and/or branch wrapper object for convenience.""" |
| 23 |
|
| 24 |
from PyQt5 import QtGui, QtWidgets |
| 25 |
|
| 26 |
from breezy import controldir, errors, osutils, urlutils |
| 27 |
|
| 28 |
from breezy.plugins.qbrz.lib.i18n import gettext |
| 29 |
|
| 30 |
|
| 31 |
class TreeBranch(object): |
| 32 |
"""Universal tree and/or branch wrapper object.""" |
| 33 |
|
| 34 |
__slots__ = ['location', 'tree', 'branch', 'relpath'] |
| 35 |
|
| 36 |
def __init__(self, location, tree, branch, relpath): |
| 37 |
"""Use open_containg method to create the object.""" |
| 38 |
self.location = location |
| 39 |
self.tree = tree |
| 40 |
self.branch = branch |
| 41 |
self.relpath = relpath |
| 42 |
|
| 43 |
@staticmethod |
| 44 |
def open_containing(location=None, require_tree=False, ui_mode=False, _critical_dialog=QtWidgets.QMessageBox.critical): |
| 45 |
"""Open the branch and tree at location (or in current directory). |
| 46 |
|
| 47 |
@return: initialized TreeBranch if opened successfully, |
| 48 |
None if branch or tree not found. |
| 49 |
|
| 50 |
@param location: URL or local path, if None then current working |
| 51 |
directory will be used. |
| 52 |
@param require_tree: if True then NoWorkingTree error will be |
| 53 |
raised if there is no working tree found. Otherwise it's |
| 54 |
acceptable to open only branch object. |
| 55 |
@param ui_mode: if True show errors in the GUI error dialogs; |
| 56 |
otherwise propagate the error up the stack. |
| 57 |
@param _critical_dialog: hook for testing. |
| 58 |
""" |
| 59 |
if location is None: |
| 60 |
location = osutils.getcwd() |
| 61 |
try: |
| 62 |
(tree, branch, relpath) = controldir.ControlDir.open_containing_tree_or_branch(location) |
| 63 |
if require_tree and tree is None: |
| 64 |
raise errors.NoWorkingTree(location) |
| 65 |
except (errors.NotBranchError, errors.NoWorkingTree) as e: |
| 66 |
if not ui_mode: |
| 67 |
raise |
| 68 |
TreeBranch._report_error(location, e, _critical_dialog) |
| 69 |
return None |
| 70 |
return TreeBranch(location, tree, branch, relpath) |
| 71 |
|
| 72 |
@staticmethod |
| 73 |
def _report_error(location, err, _critical_dialog=QtWidgets.QMessageBox.critical): |
| 74 |
"""Report error in GUI dialog. |
| 75 |
@param location: valid location for which error is reported. |
| 76 |
@param err: error object (NotBranchError or NoWorkingTree). |
| 77 |
@param _critical_dialog: callable to show error dialog. |
| 78 |
""" |
| 79 |
if isinstance(err, errors.NotBranchError): |
| 80 |
text = gettext('Not a branch "%s"') % location |
| 81 |
elif isinstance(err, errors.NoWorkingTree): |
| 82 |
text = gettext('No working tree exists for "%s"') % location |
| 83 |
_critical_dialog(None, gettext("Error"), text, gettext('&Close')) |
| 84 |
|
| 85 |
def is_lightweight_checkout(self): |
| 86 |
"""Return True if location is lightweight checkout.""" |
| 87 |
if self.tree and self.tree.controldir.root_transport.base != self.branch.controldir.root_transport.base: |
| 88 |
return True |
| 89 |
return False |
| 90 |
|
| 91 |
def is_bound(self): |
| 92 |
"""Return True if location is bound branch.""" |
| 93 |
if self.branch.get_bound_location(): |
| 94 |
return True |
| 95 |
return False |
| 96 |
|
| 97 |
def get_type(self): |
| 98 |
"""Return type of the object as string. |
| 99 |
@return: type of object ('tree', 'branch', 'light-checkout', 'bound' or None) |
| 100 |
""" |
| 101 |
if self.branch is None: |
| 102 |
return None |
| 103 |
if self.is_lightweight_checkout(): |
| 104 |
return 'light-checkout' |
| 105 |
elif self.is_bound(): |
| 106 |
return 'bound' |
| 107 |
else: |
| 108 |
if self.tree: |
| 109 |
return 'tree' |
| 110 |
else: |
| 111 |
return 'branch' |
| 112 |
|
| 113 |
def get_root(self): |
| 114 |
"""Return root working directory (or URL for treeless remote branch).""" |
| 115 |
if self.tree: |
| 116 |
return self.tree.basedir |
| 117 |
else: |
| 118 |
url = self.branch.base |
| 119 |
if url.startswith('file://'): |
| 120 |
return urlutils.local_path_from_url(url) |
| 121 |
return url |