enhance: better implementation to diff submodule changes while repository is not opened

This commit is contained in:
leo 2024-07-01 13:13:18 +08:00
parent 7f389b2e6f
commit f08acebb5e
No known key found for this signature in database

View file

@ -111,27 +111,34 @@ namespace SourceGit.ViewModels
if (latest.TextDiff != null) if (latest.TextDiff != null)
{ {
var repo = App.FindOpenedRepository(_repo); var count = latest.TextDiff.Lines.Count;
if (repo != null && repo.Submodules.Contains(_option.Path)) var isSubmodule = false;
if (count <= 3)
{ {
var submoduleDiff = new Models.SubmoduleDiff(); var submoduleDiff = new Models.SubmoduleDiff();
var submoduleRoot = $"{_repo}/{_option.Path}".Replace("\\", "/"); var submoduleRoot = $"{_repo}/{_option.Path}".Replace("\\", "/");
foreach (var line in latest.TextDiff.Lines) isSubmodule = true;
for (int i = 1; i < count; i++)
{ {
if (line.Type == Models.TextDiffLineType.Added) var line = latest.TextDiff.Lines[i];
if (!line.Content.StartsWith("Subproject commit ", StringComparison.Ordinal))
{ {
var sha = line.Content.Substring("Subproject commit ".Length); isSubmodule = false;
submoduleDiff.New = QuerySubmoduleRevision(submoduleRoot, sha); break;
} }
var sha = line.Content.Substring(18);
if (line.Type == Models.TextDiffLineType.Added)
submoduleDiff.New = QuerySubmoduleRevision(submoduleRoot, sha);
else if (line.Type == Models.TextDiffLineType.Deleted) else if (line.Type == Models.TextDiffLineType.Deleted)
{
var sha = line.Content.Substring("Subproject commit ".Length);
submoduleDiff.Old = QuerySubmoduleRevision(submoduleRoot, sha); submoduleDiff.Old = QuerySubmoduleRevision(submoduleRoot, sha);
} }
}
if (isSubmodule)
rs = submoduleDiff; rs = submoduleDiff;
} }
else
if (!isSubmodule)
{ {
latest.TextDiff.File = _option.Path; latest.TextDiff.File = _option.Path;
rs = latest.TextDiff; rs = latest.TextDiff;