File indexing completed on 2024-05-12 05:51:44

0001 /*
0002     SPDX-FileCopyrightText: 2021 Waqar Ahmed <waqar.17a@gmail.com>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 #pragma once
0007 
0008 #include <QString>
0009 #include <functional>
0010 #include <optional>
0011 
0012 namespace GitUtils
0013 {
0014 enum RefType {
0015     Head = 0x1,
0016     Remote = 0x2,
0017     Tag = 0x4,
0018     All = 0x7,
0019 };
0020 
0021 /**
0022  * @brief Represents a branch
0023  */
0024 struct Branch {
0025     /** Branch name */
0026     QString name;
0027     /** remote name, will be empty for local branches */
0028     QString remote;
0029     /** Ref type @see RefType */
0030     RefType type = All;
0031     /** last commit on this branch, may be empty **/
0032     QString lastCommit;
0033 };
0034 
0035 struct Result {
0036     QString error;
0037     int returnCode = 0;
0038 };
0039 
0040 struct CheckoutResult : public Result {
0041     QString branch;
0042 };
0043 
0044 struct StatusEntry {
0045     QString file;
0046     char x;
0047     char y;
0048 };
0049 
0050 /**
0051  * @brief check if @p repo is a git repo
0052  * @param repo is path to the repo
0053  * @return
0054  */
0055 bool isGitRepo(const QString &repo);
0056 
0057 /**
0058  * @brief get name of current branch in @p repo
0059  */
0060 QString getCurrentBranchName(const QString &repo);
0061 
0062 /**
0063  * @brief checkout to @p branch in @p repo
0064  */
0065 CheckoutResult checkoutBranch(const QString &repo, const QString &branch);
0066 
0067 /**
0068  * @brief checkout to new @p branch in @p repo from @p fromBranch
0069  */
0070 CheckoutResult checkoutNewBranch(const QString &repo, const QString &newBranch, const QString &fromBranch = QString());
0071 
0072 /**
0073  * @brief get all local and remote branches
0074  */
0075 QList<Branch> getAllBranches(const QString &repo);
0076 
0077 /**
0078  * @brief get all local and remote branches + tags
0079  */
0080 QList<Branch> getAllBranchesAndTags(const QString &repo, RefType ref = RefType::All);
0081 
0082 /**
0083  * @brief get all local branches with last commit
0084  */
0085 QList<Branch> getAllLocalBranchesWithLastCommitSubject(const QString &repo);
0086 
0087 std::pair<QString, QString> getLastCommitMessage(const QString &repo);
0088 
0089 Result deleteBranches(const QStringList &branches, const QString &repo);
0090 }
0091 
0092 Q_DECLARE_TYPEINFO(GitUtils::Branch, Q_MOVABLE_TYPE);