Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
#5840: Fix crashes when dealing with empty repositories
  • Loading branch information
codereader committed Feb 28, 2022
1 parent 5cd0c6b commit 0988f7a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
2 changes: 1 addition & 1 deletion plugins/vcs/Algorithm.h
Expand Up @@ -58,7 +58,7 @@ inline RemoteStatus analyseRemoteStatus(const std::shared_ptr<Repository>& repos
{
auto mapPath = repository->getRepositoryRelativePath(GlobalMapModule().getMapName());

if (mapPath.empty())
if (mapPath.empty() || !repository->getHead())
{
return RemoteStatus{ 0, 0, _("-") };
}
Expand Down
20 changes: 12 additions & 8 deletions plugins/vcs/Repository.cpp
Expand Up @@ -326,15 +326,19 @@ void Repository::createCommit(const CommitMetadata& metadata, const Reference::P

auto tree = index->writeTree(*this);

git_oid headOid;
error = git_reference_name_to_id(&headOid, _repository, head->getName().c_str());
GitException::ThrowOnError(error);

auto parentCommit = Commit::LookupFromOid(_repository, &headOid);

std::vector<const git_commit*> parentCommits;
parentCommits.push_back(parentCommit->_get());

// It's possible that there is no HEAD yet (first commit in the repo)
if (head)
{
git_oid headOid;
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)
{
Expand All @@ -349,7 +353,7 @@ void Repository::createCommit(const CommitMetadata& metadata, const Reference::P

git_oid commitOid;
error = git_commit_create(&commitOid,
_repository, head->getName().c_str(),
_repository, head ? head->getName().c_str() : "HEAD",
signature, signature,
nullptr, metadata.message.c_str(),
tree->_get(),
Expand Down

0 comments on commit 0988f7a

Please sign in to comment.