1#pragma once
2
3/****************************************************************************************
4 ** GitQlient is an application to manage and operate one or several Git repositories. With
5 ** GitQlient you will be able to add commits, branches and manage all the options Git provides.
6 ** Copyright (C) 2021 Francesc Martinez
7 **
8 ** LinkedIn: www.linkedin.com/in/cescmm/
9 ** Web: www.francescmm.com
10 **
11 ** This program is free software; you can redistribute it and/or
12 ** modify it under the terms of the GNU Lesser General Public
13 ** License as published by the Free Software Foundation; either
14 ** version 2 of the License, or (at your option) any later version.
15 **
16 ** This program is distributed in the hope that it will be useful,
17 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 ** Lesser General Public License for more details.
20 **
21 ** You should have received a copy of the GNU Lesser General Public
22 ** License along with this library; if not, write to the Free Software
23 ** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 ***************************************************************************************/
25
26#include <CommitInfo.h>
27#include <RevisionFiles.h>
28#include <lanes.h>
29
30#include <QHash>
31#include <QMutex>
32#include <QObject>
33#include <QSharedPointer>
34
35#include <optional>
36
37struct WipRevisionInfo;
38
39class GitCache : public QObject
40{
41 Q_OBJECT
42
43signals:
44 void signalCacheUpdated();
45
46public:
47 struct LocalBranchDistances
48 {
49 int aheadOrigin = 0;
50 int behindOrigin = 0;
51 };
52
53 explicit GitCache(QObject *parent = nullptr);
54 ~GitCache();
55
56 int commitCount() const;
57
58 CommitInfo commitInfo(const QString &sha);
59 CommitInfo commitInfo(int row);
60 CommitInfo searchCommitInfo(const QString &text, int startingPoint = 0, bool reverse = false);
61 bool isCommitInCurrentGeneologyTree(const QString &sha);
62 bool updateWipCommit(const WipRevisionInfo &wipInfo);
63 void insertCommit(CommitInfo commit);
64 void updateCommit(const QString &oldSha, CommitInfo newCommit);
65
66 bool insertRevisionFiles(const QString &sha1, const QString &sha2, const RevisionFiles &file);
67 std::optional<RevisionFiles> revisionFile(const QString &sha1, const QString &sha2) const;
68
69 void clearReferences();
70 void insertReference(const QString &sha, References::Type type, const QString &reference);
71 void deleteReference(const QString &sha, References::Type type, const QString &reference);
72 bool hasReferences(const QString &sha);
73 QStringList getReferences(const QString &sha, References::Type type);
74 QString getShaOfReference(const QString &referenceName, References::Type type) const;
75 void reloadCurrentBranchInfo(const QString &currentBranch, const QString &currentSha);
76
77 void setUntrackedFilesList(QVector<QString> untrackedFiles);
78 bool pendingLocalChanges();
79
80 QVector<QPair<QString, QStringList>> getBranches(References::Type type);
81 QMap<QString, QString> getTags(References::Type tagType) const;
82
83 void updateTags(QMap<QString, QString> remoteTags);
84
85 bool isInitialized() const { return mInitialized; }
86
87private:
88 friend class GitRepoLoader;
89
90 bool mInitialized = false;
91 bool mConfigured = true;
92 Lanes mLanes;
93 QVector<QString> mUntrackedFiles;
94
95 mutable QMutex mCommitsMutex;
96 QVector<CommitInfo *> mCommits;
97 QHash<QString, CommitInfo> mCommitsMap;
98
99 mutable QMutex mRevisionsMutex;
100 QHash<QPair<QString, QString>, RevisionFiles> mRevisionFilesMap;
101
102 mutable QMutex mReferencesMutex;
103 QHash<QString, References> mReferences;
104
105 void setup(const WipRevisionInfo &wipInfo, QVector<CommitInfo> commits);
106 void setConfigurationDone() { mConfigured = true; }
107
108 bool insertRevisionFile(const QString &sha1, const QString &sha2, const RevisionFiles &file);
109 void insertWipRevision(const WipRevisionInfo &wipInfo);
110 RevisionFiles fakeWorkDirRevFile(const QString &diffIndex, const QString &diffIndexCache);
111 void calculateLanes(CommitInfo &c);
112 auto searchCommit(const QString &text, int startingPoint = 0) const;
113 auto reverseSearchCommit(const QString &text, int startingPoint = 0) const;
114 void resetLanes(const CommitInfo &c, bool isFork);
115 bool checkSha(const QString &originalSha, const QString &currentSha) const;
116 void clearInternalData();
117};
118