|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2015 - 2022 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing a dialog to enter data for a Fetch operation. |
|
8 """ |
|
9 |
|
10 from PyQt6.QtCore import pyqtSlot |
|
11 from PyQt6.QtWidgets import QDialog, QDialogButtonBox |
|
12 |
|
13 from .Ui_GitFetchDialog import Ui_GitFetchDialog |
|
14 |
|
15 |
|
16 class GitFetchDialog(QDialog, Ui_GitFetchDialog): |
|
17 """ |
|
18 Class implementing a dialog to enter data for a Fetch operation. |
|
19 """ |
|
20 def __init__(self, vcs, repodir, parent=None): |
|
21 """ |
|
22 Constructor |
|
23 |
|
24 @param vcs reference to the git object |
|
25 @param repodir directory name of the local repository (string) |
|
26 @param parent reference to the parent widget (QWidget) |
|
27 """ |
|
28 super().__init__(parent) |
|
29 self.setupUi(self) |
|
30 |
|
31 self.__vcs = vcs |
|
32 self.__repodir = repodir |
|
33 |
|
34 self.__all = self.tr("<All>") |
|
35 self.__custom = self.tr("<Custom>") |
|
36 |
|
37 remoteUrlsList = self.__vcs.gitGetRemoteUrlsList(self.__repodir) |
|
38 self.__repos = {name: url for name, url in remoteUrlsList} |
|
39 |
|
40 self.__okButton = self.buttonBox.button( |
|
41 QDialogButtonBox.StandardButton.Ok) |
|
42 |
|
43 self.remotesComboBox.addItems(sorted(self.__repos.keys())) |
|
44 self.remotesComboBox.addItem(self.__all) |
|
45 self.remotesComboBox.addItem(self.__custom) |
|
46 |
|
47 index = self.remotesComboBox.findText("origin") |
|
48 if index == -1: |
|
49 index = 0 |
|
50 self.remotesComboBox.setCurrentIndex(index) |
|
51 |
|
52 localBranches = self.__vcs.gitGetBranchesList( |
|
53 self.__repodir, withMaster=True) |
|
54 self.localBranchComboBox.addItems([""] + sorted(localBranches)) |
|
55 self.localBranchComboBox.setEnabled(False) |
|
56 |
|
57 def __okButtonEnable(self): |
|
58 """ |
|
59 Private slot to set the enabled state of the OK button. |
|
60 """ |
|
61 self.__okButton.setEnabled( |
|
62 self.remoteBranchesList.count() > 0 or |
|
63 self.remotesComboBox.currentText() == self.__all |
|
64 ) |
|
65 |
|
66 def __updateButtonEnable(self): |
|
67 """ |
|
68 Private slot to set the enabled state of the update button. |
|
69 """ |
|
70 remote = self.remotesComboBox.currentText() |
|
71 enable = remote != self.__all |
|
72 if remote == self.__custom: |
|
73 enable = self.remoteEdit.text() != "" |
|
74 |
|
75 self.updateButton.setEnabled(enable) |
|
76 |
|
77 @pyqtSlot(str) |
|
78 def on_remotesComboBox_currentTextChanged(self, txt): |
|
79 """ |
|
80 Private slot to handle changes of the selected repository. |
|
81 |
|
82 @param txt current text of the combo box (string) |
|
83 """ |
|
84 self.remoteEdit.setReadOnly(txt != self.__custom) |
|
85 self.remoteBranchesList.setEnabled(txt != self.__all) |
|
86 self.remoteEdit.clear() |
|
87 self.remoteBranchesList.clear() |
|
88 self.__updateButtonEnable() |
|
89 self.__okButtonEnable() |
|
90 |
|
91 if txt not in [self.__all, self.__custom]: |
|
92 remoteBranches = self.__vcs.gitGetRemoteBranchesList( |
|
93 self.__repodir, txt) |
|
94 self.remoteBranchesList.addItems(sorted(remoteBranches)) |
|
95 |
|
96 if txt in self.__repos: |
|
97 self.remoteEdit.setText(self.__repos[txt]) |
|
98 |
|
99 @pyqtSlot(str) |
|
100 def on_remoteEdit_textChanged(self, txt): |
|
101 """ |
|
102 Private slot to handle changes of the URL edit. |
|
103 |
|
104 @param txt current text of the URL edit (string) |
|
105 """ |
|
106 self.__updateButtonEnable() |
|
107 |
|
108 if ( |
|
109 self.remotesComboBox.currentText() == self.__custom and |
|
110 txt != "" |
|
111 ): |
|
112 remoteBranches = self.__vcs.gitGetRemoteBranchesList( |
|
113 self.__repodir, txt) |
|
114 self.remoteBranchesList.clear() |
|
115 self.remoteBranchesList.addItems(sorted(remoteBranches)) |
|
116 |
|
117 self.__okButtonEnable() |
|
118 |
|
119 @pyqtSlot() |
|
120 def on_remoteBranchesList_itemSelectionChanged(self): |
|
121 """ |
|
122 Private slot to handle a change of selected remote branches. |
|
123 """ |
|
124 singleSelection = len(self.remoteBranchesList.selectedItems()) == 1 |
|
125 self.localBranchComboBox.setEnabled(singleSelection) |
|
126 txt = ( |
|
127 self.remoteBranchesList.selectedItems()[0].text() |
|
128 if singleSelection else |
|
129 "" |
|
130 ) |
|
131 index = self.localBranchComboBox.findText(txt) |
|
132 if index == -1: |
|
133 self.localBranchComboBox.setEditText(txt) |
|
134 else: |
|
135 self.localBranchComboBox.setCurrentIndex(index) |
|
136 |
|
137 @pyqtSlot() |
|
138 def on_updateButton_clicked(self): |
|
139 """ |
|
140 Private slot to update the list of remote branches. |
|
141 """ |
|
142 remote = self.remotesComboBox.currentText() |
|
143 if remote == self.__all: |
|
144 # shouldn't happen |
|
145 return |
|
146 |
|
147 if remote == self.__custom: |
|
148 remote = self.remoteEdit.text() |
|
149 if remote == "": |
|
150 # shouldn't happen either |
|
151 return |
|
152 |
|
153 remoteBranches = self.__vcs.gitGetRemoteBranchesList( |
|
154 self.__repodir, remote) |
|
155 self.remoteBranchesList.clear() |
|
156 self.remoteBranchesList.addItems(sorted(remoteBranches)) |
|
157 |
|
158 self.__okButtonEnable() |
|
159 |
|
160 def getData(self): |
|
161 """ |
|
162 Public method to get the entered data. |
|
163 |
|
164 @return tuple of remote name, remote url (for custom remotes), |
|
165 remote branches, local branch, a flag indicating to fetch from |
|
166 all repositories, a flag indicating to remove obsolete tracking |
|
167 references and a flag indicating to fetch tags as well |
|
168 (string, string, list of strings, string, boolean, boolean, |
|
169 boolean) |
|
170 """ |
|
171 remote = "" |
|
172 url = "" |
|
173 remoteBranches = [] |
|
174 allRepos = False |
|
175 localBranch = "" |
|
176 |
|
177 remoteRepo = self.remotesComboBox.currentText() |
|
178 if remoteRepo == self.__all: |
|
179 allRepos = True |
|
180 else: |
|
181 if remoteRepo == self.__custom: |
|
182 url = self.remoteEdit.text() |
|
183 else: |
|
184 remote = remoteRepo |
|
185 for itm in self.remoteBranchesList.selectedItems(): |
|
186 remoteBranches.append(itm.text()) |
|
187 if len(remoteBranches) == 1: |
|
188 localBranch = self.localBranchComboBox.currentText() |
|
189 |
|
190 return (remote, url, remoteBranches, localBranch, allRepos, |
|
191 self.pruneCheckBox.isChecked(), self.tagsCheckBox.isChecked()) |