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
|
#include "VcsStatus.h"
#include "i18n.h"
#include "imap.h"
#include "ui/iuserinterface.h"
#include "ui/imainframe.h"
#include "imapformat.h"
#include <wx/sizer.h>
#include <wx/bmpbuttn.h>
#include <sigc++/functors/mem_fun.h>
#include "registry/registry.h"
#include "os/file.h"
#include "os/path.h"
#include "os/fs.h"
#include "string/convert.h"
#include "gamelib.h"
#include "wxutil/menu/CommandMenuItem.h"
#include "wxutil/menu/IconTextMenuItem.h"
#include "wxutil/dialog/MessageBox.h"
#include "util/ScopedBoolLock.h"
#include "CommitDialog.h"
#include "../GitModule.h"
#include "../Diff.h"
#include "../GitException.h"
#include "../Algorithm.h"
namespace vcs
{
namespace ui
{
VcsStatus::VcsStatus(wxWindow* parent) :
_panel(loadNamedPanel(parent, "VcsStatusBar")),
_fetchTimer(this),
_statusTimer(this),
_taskInProgress(false),
_popupMenu(new wxutil::PopupMenu)
{
_mapStatus = findNamedObject<wxStaticText>(_panel, "MapStatusLabel");
_remoteStatus = findNamedObject<wxStaticText>(_panel, "RemoteStatusLabel");
_panel->SetMinSize(wxSize(300, -1));
auto* vcsMenu = findNamedObject<wxBitmapButton>(_panel, "VcsMenuButton");
vcsMenu->Hide();
vcsMenu->Bind(wxEVT_BUTTON, [this] (wxCommandEvent& ev)
{
_popupMenu->show(wxDynamicCast(ev.GetEventObject(), wxWindow));
});
Bind(wxEVT_TIMER, &VcsStatus::onIntervalReached, this);
_panel->Bind(wxEVT_IDLE, &VcsStatus::onIdle, this);
GlobalRegistry().signalForKey(RKEY_AUTO_FETCH_ENABLED).connect(
sigc::mem_fun(this, &VcsStatus::restartFetchTimer)
);
GlobalRegistry().signalForKey(RKEY_AUTO_FETCH_INTERVAL).connect(
sigc::mem_fun(this, &VcsStatus::restartFetchTimer)
);
GlobalMapModule().signal_modifiedChanged().connect(
sigc::mem_fun(this, &VcsStatus::updateMapFileStatus)
);
GlobalMapModule().signal_mapEvent().connect(
sigc::mem_fun(this, &VcsStatus::onMapEvent)
);
createPopupMenu();
_statusTimer.Start(5000);
}
VcsStatus::~VcsStatus()
{
_fetchTimer.Stop();
_statusTimer.Stop();
if (_repositoryTask.valid())
{
_repositoryTask.get(); // Wait for the thread to complete
}
if (_mapFileTask.valid())
{
_mapFileTask.get(); // Wait for the thread to complete
}
_panel->Destroy();
}
wxWindow* VcsStatus::getWidget()
{
return _panel;
}
void VcsStatus::createPopupMenu()
{
_popupMenu->addItem(std::make_shared<wxutil::MenuItem>(
new wxMenuItem(nullptr, wxID_ANY, _("Commit"), ""),
[this]() { performCommit(); },
[this]() { return canCommit(); }
));
_popupMenu->addItem(std::make_shared<wxutil::CommandMenuItem>(
new wxMenuItem(nullptr, wxID_ANY, _("Check for Server Changes"), ""),
"GitFetch",
[this]() { return !_taskInProgress; }
));
_popupMenu->addItem(std::make_shared<wxutil::MenuItem>(
new wxMenuItem(nullptr, wxID_ANY, _("Sync/Integrate Server Changes"), ""),
[this]() { performSync(_repository); },
[this]() { return canSync(); }
));
}
void VcsStatus::setRepository(const std::shared_ptr<git::Repository>& repository)
{
_repository = repository;
findNamedObject<wxBitmapButton>(_panel, "VcsMenuButton")->Show(_repository != nullptr);
if (!_repository)
{
_remoteStatus->SetLabel(_("Not under version control"));
_fetchTimer.Stop();
return;
}
_remoteStatus->SetLabel(_repository->getCurrentBranchName());
restartFetchTimer();
// Run a fetch update right after connecting to the repo, if auto-fetch is enabled
if (registry::getValue<bool>(RKEY_AUTO_FETCH_ENABLED))
{
startFetchTask();
}
}
void VcsStatus::restartFetchTimer()
{
_fetchTimer.Stop();
if (registry::getValue<bool>(RKEY_AUTO_FETCH_ENABLED))
{
int interval = static_cast<int>(registry::getValue<float>(RKEY_AUTO_FETCH_INTERVAL) * 60 * 1000);
if (interval > 0)
{
_fetchTimer.Start(interval);
}
}
}
void VcsStatus::onMapEvent(IMap::MapEvent ev)
{
if (ev == IMap::MapSaved || ev == IMap::MapLoaded)
{
updateMapFileStatus();
if (_repository)
{
analyseRemoteStatus(_repository);
}
}
// Only when saving: check whether a merge is to be completed
if (ev == IMap::MapSaved && _repository && _repository->mergeIsInProgress())
{
auto mapPath = _repository->getRepositoryRelativePath(GlobalMapModule().getMapName());
auto index = _repository->getIndex();
// Remove the conflict state of the map file after save
resolveMapFileConflictUsingOurs(_repository);
if (wxutil::Messagebox::Show(_("Complete Merge Operation?"),
_("Map has been saved. Do you want to complete the ongoing merge operation using this state?"),
::ui::IDialog::MessageType::MESSAGE_ASK) == ::ui::IDialog::RESULT_YES)
{
try
{
tryToFinishMerge(_repository);
}
catch (git::GitException& ex)
{
wxutil::Messagebox::ShowError(ex.what());
}
analyseRemoteStatus(_repository);
return;
}
}
if (ev == IMap::MapMergeOperationAborted && _repository && _repository->mergeIsInProgress())
{
// Ask the user whether to cancel the git merge status
if (wxutil::Messagebox::Show(_("Cancel Merge Operation?"),
_("You've aborted the map merge. Do you want to abort the ongoing git merge operation too?\n"
"This will perform a hard reset in the repository to the state it had before the merge was started.\n\n"
"Important: All uncommitted changes in the working tree will be lost!"),
::ui::IDialog::MessageType::MESSAGE_ASK) == ::ui::IDialog::RESULT_YES)
{
try
{
_repository->abortMerge();
}
catch (git::GitException& ex)
{
wxutil::Messagebox::ShowError(ex.what());
}
analyseRemoteStatus(_repository);
}
}
else if (ev == IMap::MapMergeOperationFinished && _repository && _repository->mergeIsInProgress())
{
wxutil::Messagebox::Show(_("Save the File to complete the Merge"),
_("Now that the map is merged, please save the file\nsuch that the git operation can be completed."),
::ui::IDialog::MessageType::MESSAGE_CONFIRM);
// Now wait for the merge to complete (MapSaved event)
}
}
void VcsStatus::startFetchTask()
{
{
std::lock_guard<std::mutex> guard(_taskLock);
if (_taskInProgress || !_repository) return;
if (!GlobalMainFrame().isActiveApp())
{
rMessage() << "Skipping fetch this round, since the app is not active." << std::endl;
return;
}
_taskInProgress = true;
}
auto repository = _repository->clone();
_repositoryTask = std::async(std::launch::async, std::bind(&VcsStatus::performFetch, this, repository));
}
void VcsStatus::onIntervalReached(wxTimerEvent& ev)
{
if (ev.GetTimer().GetId() == _fetchTimer.GetId())
{
startFetchTask();
}
else if (ev.GetTimer().GetId() == _statusTimer.GetId())
{
updateMapFileStatus();
}
}
void VcsStatus::updateMapFileStatus()
{
if (GlobalMapModule().isUnnamed())
{
setMapFileStatus(_("Map not saved yet"));
return;
}
if (GlobalMapModule().getActiveMergeOperation())
{
setMapFileStatus(_("Merging"));
return;
}
if (GlobalMapModule().isModified())
{
_mapStatus->SetLabel(_("Map is modified"));
return;
}
if (_repository)
{
auto repository = _repository->clone();
_mapFileTask = std::async(std::launch::async, std::bind(&VcsStatus::performMapFileStatusCheck, this, repository));
}
else
{
_mapStatus->SetLabel(_("Map is saved"));
}
}
void VcsStatus::onIdle(wxIdleEvent& ev)
{
ev.Skip();
}
void VcsStatus::performFetch(std::shared_ptr<git::Repository> repository)
{
auto head = repository->getHead();
if (!head)
{
_taskInProgress = false;
return;
}
try
{
repository->getUpstreamRemoteName(*head);
}
catch (const git::GitException&)
{
setRemoteStatus(git::RemoteStatus{ 0, 0, _("Not connected") });
_taskInProgress = false;
return;
}
try
{
setRemoteStatus(git::RemoteStatus{ 0, 0, _("Fetching...") });
repository->fetchFromTrackedRemote();
}
catch (const git::GitException& ex)
{
setRemoteStatus(git::RemoteStatus{ 0, 0, ex.what() });
}
analyseRemoteStatus(repository);
_taskInProgress = false;
}
void VcsStatus::analyseRemoteStatus(std::shared_ptr<git::Repository> repository)
{
try
{
setRemoteStatus(git::analyseRemoteStatus(repository));
}
catch (git::GitException& ex)
{
setRemoteStatus(git::RemoteStatus{ 0, 0, ex.what() });
}
}
void VcsStatus::performSync(std::shared_ptr<git::Repository> repository)
{
try
{
syncWithRemote(repository);
setRemoteStatus(git::analyseRemoteStatus(repository));
}
catch (git::GitException& ex)
{
setRemoteStatus(git::RemoteStatus{ 0, 0, ex.what() });
wxutil::Messagebox::ShowError(ex.what());
}
}
bool VcsStatus::canSync()
{
return !_taskInProgress && _repository && !_repository->mergeIsInProgress();
}
bool VcsStatus::canCommit()
{
return !_taskInProgress && _repository && !_repository->mergeIsInProgress();
}
void VcsStatus::performCommit()
{
if (!_repository) return;
{
std::lock_guard<std::mutex> guard(_taskLock);
if (_taskInProgress)
{
wxutil::Messagebox::Show(_("Another Task in progress"),
_("Cannot commit when another task is in progress"),
::ui::IDialog::MessageType::MESSAGE_CONFIRM);
return;
}
_taskInProgress = true;
}
try
{
git::CommitMetadata metadata;
metadata.name = _repository->getConfigValue("user.name");
metadata.email = _repository->getConfigValue("user.email");
metadata = CommitDialog::RunDialog(metadata);
if (metadata.isValid())
{
_repository->createCommit(metadata);
}
analyseRemoteStatus(_repository);
}
catch (const git::GitException& ex)
{
wxutil::Messagebox::ShowError(ex.what());
}
{
std::lock_guard<std::mutex> guard(_taskLock);
_taskInProgress = false;
}
}
void VcsStatus::setMapFileStatus(const std::string& status)
{
GlobalUserInterface().dispatch([this, status]() { _mapStatus->SetLabel(status); });
}
void VcsStatus::setRemoteStatus(const git::RemoteStatus& status)
{
GlobalUserInterface().dispatch([this, status]()
{
findNamedObject<wxWindow>(_panel, "OutgoingCommitsIcon")->Show(status.localAheadCount > 0);
findNamedObject<wxWindow>(_panel, "IncomingCommitsIcon")->Show(status.remoteAheadCount > 0);
auto* outgoingLabel = findNamedObject<wxStaticText>(_panel, "NumOutgoingCommits");
outgoingLabel->Show(status.localAheadCount > 0);
outgoingLabel->SetLabel(string::to_string(status.localAheadCount));
auto* incomingLabel = findNamedObject<wxStaticText>(_panel, "NumIncomingCommits");
incomingLabel->Show(status.remoteAheadCount > 0);
incomingLabel->SetLabel(string::to_string(status.remoteAheadCount));
_remoteStatus->SetLabel(status.label);
});
}
void VcsStatus::performMapFileStatusCheck(std::shared_ptr<git::Repository> repository)
{
try
{
auto relativePath = repository->getRepositoryRelativePath(GlobalMapModule().getMapName());
if (relativePath.empty())
{
setMapFileStatus(_("Map not in VCS"));
return;
}
if (repository->fileHasUncommittedChanges(relativePath))
{
setMapFileStatus(_("Map saved, pending commit"));
}
else if (repository->fileIsIndexed(relativePath))
{
setMapFileStatus(_("Map committed"));
}
else
{
setMapFileStatus(_("Map saved"));
}
}
catch (const git::GitException& ex)
{
setMapFileStatus(std::string("ERROR: ") + ex.what());
}
}
}
}
|