|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2015 - 2021 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing a dialog to enter data for a Push operation. |
|
8 """ |
|
9 |
|
10 from PyQt5.QtCore import pyqtSlot, Qt |
|
11 from PyQt5.QtWidgets import QDialog, QTreeWidgetItem, QComboBox |
|
12 |
|
13 from .Ui_GitPushDialog import Ui_GitPushDialog |
|
14 |
|
15 |
|
16 class GitPushDialog(QDialog, Ui_GitPushDialog): |
|
17 """ |
|
18 Class implementing a dialog to enter data for a Push operation. |
|
19 """ |
|
20 PushColumn = 0 |
|
21 LocalBranchColumn = 1 |
|
22 RemoteBranchColumn = 2 |
|
23 ForceColumn = 3 |
|
24 |
|
25 def __init__(self, vcs, repodir, parent=None): |
|
26 """ |
|
27 Constructor |
|
28 |
|
29 @param vcs reference to the git object |
|
30 @param repodir directory name of the local repository (string) |
|
31 @param parent reference to the parent widget (QWidget) |
|
32 """ |
|
33 super().__init__(parent) |
|
34 self.setupUi(self) |
|
35 |
|
36 self.__vcs = vcs |
|
37 self.__repodir = repodir |
|
38 |
|
39 remoteUrlsList = self.__vcs.gitGetRemoteUrlsList(self.__repodir) |
|
40 self.__repos = {name: url for name, url in remoteUrlsList} |
|
41 |
|
42 remoteBranches = self.__vcs.gitGetBranchesList(self.__repodir, |
|
43 remotes=True) |
|
44 self.__remotes = {} |
|
45 for remoteBranch in remoteBranches: |
|
46 repo, branch = remoteBranch.rsplit("/", 2)[-2:] |
|
47 if repo not in self.__remotes: |
|
48 self.__remotes[repo] = [] |
|
49 self.__remotes[repo].append(branch) |
|
50 |
|
51 self.__localBranches = self.__vcs.gitGetBranchesList(self.__repodir, |
|
52 withMaster=True) |
|
53 |
|
54 self.remotesComboBox.addItems(sorted(self.__repos.keys())) |
|
55 |
|
56 for localBranch in self.__localBranches: |
|
57 itm = QTreeWidgetItem(self.branchesTree, ["", localBranch, "", ""]) |
|
58 combo = QComboBox() |
|
59 combo.setEditable(True) |
|
60 combo.setSizeAdjustPolicy( |
|
61 QComboBox.SizeAdjustPolicy.AdjustToContents) |
|
62 self.branchesTree.setItemWidget( |
|
63 itm, GitPushDialog.RemoteBranchColumn, combo) |
|
64 |
|
65 self.__resizeColumns() |
|
66 |
|
67 self.branchesTree.header().setSortIndicator( |
|
68 GitPushDialog.LocalBranchColumn, Qt.SortOrder.AscendingOrder) |
|
69 |
|
70 self.forceWarningLabel.setVisible(False) |
|
71 |
|
72 index = self.remotesComboBox.findText("origin") |
|
73 if index == -1: |
|
74 index = 0 |
|
75 self.remotesComboBox.setCurrentIndex(index) |
|
76 |
|
77 def __resizeColumns(self): |
|
78 """ |
|
79 Private slot to adjust the column sizes. |
|
80 """ |
|
81 for col in range(self.branchesTree.columnCount()): |
|
82 self.branchesTree.resizeColumnToContents(col) |
|
83 |
|
84 @pyqtSlot(str) |
|
85 def on_remotesComboBox_currentTextChanged(self, txt): |
|
86 """ |
|
87 Private slot to handle changes of the selected repository. |
|
88 |
|
89 @param txt current text of the combo box (string) |
|
90 """ |
|
91 self.remoteEdit.setText(self.__repos[txt]) |
|
92 |
|
93 for row in range(self.branchesTree.topLevelItemCount()): |
|
94 itm = self.branchesTree.topLevelItem(row) |
|
95 localBranch = itm.text(GitPushDialog.LocalBranchColumn) |
|
96 combo = self.branchesTree.itemWidget( |
|
97 itm, GitPushDialog.RemoteBranchColumn) |
|
98 combo.clear() |
|
99 combo.addItems([""] + sorted(self.__remotes[txt])) |
|
100 index = combo.findText(localBranch) |
|
101 if index != -1: |
|
102 combo.setCurrentIndex(index) |
|
103 itm.setCheckState(GitPushDialog.PushColumn, |
|
104 Qt.CheckState.Checked) |
|
105 else: |
|
106 itm.setCheckState(GitPushDialog.PushColumn, |
|
107 Qt.CheckState.Unchecked) |
|
108 itm.setCheckState(GitPushDialog.ForceColumn, |
|
109 Qt.CheckState.Unchecked) |
|
110 |
|
111 self.__resizeColumns() |
|
112 |
|
113 @pyqtSlot(QTreeWidgetItem, int) |
|
114 def on_branchesTree_itemChanged(self, item, column): |
|
115 """ |
|
116 Private slot handling changes of a branch item. |
|
117 |
|
118 @param item reference to the changed item (QTreeWidgetItem) |
|
119 @param column changed column (integer) |
|
120 """ |
|
121 if column == GitPushDialog.PushColumn: |
|
122 # step 1: set the item's remote branch, if it is empty |
|
123 if ( |
|
124 item.checkState(GitPushDialog.PushColumn) == |
|
125 Qt.CheckState.Checked and |
|
126 (self.branchesTree.itemWidget( |
|
127 item, |
|
128 GitPushDialog.RemoteBranchColumn |
|
129 ).currentText() == "") |
|
130 ): |
|
131 self.branchesTree.itemWidget( |
|
132 item, GitPushDialog.RemoteBranchColumn).setEditText( |
|
133 item.text(GitPushDialog.LocalBranchColumn)) |
|
134 |
|
135 # step 2: count checked items |
|
136 checkedItemsCount = 0 |
|
137 for row in range(self.branchesTree.topLevelItemCount()): |
|
138 itm = self.branchesTree.topLevelItem(row) |
|
139 if ( |
|
140 itm.checkState(GitPushDialog.PushColumn) == |
|
141 Qt.CheckState.Checked |
|
142 ): |
|
143 checkedItemsCount += 1 |
|
144 if checkedItemsCount == len(self.__localBranches): |
|
145 self.selectAllCheckBox.setCheckState(Qt.CheckState.Checked) |
|
146 elif checkedItemsCount == 0: |
|
147 self.selectAllCheckBox.setCheckState(Qt.CheckState.Unchecked) |
|
148 else: |
|
149 self.selectAllCheckBox.setCheckState( |
|
150 Qt.CheckState.PartiallyChecked) |
|
151 |
|
152 elif column == GitPushDialog.ForceColumn: |
|
153 forceItemsCount = 0 |
|
154 for row in range(self.branchesTree.topLevelItemCount()): |
|
155 itm = self.branchesTree.topLevelItem(row) |
|
156 if ( |
|
157 itm.checkState(GitPushDialog.ForceColumn) == |
|
158 Qt.CheckState.Checked |
|
159 ): |
|
160 forceItemsCount += 1 |
|
161 self.forceWarningLabel.setVisible(forceItemsCount > 0) |
|
162 |
|
163 @pyqtSlot(int) |
|
164 def on_selectAllCheckBox_stateChanged(self, state): |
|
165 """ |
|
166 Private slot to select/deselect all branch items. |
|
167 |
|
168 @param state check state of the check box (Qt.CheckState) |
|
169 """ |
|
170 if state != Qt.CheckState.PartiallyChecked: |
|
171 for row in range(self.branchesTree.topLevelItemCount()): |
|
172 itm = self.branchesTree.topLevelItem(row) |
|
173 if itm.checkState(GitPushDialog.PushColumn) != state: |
|
174 itm.setCheckState(GitPushDialog.PushColumn, state) |
|
175 |
|
176 def getData(self): |
|
177 """ |
|
178 Public method to get the entered data. |
|
179 |
|
180 @return remote name, list of branches to be pushed, |
|
181 a flag indicating to push tags as well, a flag indicating |
|
182 to set tracking information and the push method for submodules |
|
183 @rtype tuple of (str, list of str, bool, bool, str) |
|
184 """ |
|
185 refspecs = [] |
|
186 for row in range(self.branchesTree.topLevelItemCount()): |
|
187 itm = self.branchesTree.topLevelItem(row) |
|
188 force = ( |
|
189 itm.checkState(GitPushDialog.ForceColumn) == |
|
190 Qt.CheckState.Checked |
|
191 ) |
|
192 if ( |
|
193 itm.checkState(GitPushDialog.PushColumn) == |
|
194 Qt.CheckState.Checked |
|
195 ): |
|
196 localBranch = itm.text(GitPushDialog.LocalBranchColumn) |
|
197 remoteBranch = self.branchesTree.itemWidget( |
|
198 itm, GitPushDialog.RemoteBranchColumn).currentText() |
|
199 refspecs.append("{0}{1}:{2}".format( |
|
200 "+" if force else "", localBranch, remoteBranch)) |
|
201 |
|
202 # submodule stuff (--recurse-submodules=) |
|
203 if self.submodulesOnDemandButton.isChecked(): |
|
204 submodulesPush = "on-demand" |
|
205 elif self.submodulesCheckButton.isChecked(): |
|
206 submodulesPush = "check" |
|
207 elif self.submodulesOnlyButton.isChecked(): |
|
208 submodulesPush = "only" |
|
209 else: |
|
210 submodulesPush = "no" |
|
211 |
|
212 return ( |
|
213 self.remotesComboBox.currentText(), |
|
214 refspecs, |
|
215 self.tagsCheckBox.isChecked(), |
|
216 self.trackingCheckBox.isChecked(), |
|
217 submodulesPush, |
|
218 ) |