-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgitmanager.h
More file actions
136 lines (109 loc) · 3.5 KB
/
gitmanager.h
File metadata and controls
136 lines (109 loc) · 3.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#ifndef GITMANAGER_H
#define GITMANAGER_H
#include "raventreeitem.h"
#include <QObject>
#include <git2.h>
class GitManager: public QObject
{
Q_OBJECT
public:
enum GitHEADStatusType {
GIT_HEAD_TYPE_COMMIT = 1,
GIT_HEAD_TYPE_BRANCH = 2,
GIT_HEAD_TYPE_TAG = 3
};
struct GitBranchSelectorItem {
GitHEADStatusType type;
QString name;
bool isRemote;
};
struct GitHEADStatus {
QString name;
GitHEADStatusType type;
};
struct GitStatusItem {
QString path;
git_status_t flag;
RavenTreeItem::RavenTreeCategory category;
bool deleted;
};
// Struct to send/receive data between `status()` and `status_cb()`
typedef struct status_data {
QList<GitStatusItem> statusItems;
} status_data;
struct GitDiffItem {
git_oid oldOid;
QString oldFilePath;
QString oldFileContent;
git_oid newOid;
QString newFilePath;
QString newFileContent;
bool binary;
RavenTreeItem::RavenTreeCategory category;
};
enum GitStageResponseCode {
INDEX_NOT_FOUND,
NOT_IN_INDEX,
IS_CONFLICT,
FAILED_TO_INDEX,
FAILED_TO_UNSTAGE,
FAILED_WRITE_INDEX_TO_DISK,
UNKNOWN,
DONE
};
explicit GitManager(QString dir, QObject *parent = nullptr);
~GitManager();
// FIXME: This looks unsafe.
git_repository *getRepo() {return m_repo;}
QString getRepoPath() {return m_repoPath;}
void statusAsync();
GitManager::GitHEADStatus findHEADStatus();
GitDiffItem diff(RavenTreeItem *item);
GitStageResponseCode stageItem(RavenTreeItem *item);
GitStageResponseCode unstageItem(RavenTreeItem *item);
int commit(QList<QString> items, QString msg, bool amend);
QList<GitManager::GitBranchSelectorItem> getAllBranchesAndTags();
/**
* @brief GitManager::checkoutToRef
* Checkout in `libgit2` doesn't switch branch, it simply checks files out on disk.
* Hence, we must call `git_repository_set_head` on success.
* More info: https://stackoverflow.com/a/46758861
* @param item The payload contains data used to checkout to ref (branch/tag)
* @return `nullptr` on success or QString with error message.
*/
QString checkoutToRef(GitBranchSelectorItem item);
signals:
void statusChanged(GitManager::status_data payload);
private:
git_repository *m_repo = nullptr;
QString m_repoPath = nullptr;
struct RavenFile {
QString content;
bool binary;
};
std::optional<RavenFile> getFileContent(git_oid oid);
std::optional<RavenFile> getLocalFileContent(QString absPath);
typedef struct diff_data {
git_oid old_oid;
git_oid new_oid;
QString oldPath;
QString oldAbsPath;
QString newPath;
QString newAbsPath;
git_delta_t status;
QString reqFilePath;
QString repoPath;
} diff_data;
// This MUST be called in constructor only.
int init();
static int each_file_cb(const git_diff_delta *delta,
float progress,
void *payload);
static int each_binary_file_cb(const git_diff_delta *delta,
const git_diff_binary *binary,
void *payload);
QString oid_to_str(git_oid oid);
QString getCheckoutErrorMessage();
std::string generateRefName(GitManager::GitBranchSelectorItem *item);
};
#endif // GITMANAGER_H