/
Repository.cpp
370 lines (284 loc) · 9.34 KB
/
Repository.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
#include "Repository.h"
#include <git2.h>
#include "itextstream.h"
#include "imap.h"
#include "Remote.h"
#include "Commit.h"
#include "Tree.h"
#include "Diff.h"
#include "GitException.h"
#include "os/path.h"
#include "os/file.h"
namespace vcs
{
namespace git
{
Repository::Repository(const std::string& path) :
_repository(nullptr),
_isOk(false),
_path(os::standardPathWithSlash(path))
{
if (git_repository_open(&_repository, _path.c_str()) == 0)
{
_isOk = true;
}
else
{
rMessage() << "Failed to open repository at " << _path << std::endl;
}
}
Repository::~Repository()
{
git_repository_free(_repository);
}
bool Repository::isOk() const
{
return _isOk;
}
const std::string& Repository::getPath() const
{
return _path;
}
std::string Repository::getRepositoryRelativePath(const std::string& path)
{
if (!os::fileOrDirExists(path))
{
return ""; // doesn't exist
}
auto relativePath = os::getRelativePath(path, getPath());
if (relativePath == path)
{
return ""; // outside VCS
}
return relativePath;
}
std::shared_ptr<Repository> Repository::clone()
{
return std::make_shared<Repository>(_path);
}
std::shared_ptr<Remote> Repository::getRemote(const std::string& name)
{
return Remote::CreateFromName(*this, name);
}
Reference::Ptr Repository::getHead()
{
git_reference* head;
int error = git_repository_head(&head, _repository);
if (error == GIT_EUNBORNBRANCH || error == GIT_ENOTFOUND)
{
return Reference::Ptr();
}
return std::make_shared<Reference>(head);
}
std::string Repository::getCurrentBranchName()
{
auto head = getHead();
return head ? head->getShorthandName() : std::string();
}
std::string Repository::getUpstreamRemoteName(const Reference& reference)
{
git_buf buf;
memset(&buf, 0, sizeof(git_buf));
auto error = git_branch_upstream_remote(&buf, _repository, reference.getName().c_str());
GitException::ThrowOnError(error);
std::string upstreamRemote = buf.ptr;
git_buf_dispose(&buf);
return upstreamRemote;
}
Remote::Ptr Repository::getTrackedRemote()
{
auto head = getHead();
if (!head)
{
throw GitException("Could not retrieve HEAD reference from repository");
}
auto trackedBranch = head->getUpstream();
rMessage() << head->getShorthandName() << " is set up to track " << (trackedBranch ? trackedBranch->getShorthandName() : "-") << std::endl;
if (!trackedBranch)
{
throw GitException("No tracked remote branch configured");
}
auto remoteName = getUpstreamRemoteName(*head);
rMessage() << head->getShorthandName() << " is set up to track remote " << remoteName << std::endl;
auto remote = getRemote(remoteName);
if (!remote)
{
throw GitException("Failed to get the named remote: " + remoteName);
}
return remote;
}
void Repository::fetchFromTrackedRemote()
{
auto remote = getTrackedRemote();
remote->fetch();
}
void Repository::pushToTrackedRemote()
{
auto remote = getTrackedRemote();
remote->push(*getHead()); // getHead will succeed because getTrackedRemote did
}
void Repository::fastForwardToTrackedRemote()
{
auto head = getHead();
if (!head) throw GitException("Could not retrieve HEAD reference from repository");
auto upstream = head->getUpstream();
if (!upstream) throw GitException("No tracked remote branch configured");
// Lookup the target object
git_oid targetOid;
git_reference_name_to_id(&targetOid, _repository, upstream->getName().c_str());
git_object* target;
auto error = git_object_lookup(&target, _repository, &targetOid, GIT_OBJECT_COMMIT);
GitException::ThrowOnError(error);
rMessage() << "Fast-fowarding " << head->getName() << " to upstream " << upstream->getName() << std::endl;
try
{
// Checkout the result so the workdir is in the expected state
git_checkout_options checkoutOptions = GIT_CHECKOUT_OPTIONS_INIT;
checkoutOptions.checkout_strategy = GIT_CHECKOUT_SAFE;
error = git_checkout_tree(_repository, target, &checkoutOptions);
GitException::ThrowOnError(error);
// Move the target reference to the target OID
head->setTarget(&targetOid);
rMessage() << "Fast-foward done, " << head->getName() << " is now at " << Reference::OidToString(&targetOid) << std::endl;
}
catch (const GitException& ex)
{
git_object_free(target);
throw ex;
}
}
RefSyncStatus Repository::getSyncStatusOfBranch(const Reference& reference)
{
RefSyncStatus status;
auto trackedBranch = reference.getUpstream();
if (!trackedBranch) throw GitException("No tracked branch, cannot check sync status");
git_revwalk* walker;
git_revwalk_new(&walker, _repository);
// Start from remote
git_revwalk_push_ref(walker, trackedBranch->getName().c_str());
// End at local
git_oid refOid;
git_reference_name_to_id(&refOid, _repository, reference.getName().c_str());
git_revwalk_hide(walker, &refOid);
git_oid id;
while (!git_revwalk_next(&id, walker))
{
rMessage() << Reference::OidToString(&id) << " => ";
++status.remoteCommitsAhead;
}
rMessage() << std::endl;
git_revwalk_free(walker);
// Another walk from local to remote
git_revwalk_new(&walker, _repository);
git_revwalk_push(walker, &refOid);
git_revwalk_hide_ref(walker, trackedBranch->getName().c_str());
while (!git_revwalk_next(&id, walker))
{
rMessage() << Reference::OidToString(&id) << " => ";
++status.localCommitsAhead;
}
rMessage() << std::endl;
git_revwalk_free(walker);
// Initialise the convenience flags
status.localIsUpToDate = status.localCommitsAhead == 0 && status.remoteCommitsAhead == 0;
status.localCanBePushed = status.localCommitsAhead > 0 && status.remoteCommitsAhead == 0;
return status;
}
bool Repository::isUpToDateWithRemote()
{
auto head = getHead();
if (!head)
{
rWarning() << "Could not retrieve HEAD reference from repository" << std::endl;
return false;
}
return getSyncStatusOfBranch(*head).localIsUpToDate;
}
unsigned int Repository::getFileStatus(const std::string& relativePath)
{
git_status_options options = GIT_STATUS_OPTIONS_INIT;
char* paths[] = { const_cast<char*>(relativePath.c_str()) };
options.pathspec.count = 1;
options.pathspec.strings = paths;
options.flags |= GIT_STATUS_OPT_INCLUDE_UNTRACKED | GIT_STATUS_OPT_RECURSE_UNTRACKED_DIRS;
options.show = GIT_STATUS_SHOW_WORKDIR_ONLY;
unsigned int statusFlags = 0;
auto error = git_status_foreach_ext(_repository, &options, [](const char* path, unsigned int flags, void* payload)
{
*reinterpret_cast<unsigned int*>(payload) = flags;
return 0;
}, &statusFlags);
GitException::ThrowOnError(error);
return statusFlags;
}
bool Repository::fileIsIndexed(const std::string& relativePath)
{
return (getFileStatus(relativePath) & GIT_STATUS_WT_NEW) == 0;
}
bool Repository::fileHasUncommittedChanges(const std::string& relativePath)
{
return (getFileStatus(relativePath) & GIT_STATUS_WT_MODIFIED) != 0;
}
Index::Ptr Repository::getIndex()
{
git_index* index;
auto error = git_repository_index(&index, _repository);
GitException::ThrowOnError(error);
return std::make_shared<Index>(index);
}
std::shared_ptr<Tree> Repository::getTreeByRevision(const std::string& revision)
{
git_oid revisionOid;
auto error = git_oid_fromstr(&revisionOid, revision.c_str());
GitException::ThrowOnError(error);
auto commit = Commit::LookupFromOid(_repository, &revisionOid);
return commit->getTree();
}
void Repository::createCommit(const CommitMetadata& metadata)
{
}
bool Repository::isReadyForMerge()
{
auto state = git_repository_state(_repository);
return state == GIT_REPOSITORY_STATE_NONE;
}
bool Repository::mergeIsInProgress()
{
auto state = git_repository_state(_repository);
return state == GIT_REPOSITORY_STATE_MERGE;
}
Commit::Ptr Repository::findMergeBase(const Reference& first, const Reference& second)
{
git_oid firstOid;
auto error = git_reference_name_to_id(&firstOid, _repository, first.getName().c_str());
GitException::ThrowOnError(error);
git_oid secondOid;
error = git_reference_name_to_id(&secondOid, _repository, second.getName().c_str());
GitException::ThrowOnError(error);
git_oid mergeBase;
error = git_merge_base(&mergeBase, _repository, &firstOid, &secondOid);
GitException::ThrowOnError(error);
git_commit* commit;
error = git_commit_lookup(&commit, _repository, &mergeBase);
GitException::ThrowOnError(error);
return std::make_shared<Commit>(commit);
}
std::shared_ptr<Diff> Repository::getDiff(const Reference& ref, Commit& commit)
{
git_oid refOid;
auto error = git_reference_name_to_id(&refOid, _repository, ref.getName().c_str());
GitException::ThrowOnError(error);
auto refCommit = Commit::LookupFromOid(_repository, &refOid);
auto refTree = refCommit->getTree();
git_diff* diff;
auto baseTree = commit.getTree();
error = git_diff_tree_to_tree(&diff, _repository, baseTree->_get(), refTree->_get(), nullptr);
GitException::ThrowOnError(error);
return std::make_shared<Diff>(diff);
}
git_repository* Repository::_get()
{
return _repository;
}
}
}