|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2017 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing a dialog to show the status of the submodules of the |
|
8 project. |
|
9 """ |
|
10 |
|
11 from __future__ import unicode_literals |
|
12 try: |
|
13 str = unicode |
|
14 except NameError: |
|
15 pass |
|
16 |
|
17 import os |
|
18 |
|
19 from PyQt5.QtCore import pyqtSlot, Qt, QProcess |
|
20 from PyQt5.QtWidgets import QDialog, QDialogButtonBox, QTreeWidgetItem, \ |
|
21 QHeaderView, QAbstractButton |
|
22 |
|
23 from .Ui_GitSubmodulesStatusDialog import Ui_GitSubmodulesStatusDialog |
|
24 |
|
25 import Preferences |
|
26 |
|
27 |
|
28 class GitSubmodulesStatusDialog(QDialog, Ui_GitSubmodulesStatusDialog): |
|
29 """ |
|
30 Class implementing a dialog to show the status of the submodules of the |
|
31 project. |
|
32 """ |
|
33 def __init__(self, vcs, parent=None): |
|
34 """ |
|
35 Constructor |
|
36 |
|
37 @param vcs reference to the vcs object |
|
38 @type Git |
|
39 @param parent reference to the parent widget |
|
40 @type QWidget |
|
41 """ |
|
42 super(GitSubmodulesStatusDialog, self).__init__(parent) |
|
43 self.setupUi(self) |
|
44 |
|
45 self.__statusCodes = { |
|
46 " ": self.tr("up-to-date"), |
|
47 "-": self.tr("not initialized"), |
|
48 "+": self.tr("different to index"), |
|
49 "U": self.tr("merge conflicts") |
|
50 } |
|
51 |
|
52 self.__vcs = vcs |
|
53 self.__repodir = None |
|
54 |
|
55 self.refreshButton = self.buttonBox.addButton( |
|
56 self.tr("Refresh"), QDialogButtonBox.ActionRole) |
|
57 self.refreshButton.setToolTip( |
|
58 self.tr("Press to refresh the status display")) |
|
59 |
|
60 def start(self, projectDir): |
|
61 """ |
|
62 Public method to populate the status list. |
|
63 |
|
64 @param projectDir name of the project directory |
|
65 @type str |
|
66 """ |
|
67 # find the root of the repo |
|
68 self.__repodir = projectDir |
|
69 while not os.path.isdir(os.path.join(self.__repodir, |
|
70 self.__vcs.adminDir)): |
|
71 self.__repodir = os.path.dirname(self.__repodir) |
|
72 if os.path.splitdrive(self.__repodir)[1] == os.sep: |
|
73 return |
|
74 |
|
75 self.errorGroup.hide() |
|
76 self.errors.clear() |
|
77 self.statusList.clear() |
|
78 self.buttonBox.setEnabled(False) |
|
79 |
|
80 args = self.__vcs.initCommand("submodule") |
|
81 args.append("status") |
|
82 if self.recursiveCheckBox.isChecked(): |
|
83 args.append("--recursive") |
|
84 if self.indexCheckBox.isChecked(): |
|
85 args.append("--cached") |
|
86 |
|
87 process = QProcess() |
|
88 process.setWorkingDirectory(self.__repodir) |
|
89 process.start('git', args) |
|
90 procStarted = process.waitForStarted(5000) |
|
91 if procStarted: |
|
92 finished = process.waitForFinished(30000) |
|
93 if finished and process.exitCode() == 0: |
|
94 ioEncoding = Preferences.getSystem("IOEncoding") |
|
95 output = str(process.readAllStandardOutput(), |
|
96 ioEncoding, 'replace') |
|
97 error = str(process.readAllStandardError(), |
|
98 ioEncoding, 'replace') |
|
99 if error: |
|
100 self.errors.setText(error) |
|
101 self.errorGroup.show() |
|
102 self.__processOutput(output) |
|
103 else: |
|
104 if not finished: |
|
105 self.errors.setText(self.tr( |
|
106 "The process {0} did not finish within 30 seconds.") |
|
107 .format("git")) |
|
108 else: |
|
109 self.errors.setText(self.tr( |
|
110 "The process {0} finished with an error.\n" |
|
111 "Error: {1}") |
|
112 .format("git", process.errorString())) |
|
113 self.errorGroup.show() |
|
114 else: |
|
115 self.errors.setText(self.tr( |
|
116 "The process {0} could not be started. " |
|
117 "Ensure, that it is in the search path.").format("git")) |
|
118 self.errorGroup.show() |
|
119 |
|
120 self.buttonBox.setEnabled(True) |
|
121 self.buttonBox.setFocus() |
|
122 |
|
123 def __processOutput(self, output): |
|
124 """ |
|
125 Private method to process the output and populate the list. |
|
126 |
|
127 @param output output of the submodule status command |
|
128 @type str |
|
129 """ |
|
130 for line in output.splitlines(): |
|
131 try: |
|
132 status = self.__statusCodes[line[0]] |
|
133 except KeyError: |
|
134 status = self.tr("unknown") |
|
135 lineParts = line[1:].split(None, 2) |
|
136 if len(lineParts) == 3 and lineParts[2][0] == "(": |
|
137 # get rid of leading and trailing brackets |
|
138 lineParts[2] = lineParts[2][1:-1] |
|
139 QTreeWidgetItem(self.statusList, [ |
|
140 lineParts[1], # submodule name |
|
141 status, # submodule status |
|
142 lineParts[0], # commit ID |
|
143 lineParts[2], # additional info |
|
144 ]) |
|
145 |
|
146 self.statusList.header().resizeSections( |
|
147 QHeaderView.ResizeToContents) |
|
148 |
|
149 self.statusList.setSortingEnabled(True) |
|
150 self.statusList.sortItems(0, Qt.AscendingOrder) |
|
151 self.statusList.setSortingEnabled(False) |
|
152 |
|
153 @pyqtSlot(QAbstractButton) |
|
154 def on_buttonBox_clicked(self, button): |
|
155 """ |
|
156 Private slot called by a button of the button box clicked. |
|
157 |
|
158 @param button button that was clicked (QAbstractButton) |
|
159 """ |
|
160 if button == self.buttonBox.button(QDialogButtonBox.Close): |
|
161 self.close() |
|
162 elif button == self.refreshButton: |
|
163 self.on_refreshButton_clicked() |
|
164 |
|
165 @pyqtSlot() |
|
166 def on_refreshButton_clicked(self): |
|
167 """ |
|
168 Private slot to refresh the status display. |
|
169 """ |
|
170 self.start(self.__repodir) |
|
171 |
|
172 @pyqtSlot(bool) |
|
173 def on_indexCheckBox_toggled(self, checked): |
|
174 """ |
|
175 Private slot handling a change of the index check box. |
|
176 |
|
177 @param checked check state of the check box |
|
178 @type bool |
|
179 """ |
|
180 self.on_refreshButton_clicked() |
|
181 |
|
182 @pyqtSlot(bool) |
|
183 def on_recursiveCheckBox_toggled(self, checked): |
|
184 """ |
|
185 Private slot handling a change of the recursive check box. |
|
186 |
|
187 @param checked check state of the check box |
|
188 @type bool |
|
189 """ |
|
190 self.on_refreshButton_clicked() |