5151#include < QFileInfo>
5252#include < QFont>
5353#include < QFrame>
54+ #include < QFutureWatcher>
5455#include < QGuiApplication>
5556#include < QHBoxLayout>
5657#include < QHelpEvent>
6970#include < QTimer>
7071#include < QToolTip>
7172#include < QVBoxLayout>
73+ #include < QtConcurrent/QtConcurrentRun>
7274
7375#include < utility>
7476
@@ -1170,8 +1172,8 @@ void FolderAsWorkspaceDock::ensureGitTab()
11701172 // for the controller pointer to be non-null. Using a queued one-shot
11711173 // captures the controller post-init.
11721174 if (GitController *c = gitTab->controller ()) {
1173- connect (c, &GitController::statusUpdated ,
1174- this , &FolderAsWorkspaceDock::onStatusUpdated );
1175+ connect (c, &GitController::statusRefreshed ,
1176+ this , &FolderAsWorkspaceDock::onStatusRefreshed );
11751177 }
11761178
11771179 gitTab->initializeIfNeeded ();
@@ -1181,25 +1183,28 @@ void FolderAsWorkspaceDock::ensureGitTab()
11811183 if (GitController *c = gitTab->controller ()) {
11821184 // Guard against double-connect (Qt deduplicates the same method ptr +
11831185 // sender + receiver tuple by default when UniqueConnection is set).
1184- connect (c, &GitController::statusUpdated ,
1185- this , &FolderAsWorkspaceDock::onStatusUpdated ,
1186+ connect (c, &GitController::statusRefreshed ,
1187+ this , &FolderAsWorkspaceDock::onStatusRefreshed ,
11861188 Qt::UniqueConnection);
11871189 }
11881190
11891191 // Drain any status the controller has cached at this point (rare, but
11901192 // covers the case where the new workspace inherits an in-flight refresh
11911193 // from a quick re-open of the same path).
1192- onStatusUpdated ();
1194+ onStatusRefreshed ();
11931195}
11941196
1195- void FolderAsWorkspaceDock::onStatusUpdated ()
1197+ void FolderAsWorkspaceDock::onStatusRefreshed ()
11961198{
1197- PROFILE_SCOPE (" FolderAsWorkspaceDock::onStatusUpdated " );
1199+ PROFILE_SCOPE (" FolderAsWorkspaceDock::onStatusRefreshed " );
11981200
11991201 if (!gitTab || !gitTab->controller () || !gitTab->controller ()->statusModel ()) {
12001202 // Nothing to read yet. If we previously had entries, clear them so
12011203 // stale decorations disappear (e.g. controller errored out).
1204+ m_decoratedStatusSerial = std::uint64_t (-1 );
1205+ m_submodulePaths.clear ();
12021206 if (!m_pathStatus.isEmpty ()) {
1207+ ++m_pathStatusGeneration; // a rebuild in flight must not resurrect them
12031208 const QSet<QString> stale = m_pathStatus.allIndexedPaths ();
12041209 m_pathStatus.clear ();
12051210 if (model) model->notifyPathsChanged (stale);
@@ -1208,24 +1213,35 @@ void FolderAsWorkspaceDock::onStatusUpdated()
12081213 }
12091214
12101215 const GitController *c = gitTab->controller ();
1211- const GitStatusEntries parentEntries = c->statusModel ()-> allEntries ();
1216+ const GitStatusModel *status = c->statusModel ();
12121217 const QString repoTop = c->currentRepo ();
1218+ const bool parentChanged = status->changeSerial () != m_decoratedStatusSerial;
1219+ // Same parent status and no submodules: nothing can have changed.
1220+ if (!parentChanged && m_submodulePaths.isEmpty ()) return ;
1221+
1222+ const GitStatusEntries parentEntries = status->allEntries ();
1223+ if (parentChanged) {
1224+ m_decoratedStatusSerial = status->changeSerial ();
1225+ // Stage 1: paint with parent entries only. The submodule folder itself
1226+ // is coloured here (its entry has isSubmodule=true → PathStatusIndex
1227+ // registers it as a folder), but files inside the submodule remain
1228+ // undecorated until the async sub-status returns.
1229+ applyMergedEntries (parentEntries, repoTop);
1230+ m_submodulePaths.clear ();
1231+ for (const GitStatusEntry &e : parentEntries) {
1232+ if (e.isSubmodule && !e.relPath .isEmpty () && !m_submodulePaths.contains (e.relPath ))
1233+ m_submodulePaths.append (e.relPath );
1234+ }
1235+ }
12131236
1214- // Stage 1: paint immediately with parent entries only. The submodule
1215- // folder itself is coloured here (its entry has isSubmodule=true →
1216- // PathStatusIndex registers it as a folder), but files inside the
1217- // submodule remain undecorated until the async sub-status returns.
1218- applyMergedEntries (parentEntries, repoTop);
1219-
1220- // Stage 2: collect submodule paths from the parent's entries and kick off
1221- // a fan-out fetch. The fetcher cancels any prior in-flight round, so
1222- // back-to-back refreshes coalesce safely.
1237+ // Stage 2: fan out a status fetch per submodule. The fetcher cancels any
1238+ // prior in-flight round, so back-to-back refreshes coalesce safely.
12231239 QVector<SubmoduleStatusFetcher::Submodule> submodules;
1224- for ( const GitStatusEntry &e : parentEntries) {
1225- if (!e. isSubmodule || e. relPath . isEmpty ( )) continue ;
1240+ submodules. reserve (m_submodulePaths. size ());
1241+ for ( const QString &rel : std::as_const (m_submodulePaths )) {
12261242 SubmoduleStatusFetcher::Submodule s;
1227- s.absPath = QDir::cleanPath (repoTop + QLatin1Char (' /' ) + e. relPath );
1228- s.relFromRoot = e. relPath ;
1243+ s.absPath = QDir::cleanPath (repoTop + QLatin1Char (' /' ) + rel );
1244+ s.relFromRoot = rel ;
12291245 submodules.append (s);
12301246 }
12311247
@@ -1259,19 +1275,38 @@ void FolderAsWorkspaceDock::onSubmoduleEntriesReady(const GitStatusEntries &entr
12591275 m_pendingRepoTop.clear ();
12601276}
12611277
1262- void FolderAsWorkspaceDock::applyMergedEntries (const GitStatusEntries & merged,
1278+ void FolderAsWorkspaceDock::applyMergedEntries (GitStatusEntries merged,
12631279 const QString &repoTop)
12641280{
1265- PathStatusIndex next;
1266- next.rebuild (merged, repoTop);
1267-
1268- const QSet<QString> delta = next.deltaPaths (m_pathStatus);
1269- m_pathStatus = std::move (next);
1270- if (model) {
1271- model->notifyPathsChanged (delta);
1272- } else if (auto *rm = qobject_cast<remote::RemoteFileSystemModel *>(fsModel ? fsModel->asModel () : nullptr )) {
1273- rm->notifyPathsChanged (delta);
1274- }
1281+ struct Rebuilt {
1282+ PathStatusIndex index;
1283+ QSet<QString> delta;
1284+ };
1285+ // The delta is taken against the index as it is now. A newer launch
1286+ // supersedes this one, so whichever result applies was computed against
1287+ // the m_pathStatus it replaces (synchronous changes bump the generation too).
1288+ const std::uint64_t gen = ++m_pathStatusGeneration;
1289+ auto *watcher = new QFutureWatcher<Rebuilt>(this );
1290+ connect (watcher, &QFutureWatcher<Rebuilt>::finished, this , [this , watcher, gen]() {
1291+ watcher->deleteLater ();
1292+ if (gen != m_pathStatusGeneration) return ;
1293+ Rebuilt r = watcher->future ().takeResult ();
1294+ m_pathStatus = std::move (r.index );
1295+ if (model) {
1296+ model->notifyPathsChanged (r.delta );
1297+ } else if (auto *rm = qobject_cast<remote::RemoteFileSystemModel *>(fsModel ? fsModel->asModel () : nullptr )) {
1298+ rm->notifyPathsChanged (r.delta );
1299+ }
1300+ });
1301+ // PathStatusIndex is a pair of implicitly shared hashes: the worker reads
1302+ // its own snapshot while the UI keeps painting from m_pathStatus.
1303+ watcher->setFuture (QtConcurrent::run (
1304+ [merged = std::move (merged), repoTop, previous = m_pathStatus]() {
1305+ Rebuilt r;
1306+ r.index .rebuild (merged, repoTop);
1307+ r.delta = r.index .deltaPaths (previous);
1308+ return r;
1309+ }));
12751310}
12761311
12771312SubmoduleStatusFetcher *FolderAsWorkspaceDock::ensureSubmoduleFetcher ()
0 commit comments