8 process. |
8 process. |
9 """ |
9 """ |
10 |
10 |
11 import os |
11 import os |
12 import tempfile |
12 import tempfile |
|
13 import contextlib |
13 |
14 |
14 from PyQt5.QtCore import pyqtSlot, Qt, QProcess, QTimer, QSize |
15 from PyQt5.QtCore import pyqtSlot, Qt, QProcess, QTimer, QSize |
15 from PyQt5.QtGui import QTextCursor |
16 from PyQt5.QtGui import QTextCursor |
16 from PyQt5.QtWidgets import ( |
17 from PyQt5.QtWidgets import ( |
17 QWidget, QDialogButtonBox, QMenu, QHeaderView, QTreeWidgetItem, QLineEdit, |
18 QWidget, QDialogButtonBox, QMenu, QHeaderView, QTreeWidgetItem, QLineEdit, |
48 Constructor |
49 Constructor |
49 |
50 |
50 @param vcs reference to the vcs object |
51 @param vcs reference to the vcs object |
51 @param parent parent widget (QWidget) |
52 @param parent parent widget (QWidget) |
52 """ |
53 """ |
53 super(GitStatusDialog, self).__init__(parent) |
54 super().__init__(parent) |
54 self.setupUi(self) |
55 self.setupUi(self) |
55 |
56 |
56 self.__toBeCommittedColumn = 0 |
57 self.__toBeCommittedColumn = 0 |
57 self.__statusWorkColumn = 1 |
58 self.__statusWorkColumn = 1 |
58 self.__statusIndexColumn = 2 |
59 self.__statusIndexColumn = 2 |
1068 """ |
1069 """ |
1069 Private slot to generate diff outputs for the selected item. |
1070 Private slot to generate diff outputs for the selected item. |
1070 """ |
1071 """ |
1071 self.lDiffEdit.clear() |
1072 self.lDiffEdit.clear() |
1072 self.rDiffEdit.clear() |
1073 self.rDiffEdit.clear() |
1073 try: |
1074 with contextlib.suppress(AttributeError): |
1074 self.lDiffHighlighter.regenerateRules() |
1075 self.lDiffHighlighter.regenerateRules() |
1075 self.rDiffHighlighter.regenerateRules() |
1076 self.rDiffHighlighter.regenerateRules() |
1076 except AttributeError: |
|
1077 # backward compatibility |
|
1078 pass |
|
1079 |
1077 |
1080 selectedItems = self.statusList.selectedItems() |
1078 selectedItems = self.statusList.selectedItems() |
1081 if len(selectedItems) == 1: |
1079 if len(selectedItems) == 1: |
1082 fn = os.path.join(self.dname, |
1080 fn = os.path.join(self.dname, |
1083 selectedItems[0].text(self.__pathColumn)) |
1081 selectedItems[0].text(self.__pathColumn)) |
1165 """ |
1163 """ |
1166 Private method to stage the selected lines or hunk. |
1164 Private method to stage the selected lines or hunk. |
1167 """ |
1165 """ |
1168 cursor = self.lDiffEdit.textCursor() |
1166 cursor = self.lDiffEdit.textCursor() |
1169 startIndex, endIndex = self.__selectedLinesIndexes(self.lDiffEdit) |
1167 startIndex, endIndex = self.__selectedLinesIndexes(self.lDiffEdit) |
1170 if cursor.hasSelection(): |
1168 patch = ( |
1171 patch = self.lDiffParser.createLinesPatch(startIndex, endIndex) |
1169 self.lDiffParser.createLinesPatch(startIndex, endIndex) |
1172 else: |
1170 if cursor.hasSelection() else |
1173 patch = self.lDiffParser.createHunkPatch(startIndex) |
1171 self.lDiffParser.createHunkPatch(startIndex) |
|
1172 ) |
1174 if patch: |
1173 if patch: |
1175 patchFile = self.__tmpPatchFileName() |
1174 patchFile = self.__tmpPatchFileName() |
1176 try: |
1175 try: |
1177 with open(patchFile, "w") as f: |
1176 with open(patchFile, "w") as f: |
1178 f.write(patch) |
1177 f.write(patch) |
1186 """ |
1185 """ |
1187 Private method to unstage the selected lines or hunk. |
1186 Private method to unstage the selected lines or hunk. |
1188 """ |
1187 """ |
1189 cursor = self.rDiffEdit.textCursor() |
1188 cursor = self.rDiffEdit.textCursor() |
1190 startIndex, endIndex = self.__selectedLinesIndexes(self.rDiffEdit) |
1189 startIndex, endIndex = self.__selectedLinesIndexes(self.rDiffEdit) |
1191 if cursor.hasSelection(): |
1190 patch = ( |
1192 patch = self.rDiffParser.createLinesPatch(startIndex, endIndex, |
1191 self.rDiffParser.createLinesPatch(startIndex, endIndex, |
1193 reverse=True) |
1192 reverse=True) |
1194 else: |
1193 if cursor.hasSelection() else |
1195 patch = self.rDiffParser.createHunkPatch(startIndex) |
1194 self.rDiffParser.createHunkPatch(startIndex) |
|
1195 ) |
1196 if patch: |
1196 if patch: |
1197 patchFile = self.__tmpPatchFileName() |
1197 patchFile = self.__tmpPatchFileName() |
1198 try: |
1198 try: |
1199 with open(patchFile, "w") as f: |
1199 with open(patchFile, "w") as f: |
1200 f.write(patch) |
1200 f.write(patch) |
1208 """ |
1208 """ |
1209 Private method to revert the selected lines or hunk. |
1209 Private method to revert the selected lines or hunk. |
1210 """ |
1210 """ |
1211 cursor = self.lDiffEdit.textCursor() |
1211 cursor = self.lDiffEdit.textCursor() |
1212 startIndex, endIndex = self.__selectedLinesIndexes(self.lDiffEdit) |
1212 startIndex, endIndex = self.__selectedLinesIndexes(self.lDiffEdit) |
1213 if cursor.hasSelection(): |
1213 title = ( |
1214 title = self.tr("Revert selected lines") |
1214 self.tr("Revert selected lines") |
1215 else: |
1215 if cursor.hasSelection() else |
1216 title = self.tr("Revert hunk") |
1216 self.tr("Revert hunk") |
|
1217 ) |
1217 res = E5MessageBox.yesNo( |
1218 res = E5MessageBox.yesNo( |
1218 self, |
1219 self, |
1219 title, |
1220 title, |
1220 self.tr("""Are you sure you want to revert the selected""" |
1221 self.tr("""Are you sure you want to revert the selected""" |
1221 """ changes?""")) |
1222 """ changes?""")) |