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 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491
|
#include "Repository.h"
#include "i18n.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 "Signature.h"
#include "os/path.h"
#include "os/file.h"
#include "fmt/format.h"
#if LIBGIT2_VER_MAJOR <= 0 && LIBGIT2_VER_MINOR < 28
// Compatibility to older libgit2
#define GIT_OBJECT_COMMIT GIT_OBJ_COMMIT
#endif
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;
#if LIBGIT2_VER_MAJOR <= 0 && LIBGIT2_VER_MINOR < 28
git_buf_free(&buf); // git_buf_dispose was introduced in 0.28
#else
git_buf_dispose(&buf);
#endif
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(fmt::format(_("Failed to get the named remote: {0}"), 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(_("The current branch doesn't track a remote, cannot check sync status"));
git_revwalk* walker = nullptr;
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)
{
createCommit(metadata, Reference::Ptr());
}
void Repository::createCommit(const CommitMetadata& metadata, const Reference::Ptr& additionalParent)
{
auto head = getHead();
auto index = getIndex();
rMessage() << "Creating commit with user " << metadata.name << std::endl;
Signature signature(metadata.name, metadata.email);
// Add all working copy changes
index->updateAll();
auto tree = index->writeTree(*this);
std::vector<const git_commit*> parentCommits;
// It's possible that there is no HEAD yet (first commit in the repo)
if (head)
{
git_oid headOid;
auto error = git_reference_name_to_id(&headOid, _repository, head->getName().c_str());
GitException::ThrowOnError(error);
auto parentCommit = Commit::LookupFromOid(_repository, &headOid);
parentCommits.push_back(parentCommit->_get());
}
// Check if we have an additional parent
if (additionalParent)
{
git_oid parentOid;
auto error = git_reference_name_to_id(&parentOid, _repository, additionalParent->getName().c_str());
GitException::ThrowOnError(error);
auto additionalParentCommit = Commit::LookupFromOid(_repository, &parentOid);
parentCommits.push_back(additionalParentCommit->_get());
}
git_oid commitOid;
auto error = git_commit_create(&commitOid,
_repository, head ? head->getName().c_str() : "HEAD",
signature.get(), signature.get(),
nullptr, metadata.message.c_str(),
tree->_get(),
parentCommits.size(), parentCommits.data());
GitException::ThrowOnError(error);
index->write();
rMessage() << "Commit created: " << Reference::OidToString(&commitOid) << std::endl;
}
std::string Repository::getConfigValue(const std::string& key)
{
git_config* config;
auto error = git_repository_config_snapshot(&config, _repository);
GitException::ThrowOnError(error);
try
{
const char* value;
auto error = git_config_get_string(&value, config, key.c_str());
GitException::ThrowOnError(error);
// Copy the value before free-ing the config
std::string returnValue(value);
git_config_free(config);
return returnValue;
}
catch (const GitException& ex)
{
git_config_free(config);
throw ex;
}
}
void Repository::cleanupState()
{
auto error = git_repository_state_cleanup(_repository);
GitException::ThrowOnError(error);
}
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;
}
void Repository::abortMerge()
{
if (!mergeIsInProgress())
{
return;
}
auto head = getHead();
git_oid targetOid;
auto error = git_reference_name_to_id(&targetOid, _repository, head->getName().c_str());
GitException::ThrowOnError(error);
git_object* target;
error = git_object_lookup(&target, _repository, &targetOid, GIT_OBJECT_COMMIT);
GitException::ThrowOnError(error);
git_checkout_options checkoutOptions = GIT_CHECKOUT_OPTIONS_INIT;
checkoutOptions.checkout_strategy = GIT_CHECKOUT_FORCE;
error = git_reset(_repository, target, GIT_RESET_HARD, &checkoutOptions);
GitException::ThrowOnError(error);
}
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;
}
}
}
|