Skip to content

Commit

Permalink
#5662: Fetching the remote of the current branch is working now
Browse files Browse the repository at this point in the history
  • Loading branch information
codereader committed Jul 4, 2021
1 parent 48cf0d8 commit 5f1704a
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 12 deletions.
17 changes: 16 additions & 1 deletion plugins/vcs/GitModule.cpp
Expand Up @@ -81,7 +81,22 @@ void GitModule::fetch(const cmd::ArgumentList& args)
return;
}

auto remote = _repository->getRemote("origin");
auto head = _repository->getHead();

if (!head)
{
rWarning() << "Could not retrieve HEAD reference from repository" << std::endl;
return;
}

auto trackedBranch = head->getUpstream();

rMessage() << head->getShorthandName() << " is set up to track " << (trackedBranch ? trackedBranch->getShorthandName() : "-") << std::endl;

auto remoteName = _repository->getUpstreamRemoteName(*head);
rMessage() << head->getShorthandName() << " is set up to track remote " << remoteName << std::endl;

auto remote = _repository->getRemote(remoteName);

if (!remote)
{
Expand Down
52 changes: 52 additions & 0 deletions plugins/vcs/Reference.h
@@ -0,0 +1,52 @@
#pragma once

#include <string>
#include <memory>
#include <git2.h>

namespace vcs
{

namespace git
{

class Reference final
{
private:
git_reference* _reference;

public:
using Ptr = std::shared_ptr<Reference>;

Reference(git_reference* reference) :
_reference(reference)
{}

std::string getName() const
{
return git_reference_name(_reference);
}

std::string getShorthandName() const
{
return git_reference_shorthand(_reference);
}

// Returns the upstream of this reference (if configured)
Ptr getUpstream()
{
git_reference* upstream = nullptr;
git_branch_upstream(&upstream, _reference);

return upstream != nullptr ? std::make_shared<Reference>(upstream) : Ptr();
}

~Reference()
{
git_reference_free(_reference);
}
};

}

}
2 changes: 1 addition & 1 deletion plugins/vcs/Remote.h
Expand Up @@ -10,7 +10,7 @@ namespace vcs
namespace git
{

class Remote
class Remote final
{
private:
git_remote* _remote;
Expand Down
31 changes: 21 additions & 10 deletions plugins/vcs/Repository.cpp
Expand Up @@ -39,25 +39,36 @@ std::shared_ptr<Remote> Repository::getRemote(const std::string& name)
return Remote::CreateFromName(*this, name);
}

std::string Repository::getCurrentBranchName()
Reference::Ptr Repository::getHead()
{
git_reference* head = nullptr;
git_reference* head;
int error = git_repository_head(&head, _repository);

if (error == GIT_EUNBORNBRANCH || error == GIT_ENOTFOUND)
{
return "";
return Reference::Ptr();
}

std::string branchName;
return std::make_shared<Reference>(head);
}

if (!error)
{
branchName = git_reference_shorthand(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));

git_branch_upstream_remote(&buf, _repository, reference.getName().c_str());

std::string upstreamRemote = buf.ptr;
git_buf_dispose(&buf);

git_reference_free(head);
return branchName;
return upstreamRemote;
}

git_repository* Repository::_get()
Expand Down
5 changes: 5 additions & 0 deletions plugins/vcs/Repository.h
Expand Up @@ -2,6 +2,7 @@

#include <string>
#include <memory>
#include "Reference.h"

struct git_repository;

Expand Down Expand Up @@ -35,6 +36,10 @@ class Repository final

std::string getCurrentBranchName();

std::string getUpstreamRemoteName(const Reference& reference);

Reference::Ptr getHead();

// Return the raw libgit2 object
git_repository* _get();
};
Expand Down
1 change: 1 addition & 0 deletions tools/msvc/vcs.vcxproj
Expand Up @@ -173,6 +173,7 @@
<ItemGroup>
<ClInclude Include="..\..\plugins\vcs\CredentialManager.h" />
<ClInclude Include="..\..\plugins\vcs\GitModule.h" />
<ClInclude Include="..\..\plugins\vcs\Reference.h" />
<ClInclude Include="..\..\plugins\vcs\Remote.h" />
<ClInclude Include="..\..\plugins\vcs\Repository.h" />
</ItemGroup>
Expand Down
3 changes: 3 additions & 0 deletions tools/msvc/vcs.vcxproj.filters
Expand Up @@ -26,5 +26,8 @@
<ClInclude Include="..\..\plugins\vcs\CredentialManager.h">
<Filter>src</Filter>
</ClInclude>
<ClInclude Include="..\..\plugins\vcs\Reference.h">
<Filter>src</Filter>
</ClInclude>
</ItemGroup>
</Project>

0 comments on commit 5f1704a

Please sign in to comment.