-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommitdao.cpp
138 lines (100 loc) · 3.27 KB
/
commitdao.cpp
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
137
138
#include <git2.h>
#include "commit.h"
#include "commitdao.h"
CommitDao::CommitDao()
: m_repository(nullptr)
{
}
void CommitDao::setRepository(git_repository *repository)
{
m_repository = repository;
}
QStringList CommitDao::getCommitHashList() const
{
if (!m_repository)
return QStringList();
QStringList commitHashList;
git_revwalk *walker = nullptr;
git_revwalk_new(&walker, m_repository);
git_revwalk_push_head(walker);
git_oid oid;
while (!git_revwalk_next(&oid, walker)) {
git_commit *commit = nullptr;
git_commit_lookup(&commit, m_repository, &oid);
char buffer[GIT_OID_HEXSZ+1];
git_oid_tostr(buffer, sizeof(buffer)/sizeof(char), &oid);
commitHashList.append(buffer);
git_commit_free(commit);
}
git_revwalk_free(walker);
return commitHashList;
}
QString CommitDao::getSummary(const QString &hash) const
{
Q_ASSERT(m_repository);
git_oid oid;
git_oid_fromstr(&oid, hash.toStdString().c_str());
git_commit *commit = nullptr;
git_commit_lookup(&commit, m_repository, &oid);
return QString(git_commit_summary(commit));
}
QString CommitDao::getMessage(const QString &hash) const
{
Q_ASSERT(m_repository);
git_oid oid;
git_oid_fromstr(&oid, hash.toStdString().c_str());
git_commit *commit = nullptr;
git_commit_lookup(&commit, m_repository, &oid);
return QString(git_commit_message(commit));
}
QMap<int, QString> CommitDao::getAuthor(const QString &hash) const
{
Q_ASSERT(m_repository);
QMap<int, QString> author;
git_oid oid;
git_oid_fromstr(&oid, hash.toStdString().c_str());
git_commit *commit = nullptr;
git_commit_lookup(&commit, m_repository, &oid);
const git_signature *signature = git_commit_author(commit);
author.insert(Commit::Name, signature->name);
author.insert(Commit::Email, signature->email);
return author;
}
QDateTime CommitDao::getTime(const QString &hash) const
{
Q_ASSERT(m_repository);
git_oid oid;
git_oid_fromstr(&oid, hash.toStdString().c_str());
git_commit *commit = nullptr;
git_commit_lookup(&commit, m_repository, &oid);
const git_time_t time = git_commit_time(commit);
#if (QT_VERSION >= QT_VERSION_CHECK(5, 8, 0))
return QDateTime::fromSecsSinceEpoch(time);
#else
return QDateTime::fromMSecsSinceEpoch(time * 1000);
#endif
}
QString CommitDao::getDiff(const QString &hash) const
{
Q_ASSERT(m_repository);
git_oid oid;
git_oid_fromstr(&oid, hash.toStdString().c_str());
git_commit *commit = nullptr;
git_commit_lookup(&commit, m_repository, &oid);
git_commit *parent = nullptr;
git_commit_parent(&parent, commit, 0);
git_tree *commit_tree = nullptr;
git_commit_tree(&commit_tree, commit);
git_tree *parent_tree = nullptr;
// The first commit does not have parent. In this case, create diff to an empty tree
if (parent)
git_commit_tree(&parent_tree, parent);
git_diff *diff = nullptr;
git_diff_tree_to_tree(&diff, m_repository, parent_tree, commit_tree, nullptr);
git_buf buf = GIT_BUF_INIT_CONST(nullptr, 0);
git_diff_to_buf(&buf, diff, GIT_DIFF_FORMAT_PATCH);
QString diffString = QString(buf.ptr);
git_diff_free(diff);
git_buf_free(&buf);
return diffString;
}