| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
om> |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
from PyQt5 import QtCore, QtGui, QtWidgets |
| 21 |
from breezy.config import GlobalConfig |
| 22 |
from breezy.conflicts import resolve |
| 23 |
from breezy.workingtree import WorkingTree |
| 24 |
from breezy.plugins.qbrz.lib.i18n import gettext, N_, ngettext |
| 25 |
from breezy.plugins.qbrz.lib.util import ( |
| 26 |
BTN_CLOSE, BTN_REFRESH, |
| 27 |
QBzrWindow, |
| 28 |
get_qbrz_config, |
| 29 |
StandardButton, |
| 30 |
) |
| 31 |
|
| 32 |
try: |
| 33 |
from breezy.cmdline import split as cmdline_split |
| 34 |
except ImportError: |
| 35 |
from breezy.commands import shlex_split_unicode as cmdline_split |
| 36 |
|
| 37 |
try: |
| 38 |
from breezy import mergetools |
| 39 |
except ImportError: |
| 40 |
mergetools = None |
| 41 |
|
| 42 |
|
| 43 |
class ConflictsWindow(QBzrWindow): |
| 44 |
allResolved = QtCore.pyqtSignal(bool) |
| 45 |
|
| 46 |
def __init__(self, wt_dir, parent=None): |
| 47 |
self.merge_action = None |
| 48 |
self.wt = None |
| 49 |
self.wt_dir = wt_dir |
| 50 |
QBzrWindow.__init__(self, |
| 51 |
[gettext("Conflicts")], parent) |
| 52 |
self.restoreSize("conflicts", (550, 380)) |
| 53 |
|
| 54 |
vbox = QtWidgets.QVBoxLayout(self.centralwidget) |
| 55 |
|
| 56 |
self.conflicts_list = QtWidgets.QTreeWidget(self) |
| 57 |
self.conflicts_list.setRootIsDecorated(False) |
| 58 |
self.conflicts_list.setUniformRowHeights(True) |
| 59 |
self.conflicts_list.setSelectionBehavior(QtWidgets.QAbstractItemView.SelectRows) |
| 60 |
self.conflicts_list.setSelectionMode(QtWidgets.QAbstractItemView.ExtendedSelection) |
| 61 |
self.conflicts_list.setHeaderLabels([ |
| 62 |
gettext("File"), |
| 63 |
gettext("Conflict"), |
| 64 |
]) |
| 65 |
self.conflicts_list.selectionModel().selectionChanged[QItemSelection, QItemSelection].connect(self.update_merge_tool_ui) |
| 66 |
self.conflicts_list.customContextMenuRequested[QPoint].connect(self.show_context_menu) |
| 67 |
self.conflicts_list.itemDoubleClicked[QTreeWidgetItem, int].connect(self.launch_merge_tool) |
| 68 |
self.conflicts_list.setContextMenuPolicy(QtCore.Qt.CustomContextMenu) |
| 69 |
self.conflicts_list.setSortingEnabled(True) |
| 70 |
self.conflicts_list.sortByColumn(0, QtCore.Qt.AscendingOrder) |
| 71 |
header = self.conflicts_list.header() |
| 72 |
header.setStretchLastSection(False) |
| 73 |
header.setSectionResizeMode(0, QtWidgets.QHeaderView.Stretch) |
| 74 |
header.setSectionResizeMode(1, QtWidgets.QHeaderView.ResizeToContents) |
| 75 |
vbox.addWidget(self.conflicts_list) |
| 76 |
|
| 77 |
hbox = QtWidgets.QHBoxLayout() |
| 78 |
self.merge_tools_combo = QtWidgets.QComboBox(self) |
| 79 |
self.merge_tools_combo.setEditable(False) |
| 80 |
self.merge_tools_combo.currentIndexChanged[int].connect(self.update_merge_tool_ui) |
| 81 |
|
| 82 |
self.merge_tool_error = QtWidgets.QLabel('', self) |
| 83 |
|
| 84 |
self.program_launch_button = QtWidgets.QPushButton(gettext("&Launch..."), self) |
| 85 |
self.program_launch_button.setEnabled(False) |
| 86 |
self.program_launch_button.clicked.connect(self.launch_merge_tool) |
| 87 |
self.program_label = QtWidgets.QLabel(gettext("M&erge tool:"), self) |
| 88 |
self.program_label.setBuddy(self.merge_tools_combo) |
| 89 |
hbox.addWidget(self.program_label) |
| 90 |
hbox.addWidget(self.merge_tools_combo) |
| 91 |
hbox.addWidget(self.merge_tool_error) |
| 92 |
hbox.addStretch(1) |
| 93 |
hbox.addWidget(self.program_launch_button) |
| 94 |
vbox.addLayout(hbox) |
| 95 |
|
| 96 |
self.create_context_menu() |
| 97 |
|
| 98 |
buttonbox = self.create_button_box(BTN_CLOSE) |
| 99 |
refresh = StandardButton(BTN_REFRESH) |
| 100 |
buttonbox.addButton(refresh, QtWidgets.QDialogButtonBox.ActionRole) |
| 101 |
refresh.clicked.connect(self.refresh) |
| 102 |
|
| 103 |
autobutton = QtWidgets.QPushButton(gettext('Auto-resolve'), |
| 104 |
self.centralwidget) |
| 105 |
autobutton.clicked[bool].connect(self.auto_resolve) |
| 106 |
|
| 107 |
hbox = QtWidgets.QHBoxLayout() |
| 108 |
hbox.addWidget(autobutton) |
| 109 |
hbox.addWidget(buttonbox) |
| 110 |
vbox.addLayout(hbox) |
| 111 |
self.initialize_ui() |
| 112 |
|
| 113 |
def initialize_ui(self): |
| 114 |
config = GlobalConfig() |
| 115 |
if mergetools is not None: |
| 116 |
|
| 117 |
defined_tools = list(config.get_merge_tools().keys()) |
| 118 |
|
| 119 |
defined_tools += list(mergetools.known_merge_tools.keys()) |
| 120 |
|
| 121 |
defined_tools.sort() |
| 122 |
for merge_tool in defined_tools: |
| 123 |
self.merge_tools_combo.insertItem( |
| 124 |
self.merge_tools_combo.count(), merge_tool) |
| 125 |
default_tool = config.get_user_option('bzr.default_mergetool') |
| 126 |
if default_tool is not None: |
| 127 |
self.merge_tools_combo.setCurrentIndex( |
| 128 |
self.merge_tools_combo.findText(default_tool)) |
| 129 |
|
| 130 |
|
| 131 |
self.update_merge_tool_ui() |
| 132 |
|
| 133 |
def create_context_menu(self): |
| 134 |
self.context_menu = QtWidgets.QMenu(self.conflicts_list) |
| 135 |
self.merge_action = QtWidgets.QAction(gettext("&Merge conflict"), |
| 136 |
self.context_menu) |
| 137 |
self.merge_action.triggered[bool].connect(self.launch_merge_tool) |
| 138 |
self.context_menu.addAction(self.merge_action) |
| 139 |
self.context_menu.setDefaultAction(self.merge_action) |
| 140 |
self.context_menu.addAction(gettext('Take "&THIS" version'), |
| 141 |
self.take_this) |
| 142 |
self.context_menu.addAction(gettext('Take "&OTHER" version'), |
| 143 |
self.take_other) |
| 144 |
self.context_menu.addAction(gettext("Mark as &resolved"), |
| 145 |
self.mark_item_as_resolved) |
| 146 |
|
| 147 |
def show(self): |
| 148 |
QBzrWindow.show(self) |
| 149 |
QtCore.QTimer.singleShot(1, self.load) |
| 150 |
|
| 151 |
def refresh(self): |
| 152 |
QtCore.QTimer.singleShot(1, self.load) |
| 153 |
|
| 154 |
def load(self): |
| 155 |
self.wt = wt = WorkingTree.open_containing(self.wt_dir)[0] |
| 156 |
self.set_title([gettext("Conflicts"), wt.basedir]) |
| 157 |
conflicts = self.wt.conflicts() |
| 158 |
items = [] |
| 159 |
for conflict in conflicts: |
| 160 |
item = QtWidgets.QTreeWidgetItem() |
| 161 |
item.setText(0, conflict.path) |
| 162 |
item.setText(1, gettext(conflict.typestring)) |
| 163 |
item.setData(0, QtCore.Qt.UserRole, conflict.file_id or '') |
| 164 |
item.setData(1, QtCore.Qt.UserRole, conflict.typestring) |
| 165 |
items.append(item) |
| 166 |
|
| 167 |
if len(items) == 0 and self.conflicts_list.topLevelItemCount() > 0: |
| 168 |
self.allResolved.emit(True) |
| 169 |
self.conflicts_list.clear() |
| 170 |
self.conflicts_list.addTopLevelItems(items) |
| 171 |
|
| 172 |
def auto_resolve(self): |
| 173 |
while self.wt is None: |
| 174 |
QtWidgets.QApplication.processEvents() |
| 175 |
un_resolved, resolved = self.wt.auto_resolve() |
| 176 |
if len(un_resolved) > 0: |
| 177 |
n = len(resolved) |
| 178 |
QtWidgets.QMessageBox.information(self, gettext('Conflicts'), |
| 179 |
ngettext('%d conflict auto-resolved.', |
| 180 |
'%d conflicts auto-resolved.', |
| 181 |
n) % n, |
| 182 |
gettext('&OK')) |
| 183 |
QtCore.QTimer.singleShot(1, self.load) |
| 184 |
else: |
| 185 |
QtWidgets.QMessageBox.information(self, gettext('Conflicts'), |
| 186 |
gettext('All conflicts resolved.'), |
| 187 |
gettext('&OK')) |
| 188 |
self.close() |
| 189 |
|
| 190 |
def update_merge_tool_ui(self): |
| 191 |
enabled, error_msg = self.is_merge_tool_launchable() |
| 192 |
self.merge_tool_error.setText(error_msg) |
| 193 |
self.program_launch_button.setEnabled(enabled) |
| 194 |
self.merge_action.setEnabled(enabled) |
| 195 |
|
| 196 |
def launch_merge_tool(self): |
| 197 |
items = self.conflicts_list.selectedItems() |
| 198 |
enabled, error_msg = self.is_merge_tool_launchable() |
| 199 |
if not enabled: |
| 200 |
return |
| 201 |
config = GlobalConfig() |
| 202 |
cmdline = config.find_merge_tool(str(self.merge_tools_combo.currentText())) |
| 203 |
file_id = items[0].data(0, QtCore.Qt.UserRole) |
| 204 |
if not file_id: |
| 205 |
|
| 206 |
return |
| 207 |
file_name = self.wt.abspath(self.wt.id2path(file_id)) |
| 208 |
process = QtCore.QProcess(self) |
| 209 |
def qprocess_invoker(executable, args, cleanup): |
| 210 |
def qprocess_error(error): |
| 211 |
self.show_merge_tool_error(error) |
| 212 |
cleanup(process.exitCode()) |
| 213 |
def qprocess_finished(exit_code, exit_status): |
| 214 |
cleanup(exit_code) |
| 215 |
process.error[QProcess.ProcessError].connect(qprocess_error) |
| 216 |
process.finished[int, QProcess.ExitStatus].connect(qprocess_finished) |
| 217 |
process.start(executable, args) |
| 218 |
mergetools.invoke(cmdline, file_name, qprocess_invoker) |
| 219 |
|
| 220 |
def show_merge_tool_error(self, error): |
| 221 |
msg = gettext("Error while running merge tool (code %d)") % error |
| 222 |
QtWidgets.QMessageBox.critical(self, gettext("Error"), msg) |
| 223 |
|
| 224 |
def take_this(self): |
| 225 |
self._resolve_action('take_this') |
| 226 |
|
| 227 |
def take_other(self): |
| 228 |
self._resolve_action('take_other') |
| 229 |
|
| 230 |
def mark_item_as_resolved(self): |
| 231 |
self._resolve_action('done') |
| 232 |
|
| 233 |
def _resolve_action(self, action): |
| 234 |
items = self.conflicts_list.selectedItems() |
| 235 |
file_names = [] |
| 236 |
for item in items: |
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
file_names.append(str(item.text(0))) |
| 243 |
resolve(self.wt, file_names, action=action) |
| 244 |
self.refresh() |
| 245 |
|
| 246 |
def show_context_menu(self, pos): |
| 247 |
self.context_menu.popup(self.conflicts_list.viewport().mapToGlobal(pos)) |
| 248 |
|
| 249 |
def is_merge_tool_launchable(self): |
| 250 |
if mergetools is None: |
| 251 |
return False, gettext("Bazaar 2.4 or later is required for external mergetools support") |
| 252 |
items = self.conflicts_list.selectedItems() |
| 253 |
error_msg = "" |
| 254 |
enabled = True |
| 255 |
if len(items) != 1 or items[0].data(1, QtCore.Qt.UserRole) != "text conflict": |
| 256 |
enabled = False |
| 257 |
config = GlobalConfig() |
| 258 |
tool = str(self.merge_tools_combo.currentText()) |
| 259 |
cmdline = config.find_merge_tool(tool) |
| 260 |
if cmdline is None: |
| 261 |
error_msg = gettext("Set up external_merge app in qconfig under the Merge tab") |
| 262 |
enabled = False |
| 263 |
elif not mergetools.check_availability(cmdline): |
| 264 |
enabled = False |
| 265 |
error_msg = gettext("External merge tool %(tool)s is not available") % \ |
| 266 |
{ 'tool': tool } |
| 267 |
return enabled, error_msg |
| 268 |
|
| 269 |
def is_extmerge_definition_valid(self, showErrorDialog): |
| 270 |
bzr_config = GlobalConfig() |
| 271 |
extmerge_tool = bzr_config.get_user_option("external_merge") |
| 272 |
|
| 273 |
flags = "%r" |
| 274 |
try: |
| 275 |
extmerge_tool.rindex('%r') |
| 276 |
flags = "%b" |
| 277 |
extmerge_tool.rindex('%b') |
| 278 |
flags = "%t" |
| 279 |
extmerge_tool.rindex('%t') |
| 280 |
flags = "%o" |
| 281 |
extmerge_tool.rindex('%o') |
| 282 |
except ValueError: |
| 283 |
if showErrorDialog: |
| 284 |
QtWidgets.QMessageBox.critical(self, gettext("Error"), |
| 285 |
gettext("The extmerge definition: '%(tool)s' is invalid.\n" |
| 286 |
"Missing the flag: %(flags)s. " |
| 287 |
"This must be fixed in qconfig under the Merge tab.") % { |
| 288 |
'tool': extmerge_tool, |
| 289 |
'flags': flags, |
| 290 |
}) |
| 291 |
return gettext("Missing the flag: %s. Configure in qconfig under the merge tab.") % flags |
| 292 |
return "" |
| 293 |
|
| 294 |
|
| 295 |
if 0: |
| 296 |
N_("path conflict") |
| 297 |
N_("contents conflict") |
| 298 |
N_("text conflict") |
| 299 |
N_("duplicate id") |
| 300 |
N_("duplicate") |
| 301 |
N_("parent loop") |
| 302 |
N_("unversioned parent") |
| 303 |
N_("missing parent") |
| 304 |
N_("deleting parent") |
| 305 |
N_("non-directory parent") |