-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchange_branch.py
310 lines (270 loc) · 12.8 KB
/
change_branch.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
# SPDX-License-Identifier: LGPL-2.1-or-later
# ***************************************************************************
# * *
# * Copyright (c) 2022-2025 The FreeCAD Project Association AISBL *
# * *
# * This file is part of FreeCAD. *
# * *
# * FreeCAD is free software: you can redistribute it and/or modify it *
# * under the terms of the GNU Lesser General Public License as *
# * published by the Free Software Foundation, either version 2.1 of the *
# * License, or (at your option) any later version. *
# * *
# * FreeCAD is distributed in the hope that it will be useful, but *
# * WITHOUT ANY WARRANTY; without even the implied warranty of *
# * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
# * Lesser General Public License for more details. *
# * *
# * You should have received a copy of the GNU Lesser General Public *
# * License along with FreeCAD. If not, see *
# * <https://www.gnu.org/licenses/>. *
# * *
# ***************************************************************************
"""The Change Branch dialog and utility classes and methods"""
import os
from typing import Dict
import addonmanager_freecad_interface as fci
import addonmanager_utilities as utils
from addonmanager_git import initialize_git, GitFailed
try:
from PySide import QtWidgets, QtCore
except ImportError:
try:
from PySide6 import QtWidgets, QtCore
except ImportError:
from PySide2 import QtWidgets, QtCore # pylint: disable=deprecated-module
translate = fci.translate
class ChangeBranchDialog(QtWidgets.QWidget):
"""A dialog that displays available git branches and allows the user to select one to change
to. Includes code that does that change, as well as some modal dialogs to warn them of the
possible consequences and display various error messages."""
branch_changed = QtCore.Signal(str, str)
def __init__(self, path: str, parent=None):
super().__init__(parent)
self.ui = utils.loadUi(os.path.join(os.path.dirname(__file__), "change_branch.ui"))
self.item_filter = ChangeBranchDialogFilter()
self.ui.tableView.setModel(self.item_filter)
self.item_model = ChangeBranchDialogModel(path, self)
self.item_filter.setSourceModel(self.item_model)
self.ui.tableView.sortByColumn(
2, QtCore.Qt.DescendingOrder
) # Default to sorting by remote last-changed date
# Figure out what row gets selected:
git_manager = initialize_git()
if git_manager is None:
return
row = 0
self.current_ref = git_manager.current_branch(path)
selection_model = self.ui.tableView.selectionModel()
for ref in self.item_model.branches:
if ref["ref_name"] == self.current_ref:
index = self.item_filter.mapFromSource(self.item_model.index(row, 0))
selection_model.select(index, QtCore.QItemSelectionModel.ClearAndSelect)
selection_model.select(index.siblingAtColumn(1), QtCore.QItemSelectionModel.Select)
selection_model.select(index.siblingAtColumn(2), QtCore.QItemSelectionModel.Select)
break
row += 1
# Make sure the column widths are OK:
header = self.ui.tableView.horizontalHeader()
header.setSectionResizeMode(QtWidgets.QHeaderView.ResizeToContents)
def exec(self):
"""Run the Change Branch dialog and its various sub-dialogs. May result in the branch
being changed. Code that cares if that happens should connect to the branch_changed
signal."""
if self.ui.exec() == QtWidgets.QDialog.Accepted:
selection = self.ui.tableView.selectedIndexes()
index = self.item_filter.mapToSource(selection[0])
ref = self.item_model.data(index, ChangeBranchDialogModel.RefAccessRole)
if ref["ref_name"] == self.item_model.current_branch:
# This is the one we are already on... just return
return
result = QtWidgets.QMessageBox.critical(
self,
translate("AddonsInstaller", "DANGER: Developer feature"),
translate(
"AddonsInstaller",
"DANGER: Switching branches is intended for developers and beta testers, "
"and may result in broken, non-backwards compatible documents, instability, "
"crashes, and/or the premature heat death of the universe. Are you sure you "
"want to continue?",
),
QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.Cancel,
QtWidgets.QMessageBox.Cancel,
)
if result == QtWidgets.QMessageBox.Cancel:
return
if self.item_model.dirty:
result = QtWidgets.QMessageBox.critical(
self,
translate("AddonsInstaller", "There are local changes"),
translate(
"AddonsInstaller",
"WARNING: This repo has uncommitted local changes. Are you sure you want "
"to change branches (bringing the changes with you)?",
),
QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.Cancel,
QtWidgets.QMessageBox.Cancel,
)
if result == QtWidgets.QMessageBox.Cancel:
return
self.change_branch(self.item_model.path, ref)
def change_branch(self, path: str, ref: Dict[str, str]) -> None:
"""Change the git clone in `path` to git ref `ref`. Emits the branch_changed signal
on success."""
remote_name = ref["ref_name"]
_, _, local_name = ref["ref_name"].rpartition("/")
gm = initialize_git()
if gm is None:
self._show_no_git_dialog()
return
try:
if ref["upstream"]:
gm.checkout(path, remote_name)
else:
gm.checkout(path, remote_name, args=["-b", local_name])
self.branch_changed.emit(self.current_ref, local_name)
except GitFailed:
self._show_git_failed_dialog()
def _show_no_git_dialog(self):
QtWidgets.QMessageBox.critical(
self,
translate("AddonsInstaller", "Cannot find git"),
translate(
"AddonsInstaller",
"Could not find git executable: cannot change branch",
),
QtWidgets.QMessageBox.Ok,
QtWidgets.QMessageBox.Ok,
)
def _show_git_failed_dialog(self):
QtWidgets.QMessageBox.critical(
self,
translate("AddonsInstaller", "git operation failed"),
translate(
"AddonsInstaller",
"Git returned an error code when attempting to change branch. There may be "
"more details in the Report View.",
),
QtWidgets.QMessageBox.Ok,
QtWidgets.QMessageBox.Ok,
)
class ChangeBranchDialogModel(QtCore.QAbstractTableModel):
"""The data for the dialog comes from git: this model handles the git interactions and
returns branch information as its rows. Use user data in the RefAccessRole to get information
about the git refs. RefAccessRole data is a dictionary defined by the GitManager class as the
results of a `get_branches_with_info()` call."""
branches = []
DataSortRole = QtCore.Qt.UserRole
RefAccessRole = QtCore.Qt.UserRole + 1
def __init__(self, path: str, parent=None) -> None:
super().__init__(parent)
gm = initialize_git()
self.path = path
self.branches = gm.get_branches_with_info(path)
self.current_branch = gm.current_branch(path)
self.dirty = gm.dirty(path)
self._remove_tracking_duplicates()
def rowCount(self, parent: QtCore.QModelIndex = QtCore.QModelIndex()) -> int:
"""Returns the number of rows in the model, e.g. the number of branches."""
if parent.isValid():
return 0
return len(self.branches)
def columnCount(self, parent: QtCore.QModelIndex = QtCore.QModelIndex()) -> int:
"""Returns the number of columns in the model, e.g. the number of entries in the git ref
structure (currently 3, 'ref_name', 'upstream', and 'date')."""
if parent.isValid():
return 0
return 3 # Local name, remote name, date
def data(self, index: QtCore.QModelIndex, role: int = QtCore.Qt.DisplayRole):
"""The data access method for this model. Supports four roles: ToolTipRole, DisplayRole,
DataSortRole, and RefAccessRole."""
if not index.isValid():
return None
row = index.row()
column = index.column()
if role == QtCore.Qt.ToolTipRole:
return self.branches[row]["author"] + ": " + self.branches[row]["subject"]
if role == QtCore.Qt.DisplayRole:
return self._data_display_role(column, row)
if role == ChangeBranchDialogModel.DataSortRole:
return self._data_sort_role(column, row)
if role == ChangeBranchDialogModel.RefAccessRole:
return self.branches[row]
return None
def _data_display_role(self, column, row):
dd = self.branches[row]
if column == 2:
if dd["date"] is not None:
q_date = QtCore.QDateTime.fromString(dd["date"], QtCore.Qt.DateFormat.RFC2822Date)
return QtCore.QLocale().toString(q_date, QtCore.QLocale.ShortFormat)
return None
if column == 0:
return dd["ref_name"]
if column == 1:
return dd["upstream"]
return None
def _data_sort_role(self, column, row):
if column == 2:
if self.branches[row]["date"] is not None:
q_date = QtCore.QDateTime.fromString(
self.branches[row]["date"], QtCore.Qt.DateFormat.RFC2822Date
)
return q_date
return None
if column == 0:
return self.branches[row]["ref_name"]
if column == 1:
return self.branches[row]["upstream"]
return None
def headerData(
self,
section: int,
orientation: QtCore.Qt.Orientation,
role: int = QtCore.Qt.DisplayRole,
):
"""Returns the header information for the data in this model."""
if orientation == QtCore.Qt.Vertical:
return None
if role != QtCore.Qt.DisplayRole:
return None
if section == 0:
return translate(
"AddonsInstaller",
"Local",
"Table header for local git ref name",
)
if section == 1:
return translate(
"AddonsInstaller",
"Remote tracking",
"Table header for git remote tracking branch name",
)
if section == 2:
return translate(
"AddonsInstaller",
"Last Updated",
"Table header for git update date",
)
return None
def _remove_tracking_duplicates(self):
remote_tracking_branches = []
branches_to_keep = []
for branch in self.branches:
if branch["upstream"]:
remote_tracking_branches.append(branch["upstream"])
for branch in self.branches:
if (
"HEAD" not in branch["ref_name"]
and branch["ref_name"] not in remote_tracking_branches
):
branches_to_keep.append(branch)
self.branches = branches_to_keep
class ChangeBranchDialogFilter(QtCore.QSortFilterProxyModel):
"""Uses the DataSortRole in the model to provide a comparison method to sort the data."""
def lessThan(self, left: QtCore.QModelIndex, right: QtCore.QModelIndex):
"""Compare two git refs according to the DataSortRole in the model."""
left_data = self.sourceModel().data(left, ChangeBranchDialogModel.DataSortRole)
right_data = self.sourceModel().data(right, ChangeBranchDialogModel.DataSortRole)
if left_data is None or right_data is None:
return right_data is not None
return left_data < right_data