From f6eb1281b57e549d219d7ebc4dc6b48525346b85 Mon Sep 17 00:00:00 2001 From: leo Date: Thu, 14 Mar 2024 11:09:05 +0800 Subject: [PATCH] enhance: force using StringComparison.Ordinal --- src/Commands/Blame.cs | 2 +- src/Commands/Config.cs | 2 +- src/Commands/QueryBranches.cs | 4 ++-- src/Commands/QueryCommits.cs | 2 +- src/Models/Change.cs | 8 +++++--- src/Models/Commit.cs | 2 +- src/Models/User.cs | 2 +- src/Models/Watcher.cs | 2 +- src/ViewModels/CommitDetail.cs | 6 +++--- src/ViewModels/FileTreeNode.cs | 7 ++++--- src/ViewModels/Histories.cs | 3 ++- src/ViewModels/Repository.cs | 2 +- 12 files changed, 23 insertions(+), 19 deletions(-) diff --git a/src/Commands/Blame.cs b/src/Commands/Blame.cs index 0598fa0f..b8c977e7 100644 --- a/src/Commands/Blame.cs +++ b/src/Commands/Blame.cs @@ -38,7 +38,7 @@ namespace SourceGit.Commands { if (_result.IsBinary) return; if (string.IsNullOrEmpty(line)) return; - if (line.IndexOf('\0') >= 0) { + if (line.IndexOf('\0', StringComparison.Ordinal) >= 0) { _result.IsBinary = true; _result.LineInfos.Clear(); return; diff --git a/src/Commands/Config.cs b/src/Commands/Config.cs index 4a0ebb30..dfd58e47 100644 --- a/src/Commands/Config.cs +++ b/src/Commands/Config.cs @@ -17,7 +17,7 @@ namespace SourceGit.Commands { if (output.IsSuccess) { var lines = output.StdOut.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries); foreach (var line in lines) { - var idx = line.IndexOf('='); + var idx = line.IndexOf('=', StringComparison.Ordinal); if (idx != -1) { var key = line.Substring(0, idx).Trim(); var val = line.Substring(idx+1).Trim(); diff --git a/src/Commands/QueryBranches.cs b/src/Commands/QueryBranches.cs index 630ba8c3..859f909f 100644 --- a/src/Commands/QueryBranches.cs +++ b/src/Commands/QueryBranches.cs @@ -36,14 +36,14 @@ namespace SourceGit.Commands { var branch = new Models.Branch(); var refName = parts[0]; - if (refName.EndsWith("/HEAD")) return; + if (refName.EndsWith("/HEAD", StringComparison.Ordinal)) return; if (refName.StartsWith(PREFIX_LOCAL, StringComparison.Ordinal)) { branch.Name = refName.Substring(PREFIX_LOCAL.Length); branch.IsLocal = true; } else if (refName.StartsWith(PREFIX_REMOTE, StringComparison.Ordinal)) { var name = refName.Substring(PREFIX_REMOTE.Length); - var shortNameIdx = name.IndexOf('/'); + var shortNameIdx = name.IndexOf('/', StringComparison.Ordinal); if (shortNameIdx < 0) return; branch.Remote = name.Substring(0, shortNameIdx); diff --git a/src/Commands/QueryCommits.cs b/src/Commands/QueryCommits.cs index 0d6c31e2..81424409 100644 --- a/src/Commands/QueryCommits.cs +++ b/src/Commands/QueryCommits.cs @@ -51,7 +51,7 @@ namespace SourceGit.Commands { current = new Models.Commit(); line = line.Substring(7); - var decoratorStart = line.IndexOf('('); + var decoratorStart = line.IndexOf('(', StringComparison.Ordinal); if (decoratorStart < 0) { current.SHA = line.Trim(); } else { diff --git a/src/Models/Change.cs b/src/Models/Change.cs index 15596a13..7b6b5958 100644 --- a/src/Models/Change.cs +++ b/src/Models/Change.cs @@ -1,4 +1,6 @@ -namespace SourceGit.Models { +using System; + +namespace SourceGit.Models { public enum ChangeViewMode { List, Grid, @@ -36,12 +38,12 @@ WorkTree = workTree; if (index == ChangeState.Renamed || workTree == ChangeState.Renamed) { - var idx = Path.IndexOf('\t'); + var idx = Path.IndexOf('\t', StringComparison.Ordinal); if (idx >= 0) { OriginalPath = Path.Substring(0, idx); Path = Path.Substring(idx + 1); } else { - idx = Path.IndexOf(" -> "); + idx = Path.IndexOf(" -> ", StringComparison.Ordinal); if (idx > 0) { OriginalPath = Path.Substring(0, idx); Path = Path.Substring(idx + 4); diff --git a/src/Models/Commit.cs b/src/Models/Commit.cs index 36261cf4..5b4f1ff6 100644 --- a/src/Models/Commit.cs +++ b/src/Models/Commit.cs @@ -31,7 +31,7 @@ namespace SourceGit.Models { } public static void ParseUserAndTime(string data, ref User user, ref ulong time) { - var userEndIdx = data.IndexOf('>'); + var userEndIdx = data.IndexOf('>', StringComparison.Ordinal); if (userEndIdx < 0) return; var timeEndIdx = data.IndexOf(' ', userEndIdx + 2); diff --git a/src/Models/User.cs b/src/Models/User.cs index e5ed39ef..7f7a85ef 100644 --- a/src/Models/User.cs +++ b/src/Models/User.cs @@ -23,7 +23,7 @@ namespace SourceGit.Models { if (Caches.ContainsKey(data)) { return Caches[data]; } else { - var nameEndIdx = data.IndexOf('<'); + var nameEndIdx = data.IndexOf('<', System.StringComparison.Ordinal); var name = nameEndIdx >= 2 ? data.Substring(0, nameEndIdx - 1) : string.Empty; var email = data.Substring(nameEndIdx + 1); diff --git a/src/Models/Watcher.cs b/src/Models/Watcher.cs index d20e52d3..9ecbaa1d 100644 --- a/src/Models/Watcher.cs +++ b/src/Models/Watcher.cs @@ -142,7 +142,7 @@ namespace SourceGit.Models { } else if (name.Equals("HEAD", StringComparison.Ordinal) || name.StartsWith("refs/heads/", StringComparison.Ordinal) || name.StartsWith("refs/remotes/", StringComparison.Ordinal) || - name.StartsWith("worktrees/")) { + name.StartsWith("worktrees/", StringComparison.Ordinal)) { _updateBranch = DateTime.Now.AddSeconds(.5).ToFileTime(); } else if (name.StartsWith("objects/", StringComparison.Ordinal) || name.Equals("index", StringComparison.Ordinal)) { _updateWC = DateTime.Now.AddSeconds(1).ToFileTime(); diff --git a/src/ViewModels/CommitDetail.cs b/src/ViewModels/CommitDetail.cs index 4bdcdfca..3c031ca8 100644 --- a/src/ViewModels/CommitDetail.cs +++ b/src/ViewModels/CommitDetail.cs @@ -360,14 +360,14 @@ namespace SourceGit.ViewModels { } var content = new Commands.QueryFileContent(_repo, _commit.SHA, file.Path).Result(); - if (content.StartsWith("version https://git-lfs.github.com/spec/", StringComparison.OrdinalIgnoreCase)) { + if (content.StartsWith("version https://git-lfs.github.com/spec/", StringComparison.Ordinal)) { var obj = new Models.RevisionLFSObject() { Object = new Models.LFSObject() }; var lines = content.Split('\n', StringSplitOptions.RemoveEmptyEntries); if (lines.Length == 3) { foreach (var line in lines) { - if (line.StartsWith("oid sha256:")) { + if (line.StartsWith("oid sha256:", StringComparison.Ordinal)) { obj.Object.Oid = line.Substring(11); - } else if (line.StartsWith("size ")) { + } else if (line.StartsWith("size ", StringComparison.Ordinal)) { obj.Object.Size = long.Parse(line.Substring(5)); } } diff --git a/src/ViewModels/FileTreeNode.cs b/src/ViewModels/FileTreeNode.cs index 94f30561..cec0620e 100644 --- a/src/ViewModels/FileTreeNode.cs +++ b/src/ViewModels/FileTreeNode.cs @@ -1,4 +1,5 @@ using CommunityToolkit.Mvvm.ComponentModel; +using System; using System.Collections.Generic; namespace SourceGit.ViewModels { @@ -19,7 +20,7 @@ namespace SourceGit.ViewModels { var expanded = changes.Count <= 50; foreach (var c in changes) { - var sepIdx = c.Path.IndexOf('/'); + var sepIdx = c.Path.IndexOf('/', StringComparison.Ordinal); if (sepIdx == -1) { nodes.Add(new FileTreeNode() { FullPath = c.Path, @@ -80,7 +81,7 @@ namespace SourceGit.ViewModels { var expanded = files.Count <= 50; foreach (var f in files) { - var sepIdx = f.Path.IndexOf('/'); + var sepIdx = f.Path.IndexOf('/', StringComparison.Ordinal); if (sepIdx == -1) { nodes.Add(new FileTreeNode() { FullPath = f.Path, @@ -139,7 +140,7 @@ namespace SourceGit.ViewModels { foreach (var node in nodes) { if (node.FullPath == path) return node; - if (node.IsFolder && path.StartsWith(node.FullPath + "/")) { + if (node.IsFolder && path.StartsWith(node.FullPath + "/", StringComparison.Ordinal)) { var foundInChildren = SelectByPath(node.Children, path); if (foundInChildren != null) { node.IsExpanded = true; diff --git a/src/ViewModels/Histories.cs b/src/ViewModels/Histories.cs index 578d6300..99642050 100644 --- a/src/ViewModels/Histories.cs +++ b/src/ViewModels/Histories.cs @@ -2,6 +2,7 @@ using Avalonia.Platform.Storage; using Avalonia.Threading; using CommunityToolkit.Mvvm.ComponentModel; +using System; using System.Collections; using System.Collections.Generic; using System.Threading.Tasks; @@ -72,7 +73,7 @@ namespace SourceGit.ViewModels { } public void NavigateTo(string commitSHA) { - var commit = _commits.Find(x => x.SHA.StartsWith(commitSHA)); + var commit = _commits.Find(x => x.SHA.StartsWith(commitSHA, StringComparison.Ordinal)); if (commit != null) { AutoSelectedCommit = commit; diff --git a/src/ViewModels/Repository.cs b/src/ViewModels/Repository.cs index f190d7f8..dcf4ce49 100644 --- a/src/ViewModels/Repository.cs +++ b/src/ViewModels/Repository.cs @@ -446,7 +446,7 @@ namespace SourceGit.ViewModels { var limits = $"-{Preference.Instance.MaxHistoryCommits} "; var validFilters = new List(); foreach (var filter in Filters) { - if (filter.StartsWith("refs/")) { + if (filter.StartsWith("refs/", StringComparison.Ordinal)) { if (_branches.FindIndex(x => x.FullName == filter) >= 0) validFilters.Add(filter); } else { if (_tags.FindIndex(t => t.Name == filter) >= 0) validFilters.Add(filter);