File indexing completed on 2024-04-21 05:40:58

0001 /*
0002     SPDX-FileCopyrightText: 2010 Peter Penz <peter.penz@gmx.at>
0003     SPDX-FileCopyrightText: 2010 Sebastian Doerner <sebastian@sebastian-doerner.de>
0004 
0005     SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 #ifndef FILEVIEWGITPLUGIN_H
0009 #define FILEVIEWGITPLUGIN_H
0010 
0011 #include <Dolphin/KVersionControlPlugin>
0012 #include <KIO/CommandLauncherJob>
0013 #include <KJobUiDelegate>
0014 #include <KDialogJobUiDelegate>
0015 
0016 #include <KFileItem>
0017 
0018 #include <QList>
0019 #include <QHash>
0020 #include <QProcess>
0021 #include <QString>
0022 
0023 /**
0024  * @brief Git implementation for the KVersionControlPlugin2 interface.
0025  */
0026 class FileViewGitPlugin : public KVersionControlPlugin
0027 {
0028     Q_OBJECT
0029 
0030 public:
0031     FileViewGitPlugin(QObject* parent, const QList<QVariant>& args);
0032     ~FileViewGitPlugin() override;
0033     QString fileName() const override;
0034     QString localRepositoryRoot(const QString& directory) const override;
0035     bool beginRetrieval(const QString& directory) override;
0036     void endRetrieval() override;
0037     ItemVersion itemVersion(const KFileItem& item) const override;
0038     QList<QAction*> versionControlActions(const KFileItemList& items) const override;
0039     QList<QAction*> outOfVersionControlActions(const KFileItemList& items) const override;
0040 
0041 private Q_SLOTS:
0042     void addFiles();
0043     void revertFiles();
0044     void showLocalChanges();
0045     void removeFiles();
0046     void checkout();
0047     void commit();
0048     void createTag();
0049     void push();
0050     void pull();
0051     void log();
0052     void showDiff(const QUrl &link);
0053     void merge();
0054 
0055     void slotOperationCompleted(int exitCode, QProcess::ExitStatus exitStatus);
0056     void slotOperationError();
0057 private:
0058     QList<QAction*> contextMenuFilesActions(const KFileItemList& items) const;
0059     QList<QAction*> contextMenuDirectoryActions(const QString& directory) const;
0060     /**
0061      * Reads into buffer from device until we reach the next \0 or maxChars have been read.
0062      * @returns The number of characters read.
0063      */
0064     int readUntilZeroChar(QIODevice* device, char* buffer, const int maxChars);
0065     /**
0066      * Parses the output of the git push command and returns an appropriate message,
0067      * that should be displayed to the user.
0068      * @returns The error or success message to be printed to the user
0069      */
0070     QString parsePushOutput();
0071     /**
0072      * Parses the output of the git pull command and returns an appropriate message,
0073      * that should be displayed to the user.
0074      * @returns The error or success message to be printed to the user
0075      */
0076     QString parsePullOutput();
0077     /**
0078      * Executes the command "git {svnCommand}" for the files that have been
0079      * set by getting the context menu actions (see contextMenuActions()).
0080      * @param infoMsg     Message that should be shown before the command is executed.
0081      * @param errorMsg    Message that should be shown if the execution of the command
0082      *                    has failed.
0083      * @param operationCompletedMsg
0084      *                    Message that should be shown if the execution of the command
0085      *                    has been completed successfully.
0086      */
0087     void execGitCommand(const QString& gitCommand,
0088                         const QStringList& arguments,
0089                         const QString& infoMsg,
0090                         const QString& errorMsg,
0091                         const QString& operationCompletedMsg);
0092     void startGitCommandProcess();
0093 private:
0094     bool m_pendingOperation;
0095     /**
0096      * Contains all files in the current directory, whose version state is not
0097      * NormalVersion and directories containing such files (except for directories
0098      * whose only special contained file type is IgnoredVersion).
0099      */
0100     QHash<QString, ItemVersion> m_versionInfoHash;
0101     QAction* m_addAction;
0102     QAction* m_revertAction;
0103     QAction* m_showLocalChangesAction;
0104     QAction* m_removeAction;
0105     QAction* m_checkoutAction;
0106     QAction* m_commitAction;
0107     QAction* m_tagAction;
0108     QAction* m_pushAction;
0109     QAction* m_pullAction;
0110     QAction* m_logAction;
0111     QAction* m_mergeAction;
0112 
0113     QString m_currentDir;
0114     QProcess m_process;
0115     QString m_command;
0116     QStringList m_arguments;
0117     QString m_operationCompletedMsg;
0118     QString m_errorMsg;
0119 
0120     QWidget* m_parentWidget;
0121 
0122     //Current targets. m_contextItems is used if and only if m_contextDir is empty.
0123     mutable QString m_contextDir;
0124     mutable KFileItemList m_contextItems;
0125 
0126     // Utility method, because the method call is the same except for the command
0127     void runCommand(const QString &command)
0128     {
0129         auto *job = new KIO::CommandLauncherJob(command);
0130         job->setWorkingDirectory(m_currentDir);
0131         job->setUiDelegate(new KDialogJobUiDelegate(KJobUiDelegate::AutoHandlingEnabled, nullptr));
0132         job->start();
0133     }
0134 };
0135 #endif // FILEVIEWGITPLUGIN_H
0136