sourcegit/src/ViewModels/DiffContext.cs

290 lines
11 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
2024-03-27 18:34:08 -07:00
using Avalonia.Media.Imaging;
using Avalonia.Threading;
using CommunityToolkit.Mvvm.ComponentModel;
namespace SourceGit.ViewModels
{
public class DiffContext : ObservableObject
{
public string Title
{
get => _title;
}
public bool IgnoreWhitespace
{
get => _ignoreWhitespace;
set
{
if (SetProperty(ref _ignoreWhitespace, value))
LoadDiffContent();
}
}
public string FileModeChange
{
get => _fileModeChange;
private set => SetProperty(ref _fileModeChange, value);
}
public bool IsTextDiff
{
get => _isTextDiff;
private set => SetProperty(ref _isTextDiff, value);
}
public object Content
{
get => _content;
private set => SetProperty(ref _content, value);
}
public int UnifiedLines
{
get => _unifiedLines;
private set => SetProperty(ref _unifiedLines, value);
}
public DiffContext(string repo, Models.DiffOption option, DiffContext previous = null)
{
_repo = repo;
_option = option;
if (previous != null)
{
_isTextDiff = previous._isTextDiff;
_content = previous._content;
_unifiedLines = previous._unifiedLines;
_ignoreWhitespace = previous._ignoreWhitespace;
_info = previous._info;
}
if (string.IsNullOrEmpty(_option.OrgPath) || _option.OrgPath == "/dev/null")
_title = _option.Path;
else
_title = $"{_option.OrgPath} → {_option.Path}";
LoadDiffContent();
}
feature: diff - toggle show all lines (#615) (#652) * Renamed 1 of 2 SyncScrollOffset props, for clarity The property "SyncScrollOffset" in TextDiff is distinct from the one with the same name in TwoSideTextDiff. These two properties are used in separate (though slightly related) ways and are not really connected. The one in TwoSideTextDiff is mainly used to keep the scroll-pos of the two SingleSideTextDiffPresenter views in sync (aligned), while the one in TextDiff is used only to preserve/reset the scroll-pos in the single CombinedTextDiffPresenter view when (re)loading Diff Content (so not really syncing anything). To clarify this and to make the two properties more distinguishable, I renamed the one in TextDiff to simply "ScrollOffset". * Added icon and string for "Show All Lines" New StreamGeometry "Icons.Lines.All" using SVG path from "text_line_spacing_regular" at https://avaloniaui.github.io/icons.html. New String "Text.Diff.VisualLines.All" for en_US locale (no translations yet). * Implemented new TextDiff feature "Show All Lines" (toggle) * Added new ToggleButton in DiffView toolbar, visible when IsTextDiff, disabling the buttons "Increase/Decrease Number of Visible Lines" when on. * Added new Preference property "UseFullTextDiff". * Added StyledProperty "UseFullTextDiffProperty" in TextDiffView, with a DataTemplate binding to the corresponding preference property. * When changed, UseFullTextDiffProperty is handled identically as UseSideBySideDiffProperty (via new helper method RefreshContent(), for unification with OnDataContextChanged()). * Added new method DiffContext.ToggleFullTextDiff() for changing the preference property and reloading the diff content. * Implemented the new feature by overriding the "unified" (number of context lines) for Commands.Diff() with a very high number. NOTE: The number used (~1 billion) is supposed to be the highest one working on Mac, according to this forum comment: https://stackoverflow.com/questions/28727424/for-git-diff-is-there-a-uinfinity-option-to-show-the-whole-file#comment135202820_28846576
2024-11-04 00:32:51 -08:00
public void ToggleFullTextDiff()
{
Preference.Instance.UseFullTextDiff = !Preference.Instance.UseFullTextDiff;
LoadDiffContent();
}
public void IncrUnified()
{
UnifiedLines = _unifiedLines + 1;
LoadDiffContent();
}
public void DecrUnified()
{
UnifiedLines = Math.Max(4, _unifiedLines - 1);
LoadDiffContent();
}
public void OpenExternalMergeTool()
{
var toolType = Preference.Instance.ExternalMergeToolType;
var toolPath = Preference.Instance.ExternalMergeToolPath;
Task.Run(() => Commands.MergeTool.OpenForDiff(_repo, toolType, toolPath, _option));
}
private void LoadDiffContent()
{
if (_option.Path.EndsWith('/'))
{
Content = null;
IsTextDiff = false;
return;
}
Task.Run(() =>
{
feature: diff - toggle show all lines (#615) (#652) * Renamed 1 of 2 SyncScrollOffset props, for clarity The property "SyncScrollOffset" in TextDiff is distinct from the one with the same name in TwoSideTextDiff. These two properties are used in separate (though slightly related) ways and are not really connected. The one in TwoSideTextDiff is mainly used to keep the scroll-pos of the two SingleSideTextDiffPresenter views in sync (aligned), while the one in TextDiff is used only to preserve/reset the scroll-pos in the single CombinedTextDiffPresenter view when (re)loading Diff Content (so not really syncing anything). To clarify this and to make the two properties more distinguishable, I renamed the one in TextDiff to simply "ScrollOffset". * Added icon and string for "Show All Lines" New StreamGeometry "Icons.Lines.All" using SVG path from "text_line_spacing_regular" at https://avaloniaui.github.io/icons.html. New String "Text.Diff.VisualLines.All" for en_US locale (no translations yet). * Implemented new TextDiff feature "Show All Lines" (toggle) * Added new ToggleButton in DiffView toolbar, visible when IsTextDiff, disabling the buttons "Increase/Decrease Number of Visible Lines" when on. * Added new Preference property "UseFullTextDiff". * Added StyledProperty "UseFullTextDiffProperty" in TextDiffView, with a DataTemplate binding to the corresponding preference property. * When changed, UseFullTextDiffProperty is handled identically as UseSideBySideDiffProperty (via new helper method RefreshContent(), for unification with OnDataContextChanged()). * Added new method DiffContext.ToggleFullTextDiff() for changing the preference property and reloading the diff content. * Implemented the new feature by overriding the "unified" (number of context lines) for Commands.Diff() with a very high number. NOTE: The number used (~1 billion) is supposed to be the highest one working on Mac, according to this forum comment: https://stackoverflow.com/questions/28727424/for-git-diff-is-there-a-uinfinity-option-to-show-the-whole-file#comment135202820_28846576
2024-11-04 00:32:51 -08:00
// NOTE: Here we override the UnifiedLines value (if UseFullTextDiff is on).
// There is no way to tell a git-diff to use "ALL lines of context",
// so instead we set a very high number for the "lines of context" parameter.
var numLines = Preference.Instance.UseFullTextDiff ? 999999999 : _unifiedLines;
var latest = new Commands.Diff(_repo, _option, numLines, _ignoreWhitespace).Result();
var info = new Info(_option, numLines, _ignoreWhitespace, latest);
if (_info != null && info.IsSame(_info))
return;
_info = info;
var rs = null as object;
if (latest.TextDiff != null)
{
var count = latest.TextDiff.Lines.Count;
var isSubmodule = false;
if (count <= 3)
{
var submoduleDiff = new Models.SubmoduleDiff();
var submoduleRoot = $"{_repo}/{_option.Path}".Replace("\\", "/");
isSubmodule = true;
for (int i = 1; i < count; i++)
{
var line = latest.TextDiff.Lines[i];
if (!line.Content.StartsWith("Subproject commit ", StringComparison.Ordinal))
{
isSubmodule = false;
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)
submoduleDiff.Old = QuerySubmoduleRevision(submoduleRoot, sha);
}
if (isSubmodule)
rs = submoduleDiff;
}
if (!isSubmodule)
{
latest.TextDiff.File = _option.Path;
rs = latest.TextDiff;
}
}
else if (latest.IsBinary)
{
var oldPath = string.IsNullOrEmpty(_option.OrgPath) ? _option.Path : _option.OrgPath;
var ext = Path.GetExtension(_option.Path);
if (IMG_EXTS.Contains(ext))
{
var imgDiff = new Models.ImageDiff();
if (_option.Revisions.Count == 2)
{
(imgDiff.Old, imgDiff.OldFileSize) = BitmapFromRevisionFile(_repo, _option.Revisions[0], oldPath);
(imgDiff.New, imgDiff.NewFileSize) = BitmapFromRevisionFile(_repo, _option.Revisions[1], _option.Path);
}
else
{
if (!oldPath.Equals("/dev/null", StringComparison.Ordinal))
(imgDiff.Old, imgDiff.OldFileSize) = BitmapFromRevisionFile(_repo, "HEAD", oldPath);
2024-06-06 00:31:02 -07:00
var fullPath = Path.Combine(_repo, _option.Path);
if (File.Exists(fullPath))
{
imgDiff.New = new Bitmap(fullPath);
imgDiff.NewFileSize = new FileInfo(fullPath).Length;
}
}
rs = imgDiff;
}
else
{
var binaryDiff = new Models.BinaryDiff();
if (_option.Revisions.Count == 2)
{
binaryDiff.OldSize = new Commands.QueryFileSize(_repo, oldPath, _option.Revisions[0]).Result();
binaryDiff.NewSize = new Commands.QueryFileSize(_repo, _option.Path, _option.Revisions[1]).Result();
}
else
{
var fullPath = Path.Combine(_repo, _option.Path);
binaryDiff.OldSize = new Commands.QueryFileSize(_repo, oldPath, "HEAD").Result();
2024-03-27 18:34:08 -07:00
binaryDiff.NewSize = File.Exists(fullPath) ? new FileInfo(fullPath).Length : 0;
}
rs = binaryDiff;
}
}
else if (latest.IsLFS)
{
rs = latest.LFSDiff;
}
else
{
rs = new Models.NoOrEOLChange();
}
Dispatcher.UIThread.Post(() =>
{
if (_content is Models.TextDiff old && rs is Models.TextDiff cur && old.File == cur.File)
feature: diff - toggle show all lines (#615) (#652) * Renamed 1 of 2 SyncScrollOffset props, for clarity The property "SyncScrollOffset" in TextDiff is distinct from the one with the same name in TwoSideTextDiff. These two properties are used in separate (though slightly related) ways and are not really connected. The one in TwoSideTextDiff is mainly used to keep the scroll-pos of the two SingleSideTextDiffPresenter views in sync (aligned), while the one in TextDiff is used only to preserve/reset the scroll-pos in the single CombinedTextDiffPresenter view when (re)loading Diff Content (so not really syncing anything). To clarify this and to make the two properties more distinguishable, I renamed the one in TextDiff to simply "ScrollOffset". * Added icon and string for "Show All Lines" New StreamGeometry "Icons.Lines.All" using SVG path from "text_line_spacing_regular" at https://avaloniaui.github.io/icons.html. New String "Text.Diff.VisualLines.All" for en_US locale (no translations yet). * Implemented new TextDiff feature "Show All Lines" (toggle) * Added new ToggleButton in DiffView toolbar, visible when IsTextDiff, disabling the buttons "Increase/Decrease Number of Visible Lines" when on. * Added new Preference property "UseFullTextDiff". * Added StyledProperty "UseFullTextDiffProperty" in TextDiffView, with a DataTemplate binding to the corresponding preference property. * When changed, UseFullTextDiffProperty is handled identically as UseSideBySideDiffProperty (via new helper method RefreshContent(), for unification with OnDataContextChanged()). * Added new method DiffContext.ToggleFullTextDiff() for changing the preference property and reloading the diff content. * Implemented the new feature by overriding the "unified" (number of context lines) for Commands.Diff() with a very high number. NOTE: The number used (~1 billion) is supposed to be the highest one working on Mac, according to this forum comment: https://stackoverflow.com/questions/28727424/for-git-diff-is-there-a-uinfinity-option-to-show-the-whole-file#comment135202820_28846576
2024-11-04 00:32:51 -08:00
cur.ScrollOffset = old.ScrollOffset;
FileModeChange = latest.FileModeChange;
Content = rs;
IsTextDiff = rs is Models.TextDiff;
});
});
}
private (Bitmap, long) BitmapFromRevisionFile(string repo, string revision, string file)
{
var stream = Commands.QueryFileContent.Run(repo, revision, file);
var size = stream.Length;
return size > 0 ? (new Bitmap(stream), size) : (null, size);
}
private Models.RevisionSubmodule QuerySubmoduleRevision(string repo, string sha)
{
var commit = new Commands.QuerySingleCommit(repo, sha).Result();
if (commit != null)
{
var body = new Commands.QueryCommitFullMessage(repo, sha).Result();
return new Models.RevisionSubmodule() { Commit = commit, FullMessage = body };
}
return new Models.RevisionSubmodule()
{
Commit = new Models.Commit() { SHA = sha },
FullMessage = string.Empty,
};
}
private static readonly HashSet<string> IMG_EXTS = new HashSet<string>()
{
".ico", ".bmp", ".jpg", ".png", ".jpeg", ".webp"
};
private class Info
{
public string Argument { get; set; }
public int UnifiedLines { get; set; }
public bool IgnoreWhitespace { get; set; }
public string OldHash { get; set; }
public string NewHash { get; set; }
public Info(Models.DiffOption option, int unifiedLines, bool ignoreWhitespace, Models.DiffResult result)
{
Argument = option.ToString();
UnifiedLines = unifiedLines;
IgnoreWhitespace = ignoreWhitespace;
OldHash = result.OldHash;
NewHash = result.NewHash;
}
public bool IsSame(Info other)
{
return Argument.Equals(other.Argument, StringComparison.Ordinal) &&
UnifiedLines == other.UnifiedLines &&
IgnoreWhitespace == other.IgnoreWhitespace &&
OldHash.Equals(other.OldHash, StringComparison.Ordinal) &&
NewHash.Equals(other.NewHash, StringComparison.Ordinal);
}
}
2024-07-14 09:30:31 -07:00
private readonly string _repo;
private readonly Models.DiffOption _option = null;
2024-07-14 09:30:31 -07:00
private string _title;
private string _fileModeChange = string.Empty;
private int _unifiedLines = 4;
private bool _isTextDiff = false;
private bool _ignoreWhitespace = false;
private object _content = null;
private Info _info = null;
}
}