forked from chengxiangfei/libqgit2
/
qgitrepository.cpp
284 lines (242 loc) · 7.96 KB
/
qgitrepository.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/******************************************************************************
* This file is part of the Gluon Development Platform
* Copyright (c) 2011 Laszlo Papp <djszapi@archlinux.us>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "qgitrepository.h"
#include "qgitcommit.h"
#include "qgittag.h"
#include "qgittree.h"
#include "qgitblob.h"
#include "qgitsignature.h"
#include <git2/errors.h>
#include <git2/repository.h>
#include <git2/refs.h>
#include <git2/commit.h>
#include <git2/tag.h>
#include <git2/tree.h>
#include <git2/blob.h>
#include <QtCore/QFile>
#include <QtCore/QVector>
using namespace LibQGit2;
namespace {
void do_not_free(git_repository*) {}
}
QGitRepository::QGitRepository(git_repository *repository, bool own)
: d(repository, own ? git_repository_free : do_not_free)
{
}
QGitRepository::QGitRepository( const QGitRepository& other )
: d(other.d)
{
}
QGitRepository::~QGitRepository()
{
}
int QGitRepository::init(const QString& path, bool isBare)
{
d.clear();
git_repository *repo = 0;
int ret = git_repository_init(&repo, QFile::encodeName(path), isBare);
if (ret == GIT_SUCCESS)
d = ptr_type(repo, git_repository_free);
return ret;
}
int QGitRepository::open(const QString& path)
{
d.clear();
git_repository *repo = 0;
int ret = git_repository_open(&repo, QFile::encodeName(path));
if (ret == GIT_SUCCESS)
d = ptr_type(repo, git_repository_free);
return ret;
}
int QGitRepository::open(const QString& gitDir,
const QString& gitObjectDirectory,
const QString& gitIndexFile,
const QString& gitWorkTree)
{
d.clear();
git_repository *repo = 0;
int ret = git_repository_open2(&repo,
QFile::encodeName(gitDir),
QFile::encodeName(gitObjectDirectory),
QFile::encodeName(gitIndexFile),
QFile::encodeName(gitWorkTree));
if (ret == GIT_SUCCESS)
d = ptr_type(repo, git_repository_free);
return ret;
}
int QGitRepository::open(const QString& gitDir,
QGitDatabase *objectDatabase,
const QString& gitIndexFile,
const QString& gitWorkTree)
{
d.clear();
git_repository *repo = 0;
int ret = git_repository_open3(&repo,
QFile::encodeName(gitDir),
objectDatabase->data(),
QFile::encodeName(gitIndexFile),
QFile::encodeName(gitWorkTree));
if (ret == GIT_SUCCESS)
d = ptr_type(repo, git_repository_free);
return ret;
}
QGitRef QGitRepository::head()
{
git_reference *ref = 0;
git_repository_head(&ref, data());
return QGitRef(ref);
}
bool QGitRepository::isHeadDetached() const
{
return git_repository_head_detached(data()) == 1;
}
bool QGitRepository::isHeadOrphan() const
{
return git_repository_head_orphan(data()) == 1;
}
bool QGitRepository::isEmpty() const
{
return git_repository_is_empty(data()) == 1;
}
bool QGitRepository::isBare() const
{
return git_repository_is_bare(data()) == 1;
}
QString QGitRepository::path() const
{
return QFile::decodeName(git_repository_path(data(), GIT_REPO_PATH));
}
QString QGitRepository::indexPath() const
{
return QFile::decodeName(git_repository_path(data(), GIT_REPO_PATH_INDEX));
}
QString QGitRepository::databasePath() const
{
return QFile::decodeName(git_repository_path(data(), GIT_REPO_PATH_ODB));
}
QString QGitRepository::workDirPath() const
{
return QFile::decodeName(git_repository_path(data(), GIT_REPO_PATH_WORKDIR));
}
QGitRef QGitRepository::lookupRef(const QString& name)
{
git_reference *ref = 0;
git_reference_lookup(&ref, data(), QFile::encodeName(name));
return QGitRef(ref);
}
QGitCommit QGitRepository::lookupCommit(const QGitOId& oid)
{
git_commit *commit = 0;
git_commit_lookup_prefix(&commit, data(), oid.data(), oid.length());
return QGitCommit(commit);
}
QGitTag QGitRepository::lookupTag(const QGitOId& oid)
{
git_tag *tag = 0;
git_tag_lookup_prefix(&tag, data(), oid.data(), oid.length());
return QGitTag(tag);
}
QGitTree QGitRepository::lookupTree(const QGitOId& oid)
{
git_tree *tree = 0;
git_tree_lookup_prefix(&tree, data(), oid.data(), oid.length());
return QGitTree(tree);
}
QGitBlob QGitRepository::lookupBlob(const QGitOId& oid)
{
git_blob *blob = 0;
git_blob_lookup_prefix(&blob, data(), oid.data(), oid.length());
return QGitBlob(blob);
}
QGitObject QGitRepository::lookupAny(const QGitOId &oid)
{
git_object *object = 0;
git_object_lookup_prefix(&object, data(), oid.data(), oid.length(), GIT_OBJ_ANY);
return QGitObject(object);
}
QGitRef QGitRepository::createRef(const QString& name, const QGitOId& oid, bool overwrite)
{
git_reference *ref = 0;
git_reference_create_oid(&ref, data(), QFile::encodeName(name), oid.data(), overwrite);
return QGitRef(ref);
}
QGitRef QGitRepository::createSymbolicRef(const QString& name, const QString& target, bool overwrite)
{
git_reference *ref = 0;
git_reference_create_symbolic(&ref, data(), QFile::encodeName(name), QFile::encodeName(target), overwrite);
return QGitRef(ref);
}
QGitOId QGitRepository::createCommit(const QString& ref,
const QGitSignatureRef& author,
const QGitSignatureRef& committer,
const QString& message,
const QGitTree& tree,
const QList<QGitCommit>& parents)
{
QVector<const git_commit*> p;
foreach (const QGitCommit& parent, parents) {
p.append(parent.data());
}
QGitOId oid;
git_commit_create(oid.data(), data(), QFile::encodeName(ref), author.data(), committer.data(),
NULL, message.toUtf8(), tree.data(), p.size(), p.data());
return oid;
}
QGitOId QGitRepository::createTag(const QString& name,
const QGitObject& target,
const QGitSignatureRef& tagger,
const QString& message,
bool overwrite)
{
QGitOId oid;
git_tag_create(oid.data(), data(), QFile::encodeName(name), target.data(),
tagger.data(), message.toUtf8(), overwrite);
return oid;
}
QGitOId QGitRepository::createBlobFromFile(const QString& path)
{
QGitOId oid;
git_blob_create_fromfile(oid.data(), data(), QFile::encodeName(path));
return oid;
}
QGitOId QGitRepository::createBlobFromBuffer(const QByteArray& buffer)
{
QGitOId oid;
git_blob_create_frombuffer(oid.data(), data(), buffer.data(), buffer.size());
return oid;
}
QGitDatabase* QGitRepository::database() const
{
QGitDatabase *database = new QGitDatabase(git_repository_database(data()));
return database;
}
QGitIndex QGitRepository::index() const
{
git_index *idx;
git_repository_index(&idx, data());
return QGitIndex(idx);
}
git_repository* QGitRepository::data() const
{
return d.data();
}
const git_repository* QGitRepository::constData() const
{
return d.data();
}