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

FileTreeContextMenu_Opening: Handle null revision #10684

Merged
merged 1 commit into from
Jan 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions GitUI/CommandsDialogs/RememberFileContextMenuController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ public FileStatusItem CreateFileStatusItem(string name, GitRevision rev)
/// <param name="isSecondRevision">true if second revision can be used as the first item.</param>
/// <returns>If the item can be used.</returns>
[Pure]
public bool ShouldEnableFirstItemDiff(FileStatusItem item, bool isSecondRevision)
public bool ShouldEnableFirstItemDiff(FileStatusItem? item, bool isSecondRevision)
{
// First item must be a git reference existing in the revision, i.e. other than work tree
return ShouldEnableSecondItemDiff(item, isSecondRevision: isSecondRevision)
&& (isSecondRevision ? item.SecondRevision : item.FirstRevision)?.ObjectId != ObjectId.WorkTreeId;
&& (isSecondRevision ? item?.SecondRevision : item?.FirstRevision)?.ObjectId != ObjectId.WorkTreeId;
}

[Pure]
Expand Down
21 changes: 12 additions & 9 deletions GitUI/CommandsDialogs/RevisionFileTreeControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using GitUI.CommandsDialogs.BrowseDialog;
using GitUI.Hotkey;
using GitUI.Properties;
using GitUI.UserControls;
using GitUIPluginInterfaces;
using Microsoft;
using ResourceManager;
Expand Down Expand Up @@ -660,16 +661,18 @@ private void fileTreeCleanWorkingTreeToolStripMenuItem_Click(object sender, Even

private void FileTreeContextMenu_Opening(object sender, System.ComponentModel.CancelEventArgs e)
{
var gitItem = tvGitTree.SelectedNode?.Tag as GitItem;
var itemSelected = gitItem is not null;
var isFile = gitItem?.ObjectType == GitObjectType.Blob;
var isFolder = gitItem?.ObjectType == GitObjectType.Tree;
var isFileOrFolder = isFile || isFolder;
GitItem? gitItem = tvGitTree.SelectedNode?.Tag as GitItem;
bool itemSelected = gitItem is not null;
bool isFile = gitItem?.ObjectType == GitObjectType.Blob;
bool isFolder = gitItem?.ObjectType == GitObjectType.Tree;
bool isFileOrFolder = isFile || isFolder;

// Many items does not make sense if a local file does not exist, why this is used for Enabled
var isExistingFileOrDirectory = gitItem is not null && FormBrowseUtil.IsFileOrDirectory(_fullPathResolver.Resolve(gitItem.FileName));
bool isExistingFileOrDirectory = gitItem is not null && FormBrowseUtil.IsFileOrDirectory(_fullPathResolver.Resolve(gitItem.FileName));

var openSubVisible = gitItem?.ObjectType == GitObjectType.Commit && isExistingFileOrDirectory;
filterFileInGridToolStripMenuItem.Enabled = itemSelected;

bool openSubVisible = gitItem?.ObjectType == GitObjectType.Commit && isExistingFileOrDirectory;
openSubmoduleMenuItem.Visible = openSubVisible;
if (openSubVisible)
{
Expand Down Expand Up @@ -700,8 +703,8 @@ private void FileTreeContextMenu_Opening(object sender, System.ComponentModel.Ca
openWithDifftoolToolStripMenuItem.Visible = isFile;
openWithToolStripMenuItem.Visible = isFile;
openWithToolStripMenuItem.Enabled = isExistingFileOrDirectory;
Validates.NotNull(_revision);
var fsi = _rememberFileContextMenuController.CreateFileStatusItem(gitItem?.FileName ?? "", _revision);
FileStatusItem fsi = _revision is null ? null
: _rememberFileContextMenuController.CreateFileStatusItem(gitItem?.FileName ?? "", _revision);
diffWithRememberedFileToolStripMenuItem.Visible = _rememberFileContextMenuController.RememberedDiffFileItem is not null;
diffWithRememberedFileToolStripMenuItem.Enabled = isFile && fsi != _rememberFileContextMenuController.RememberedDiffFileItem
&& _rememberFileContextMenuController.ShouldEnableSecondItemDiff(fsi);
Expand Down