File indexing completed on 2024-04-21 05:41:04

0001 /*
0002     SPDX-FileCopyrightText: 2019-2020 Nikolai Krasheninnikov <nkrasheninnikov@yandex.ru>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #ifndef SVNCOMMANDS_H
0008 #define SVNCOMMANDS_H
0009 
0010 #include <QString>
0011 #include <QDateTime>
0012 #include <QtGlobal>
0013 
0014 #include <Dolphin/KVersionControlPlugin>
0015 
0016 class QTemporaryFile;
0017 class QFileDevice;
0018 
0019 /**
0020  * SVN commands execution results.
0021  */
0022 struct CommandResult {
0023     bool success;       ///< True if return code is '0' (normal execution).
0024     QString stdOut;     ///< Process stdout.
0025     QString stdErr;     ///< Process stderr.
0026 };
0027 
0028 /**
0029  * Path information for log entry.
0030  */
0031 struct affectedPath {
0032     QString action;     ///< Action type: "D" for delete, "M" for modified, etc.
0033     bool propMods;      ///< Property changes by commit.
0034     bool textMods;      ///< File changes by commit.
0035     QString kind;       ///< Path type: "file", "dir", etc.
0036 
0037     QString path;       ///< Path itself.
0038 };
0039 
0040 /**
0041  * A single log entry.
0042  */
0043 struct logEntry {
0044     ulong revision;                         ///< Revision number.
0045     QString author;                         ///< Commit author.
0046     QDateTime date;                         ///< Commit time and date.
0047     QVector<affectedPath> affectedPaths;    ///< Affected paths (files or dirs).
0048     QString msg;                            ///< Commit message.
0049 };
0050 
0051 /**
0052  * \brief SVN support functions.
0053  *
0054  * \note All functions are synchronous i.e. blocking. Each of them waits for svn process to finish.
0055  */
0056 class SvnCommands {
0057 public:
0058     /**
0059      * Returns file \p filePath local revision. Local revision means last known file revision, not
0060      * last SVN repository revision.
0061      *
0062      * \return Local revision, 0 in case of error.
0063      *
0064      * \note This function uses only local SVN data without connection to a remote so it's fast.
0065      * \sa remoteRevision()
0066      */
0067     static ulong localRevision(const QString& filePath);
0068 
0069     /**
0070      * Returns file \p filePath remote revision. Remote revision means last known SVN repository file
0071      * revision. This function uses only current SVN data and doesn't connect to a remote.
0072      *
0073      * \return Local revision, 0 in case of error.
0074      *
0075      * \note This function uses only local SVN data without connection to a remote so it's fast.
0076      * \sa localRevision()
0077      */
0078     static ulong remoteRevision(const QString& filePath);
0079 
0080     /**
0081      * For file \p filePath return its full remote repository URL path.
0082      *
0083      * \return Remote path, empty QString in case of error.
0084      *
0085      * \note This function uses only local SVN data without connection to a remote so it's fast.
0086      */
0087     static QString remoteItemUrl(const QString& filePath);
0088 
0089     /**
0090      * From a file \p filePath returns full remote repository URL in which this file located. For
0091      * every file in the repository URL is the same, i.e. returns path used for initial 'svn co'.
0092      *
0093      * \return Remote path, empty QString in case of error.
0094      *
0095      * \note This function uses only local SVN data without connection to a remote so it's fast.
0096      */
0097     static QString remoteRootUrl(const QString& filePath);
0098 
0099     /**
0100      * From a file \p filePath returns relative repository URL in which this file located. So,
0101      * for example, a root repository file "file.txt" will have relative URL "^/file.txt".
0102      *
0103      * \return Relative repository URL, empty QString in case of error.
0104      *
0105      * \note This function uses only local SVN data without connection to a remote so it's fast.
0106      */
0107     static QString remoteRelativeUrl(const QString& filePath);
0108 
0109     /**
0110      * From file \p filePath returns full working copy root path this file contains.
0111      *
0112      * \return Full local working copy root path, empty QString in case of error.
0113      *
0114      * \note This function uses only local SVN data without connection to a remote so it's fast.
0115      */
0116     static QString localRoot(const QString& filePath);
0117 
0118     /**
0119      * Updates selected \p filePath to revision \p revision. \p filePath could be a single file or a
0120      * directory. It also could be an absolute or relative.
0121      *
0122      * \return True on success, false either.
0123      *
0124      * \note This function can be really time consuming.
0125      */
0126     static bool updateToRevision(const QString& filePath, ulong revision);
0127 
0128     /**
0129      * Discards all local changes in a \p filePath. \p filePath could be a single file or a directory.
0130      * It also could be an absolute or relative.
0131      *
0132      * \return True on success, false either.
0133      *
0134      * \note This function uses only local SVN data without connection to a remote so it's fast.
0135      */
0136     static bool revertLocalChanges(const QString& filePath);
0137 
0138     /**
0139      * Reverts selected \p filePath to revision \p revision. \p filePath could be a single file or a
0140      * directory. It also could be an absolute or relative.
0141      *
0142      * \return True on success, false either.
0143      */
0144     static bool revertToRevision(const QString& filePath, ulong revision);
0145 
0146     /**
0147      * Runs 'svn cleanup' on a \p dir to remove write locks, resume unfinished operations, etc. Its
0148      * restores directory state if Subversion client has crushed.
0149      * Also this command could be used to remove unversioned or ignored files.
0150      *
0151      * \return Filled up \p commandResult structure.
0152      */
0153     static CommandResult cleanup(const QString& dir, bool removeUnversioned = false, bool removeIgnored = false, bool includeExternals = false);
0154 
0155     /**
0156      * Export URL \p path at revision \p rev to a file \p file. URL could be a remote URL to a file
0157      * or directory or path to a local file or directory (both relative or absolute). File should
0158      * already be opened or ready to be opened. Freeing resources is up to the caller.
0159      *
0160      * \return True if export success, false either.
0161      *
0162      * \note \p file should already be created with \p new.
0163      */
0164     static bool exportFile(const QUrl& path, ulong rev, QFileDevice *file);
0165     static bool exportFile(const QUrl& path, ulong rev, QTemporaryFile *file);
0166 
0167     /**
0168      * Get SVN log for a following \p filePath (could be a directory or separate file, relative or
0169      * absolute paths accepted). Log starts from revision \p fromRevision and goes for \p maxEntries
0170      * previous revisions. The default revision (0) means current revision.
0171      *
0172      * \return Full log, nullptr in case of error.
0173      *
0174      * \note This function is really time consuming.
0175      */
0176     static QSharedPointer< QVector<logEntry> > getLog(const QString& filePath, uint maxEntries = 255, ulong fromRevision = 0);
0177 
0178     /**
0179      * Check out a working copy of repository \p URL (local URL starts with a 'file://') to a local
0180      * path \p whereto (could be relative ot absolute).
0181      *
0182      * \return True if check out success, false either.
0183      *
0184      * \note This function can be really time consuming.
0185      */
0186     static bool checkoutRepository(const QString& url, bool ignoreExternals, const QString& whereto);
0187 };
0188 
0189 #endif  // SVNCOMMANDS_H