Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow forced reload of revisions #11205

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
56 changes: 31 additions & 25 deletions GitUI/CommandsDialogs/FormBrowse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -640,7 +640,7 @@ private void RefreshRevisions(Func<RefsFilter, IReadOnlyList<IGitRef>> getRefs)
return;
}

RevisionGrid.PerformRefreshRevisions(getRefs, forceRefresh: true);
RevisionGrid.PerformRefreshRevisions(getRefs, forceRefreshRefs: true);

InternalInitialize();
ToolStripFilters.RefreshRevisionFunction(getRefs);
Expand Down Expand Up @@ -1661,39 +1661,45 @@ private void SetGitModule(object sender, GitModuleEventArgs e)
if (Module.IsValidGitWorkingDir())
{
RevisionGrid.SuspendRefreshRevisions();
string path = Module.WorkingDir;
ThreadHelper.JoinableTaskFactory.Run(() => RepositoryHistoryManager.Locals.AddAsMostRecentAsync(path));
AppSettings.RecentWorkingDir = path;
try
{
string path = Module.WorkingDir;
ThreadHelper.JoinableTaskFactory.Run(() => RepositoryHistoryManager.Locals.AddAsMostRecentAsync(path));
AppSettings.RecentWorkingDir = path;

HideDashboard();
HideDashboard();

if (!string.Equals(originalWorkingDir, Module.WorkingDir, StringComparison.Ordinal))
{
ChangeTerminalActiveFolder(Module.WorkingDir);
if (!string.Equals(originalWorkingDir, Module.WorkingDir, StringComparison.Ordinal))
{
ChangeTerminalActiveFolder(Module.WorkingDir);

#if DEBUG
// Current encodings
Debug.WriteLine($"Encodings for {Module.WorkingDir}");
Debug.WriteLine($"Files content encoding: {Module.FilesEncoding.EncodingName}");
Debug.WriteLine($"Commit encoding: {Module.CommitEncoding.EncodingName}");
if (Module.LogOutputEncoding.CodePage != Module.CommitEncoding.CodePage)
{
Debug.WriteLine($"Log output encoding: {Module.LogOutputEncoding.EncodingName}");
}
// Current encodings
Debug.WriteLine($"Encodings for {Module.WorkingDir}");
Debug.WriteLine($"Files content encoding: {Module.FilesEncoding.EncodingName}");
Debug.WriteLine($"Commit encoding: {Module.CommitEncoding.EncodingName}");
if (Module.LogOutputEncoding.CodePage != Module.CommitEncoding.CodePage)
{
Debug.WriteLine($"Log output encoding: {Module.LogOutputEncoding.EncodingName}");
}
#endif

// Reset the filter when switching repos
// Reset the filter when switching repos

// If we're applying custom branch or revision filters - reset them
RevisionGrid.ResetAllFilters();
ToolStripFilters.ClearQuickFilters();
revisionDiff.RepositoryChanged();
}
// If we're applying custom branch or revision filters - reset them
RevisionGrid.ResetAllFilters();
ToolStripFilters.ClearQuickFilters();
revisionDiff.RepositoryChanged();
}

RevisionInfo.SetRevisionWithChildren(revision: null, children: Array.Empty<ObjectId>());
RevisionGrid.ResumeRefreshRevisions();
RevisionInfo.SetRevisionWithChildren(revision: null, children: Array.Empty<ObjectId>());
}
finally
{
RevisionGrid.ResumeRefreshRevisions();

RefreshRevisions();
RefreshRevisions();
}
}
else
{
Expand Down
107 changes: 60 additions & 47 deletions GitUI/UserControls/RevisionGrid/RevisionGridControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public sealed partial class RevisionGridControl : GitModuleControl, ICheckRefs,
/// </summary>
private Lazy<IReadOnlyCollection<string>>? _ambiguousRefs;

private int _updatingFilters;
private int _suspendRefreshCounter;

private IDisposable? _revisionSubscription;
private GitRevision? _baseCommitToCompare;
Expand Down Expand Up @@ -471,15 +471,15 @@ public void DisableContextMenu()
/// Prevents revisions refreshes and stops <see cref="PerformRefreshRevisions"/> from executing
/// until <see cref="ResumeRefreshRevisions"/> is called.
/// </summary>
internal void SuspendRefreshRevisions() => _updatingFilters++;
internal void SuspendRefreshRevisions() => ++_suspendRefreshCounter;

/// <summary>
/// Resume revisions refreshes.
/// </summary>
internal void ResumeRefreshRevisions()
{
--_updatingFilters;
DebugHelpers.Assert(_updatingFilters >= 0, $"{nameof(ResumeRefreshRevisions)} was called without matching {nameof(SuspendRefreshRevisions)}!");
--_suspendRefreshCounter;
DebugHelpers.Assert(_suspendRefreshCounter >= 0, $"{nameof(ResumeRefreshRevisions)} was called without matching {nameof(SuspendRefreshRevisions)}!");
}

public void SetAndApplyBranchFilter(string filter)
Expand Down Expand Up @@ -851,29 +851,38 @@ private void ShowLoading(bool showSpinner = true)
}

/// <summary>
/// Indicates whether the revision grid can be refreshed, i.e. it is not currently being refreshed
/// or it is not in a middle of reconfiguration process guarded by <see cref="SuspendRefreshRevisions"/>
/// Indicates whether the revision grid can be refreshed,
/// i.e. it is not in a middle of reconfiguration process guarded by <see cref="SuspendRefreshRevisions"/>
/// and <see cref="ResumeRefreshRevisions"/>.
/// </summary>
private bool CanRefresh => !_isRefreshingRevisions && _updatingFilters == 0;
private bool CanRefresh => _suspendRefreshCounter == 0;

#region PerformRefreshRevisions

/// <summary>
/// Queries git for the new set of revisions and refreshes the grid.
/// </summary>
/// <exception cref="Exception"></exception>
/// <param name="forceRefresh">Refresh may be required as references may be changed.</param>
public void PerformRefreshRevisions(Func<RefsFilter, IReadOnlyList<IGitRef>> getRefs = null, bool forceRefresh = false)
/// <param name="forceRefreshRefs">Refresh may be required as references may be changed.</param>
public void PerformRefreshRevisions(Func<RefsFilter, IReadOnlyList<IGitRef>> getRefs = null, bool forceRefreshRefs = false)
{
ThreadHelper.AssertOnUIThread();

if (!CanRefresh)
{
Trace.WriteLine("Ignoring refresh as RefreshRevisions() is already running.");
Trace.WriteLine("Ignoring refresh as RefreshRevisions() is suspended.");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nit] For any trace I'd add a prefix that would help identifying the source, e.g.

Suggested change
Trace.WriteLine("Ignoring refresh as RefreshRevisions() is suspended.");
Trace.WriteLine("[PerformRefreshRevisions] Ignoring refresh as RefreshRevisions() is suspended.");

return;
}

if (_isRefreshingRevisions)
{
Trace.WriteLine("Forcing refresh, cancel already running RefreshRevisions().");
if (!_gridView.IsDataLoadComplete)
{
_gridView.MarkAsDataLoadingComplete();
}
}

IGitModule capturedModule = Module;

// Reset the "cache" for current branch
Expand Down Expand Up @@ -1081,7 +1090,7 @@ public void PerformRefreshRevisions(Func<RefsFilter, IReadOnlyList<IGitRef>> get
});

// Initiate update left panel
RevisionsLoading?.Invoke(this, new RevisionLoadEventArgs(this, UICommands, getUnfilteredRefs, getStashRevs, forceRefresh));
RevisionsLoading?.Invoke(this, new RevisionLoadEventArgs(this, UICommands, getUnfilteredRefs, getStashRevs, forceRefreshRefs));
}
catch
{
Expand Down Expand Up @@ -1222,55 +1231,59 @@ string BuildPathFilter(string? path)

void OnRevisionRead(GitRevision revision)
{
if (!firstRevisionReceived)
{
// Wait for refs,CurrentCheckout and stashes as second step
this.InvokeAndForget(() => ShowLoading(showSpinner: false));
semaphoreUpdateGrid.Wait(cancellationToken);
semaphoreUpdateGrid.Wait(cancellationToken);
firstRevisionReceived = true;
}

if (stashesById is not null)
try
{
if (stashesById.TryGetValue(revision.ObjectId, out GitRevision gridStash))
if (!firstRevisionReceived)
{
revision.ReflogSelector = gridStash.ReflogSelector;

// Do not add this again (when the main commit is handled)
stashesById.Remove(revision.ObjectId);
// Wait for refs,CurrentCheckout and stashes as second step
this.InvokeAndForget(() => ShowLoading(showSpinner: false));
semaphoreUpdateGrid.Wait(cancellationToken);
semaphoreUpdateGrid.Wait(cancellationToken);
Comment on lines +1240 to +1241
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we doing this twice?

Copy link
Member Author

@mstv mstv Dec 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The answer is not far away:

                        // Wait for refs,CurrentCheckout and stashes as second step
                        this.InvokeAndForget(() => ShowLoading(showSpinner: false));
                        semaphoreUpdateGrid.Wait(cancellationToken);
                        semaphoreUpdateGrid.Wait(cancellationToken);

firstRevisionReceived = true;
}
else if (stashesByParentId?.Contains(revision.ObjectId) is true)

if (stashesById is not null)
{
foreach (GitRevision stash in stashesByParentId[revision.ObjectId])
if (stashesById.TryGetValue(revision.ObjectId, out GitRevision gridStash))
{
// Add if not already added (reflogs etc list before parent commit)
if (stashesById.ContainsKey(stash.ObjectId))
revision.ReflogSelector = gridStash.ReflogSelector;

// Do not add this again (when the main commit is handled)
stashesById.Remove(revision.ObjectId);
}
else if (stashesByParentId?.Contains(revision.ObjectId) is true)
{
foreach (GitRevision stash in stashesByParentId[revision.ObjectId])
{
_gridView.Add(stash);
if (untrackedByStashId.TryGetValue(stash.ObjectId, out GitRevision untracked))
// Add if not already added (reflogs etc list before parent commit)
if (stashesById.ContainsKey(stash.ObjectId))
{
_gridView.Add(untracked);
_gridView.Add(stash);
if (untrackedByStashId.TryGetValue(stash.ObjectId, out GitRevision untracked))
{
_gridView.Add(untracked);
}
}
}
}
}
}

// Look up any refs associated with this revision
revision.Refs = refsByObjectId[revision.ObjectId].AsReadOnlyList();
// Look up any refs associated with this revision
revision.Refs = refsByObjectId[revision.ObjectId].AsReadOnlyList();

if (!headIsHandled && (revision.ObjectId.Equals(CurrentCheckout) || CurrentCheckout is null))
if (!headIsHandled && (revision.ObjectId.Equals(CurrentCheckout) || CurrentCheckout is null))
{
// Insert artificial worktree/index just before HEAD (CurrentCheckout)
// If grid is filtered and HEAD not visible, insert in OnRevisionReadCompleted()
headIsHandled = true;
AddArtificialRevisions();
}

_gridView.Add(revision);
}
catch (OperationCanceledException)
{
// Insert artificial worktree/index just before HEAD (CurrentCheckout)
// If grid is filtered and HEAD not visible, insert in OnRevisionReadCompleted()
headIsHandled = true;
AddArtificialRevisions();
}

_gridView.Add(revision);

return;
}

bool ShowArtificialRevisions()
Expand Down Expand Up @@ -1367,7 +1380,7 @@ void OnRevisionReadCompleted()
}

_isRefreshingRevisions = false;
RevisionsLoaded?.Invoke(this, new RevisionLoadEventArgs(this, UICommands, getUnfilteredRefs, getStashRevs, forceRefresh));
RevisionsLoaded?.Invoke(this, new RevisionLoadEventArgs(this, UICommands, getUnfilteredRefs, getStashRevs, forceRefreshRefs));
});
return;
}
Expand Down Expand Up @@ -1433,7 +1446,7 @@ void OnRevisionReadCompleted()

SetPage(_gridView);
_isRefreshingRevisions = false;
RevisionsLoaded?.Invoke(this, new RevisionLoadEventArgs(this, UICommands, getUnfilteredRefs, getStashRevs, forceRefresh));
RevisionsLoaded?.Invoke(this, new RevisionLoadEventArgs(this, UICommands, getUnfilteredRefs, getStashRevs, forceRefreshRefs));
HighlightRevisionsByAuthor(GetSelectedRevisions());

await TaskScheduler.Default;
Expand Down