Git: Block branch refresh for actions that open a new editor

Refresh is called twice - first time when the editor is just opened, on
this call the repository can be either empty, or the active project (even
if the current file is from another repository). After the editor is
initialized, setSource is called, and the state is recovered.

But between these 2 calls, m_repository is empty, and synchronous actions
are still executed (and fail).

Fixes: QTCREATORBUG-26952
Change-Id: I1767b35f0e9f24da2c447d0b565b410742c560d9
Reviewed-by: André Hartmann <aha_1980@gmx.de>
Reviewed-by: Robert Löhning <robert.loehning@qt.io>
Reviewed-by: hjk <hjk@qt.io>
This commit is contained in:
Orgad Shaneh
2022-01-31 12:02:37 +02:00
committed by Orgad Shaneh
parent 612529f2a7
commit dd70101d7b
2 changed files with 25 additions and 6 deletions

View File

@@ -76,6 +76,18 @@ protected:
} }
}; };
struct SetInContext
{
SetInContext(bool &block) : m_block(block)
{
m_origValue = m_block;
m_block = true;
}
~SetInContext() { m_block = m_origValue; }
bool &m_block;
bool m_origValue;
};
BranchView::BranchView() BranchView::BranchView()
: m_includeOldEntriesAction(new QAction(tr("Include Old Entries"), this)) : m_includeOldEntriesAction(new QAction(tr("Include Old Entries"), this))
, m_includeTagsAction(new QAction(tr("Include Tags"), this)) , m_includeTagsAction(new QAction(tr("Include Tags"), this))
@@ -148,7 +160,7 @@ void BranchView::refreshIfSame(const FilePath &repository)
void BranchView::refresh(const FilePath &repository, bool force) void BranchView::refresh(const FilePath &repository, bool force)
{ {
if (m_repository == repository && !force) if (m_blockRefresh || (m_repository == repository && !force))
return; return;
m_repository = repository; m_repository = repository;
@@ -261,8 +273,10 @@ void BranchView::slotCustomContextMenu(const QPoint &point)
contextMenu.addSeparator(); contextMenu.addSeparator();
contextMenu.addAction(tr("&Diff"), this, [this] { contextMenu.addAction(tr("&Diff"), this, [this] {
const QString fullName = m_model->fullName(selectedIndex(), true); const QString fullName = m_model->fullName(selectedIndex(), true);
if (!fullName.isEmpty()) if (!fullName.isEmpty()) {
SetInContext block(m_blockRefresh);
GitClient::instance()->diffBranch(m_repository, fullName); GitClient::instance()->diffBranch(m_repository, fullName);
}
}); });
contextMenu.addAction(tr("&Log"), this, [this] { log(selectedIndex()); }); contextMenu.addAction(tr("&Log"), this, [this] { log(selectedIndex()); });
contextMenu.addAction(tr("Reflo&g"), this, [this] { reflog(selectedIndex()); }); contextMenu.addAction(tr("Reflo&g"), this, [this] { reflog(selectedIndex()); });
@@ -582,15 +596,19 @@ bool BranchView::cherryPick()
void BranchView::log(const QModelIndex &idx) void BranchView::log(const QModelIndex &idx)
{ {
const QString branchName = m_model->fullName(idx, true); const QString branchName = m_model->fullName(idx, true);
if (!branchName.isEmpty()) if (branchName.isEmpty())
GitClient::instance()->log(m_repository, QString(), false, {branchName}); return;
SetInContext block(m_blockRefresh);
GitClient::instance()->log(m_repository, QString(), false, {branchName});
} }
void BranchView::reflog(const QModelIndex &idx) void BranchView::reflog(const QModelIndex &idx)
{ {
const QString branchName = m_model->fullName(idx, true); const QString branchName = m_model->fullName(idx, true);
if (!branchName.isEmpty()) if (branchName.isEmpty())
GitClient::instance()->reflog(m_repository, branchName); return;
SetInContext block(m_blockRefresh);
GitClient::instance()->reflog(m_repository, branchName);
} }
void BranchView::push() void BranchView::push()

View File

@@ -97,6 +97,7 @@ private:
BranchModel *m_model = nullptr; BranchModel *m_model = nullptr;
BranchFilterModel *m_filterModel = nullptr; BranchFilterModel *m_filterModel = nullptr;
Utils::FilePath m_repository; Utils::FilePath m_repository;
bool m_blockRefresh = false;
}; };
class BranchViewFactory : public Core::INavigationWidgetFactory class BranchViewFactory : public Core::INavigationWidgetFactory