Skip to content

Commit

Permalink
#5662: Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
codereader committed Jul 11, 2021
1 parent 5f2aeb4 commit dab0dec
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 26 deletions.
74 changes: 49 additions & 25 deletions plugins/vcs/ui/VcsStatus.cpp
Expand Up @@ -37,8 +37,8 @@ VcsStatus::VcsStatus(wxWindow* parent) :
_mapStatus = new wxStaticText(this, wxID_ANY, "");
table->Add(_mapStatus, 0, wxLEFT, 6);

_text = new wxStaticText(this, wxID_ANY, _("Not under Version Control"));
table->Add(_text, 0, wxALIGN_RIGHT | wxRIGHT, 6);
_remoteStatus = new wxStaticText(this, wxID_ANY, _("Not under Version Control"));
table->Add(_remoteStatus, 0, wxALIGN_RIGHT | wxRIGHT, 6);

Bind(wxEVT_TIMER, &VcsStatus::onIntervalReached, this);
Bind(wxEVT_IDLE, &VcsStatus::onIdle, this);
Expand Down Expand Up @@ -78,12 +78,12 @@ void VcsStatus::setRepository(const std::shared_ptr<git::Repository>& repository

if (!_repository)
{
_text->SetLabel(_("Not under version control"));
_remoteStatus->SetLabel(_("Not under version control"));
_timer.Stop();
return;
}

_text->SetLabel(_repository->getCurrentBranchName());
_remoteStatus->SetLabel(_repository->getCurrentBranchName());
restartTimer();

// Run a fetch update right after connecting to the repo, if auto-fetch is enabled
Expand Down Expand Up @@ -118,17 +118,20 @@ void VcsStatus::onMapEvent(IMap::MapEvent ev)

void VcsStatus::startFetchTask()
{
std::lock_guard<std::mutex> guard(_taskLock);
{
std::lock_guard<std::mutex> guard(_taskLock);

if (_fetchInProgress || !_repository) return;
if (_fetchInProgress || !_repository) return;

if (!GlobalMainFrame().isActiveApp())
{
rMessage() << "Skipping fetch this round, since the app is not active." << std::endl;
return;
if (!GlobalMainFrame().isActiveApp())
{
rMessage() << "Skipping fetch this round, since the app is not active." << std::endl;
return;
}

_fetchInProgress = true;
}

_fetchInProgress = true;
auto repository = _repository->clone();
_fetchTask = std::async(std::launch::async, std::bind(&VcsStatus::performFetch, this, repository));
}
Expand Down Expand Up @@ -163,36 +166,50 @@ void VcsStatus::onIdle(wxIdleEvent& ev)

void VcsStatus::performFetch(std::shared_ptr<git::Repository> repository)
{
if (!repository->getHead()) return;
std::string statusText;

GlobalUserInterface().dispatch([this]() { _text->SetLabel(_("Fetching...")); });
auto head = repository->getHead();

std::string text;
if (!head)
{
_fetchInProgress = false;
return;
}

try
{
repository->fetchFromTrackedRemote();

std::lock_guard<std::mutex> guard(_taskLock);
repository->getUpstreamRemoteName(*head);
}
catch (const git::GitException&)
{
setRemoteStatus(_("Not connected"));
_fetchInProgress = false;
return;
}

try
{
setRemoteStatus(_("Fetching..."));

repository->fetchFromTrackedRemote();

auto status = repository->getSyncStatusOfBranch(*repository->getHead());

if (status.localIsUpToDate)
{
text = _("Up to date");
statusText = _("Up to date");
}
else if (status.localCanBePushed)
{
text = fmt::format(_("{0} to push"), status.localCommitsAhead);
statusText = fmt::format(_("{0} to push"), status.localCommitsAhead);
}
else if (status.localCommitsAhead == 0)
{
text = fmt::format(_("{0} to integrate"), status.remoteCommitsAhead);
statusText = fmt::format(_("{0} to integrate"), status.remoteCommitsAhead);
}
else
{
text = fmt::format(_("{0} to push, {1} to integrate"), status.localCommitsAhead, status.remoteCommitsAhead);
statusText = fmt::format(_("{0} to push, {1} to integrate"), status.localCommitsAhead, status.remoteCommitsAhead);
}

if (status.remoteCommitsAhead > 0)
Expand All @@ -212,28 +229,35 @@ void VcsStatus::performFetch(std::shared_ptr<git::Repository> repository)

if (diffAgainstBase->containsFile(mapPath))
{
text = fmt::format(_("{0} possible conflict"), status.remoteCommitsAhead);
statusText = fmt::format(_("{0} possible conflict"), status.remoteCommitsAhead);
}
else
{
text = fmt::format(_("{0} no conflicts"), status.remoteCommitsAhead);
statusText = fmt::format(_("{0} no conflicts"), status.remoteCommitsAhead);
}
}
}
}
catch (const git::GitException& ex)
{
text = ex.what();
statusText = ex.what();
}

GlobalUserInterface().dispatch([this, text]() { _text->SetLabel(text); });
setRemoteStatus(statusText);

_fetchInProgress = false;
}

void VcsStatus::setMapFileStatus(const std::string& status)
{
GlobalUserInterface().dispatch([this, status]() { _mapStatus->SetLabel(status); });
}

void VcsStatus::setRemoteStatus(const std::string& status)
{
GlobalUserInterface().dispatch([this, status]() { _remoteStatus->SetLabel(status); });
}

std::string VcsStatus::getRepositoryRelativePath(const std::string& path, const std::shared_ptr<git::Repository>& repository)
{
if (!os::fileOrDirExists(path))
Expand Down
3 changes: 2 additions & 1 deletion plugins/vcs/ui/VcsStatus.h
Expand Up @@ -29,7 +29,7 @@ class VcsStatus final :

std::shared_ptr<git::Repository> _repository;

wxStaticText* _text;
wxStaticText* _remoteStatus;
wxStaticText* _mapStatus;

public:
Expand All @@ -50,6 +50,7 @@ class VcsStatus final :
void updateMapFileStatus();
void onMapEvent(IMap::MapEvent ev);
void setMapFileStatus(const std::string& status);
void setRemoteStatus(const std::string& status);

std::string getRepositoryRelativePath(const std::string& path, const std::shared_ptr<git::Repository>& repository);
};
Expand Down

0 comments on commit dab0dec

Please sign in to comment.