From a94a30af90ddcb8bad08220e171af20fd1f31b19 Mon Sep 17 00:00:00 2001 From: Drew Noakes Date: Thu, 12 Jul 2018 21:45:45 +0100 Subject: [PATCH] Further use of ObjectId Make most GitSubmoduleStatus properties readonly. Add IGitRef.ObjectId. --- GitCommands/Git/GitCommandHelpers.cs | 43 +++++++++++-------- GitCommands/Git/GitItemStatus.cs | 4 +- GitCommands/Git/GitModule.cs | 15 ++++--- GitCommands/Git/GitRef.cs | 22 +++++----- GitCommands/Git/GitSubmoduleStatus.cs | 32 ++++++++++---- GitCommands/Remote/GitRemoteManager.cs | 4 +- GitCommands/RevisionReader.cs | 6 +-- GitUI/CommandsDialogs/FormStash.cs | 2 +- .../RevisionFileTreeControl.cs | 2 +- GitUI/Editor/FileViewer.cs | 17 +++++--- GitUI/GitUIExtensions.cs | 4 +- Plugins/GitUIPluginInterfaces/IGitItem.cs | 1 + .../GitStatistics/FormGitStatistics.cs | 9 ++-- ResourceManager/LocalizationHelpers.cs | 14 +++--- .../Git/GitCommandHelpersTest.cs | 19 ++++---- .../Git/Gpg/GitGpgControllerTests.cs | 12 +++--- .../RevisionFileTreeControllerTests.cs | 12 +++--- 17 files changed, 123 insertions(+), 95 deletions(-) diff --git a/GitCommands/Git/GitCommandHelpers.cs b/GitCommands/Git/GitCommandHelpers.cs index 35f025191b0..0107268d831 100644 --- a/GitCommands/Git/GitCommandHelpers.cs +++ b/GitCommands/Git/GitCommandHelpers.cs @@ -707,7 +707,7 @@ public static GitSubmoduleStatus GetCurrentSubmoduleChanges(GitModule module, st { Patch patch = module.GetCurrentChanges(fileName, oldFileName, staged, "", module.FilesEncoding); string text = patch != null ? patch.Text : ""; - return GetSubmoduleStatus(text, module, fileName); + return ParseSubmoduleStatus(text, module, fileName); } [CanBeNull] @@ -717,14 +717,21 @@ public static GitSubmoduleStatus GetCurrentSubmoduleChanges(GitModule module, st } [CanBeNull] - public static GitSubmoduleStatus GetSubmoduleStatus(string text, GitModule module, string fileName) + public static GitSubmoduleStatus ParseSubmoduleStatus(string text, GitModule module, string fileName) { if (string.IsNullOrEmpty(text)) { return null; } - var status = new GitSubmoduleStatus(); + string name = null; + string oldName = null; + bool isDirty = false; + ObjectId commitId = null; + ObjectId oldCommitId = null; + int? addedCommits = null; + int? removedCommits = null; + using (var reader = new StringReader(text)) { string line = reader.ReadLine(); @@ -734,16 +741,16 @@ public static GitSubmoduleStatus GetSubmoduleStatus(string text, GitModule modul var match = Regex.Match(line, @"diff --git [abic]/(.+)\s[abwi]/(.+)"); if (match.Groups.Count > 1) { - status.Name = match.Groups[1].Value; - status.OldName = match.Groups[2].Value; + name = match.Groups[1].Value; + oldName = match.Groups[2].Value; } else { match = Regex.Match(line, @"diff --cc (.+)"); if (match.Groups.Count > 1) { - status.Name = match.Groups[1].Value; - status.OldName = match.Groups[1].Value; + name = match.Groups[1].Value; + oldName = match.Groups[1].Value; } } } @@ -761,38 +768,38 @@ public static GitSubmoduleStatus GetSubmoduleStatus(string text, GitModule modul } char c = line[0]; - const string commit = "commit "; + const string commitStr = "commit "; string hash = ""; - int pos = line.IndexOf(commit); + int pos = line.IndexOf(commitStr); if (pos >= 0) { - hash = line.Substring(pos + commit.Length); + hash = line.Substring(pos + commitStr.Length); } - bool isDirty = hash.EndsWith("-dirty"); + bool endsWithDirty = hash.EndsWith("-dirty"); hash = hash.Replace("-dirty", ""); if (c == '-') { - status.OldCommit = hash; + oldCommitId = ObjectId.Parse(hash); } else if (c == '+') { - status.Commit = hash; - status.IsDirty = isDirty; + commitId = ObjectId.Parse(hash); + isDirty = endsWithDirty; } // TODO: Support combined merge } } - if (status.OldCommit != null && status.Commit != null) + if (oldCommitId != null && commitId != null) { var submodule = module.GetSubmodule(fileName); - status.AddedCommits = submodule.GetCommitCount(status.Commit, status.OldCommit); - status.RemovedCommits = submodule.GetCommitCount(status.OldCommit, status.Commit); + addedCommits = submodule.GetCommitCount(commitId.ToString(), oldCommitId.ToString()); + removedCommits = submodule.GetCommitCount(oldCommitId.ToString(), commitId.ToString()); } - return status; + return new GitSubmoduleStatus(name, oldName, isDirty, commitId, oldCommitId, addedCommits, removedCommits); } /// diff --git a/GitCommands/Git/GitItemStatus.cs b/GitCommands/Git/GitItemStatus.cs index 7fd7e960417..7ddd526680b 100644 --- a/GitCommands/Git/GitItemStatus.cs +++ b/GitCommands/Git/GitItemStatus.cs @@ -2,6 +2,7 @@ using System.Diagnostics; using System.Text; using System.Threading.Tasks; +using GitUIPluginInterfaces; using JetBrains.Annotations; using Microsoft.VisualStudio.Threading; @@ -39,7 +40,8 @@ private enum Flags public string Name { get; set; } public string OldName { get; set; } - public string TreeGuid { get; set; } + [CanBeNull] + public ObjectId TreeGuid { get; set; } public string RenameCopyPercentage { get; set; } // Staged is three state and has no default status diff --git a/GitCommands/Git/GitModule.cs b/GitCommands/Git/GitModule.cs index 86758b2e8b5..7149764e0c6 100644 --- a/GitCommands/Git/GitModule.cs +++ b/GitCommands/Git/GitModule.cs @@ -2464,7 +2464,7 @@ public IReadOnlyList GetTreeFiles(ObjectId treeGuid, bool full) IsChanged = false, IsDeleted = false, Name = file.Name, - TreeGuid = file.Guid, + TreeGuid = file.ObjectId, Staged = StagedStatus.None }).ToList(); @@ -2550,7 +2550,7 @@ private void GetSubmoduleStatus(IReadOnlyList status, string firs Patch patch = GetSingleDiff(firstRevision, secondRevision, item.Name, item.OldName, "", SystemEncoding, true); string text = patch != null ? patch.Text : ""; - var submoduleStatus = GitCommandHelpers.GetSubmoduleStatus(text, this, item.Name); + var submoduleStatus = GitCommandHelpers.ParseSubmoduleStatus(text, this, item.Name); if (submoduleStatus.Commit != submoduleStatus.OldCommit) { var submodule = submoduleStatus.GetSubmodule(this); @@ -2941,7 +2941,7 @@ public IReadOnlyList ParseRefs([NotNull] string refList) foreach (Match match in matches) { var refName = match.Groups["refname"].Value; - var objectId = match.Groups["objectid"].Value; + var objectId = ObjectId.Parse(match.Groups["objectid"].Value); var remoteName = GitRefName.GetRemoteName(refName); var head = new GitRef(this, objectId, refName, remoteName); @@ -3362,7 +3362,8 @@ public string GetFileText(string id, Encoding encoding) return RunCacheableGitCmd($"cat-file blob \"{id}\"", encoding); } - public string GetFileBlobHash(string fileName, ObjectId objectId) + [CanBeNull] + public ObjectId GetFileBlobHash(string fileName, ObjectId objectId) { if (objectId == ObjectId.UnstagedId) { @@ -3377,7 +3378,7 @@ public string GetFileBlobHash(string fileName, ObjectId objectId) var lines = RunGitCmd($"ls-files -s \"{fileName}\"").Split(' ', '\t'); if (lines.Length >= 2) { - return lines[1]; + return ObjectId.Parse(lines[1]); } } else @@ -3385,11 +3386,11 @@ public string GetFileBlobHash(string fileName, ObjectId objectId) var lines = RunGitCmd($"ls-tree -r {objectId} \"{fileName}\"").Split(' ', '\t'); if (lines.Length >= 3) { - return lines[2]; + return ObjectId.Parse(lines[2]); } } - return ""; + return null; } [CanBeNull] diff --git a/GitCommands/Git/GitRef.cs b/GitCommands/Git/GitRef.cs index 5bedf02d797..ad5667cc2c9 100644 --- a/GitCommands/Git/GitRef.cs +++ b/GitCommands/Git/GitRef.cs @@ -7,23 +7,23 @@ namespace GitCommands { - public class GitRef : IGitRef + public sealed class GitRef : IGitRef { private readonly string _mergeSettingName; private readonly string _remoteSettingName; public IGitModule Module { get; } - public GitRef(IGitModule module, string guid, string completeName) - : this(module, guid, completeName, string.Empty) + public GitRef(IGitModule module, [CanBeNull] ObjectId objectId, string completeName) + : this(module, objectId, completeName, string.Empty) { } - public GitRef(IGitModule module, string guid, string completeName, string remote) + public GitRef(IGitModule module, [CanBeNull] ObjectId objectId, string completeName, string remote) { Module = module; - Guid = guid; - IsSelected = false; + ObjectId = objectId; + Guid = objectId?.ToString(); CompleteName = completeName; Remote = remote; @@ -41,7 +41,7 @@ public GitRef(IGitModule module, string guid, string completeName, string remote } public string CompleteName { get; } - public bool IsSelected { get; set; } + public bool IsSelected { get; set; } = false; public bool IsSelectedHeadMergeSource { get; set; } public bool IsTag { get; } public bool IsHead { get; } @@ -125,15 +125,15 @@ public static GitRef NoHead(GitModule module) #region IGitItem Members + [CanBeNull] + public ObjectId ObjectId { get; } + [CanBeNull] public string Guid { get; } public string Name { get; } #endregion - public override string ToString() - { - return CompleteName; - } + public override string ToString() => CompleteName; [CanBeNull] private string ParseName() diff --git a/GitCommands/Git/GitSubmoduleStatus.cs b/GitCommands/Git/GitSubmoduleStatus.cs index f209d843374..c173f58875b 100644 --- a/GitCommands/Git/GitSubmoduleStatus.cs +++ b/GitCommands/Git/GitSubmoduleStatus.cs @@ -1,15 +1,29 @@ -namespace GitCommands +using GitUIPluginInterfaces; + +namespace GitCommands { public sealed class GitSubmoduleStatus { - public string Name { get; set; } - public string OldName { get; set; } - public bool IsDirty { get; set; } - public string Commit { get; set; } - public string OldCommit { get; set; } + public string Name { get; } + public string OldName { get; } + public bool IsDirty { get; } + public ObjectId Commit { get; } + public ObjectId OldCommit { get; } + public int? AddedCommits { get; } + public int? RemovedCommits { get; } + public SubmoduleStatus Status { get; set; } = SubmoduleStatus.Unknown; - public int? AddedCommits { get; set; } - public int? RemovedCommits { get; set; } + + public GitSubmoduleStatus(string name, string oldName, bool isDirty, ObjectId commit, ObjectId oldCommit, int? addedCommits, int? removedCommits) + { + Name = name; + OldName = oldName; + IsDirty = isDirty; + Commit = commit; + OldCommit = oldCommit; + AddedCommits = addedCommits; + RemovedCommits = removedCommits; + } public GitModule GetSubmodule(GitModule module) { @@ -24,7 +38,7 @@ public void CheckSubmoduleStatus(GitModule submodule) return; } - Status = submodule.CheckSubmoduleStatus(Commit, OldCommit); + Status = submodule.CheckSubmoduleStatus(Commit.ToString(), OldCommit.ToString()); } public string AddedAndRemovedString() diff --git a/GitCommands/Remote/GitRemoteManager.cs b/GitCommands/Remote/GitRemoteManager.cs index 8900bb1bc79..b98a896119b 100644 --- a/GitCommands/Remote/GitRemoteManager.cs +++ b/GitCommands/Remote/GitRemoteManager.cs @@ -112,7 +112,7 @@ public string GetDefaultPushRemote(GitRemote remote, string branch) var module = GetModule(); bool IsSettingForBranch(string setting, string branchName) { - var head = new GitRef(module, string.Empty, setting); + var head = new GitRef(module, null, setting); return head.IsHead && head.Name.Equals(branchName, StringComparison.OrdinalIgnoreCase); } @@ -120,7 +120,7 @@ bool IsSettingForBranch(string setting, string branchName) .Select(s => s.Split(':')) .Where(t => t.Length == 2) .Where(t => IsSettingForBranch(t[0], branch)) - .Select(t => new GitRef(module, string.Empty, t[1])) + .Select(t => new GitRef(module, null, t[1])) .FirstOrDefault(h => h.IsHead); return remoteHead?.Name; diff --git a/GitCommands/RevisionReader.cs b/GitCommands/RevisionReader.cs index d373571e581..8932b21eef3 100644 --- a/GitCommands/RevisionReader.cs +++ b/GitCommands/RevisionReader.cs @@ -83,7 +83,7 @@ private async Task ExecuteAsync( LatestRefs = module.GetRefs(); UpdateSelectedRef(module, LatestRefs, branchName); - var refsByObjectId = LatestRefs.ToLookup(head => head.Guid); + var refsByObjectId = LatestRefs.ToLookup(head => head.ObjectId); token.ThrowIfCancellationRequested(); @@ -144,8 +144,8 @@ private async Task ExecuteAsync( revision.Body = null; } - // Look up any refs associate with this revision - revision.Refs = refsByObjectId[revision.Guid].AsReadOnlyList(); + // Look up any refs associated with this revision + revision.Refs = refsByObjectId[revision.ObjectId].AsReadOnlyList(); RevisionCount++; diff --git a/GitUI/CommandsDialogs/FormStash.cs b/GitUI/CommandsDialogs/FormStash.cs index 54058628cd8..d240adab0d1 100644 --- a/GitUI/CommandsDialogs/FormStash.cs +++ b/GitUI/CommandsDialogs/FormStash.cs @@ -153,7 +153,7 @@ private void StashedSelectedIndexChanged(object sender, EventArgs e) { View.ViewTextAsync( stashedItem.Name, - LocalizationHelpers.GetSubmoduleText(Module, stashedItem.Name, stashedItem.TreeGuid)); + LocalizationHelpers.GetSubmoduleText(Module, stashedItem.Name, stashedItem.TreeGuid?.ToString())); } } else diff --git a/GitUI/CommandsDialogs/RevisionFileTreeControl.cs b/GitUI/CommandsDialogs/RevisionFileTreeControl.cs index df96fd7c78e..7c4bdeea9b6 100644 --- a/GitUI/CommandsDialogs/RevisionFileTreeControl.cs +++ b/GitUI/CommandsDialogs/RevisionFileTreeControl.cs @@ -365,7 +365,7 @@ private void tvGitTree_AfterSelect(object sender, TreeViewEventArgs e) { case GitObjectType.Blob: { - FileText.ViewGitItemAsync(gitItem.FileName, gitItem.Guid); + FileText.ViewGitItemAsync(gitItem.FileName, gitItem.ObjectId); break; } diff --git a/GitUI/Editor/FileViewer.cs b/GitUI/Editor/FileViewer.cs index 7f4e1f6ced9..39b67c6cf58 100644 --- a/GitUI/Editor/FileViewer.cs +++ b/GitUI/Editor/FileViewer.cs @@ -512,27 +512,29 @@ public Task ViewGitItemRevisionAsync(string fileName, ObjectId objectId) else { // Retrieve blob, same as GitItemStatus.TreeGuid - string blob = Module.GetFileBlobHash(fileName, objectId); + var blob = Module.GetFileBlobHash(fileName, objectId); return ViewGitItemAsync(fileName, blob); } } - public Task ViewGitItemAsync(string fileName, string guid) + public Task ViewGitItemAsync(string fileName, [CanBeNull] ObjectId objectId) { + var sha = objectId?.ToString(); + return ViewItemAsync( fileName, getImage: GetImage, getFileText: GetFileTextIfBlobExists, - getSubmoduleText: () => LocalizationHelpers.GetSubmoduleText(Module, fileName.TrimEnd('/'), guid), - openWithDifftool: () => Module.OpenWithDifftool(fileName, firstRevision: guid)); + getSubmoduleText: () => LocalizationHelpers.GetSubmoduleText(Module, fileName.TrimEnd('/'), sha), + openWithDifftool: () => Module.OpenWithDifftool(fileName, firstRevision: sha)); - string GetFileTextIfBlobExists() => guid != "" ? Module.GetFileText(guid, Encoding) : ""; + string GetFileTextIfBlobExists() => sha != null ? Module.GetFileText(sha, Encoding) : ""; Image GetImage() { try { - using (var stream = Module.GetFileStream(guid)) + using (var stream = Module.GetFileStream(sha)) { return CreateImage(fileName, stream); } @@ -596,7 +598,8 @@ private Task ViewItemAsync(string fileName, Func getImage, Func g } } - private static Image CreateImage(string fileName, Stream stream) + [NotNull] + private static Image CreateImage([NotNull] string fileName, [NotNull] Stream stream) { if (IsIcon()) { diff --git a/GitUI/GitUIExtensions.cs b/GitUI/GitUIExtensions.cs index ca9ecc2a05d..2b34c4cdd0a 100644 --- a/GitUI/GitUIExtensions.cs +++ b/GitUI/GitUIExtensions.cs @@ -29,7 +29,7 @@ private static Patch GetItemPatch( [NotNull] Encoding encoding) { // Files with tree guid should be presented with normal diff - var isTracked = file.IsTracked || (file.TreeGuid.IsNotNullOrWhitespace() && secondRevision != null); + var isTracked = file.IsTracked || (file.TreeGuid != null && secondRevision != null); return module.GetSingleDiff(firstRevision?.ToString(), secondRevision?.ToString(), file.Name, file.OldName, diffArgs, encoding, true, isTracked); } @@ -101,7 +101,7 @@ public static Task ViewChangesAsync( if (firstRevision == null) { // The previous commit does not exist, nothing to compare with - if (file.TreeGuid.IsNullOrEmpty()) + if (file.TreeGuid != null) { if (secondRevision == null) { diff --git a/Plugins/GitUIPluginInterfaces/IGitItem.cs b/Plugins/GitUIPluginInterfaces/IGitItem.cs index 53119d49b3b..d3d87426786 100644 --- a/Plugins/GitUIPluginInterfaces/IGitItem.cs +++ b/Plugins/GitUIPluginInterfaces/IGitItem.cs @@ -2,6 +2,7 @@ { public interface IGitItem { + ObjectId ObjectId { get; } string Guid { get; } string Name { get; } } diff --git a/Plugins/Statistics/GitStatistics/FormGitStatistics.cs b/Plugins/Statistics/GitStatistics/FormGitStatistics.cs index fdf69b8a803..a3b87ba8df0 100644 --- a/Plugins/Statistics/GitStatistics/FormGitStatistics.cs +++ b/Plugins/Statistics/GitStatistics/FormGitStatistics.cs @@ -181,16 +181,17 @@ var module in private void LoadLinesOfCodeForModule(IGitModule module) { - var result = module.GetTree("HEAD", full: true); - var filesToCheck = new List(); - filesToCheck.AddRange(result.Select(file => Path.Combine(module.WorkingDir, file.Name))); + var filesToCheck = module + .GetTree("HEAD", full: true) + .Select(file => Path.Combine(module.WorkingDir, file.Name)) + .ToList(); _lineCounter.FindAndAnalyzeCodeFiles(_codeFilePattern, DirectoriesToIgnore, filesToCheck); } private void lineCounter_LinesOfCodeUpdated(object sender, EventArgs e) { - LineCounter lineCounter = (LineCounter)sender; + var lineCounter = (LineCounter)sender; // Must do this synchronously because lineCounter.LinesOfCodePerExtension might change while we are iterating over it otherwise. var extensionValues = new decimal[lineCounter.LinesOfCodePerExtension.Count]; diff --git a/ResourceManager/LocalizationHelpers.cs b/ResourceManager/LocalizationHelpers.cs index b35094da1cd..263d9bf583c 100644 --- a/ResourceManager/LocalizationHelpers.cs +++ b/ResourceManager/LocalizationHelpers.cs @@ -109,7 +109,7 @@ public static string GetSubmoduleText(GitModule superproject, string name, strin public static string ProcessSubmodulePatch(GitModule module, string fileName, Patch patch) { string text = patch?.Text; - var status = GitCommandHelpers.GetSubmoduleStatus(text, module, fileName); + var status = GitCommandHelpers.ParseSubmoduleStatus(text, module, fileName); if (status == null) { return ""; @@ -138,13 +138,13 @@ public static string ProcessSubmoduleStatus([NotNull] GitModule module, [NotNull ICommitDataManager commitDataManager = new CommitDataManager(() => gitModule); sb.AppendLine(); - sb.AppendLine("From:\t" + (status.OldCommit ?? "null")); + sb.AppendLine("From:\t" + (status.OldCommit?.ToString() ?? "null")); CommitData oldCommitData = null; if (gitModule.IsValidGitWorkingDir()) { if (status.OldCommit != null) { - oldCommitData = commitDataManager.GetCommitData(status.OldCommit, out _); + oldCommitData = commitDataManager.GetCommitData(status.OldCommit.ToString(), out _); } if (oldCommitData != null) @@ -165,13 +165,13 @@ public static string ProcessSubmoduleStatus([NotNull] GitModule module, [NotNull sb.AppendLine(); string dirty = !status.IsDirty ? "" : " (dirty)"; - sb.AppendLine("To:\t\t" + (status.Commit ?? "null") + dirty); + sb.AppendLine("To:\t\t" + (status.Commit?.ToString() ?? "null") + dirty); CommitData commitData = null; if (gitModule.IsValidGitWorkingDir()) { if (status.Commit != null) { - commitData = commitDataManager.GetCommitData(status.Commit, out _); + commitData = commitDataManager.GetCommitData(status.Commit.ToString(), out _); } if (commitData != null) @@ -191,7 +191,7 @@ public static string ProcessSubmoduleStatus([NotNull] GitModule module, [NotNull } sb.AppendLine(); - var submoduleStatus = gitModule.CheckSubmoduleStatus(status.Commit, status.OldCommit, commitData, oldCommitData); + var submoduleStatus = gitModule.CheckSubmoduleStatus(status.Commit?.ToString(), status.OldCommit?.ToString(), commitData, oldCommitData); sb.Append("Type: "); switch (submoduleStatus) { @@ -253,7 +253,7 @@ public static string ProcessSubmoduleStatus([NotNull] GitModule module, [NotNull } } - string diffs = gitModule.GetDiffFilesText(status.OldCommit, status.Commit); + string diffs = gitModule.GetDiffFilesText(status.OldCommit?.ToString(), status.Commit?.ToString()); if (!string.IsNullOrEmpty(diffs)) { sb.AppendLine("\nDifferences:"); diff --git a/UnitTests/GitCommandsTests/Git/GitCommandHelpersTest.cs b/UnitTests/GitCommandsTests/Git/GitCommandHelpersTest.cs index ac9fefdce6f..e14a51dae66 100644 --- a/UnitTests/GitCommandsTests/Git/GitCommandHelpersTest.cs +++ b/UnitTests/GitCommandsTests/Git/GitCommandHelpersTest.cs @@ -1,6 +1,7 @@ using System; using GitCommands; using GitCommands.Git; +using GitUIPluginInterfaces; using NUnit.Framework; using ResourceManager; @@ -354,11 +355,11 @@ public void GetSubmoduleNamesFromDiffTest() string text = "diff --git a/Externals/conemu-inside b/Externals/conemu-inside\nindex a17ea0c..b5a3d51 160000\n--- a/Externals/conemu-inside\n+++ b/Externals/conemu-inside\n@@ -1 +1 @@\n-Subproject commit a17ea0c8ebe9d8cd7e634ba44559adffe633c11d\n+Subproject commit b5a3d51777c85a9aeee534c382b5ccbb86b485d3\n"; string fileName = "Externals/conemu-inside"; - GitSubmoduleStatus status = GitCommandHelpers.GetSubmoduleStatus(text, testModule, fileName); + GitSubmoduleStatus status = GitCommandHelpers.ParseSubmoduleStatus(text, testModule, fileName); - Assert.AreEqual(status.Commit, "b5a3d51777c85a9aeee534c382b5ccbb86b485d3"); + Assert.AreEqual(status.Commit, ObjectId.Parse("b5a3d51777c85a9aeee534c382b5ccbb86b485d3")); Assert.AreEqual(status.Name, fileName); - Assert.AreEqual(status.OldCommit, "a17ea0c8ebe9d8cd7e634ba44559adffe633c11d"); + Assert.AreEqual(status.OldCommit, ObjectId.Parse("a17ea0c8ebe9d8cd7e634ba44559adffe633c11d")); Assert.AreEqual(status.OldName, fileName); // Submodule name with spaces in the name @@ -366,11 +367,11 @@ public void GetSubmoduleNamesFromDiffTest() text = "diff --git a/Assets/Core/Vehicle Physics core assets b/Assets/Core/Vehicle Physics core assets\nindex 2fb8851..0cc457d 160000\n--- a/Assets/Core/Vehicle Physics core assets\t\n+++ b/Assets/Core/Vehicle Physics core assets\t\n@@ -1 +1 @@\n-Subproject commit 2fb88514cfdc37a2708c24f71eca71c424b8d402\n+Subproject commit 0cc457d030e92f804569407c7cd39893320f9740\n"; fileName = "Assets/Core/Vehicle Physics core assets"; - status = GitCommandHelpers.GetSubmoduleStatus(text, testModule, fileName); + status = GitCommandHelpers.ParseSubmoduleStatus(text, testModule, fileName); - Assert.AreEqual(status.Commit, "0cc457d030e92f804569407c7cd39893320f9740"); + Assert.AreEqual(status.Commit, ObjectId.Parse("0cc457d030e92f804569407c7cd39893320f9740")); Assert.AreEqual(status.Name, fileName); - Assert.AreEqual(status.OldCommit, "2fb88514cfdc37a2708c24f71eca71c424b8d402"); + Assert.AreEqual(status.OldCommit, ObjectId.Parse("2fb88514cfdc37a2708c24f71eca71c424b8d402")); Assert.AreEqual(status.OldName, fileName); // Submodule name in reverse diff, rename @@ -378,11 +379,11 @@ public void GetSubmoduleNamesFromDiffTest() text = "diff --git b/Externals/conemu-inside-b a/Externals/conemu-inside-a\nindex a17ea0c..b5a3d51 160000\n--- b/Externals/conemu-inside-b\n+++ a/Externals/conemu-inside-a\n@@ -1 +1 @@\n-Subproject commit a17ea0c8ebe9d8cd7e634ba44559adffe633c11d\n+Subproject commit b5a3d51777c85a9aeee534c382b5ccbb86b485d3\n"; fileName = "Externals/conemu-inside-b"; - status = GitCommandHelpers.GetSubmoduleStatus(text, testModule, fileName); + status = GitCommandHelpers.ParseSubmoduleStatus(text, testModule, fileName); - Assert.AreEqual(status.Commit, "b5a3d51777c85a9aeee534c382b5ccbb86b485d3"); + Assert.AreEqual(status.Commit, ObjectId.Parse("b5a3d51777c85a9aeee534c382b5ccbb86b485d3")); Assert.AreEqual(status.Name, fileName); - Assert.AreEqual(status.OldCommit, "a17ea0c8ebe9d8cd7e634ba44559adffe633c11d"); + Assert.AreEqual(status.OldCommit, ObjectId.Parse("a17ea0c8ebe9d8cd7e634ba44559adffe633c11d")); fileName = "Externals/conemu-inside-a"; Assert.AreEqual(status.OldName, fileName); } diff --git a/UnitTests/GitCommandsTests/Git/Gpg/GitGpgControllerTests.cs b/UnitTests/GitCommandsTests/Git/Gpg/GitGpgControllerTests.cs index e852daf5e24..ea8a4567fea 100644 --- a/UnitTests/GitCommandsTests/Git/Gpg/GitGpgControllerTests.cs +++ b/UnitTests/GitCommandsTests/Git/Gpg/GitGpgControllerTests.cs @@ -67,7 +67,7 @@ public async Task Validate_GetRevisionTagSignatureStatusAsync(TagStatus tagStatu var revision = new GitRevision(objectId) { Refs = Enumerable.Range(0, numberOfTags) - .Select(_ => new GitRef(_module(), objectId.ToString(), gitRefCompleteName)) + .Select(_ => new GitRef(_module(), objectId, gitRefCompleteName)) .ToList() }; @@ -83,7 +83,7 @@ public async Task Validate_GetRevisionTagSignatureStatusAsync_one_tag(TagStatus { var objectId = ObjectId.Random(); - var gitRef = new GitRef(_module(), objectId.ToString(), "refs/tags/FirstTag^{}"); + var gitRef = new GitRef(_module(), objectId, "refs/tags/FirstTag^{}"); var revision = new GitRevision(objectId) { Refs = new[] { gitRef } }; @@ -132,7 +132,7 @@ public void Validate_GetTagVerifyMessage(int usefulTagRefNumber, string expected case 0: { // Tag but not dereference - var gitRef = new GitRef(_module(), objectId.ToString(), "refs/tags/TagName"); + var gitRef = new GitRef(_module(), objectId, "refs/tags/TagName"); revision.Refs = new[] { gitRef }; _module().RunGitCmd($"verify-tag {gitRef.LocalName}").Returns(""); @@ -143,7 +143,7 @@ public void Validate_GetTagVerifyMessage(int usefulTagRefNumber, string expected case 1: { // One tag that's also IsDereference == true - var gitRef = new GitRef(_module(), objectId.ToString(), "refs/tags/TagName^{}"); + var gitRef = new GitRef(_module(), objectId, "refs/tags/TagName^{}"); revision.Refs = new[] { gitRef }; _module().RunGitCmd($"verify-tag {gitRef.LocalName}").Returns(gitRef.LocalName); @@ -154,12 +154,12 @@ public void Validate_GetTagVerifyMessage(int usefulTagRefNumber, string expected case 2: { // Two tag that's also IsDereference == true - var gitRef1 = new GitRef(_module(), objectId.ToString(), "refs/tags/FirstTag^{}"); + var gitRef1 = new GitRef(_module(), objectId, "refs/tags/FirstTag^{}"); revision.Refs = new[] { gitRef1 }; _module().RunGitCmd($"verify-tag {gitRef1.LocalName}").Returns(gitRef1.LocalName); - var gitRef2 = new GitRef(_module(), objectId.ToString(), "refs/tags/SecondTag^{}"); + var gitRef2 = new GitRef(_module(), objectId, "refs/tags/SecondTag^{}"); revision.Refs = new[] { gitRef1, gitRef2 }; _module().RunGitCmd($"verify-tag {gitRef2.LocalName}").Returns(gitRef2.LocalName); diff --git a/UnitTests/GitUITests/CommandsDialogs/RevisionFileTreeControllerTests.cs b/UnitTests/GitUITests/CommandsDialogs/RevisionFileTreeControllerTests.cs index f9a4fa4052d..079d1b15b51 100644 --- a/UnitTests/GitUITests/CommandsDialogs/RevisionFileTreeControllerTests.cs +++ b/UnitTests/GitUITests/CommandsDialogs/RevisionFileTreeControllerTests.cs @@ -1,5 +1,4 @@ -using System.Collections.Generic; -using System.Diagnostics.CodeAnalysis; +using System.Diagnostics.CodeAnalysis; using System.Windows.Forms; using FluentAssertions; using GitCommands; @@ -206,14 +205,13 @@ private class MockGitItem : IGitItem public MockGitItem(string name) { Name = name; + ObjectId = ObjectId.Random(); + Guid = ObjectId.ToString(); } - public string Guid { get; } = ObjectId.Random().ToString(); - public bool IsBlob { get; } - public bool IsCommit { get; } - public bool IsTree { get; } + public string Guid { get; } + public ObjectId ObjectId { get; } public string Name { get; } - public IEnumerable SubItems { get; } } } }