src/eric7/Plugins/VcsPlugins/vcsGit/GitPatchStatisticsDialog.py

Sat, 31 Dec 2022 16:23:21 +0100

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Sat, 31 Dec 2022 16:23:21 +0100
branch
eric7
changeset 9653
e67609152c5e
parent 9616
13aa04c979d7
child 10438
4cd7e5a8b3cf
permissions
-rw-r--r--

Updated copyright for 2023.

# -*- coding: utf-8 -*-

# Copyright (c) 2015 - 2023 Detlev Offenbach <detlev@die-offenbachs.de>
#

"""
Module implementing a dialog to show some patch file statistics.
"""

from PyQt6.QtCore import QProcess, Qt
from PyQt6.QtWidgets import QDialog, QHeaderView, QTreeWidgetItem

from eric7 import Preferences
from eric7.EricWidgets import EricMessageBox

from .Ui_GitPatchStatisticsDialog import Ui_GitPatchStatisticsDialog


class GitPatchStatisticsDialog(QDialog, Ui_GitPatchStatisticsDialog):
    """
    Class implementing a dialog to show some patch file statistics.
    """

    def __init__(self, vcs, parent=None):
        """
        Constructor

        @param vcs reference to the VCS object (Git)
        @param parent reference to the parent widget (QWidget)
        """
        super().__init__(parent)
        self.setupUi(self)
        self.setWindowFlags(Qt.WindowType.Window)

        self.__vcs = vcs

        self.changesTreeWidget.headerItem().setText(
            self.changesTreeWidget.columnCount(), ""
        )
        self.changesTreeWidget.header().setSortIndicator(2, Qt.SortOrder.AscendingOrder)

    def start(self, projectDir, patchCheckData):
        """
        Public method to start the statistics process.

        @param projectDir directory name of the project (string)
        @param patchCheckData tuple of data as returned by the
            GitPatchFilesDialog.getData() method
        """
        from .GitPatchFilesDialog import GitPatchFilesDialog

        self.__patchCheckData = patchCheckData

        self.changesTreeWidget.clear()
        self.summaryEdit.clear()

        # find the root of the repo
        repodir = self.__vcs.findRepoRoot(projectDir)
        if not repodir:
            return

        dlg = GitPatchFilesDialog(repodir, patchCheckData)
        if dlg.exec() == QDialog.DialogCode.Accepted:
            patchFilesList, stripCount, inaccurateEof, recount = dlg.getData()
            self.__patchCheckData = (patchFilesList, stripCount, inaccurateEof, recount)
            if patchFilesList:
                process = QProcess()
                process.setWorkingDirectory(repodir)

                # step 1: get the statistics
                args = self.__vcs.initCommand("apply")
                args.append("--numstat")
                if inaccurateEof:
                    args.append("--inaccurate-eof")
                if recount:
                    args.append("--recount")
                args.append("-p{0}".format(stripCount))
                args.extend(patchFilesList)

                process.start("git", args)
                procStarted = process.waitForStarted(5000)
                if not procStarted:
                    EricMessageBox.critical(
                        self,
                        self.tr("Process Generation Error"),
                        self.tr(
                            "The process {0} could not be started. "
                            "Ensure, that it is in the search path."
                        ).format("git"),
                    )
                    return
                else:
                    finished = process.waitForFinished(30000)
                    if finished and process.exitCode() == 0:
                        output = str(
                            process.readAllStandardOutput(),
                            Preferences.getSystem("IOEncoding"),
                            "replace",
                        )
                        for line in output.splitlines():
                            self.__createStatisticsItem(line)

                # step 2: get the summary
                args = self.__vcs.initCommand("apply")
                args.append("--summary")
                if inaccurateEof:
                    args.append("--inaccurate-eof")
                if recount:
                    args.append("--recount")
                args.append("-p{0}".format(stripCount))
                args.extend(patchFilesList)

                process.start("git", args)
                procStarted = process.waitForStarted(5000)
                if not procStarted:
                    EricMessageBox.critical(
                        self,
                        self.tr("Process Generation Error"),
                        self.tr(
                            "The process {0} could not be started. "
                            "Ensure, that it is in the search path."
                        ).format("git"),
                    )
                    return
                else:
                    finished = process.waitForFinished(30000)
                    if finished and process.exitCode() == 0:
                        output = str(
                            process.readAllStandardOutput(),
                            Preferences.getSystem("IOEncoding"),
                            "replace",
                        )
                        for line in output.splitlines():
                            self.summaryEdit.appendPlainText(line.strip())

    def __createStatisticsItem(self, line):
        """
        Private method to create a file statistics entry.

        @param line string with file statistics data (string)
        """
        insertions, deletions, filename = line.strip().split(None, 2)
        itm = QTreeWidgetItem(self.changesTreeWidget, [insertions, deletions, filename])
        itm.setTextAlignment(0, Qt.AlignmentFlag.AlignRight)
        itm.setTextAlignment(1, Qt.AlignmentFlag.AlignRight)

    def __resizeColumns(self):
        """
        Private method to resize the list columns.
        """
        self.changesTreeWidget.header().resizeSections(
            QHeaderView.ResizeMode.ResizeToContents
        )
        self.changesTreeWidget.header().setStretchLastSection(True)

    def getData(self):
        """
        Public method to get the data used to generate the statistics.

        @return tuple of list of patch files, strip count, flag indicating
            that the patch has inaccurate end-of-file marker and a flag
            indicating to not trust the line count information
            (list of string, integer, boolean, boolean)
        """
        return self.__patchCheckData

eric ide

mercurial