diff --git a/src/App.axaml.cs b/src/App.axaml.cs index b5868ca1..94bcb7ef 100644 --- a/src/App.axaml.cs +++ b/src/App.axaml.cs @@ -301,7 +301,7 @@ namespace SourceGit return await clipboard.GetTextAsync(); } } - return default; + return null; } public static string Text(string key, params object[] args) @@ -323,8 +323,7 @@ namespace SourceGit icon.Height = 12; icon.Stretch = Stretch.Uniform; - var geo = Current?.FindResource(key) as StreamGeometry; - if (geo != null) + if (Current?.FindResource(key) is StreamGeometry geo) icon.Data = geo; return icon; @@ -682,8 +681,7 @@ namespace SourceGit } var name = sb.ToString(); - var idx = name.IndexOf('#'); - if (idx >= 0) + if (name.Contains('#')) { if (!name.Equals("fonts:Inter#Inter", StringComparison.Ordinal) && !name.Equals("fonts:SourceGit#JetBrains Mono", StringComparison.Ordinal)) diff --git a/src/Commands/Blame.cs b/src/Commands/Blame.cs index 4fa8b317..1fc51fa4 100644 --- a/src/Commands/Blame.cs +++ b/src/Commands/Blame.cs @@ -51,7 +51,7 @@ namespace SourceGit.Commands private void ParseLine(string line) { - if (line.IndexOf('\0', StringComparison.Ordinal) >= 0) + if (line.Contains('\0', StringComparison.Ordinal)) { _result.IsBinary = true; _result.LineInfos.Clear(); diff --git a/src/Commands/Command.cs b/src/Commands/Command.cs index 9bfa1c15..975922fc 100644 --- a/src/Commands/Command.cs +++ b/src/Commands/Command.cs @@ -194,7 +194,7 @@ namespace SourceGit.Commands private void HandleOutput(string line, List errs) { - line = line ?? string.Empty; + line ??= string.Empty; Log?.AppendLine(line); // Lines to hide in error message. diff --git a/src/Commands/Commit.cs b/src/Commands/Commit.cs index 17410bc9..1585e7e3 100644 --- a/src/Commands/Commit.cs +++ b/src/Commands/Commit.cs @@ -34,6 +34,6 @@ namespace SourceGit.Commands return succ; } - private string _tmpFile = string.Empty; + private readonly string _tmpFile; } } diff --git a/src/Commands/LFS.cs b/src/Commands/LFS.cs index e621ed7d..18d2ba93 100644 --- a/src/Commands/LFS.cs +++ b/src/Commands/LFS.cs @@ -10,7 +10,7 @@ namespace SourceGit.Commands [GeneratedRegex(@"^(.+)\s+([\w.]+)\s+\w+:(\d+)$")] private static partial Regex REG_LOCK(); - class SubCmd : Command + private class SubCmd : Command { public SubCmd(string repo, string args, Models.ICommandLog log) { diff --git a/src/Commands/QueryCommitsForInteractiveRebase.cs b/src/Commands/QueryCommitsForInteractiveRebase.cs index 615060a5..9f238319 100644 --- a/src/Commands/QueryCommitsForInteractiveRebase.cs +++ b/src/Commands/QueryCommitsForInteractiveRebase.cs @@ -90,6 +90,6 @@ namespace SourceGit.Commands private List _commits = []; private Models.InteractiveCommit _current = null; - private string _boundary = ""; + private readonly string _boundary; } } diff --git a/src/Commands/QueryFileSize.cs b/src/Commands/QueryFileSize.cs index 9016d826..30af7715 100644 --- a/src/Commands/QueryFileSize.cs +++ b/src/Commands/QueryFileSize.cs @@ -16,9 +16,6 @@ namespace SourceGit.Commands public long Result() { - if (_result != 0) - return _result; - var rs = ReadToEnd(); if (rs.IsSuccess) { @@ -29,7 +26,5 @@ namespace SourceGit.Commands return 0; } - - private readonly long _result = 0; } } diff --git a/src/Commands/UnstageChangesForAmend.cs b/src/Commands/UnstageChangesForAmend.cs index c930f136..19def067 100644 --- a/src/Commands/UnstageChangesForAmend.cs +++ b/src/Commands/UnstageChangesForAmend.cs @@ -23,13 +23,11 @@ namespace SourceGit.Commands _patchBuilder.Append(c.DataForAmend.ObjectHash); _patchBuilder.Append("\t"); _patchBuilder.Append(c.OriginalPath); - _patchBuilder.Append("\n"); } else if (c.Index == Models.ChangeState.Added) { _patchBuilder.Append("0 0000000000000000000000000000000000000000\t"); _patchBuilder.Append(c.Path); - _patchBuilder.Append("\n"); } else if (c.Index == Models.ChangeState.Deleted) { @@ -37,7 +35,6 @@ namespace SourceGit.Commands _patchBuilder.Append(c.DataForAmend.ObjectHash); _patchBuilder.Append("\t"); _patchBuilder.Append(c.Path); - _patchBuilder.Append("\n"); } else { @@ -46,8 +43,9 @@ namespace SourceGit.Commands _patchBuilder.Append(c.DataForAmend.ObjectHash); _patchBuilder.Append("\t"); _patchBuilder.Append(c.Path); - _patchBuilder.Append("\n"); } + + _patchBuilder.Append("\n"); } } diff --git a/src/Models/AvatarManager.cs b/src/Models/AvatarManager.cs index 2edcb619..fa07975d 100644 --- a/src/Models/AvatarManager.cs +++ b/src/Models/AvatarManager.cs @@ -38,7 +38,7 @@ namespace SourceGit.Models [GeneratedRegex(@"^(?:(\d+)\+)?(.+?)@.+\.github\.com$")] private static partial Regex REG_GITHUB_USER_EMAIL(); - private object _synclock = new object(); + private readonly Lock _synclock = new(); private string _storePath; private List _avatars = new List(); private Dictionary _resources = new Dictionary(); @@ -144,8 +144,7 @@ namespace SourceGit.Models if (_defaultAvatars.Contains(email)) return null; - if (_resources.ContainsKey(email)) - _resources.Remove(email); + _resources.Remove(email); var localFile = Path.Combine(_storePath, GetEmailHash(email)); if (File.Exists(localFile)) @@ -179,8 +178,7 @@ namespace SourceGit.Models lock (_synclock) { - if (!_requesting.Contains(email)) - _requesting.Add(email); + _requesting.Add(email); } return null; @@ -200,10 +198,7 @@ namespace SourceGit.Models if (image == null) return; - if (_resources.ContainsKey(email)) - _resources[email] = image; - else - _resources.Add(email, image); + _resources[email] = image; _requesting.Remove(email); diff --git a/src/Models/Commit.cs b/src/Models/Commit.cs index 9a7933c8..f0f4b39b 100644 --- a/src/Models/Commit.cs +++ b/src/Models/Commit.cs @@ -19,7 +19,7 @@ namespace SourceGit.Models public class Commit { // As retrieved by: git mktree chunks, Dictionary hashes, string data, int start) { - int hash; - if (hashes.TryGetValue(data, out hash)) + if (hashes.TryGetValue(data, out var hash)) { chunks.Add(new Chunk(hash, start, data.Length)); } diff --git a/src/Models/User.cs b/src/Models/User.cs index 066ab747..0b4816fe 100644 --- a/src/Models/User.cs +++ b/src/Models/User.cs @@ -26,11 +26,7 @@ namespace SourceGit.Models public override bool Equals(object obj) { - if (obj == null || !(obj is User)) - return false; - - var other = obj as User; - return Name == other.Name && Email == other.Email; + return obj is User other && Name == other.Name && Email == other.Email; } public override int GetHashCode() diff --git a/src/Models/Watcher.cs b/src/Models/Watcher.cs index ccdc645f..a3cfc329 100644 --- a/src/Models/Watcher.cs +++ b/src/Models/Watcher.cs @@ -246,7 +246,7 @@ namespace SourceGit.Models private long _updateStashes = 0; private long _updateTags = 0; - private object _lockSubmodule = new object(); + private readonly Lock _lockSubmodule = new(); private List _submodules = new List(); } } diff --git a/src/Native/Windows.cs b/src/Native/Windows.cs index adca05f3..8042ac47 100644 --- a/src/Native/Windows.cs +++ b/src/Native/Windows.cs @@ -128,7 +128,7 @@ namespace SourceGit.Native Microsoft.Win32.RegistryView.Registry64); var git = reg.OpenSubKey("SOFTWARE\\GitForWindows"); - if (git != null && git.GetValue("InstallPath") is string installPath) + if (git?.GetValue("InstallPath") is string installPath) { return Path.Combine(installPath, "bin", "git.exe"); } @@ -181,7 +181,7 @@ namespace SourceGit.Native break; case "cmd": - return "C:\\Windows\\System32\\cmd.exe"; + return @"C:\Windows\System32\cmd.exe"; case "wt": var wtFinder = new StringBuilder("wt.exe", 512); if (PathFindOnPath(wtFinder, null)) @@ -199,8 +199,8 @@ namespace SourceGit.Native finder.VSCode(FindVSCode); finder.VSCodeInsiders(FindVSCodeInsiders); finder.VSCodium(FindVSCodium); - finder.Fleet(() => $"{Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)}\\Programs\\Fleet\\Fleet.exe"); - finder.FindJetBrainsFromToolbox(() => $"{Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)}\\JetBrains\\Toolbox"); + finder.Fleet(() => $@"{Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)}\Programs\Fleet\Fleet.exe"); + finder.FindJetBrainsFromToolbox(() => $@"{Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)}\JetBrains\Toolbox"); finder.SublimeText(FindSublimeText); finder.TryAdd("Visual Studio", "vs", FindVisualStudio, GenerateCommandlineArgsForVisualStudio); return finder.Founded; diff --git a/src/ViewModels/CheckoutAndFastForward.cs b/src/ViewModels/CheckoutAndFastForward.cs index 8e62a452..97ab86d7 100644 --- a/src/ViewModels/CheckoutAndFastForward.cs +++ b/src/ViewModels/CheckoutAndFastForward.cs @@ -9,7 +9,7 @@ namespace SourceGit.ViewModels get; } - public Models.Branch RemoteBrach + public Models.Branch RemoteBranch { get; } @@ -35,7 +35,7 @@ namespace SourceGit.ViewModels { _repo = repo; LocalBranch = localBranch; - RemoteBrach = remoteBranch; + RemoteBranch = remoteBranch; } public override Task Sure() @@ -54,7 +54,7 @@ namespace SourceGit.ViewModels if (DiscardLocalChanges) { - succ = new Commands.Checkout(_repo.FullPath).Use(log).Branch(LocalBranch.Name, RemoteBrach.Head, true, true); + succ = new Commands.Checkout(_repo.FullPath).Use(log).Branch(LocalBranch.Name, RemoteBranch.Head, true, true); } else { @@ -72,7 +72,7 @@ namespace SourceGit.ViewModels needPopStash = true; } - succ = new Commands.Checkout(_repo.FullPath).Use(log).Branch(LocalBranch.Name, RemoteBrach.Head, false, true); + succ = new Commands.Checkout(_repo.FullPath).Use(log).Branch(LocalBranch.Name, RemoteBranch.Head, false, true); } if (succ) diff --git a/src/ViewModels/CommitDetail.cs b/src/ViewModels/CommitDetail.cs index 3ec168db..fbecf30e 100644 --- a/src/ViewModels/CommitDetail.cs +++ b/src/ViewModels/CommitDetail.cs @@ -569,14 +569,14 @@ namespace SourceGit.ViewModels if (!token.IsCancellationRequested) Dispatcher.UIThread.Invoke(() => FullMessage = new Models.CommitFullMessage { Message = message, Inlines = inlines }); - }); + }, token); Task.Run(() => { var signInfo = new Commands.QueryCommitSignInfo(_repo.FullPath, _commit.SHA, !_repo.HasAllowedSignersFile).Result(); if (!token.IsCancellationRequested) Dispatcher.UIThread.Invoke(() => SignInfo = signInfo); - }); + }, token); if (Preferences.Instance.ShowChildren) { @@ -587,7 +587,7 @@ namespace SourceGit.ViewModels var children = cmd.Result(); if (!token.IsCancellationRequested) Dispatcher.UIThread.Post(() => Children = children); - }); + }, token); } Task.Run(() => @@ -617,7 +617,7 @@ namespace SourceGit.ViewModels SelectedChanges = null; }); } - }); + }, token); } private Models.InlineElementCollector ParseInlinesInMessage(string message) diff --git a/src/ViewModels/ConfigureWorkspace.cs b/src/ViewModels/ConfigureWorkspace.cs index ec885b2f..49cd649b 100644 --- a/src/ViewModels/ConfigureWorkspace.cs +++ b/src/ViewModels/ConfigureWorkspace.cs @@ -29,8 +29,7 @@ namespace SourceGit.ViewModels public ConfigureWorkspace() { - Workspaces = new AvaloniaList(); - Workspaces.AddRange(Preferences.Instance.Workspaces); + Workspaces = new AvaloniaList(Preferences.Instance.Workspaces); } public void Add() diff --git a/src/ViewModels/CreateTag.cs b/src/ViewModels/CreateTag.cs index 091a95a0..d3cd512b 100644 --- a/src/ViewModels/CreateTag.cs +++ b/src/ViewModels/CreateTag.cs @@ -65,8 +65,7 @@ namespace SourceGit.ViewModels public static ValidationResult ValidateTagName(string name, ValidationContext ctx) { - var creator = ctx.ObjectInstance as CreateTag; - if (creator != null) + if (ctx.ObjectInstance is CreateTag creator) { var found = creator._repo.Tags.Find(x => x.Name == name); if (found != null) diff --git a/src/ViewModels/Histories.cs b/src/ViewModels/Histories.cs index a1f71409..ed6d6c48 100644 --- a/src/ViewModels/Histories.cs +++ b/src/ViewModels/Histories.cs @@ -347,7 +347,7 @@ namespace SourceGit.ViewModels log = _repo.CreateLog("Save as Patch"); var folder = picker[0]; - var folderPath = folder is { Path: { IsAbsoluteUri: true } path } ? path.LocalPath : folder?.Path.ToString(); + var folderPath = folder is { Path: { IsAbsoluteUri: true } path } ? path.LocalPath : folder.Path.ToString(); var succ = false; for (var i = 0; i < selected.Count; i++) { @@ -707,7 +707,7 @@ namespace SourceGit.ViewModels log = _repo.CreateLog("Save as Patch"); var folder = selected[0]; - var folderPath = folder is { Path: { IsAbsoluteUri: true } path } ? path.LocalPath : folder?.Path.ToString(); + var folderPath = folder is { Path: { IsAbsoluteUri: true } path } ? path.LocalPath : folder.Path.ToString(); var saveTo = GetPatchFileName(folderPath, commit); var succ = await Task.Run(() => new Commands.FormatPatch(_repo.FullPath, commit.SHA, saveTo).Use(log).Exec()); if (succ) diff --git a/src/ViewModels/InProgressContexts.cs b/src/ViewModels/InProgressContexts.cs index 593b2a72..a1a87a42 100644 --- a/src/ViewModels/InProgressContexts.cs +++ b/src/ViewModels/InProgressContexts.cs @@ -4,7 +4,7 @@ namespace SourceGit.ViewModels { public abstract class InProgressContext { - public InProgressContext(string repo, string cmd) + protected InProgressContext(string repo, string cmd) { _repo = repo; _cmd = cmd; diff --git a/src/ViewModels/InteractiveRebase.cs b/src/ViewModels/InteractiveRebase.cs index 7811014a..01e38e41 100644 --- a/src/ViewModels/InteractiveRebase.cs +++ b/src/ViewModels/InteractiveRebase.cs @@ -106,7 +106,7 @@ namespace SourceGit.ViewModels set { if (SetProperty(ref _selectedItem, value)) - DetailContext.Commit = value != null ? value.Commit : null; + DetailContext.Commit = value?.Commit; } } diff --git a/src/ViewModels/Launcher.cs b/src/ViewModels/Launcher.cs index 187ff3aa..e6c55be2 100644 --- a/src/ViewModels/Launcher.cs +++ b/src/ViewModels/Launcher.cs @@ -298,15 +298,10 @@ namespace SourceGit.ViewModels if (removeIdx == activeIdx) { ActivePage = Pages[removeIdx > 0 ? removeIdx - 1 : removeIdx + 1]; - CloseRepositoryInTab(page); - Pages.RemoveAt(removeIdx); - } - else - { - CloseRepositoryInTab(page); - Pages.RemoveAt(removeIdx); } + CloseRepositoryInTab(page); + Pages.RemoveAt(removeIdx); GC.Collect(); } diff --git a/src/ViewModels/MergeMultiple.cs b/src/ViewModels/MergeMultiple.cs index d9a3bd55..4522fb64 100644 --- a/src/ViewModels/MergeMultiple.cs +++ b/src/ViewModels/MergeMultiple.cs @@ -71,12 +71,10 @@ namespace SourceGit.ViewModels } else if (t is Models.Commit commit) { - var d = commit.Decorators.Find(x => - { - return x.Type == Models.DecoratorType.LocalBranchHead || - x.Type == Models.DecoratorType.RemoteBranchHead || - x.Type == Models.DecoratorType.Tag; - }); + var d = commit.Decorators.Find(x => x.Type is + Models.DecoratorType.LocalBranchHead or + Models.DecoratorType.RemoteBranchHead or + Models.DecoratorType.Tag); if (d != null) ret.Add(d.Name); diff --git a/src/ViewModels/Repository.cs b/src/ViewModels/Repository.cs index b97bbf5d..75c6b4ed 100644 --- a/src/ViewModels/Repository.cs +++ b/src/ViewModels/Repository.cs @@ -736,11 +736,8 @@ namespace SourceGit.ViewModels { menu.Items.Add(new MenuItem() { Header = "-" }); - foreach (var url in urls) + foreach (var (name, addr) in urls) { - var name = url.Key; - var addr = url.Value; - var item = new MenuItem(); item.Header = App.Text("Repository.Visit", name); item.Icon = App.CreateMenuIcon("Icons.Remotes"); @@ -2468,7 +2465,7 @@ namespace SourceGit.ViewModels public ContextMenu CreateContextMenuForTagSortMode() { var mode = _settings.TagSortMode; - var changeMode = new Action((m) => + var changeMode = new Action(m => { if (_settings.TagSortMode != m) { @@ -2746,10 +2743,7 @@ namespace SourceGit.ViewModels { foreach (var node in nodes) { - if (filters.TryGetValue(node.Path, out var value)) - node.FilterMode = value; - else - node.FilterMode = Models.FilterMode.None; + node.FilterMode = filters.GetValueOrDefault(node.Path, Models.FilterMode.None); if (!node.IsBranch) UpdateBranchTreeFilterMode(node.Children, filters); @@ -2760,10 +2754,7 @@ namespace SourceGit.ViewModels { foreach (var tag in _tags) { - if (filters.TryGetValue(tag.Name, out var value)) - tag.FilterMode = value; - else - tag.FilterMode = Models.FilterMode.None; + tag.FilterMode = filters.GetValueOrDefault(tag.Name, Models.FilterMode.None); } } @@ -2952,7 +2943,7 @@ namespace SourceGit.ViewModels private List _matchedFilesForSearching = null; private string _filter = string.Empty; - private object _lockRemotes = new object(); + private readonly Lock _lockRemotes = new(); private List _remotes = new List(); private List _branches = new List(); private Models.Branch _currentBranch = null; diff --git a/src/ViewModels/RepositoryConfigure.cs b/src/ViewModels/RepositoryConfigure.cs index 9ccbaccd..d69ff711 100644 --- a/src/ViewModels/RepositoryConfigure.cs +++ b/src/ViewModels/RepositoryConfigure.cs @@ -156,7 +156,7 @@ namespace SourceGit.ViewModels foreach (var service in Preferences.Instance.OpenAIServices) AvailableOpenAIServices.Add(service.Name); - if (AvailableOpenAIServices.IndexOf(PreferredOpenAIService) == -1) + if (!AvailableOpenAIServices.Contains(PreferredOpenAIService)) PreferredOpenAIService = "---"; _cached = new Commands.Config(repo.FullPath).ListAll(); diff --git a/src/ViewModels/ScanRepositories.cs b/src/ViewModels/ScanRepositories.cs index 21cd9bf8..4c7eec4b 100644 --- a/src/ViewModels/ScanRepositories.cs +++ b/src/ViewModels/ScanRepositories.cs @@ -128,14 +128,14 @@ namespace SourceGit.ViewModels private RepositoryNode FindOrCreateGroupRecursive(List collection, string path) { - var idx = path.IndexOf('/'); - if (idx < 0) - return FindOrCreateGroup(collection, path); + RepositoryNode node = null; + foreach (var name in path.Split('/')) + { + node = FindOrCreateGroup(collection, name); + collection = node.SubNodes; + } - var name = path.Substring(0, idx); - var tail = path.Substring(idx + 1); - var parent = FindOrCreateGroup(collection, name); - return FindOrCreateGroupRecursive(parent.SubNodes, tail); + return node; } private RepositoryNode FindOrCreateGroup(List collection, string name) diff --git a/src/Views/AssumeUnchangedManager.axaml b/src/Views/AssumeUnchangedManager.axaml index e8baaeb4..9f4d007d 100644 --- a/src/Views/AssumeUnchangedManager.axaml +++ b/src/Views/AssumeUnchangedManager.axaml @@ -66,7 +66,7 @@ - +