From 5f1704a515b0c8b68bbea27878626773789b1734 Mon Sep 17 00:00:00 2001 From: codereader Date: Sun, 4 Jul 2021 20:52:43 +0200 Subject: [PATCH] #5662: Fetching the remote of the current branch is working now --- plugins/vcs/GitModule.cpp | 17 ++++++++++- plugins/vcs/Reference.h | 52 ++++++++++++++++++++++++++++++++++ plugins/vcs/Remote.h | 2 +- plugins/vcs/Repository.cpp | 31 +++++++++++++------- plugins/vcs/Repository.h | 5 ++++ tools/msvc/vcs.vcxproj | 1 + tools/msvc/vcs.vcxproj.filters | 3 ++ 7 files changed, 99 insertions(+), 12 deletions(-) create mode 100644 plugins/vcs/Reference.h diff --git a/plugins/vcs/GitModule.cpp b/plugins/vcs/GitModule.cpp index 99f62d01bd..938cbe16d9 100644 --- a/plugins/vcs/GitModule.cpp +++ b/plugins/vcs/GitModule.cpp @@ -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) { diff --git a/plugins/vcs/Reference.h b/plugins/vcs/Reference.h new file mode 100644 index 0000000000..716123aae2 --- /dev/null +++ b/plugins/vcs/Reference.h @@ -0,0 +1,52 @@ +#pragma once + +#include +#include +#include + +namespace vcs +{ + +namespace git +{ + +class Reference final +{ +private: + git_reference* _reference; + +public: + using Ptr = std::shared_ptr; + + 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(upstream) : Ptr(); + } + + ~Reference() + { + git_reference_free(_reference); + } +}; + +} + +} diff --git a/plugins/vcs/Remote.h b/plugins/vcs/Remote.h index 25b3e1c2df..af53e32e3b 100644 --- a/plugins/vcs/Remote.h +++ b/plugins/vcs/Remote.h @@ -10,7 +10,7 @@ namespace vcs namespace git { -class Remote +class Remote final { private: git_remote* _remote; diff --git a/plugins/vcs/Repository.cpp b/plugins/vcs/Repository.cpp index 6da4c37774..c09821806d 100644 --- a/plugins/vcs/Repository.cpp +++ b/plugins/vcs/Repository.cpp @@ -39,25 +39,36 @@ std::shared_ptr 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(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() diff --git a/plugins/vcs/Repository.h b/plugins/vcs/Repository.h index 202ce30147..e2b3c977ba 100644 --- a/plugins/vcs/Repository.h +++ b/plugins/vcs/Repository.h @@ -2,6 +2,7 @@ #include #include +#include "Reference.h" struct git_repository; @@ -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(); }; diff --git a/tools/msvc/vcs.vcxproj b/tools/msvc/vcs.vcxproj index fe31f9ddc8..62c5953e44 100644 --- a/tools/msvc/vcs.vcxproj +++ b/tools/msvc/vcs.vcxproj @@ -173,6 +173,7 @@ + diff --git a/tools/msvc/vcs.vcxproj.filters b/tools/msvc/vcs.vcxproj.filters index 5d29af3e36..faeb1b5ddf 100644 --- a/tools/msvc/vcs.vcxproj.filters +++ b/tools/msvc/vcs.vcxproj.filters @@ -26,5 +26,8 @@ src + + src + \ No newline at end of file