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

Check if path exists before creating FileInfo #8840

Merged
merged 1 commit into from
Feb 10, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions GitCommands/FileInfoExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ public static class FileInfoExtensions
/// </summary>
public static void MakeFileTemporaryWritable(string fileName, Action<string> writableAction)
{
var fileInfo = new FileInfo(fileName);
if (!fileInfo.Exists)
if (!File.Exists(fileName))
{
// The file doesn't exist yet, no need to make it writable
writableAction(fileName);
return;
}

var fileInfo = new FileInfo(fileName);
var oldAttributes = fileInfo.Attributes;
fileInfo.Attributes = FileAttributes.Normal;
writableAction(fileName);
Expand Down
10 changes: 5 additions & 5 deletions GitUI/CommandsDialogs/BrowseDialog/FormBrowseUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ internal static class FormBrowseUtil
{
public static bool FileOrParentDirectoryExists(string path)
{
var fileInfo = new FileInfo(path);
return fileInfo.Exists || (fileInfo.Directory is not null && fileInfo.Directory.Exists);
return File.Exists(path) || (Directory.Exists(path) && new FileInfo(path).Directory.Exists);
}

public static bool IsFileOrDirectory(string path)
Expand All @@ -17,13 +16,14 @@ public static bool IsFileOrDirectory(string path)

public static void ShowFileOrParentFolderInFileExplorer(string path)
{
var fileInfo = new FileInfo(path);
if (fileInfo.Exists)
if (File.Exists(path))
{
var fileInfo = new FileInfo(path);
OsShellUtil.SelectPathInFileExplorer(fileInfo.FullName);
}
else if (fileInfo.Directory.Exists)
else if (Directory.Exists(path))
{
var fileInfo = new FileInfo(path);
OsShellUtil.OpenWithFileExplorer(fileInfo.Directory.FullName);
}
}
Expand Down
5 changes: 0 additions & 5 deletions GitUI/CommandsDialogs/FormBrowse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2467,11 +2467,6 @@ public static void OpenContainingFolder(FileStatusList diffFiles, GitModule modu
foreach (var item in diffFiles.SelectedItems)
{
string filePath = PathUtil.Combine(module.WorkingDir, item.Item.Name.ToNativePath());
if (string.IsNullOrWhiteSpace(filePath))
{
continue;
}

Comment on lines -2470 to -2474
Copy link
Member

Choose a reason for hiding this comment

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

All other changes make sense, but this change is surprising in the given context. Could you please elaborate on this?

Copy link
Member Author

Choose a reason for hiding this comment

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

null annotation is coming soon...
null is an acceptable input with the change so the caller do not need to check

FormBrowseUtil.ShowFileOrParentFolderInFileExplorer(filePath);
}
}
Expand Down
2 changes: 1 addition & 1 deletion GitUI/CommandsDialogs/RevisionDiffControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,7 @@ private void UpdateStatusOfMenuItems()
foreach (var item in DiffFiles.SelectedItems)
{
string filePath = _fullPathResolver.Resolve(item.Item.Name);
if (filePath is not null && FormBrowseUtil.FileOrParentDirectoryExists(filePath))
if (FormBrowseUtil.FileOrParentDirectoryExists(filePath))
{
openContainingFolderToolStripMenuItem.Enabled = true;
break;
Expand Down