feature(LFS): LFS diff and preview support

This commit is contained in:
leo 2020-09-25 16:30:28 +08:00
parent f3ee044818
commit 77ba0e6cdb
7 changed files with 184 additions and 52 deletions

View file

@ -31,14 +31,6 @@ namespace SourceGit.Git {
Both, Both,
} }
/// <summary>
/// Binary change.
/// </summary>
public class BinaryChange {
public long Size = 0;
public long PreSize = 0;
}
/// <summary> /// <summary>
/// Block /// Block
/// </summary> /// </summary>
@ -63,9 +55,9 @@ namespace SourceGit.Git {
} }
/// <summary> /// <summary>
/// Diff result. /// Text file change.
/// </summary> /// </summary>
public class Result { public class TextChange {
public bool IsValid = false; public bool IsValid = false;
public bool IsBinary = false; public bool IsBinary = false;
public List<Block> Blocks = new List<Block>(); public List<Block> Blocks = new List<Block>();
@ -119,14 +111,31 @@ namespace SourceGit.Git {
} }
} }
/// <summary>
/// Binary change.
/// </summary>
public class BinaryChange {
public long Size = 0;
public long PreSize = 0;
}
/// <summary>
/// Change for LFS object information.
/// </summary>
public class LFSChange {
public LFSObject Old;
public LFSObject New;
public bool IsValid => Old != null || New != null;
}
/// <summary> /// <summary>
/// Run diff process. /// Run diff process.
/// </summary> /// </summary>
/// <param name="repo"></param> /// <param name="repo"></param>
/// <param name="args"></param> /// <param name="args"></param>
/// <returns></returns> /// <returns></returns>
public static Result Run(Repository repo, string args) { public static TextChange GetTextChange(Repository repo, string args) {
var rs = new Result(); var rs = new TextChange();
var current = new Block(); var current = new Block();
var left = 0; var left = 0;
var right = 0; var right = 0;
@ -259,5 +268,37 @@ namespace SourceGit.Git {
return change; return change;
} }
/// <summary>
/// Get LFS object changes.
/// </summary>
/// <param name="repo"></param>
/// <param name="args"></param>
/// <returns></returns>
public static LFSChange GetLFSChange(Repository repo, string args) {
var rc = new LFSChange();
repo.RunCommand($"diff --ignore-cr-at-eol {args}", line => {
if (line[0] == '-') {
if (rc.Old == null) rc.Old = new LFSObject();
line = line.Substring(1);
if (line.StartsWith("oid sha256:")) {
rc.Old.OID = line.Substring(11);
} else if (line.StartsWith("size ")) {
rc.Old.Size = int.Parse(line.Substring(5));
}
} else if (line[0] == '+') {
if (rc.New == null) rc.New = new LFSObject();
line = line.Substring(1);
if (line.StartsWith("oid sha256:")) {
rc.New.OID = line.Substring(11);
} else if (line.StartsWith("size ")) {
rc.New.Size = int.Parse(line.Substring(5));
}
}
});
return rc;
}
} }
} }

18
src/Git/LFS.cs Normal file
View file

@ -0,0 +1,18 @@
namespace SourceGit.Git {
/// <summary>
/// Object filtered by LFS
/// </summary>
public class LFSObject {
/// <summary>
/// Object id
/// </summary>
public string OID { get; set; }
/// <summary>
/// Object size.
/// </summary>
public long Size { get; set; }
}
}

View file

@ -999,11 +999,46 @@ namespace SourceGit.Git {
/// <returns></returns> /// <returns></returns>
public long GetFileSize(string sha, string path) { public long GetFileSize(string sha, string path) {
long size = 0; long size = 0;
RunCommand($"cat-file -s {sha}:\"{path}\"", line => { RunCommand($"cat-file -s {sha}:\"{path}\"", line => {
if (!long.TryParse(line, out size)) size = 0; if (!long.TryParse(line, out size)) size = 0;
}); });
return size; return size;
} }
/// <summary>
/// Detect if a file is managed by LFS.
/// </summary>
/// <param name="path">File path</param>
/// <returns></returns>
public bool IsLFSFiltered(string path) {
bool ok = false;
RunCommand($"check-attr -a -z \"{path}\"", line => {
ok = ok || line.Contains("filter\0lfs");
});
return ok;
}
/// <summary>
/// Get LFS object information.
/// </summary>
/// <param name="sha"></param>
/// <param name="path"></param>
/// <returns></returns>
public LFSObject GetLFSObject(string sha, string path) {
LFSObject obj = new LFSObject();
RunCommand($"show {sha}:\"{path}\"", line => {
if (line.StartsWith("oid")) {
obj.OID = line.Substring(3).Replace("sha256:", "").Trim();
} else if (line.StartsWith("size")) {
obj.Size = int.Parse(line.Substring(4).Trim());
}
});
return obj;
}
#endregion #endregion
#region METHOD_GITFLOW #region METHOD_GITFLOW

View file

@ -2,6 +2,7 @@
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Geometry x:Key="Icon.Git">M1004.824 466.4L557.72 19.328c-25.728-25.76-67.488-25.76-93.28 0L360.568 123.2l78.176 78.176c12.544-5.984 26.56-9.376 41.376-9.376 53.024 0 96 42.976 96 96 0 14.816-3.36 28.864-9.376 41.376l127.968 127.968c12.544-5.984 26.56-9.376 41.376-9.376 53.024 0 96 42.976 96 96s-42.976 96-96 96-96-42.976-96-96c0-14.816 3.36-28.864 9.376-41.376L521.496 374.624a88.837 88.837 0 0 1-9.376 3.872v266.976c37.28 13.184 64 48.704 64 90.528 0 53.024-42.976 96-96 96s-96-42.976-96-96c0-41.792 26.72-77.344 64-90.528V378.496c-37.28-13.184-64-48.704-64-90.528 0-14.816 3.36-28.864 9.376-41.376l-78.176-78.176L19.416 464.288c-25.76 25.792-25.76 67.52 0 93.28l447.136 447.072c25.728 25.76 67.488 25.76 93.28 0l444.992-444.992c25.76-25.76 25.76-67.552 0-93.28z</Geometry> <Geometry x:Key="Icon.Git">M1004.824 466.4L557.72 19.328c-25.728-25.76-67.488-25.76-93.28 0L360.568 123.2l78.176 78.176c12.544-5.984 26.56-9.376 41.376-9.376 53.024 0 96 42.976 96 96 0 14.816-3.36 28.864-9.376 41.376l127.968 127.968c12.544-5.984 26.56-9.376 41.376-9.376 53.024 0 96 42.976 96 96s-42.976 96-96 96-96-42.976-96-96c0-14.816 3.36-28.864 9.376-41.376L521.496 374.624a88.837 88.837 0 0 1-9.376 3.872v266.976c37.28 13.184 64 48.704 64 90.528 0 53.024-42.976 96-96 96s-96-42.976-96-96c0-41.792 26.72-77.344 64-90.528V378.496c-37.28-13.184-64-48.704-64-90.528 0-14.816 3.36-28.864 9.376-41.376l-78.176-78.176L19.416 464.288c-25.76 25.792-25.76 67.52 0 93.28l447.136 447.072c25.728 25.76 67.488 25.76 93.28 0l444.992-444.992c25.76-25.76 25.76-67.552 0-93.28z</Geometry>
<Geometry x:Key="Icon.Submodule">M557.696 545.347L789.873 402.66c23.998-14.999 31.297-46.496 16.398-70.493-14.798-23.798-45.995-31.197-69.993-16.699L506.501 456.555 277.123 315.37c-24.098-14.798-55.595-7.3-70.493 16.799-14.799 24.097-7.3 55.594 16.798 70.493l231.778 142.586V819.12c0 28.297 22.897 51.195 51.195 51.195 28.297 0 51.195-22.898 51.195-51.195V545.347h0.1zM506.5 0l443.356 255.975v511.95L506.501 1023.9 63.144 767.925v-511.95L506.5 0z</Geometry> <Geometry x:Key="Icon.Submodule">M557.696 545.347L789.873 402.66c23.998-14.999 31.297-46.496 16.398-70.493-14.798-23.798-45.995-31.197-69.993-16.699L506.501 456.555 277.123 315.37c-24.098-14.798-55.595-7.3-70.493 16.799-14.799 24.097-7.3 55.594 16.798 70.493l231.778 142.586V819.12c0 28.297 22.897 51.195 51.195 51.195 28.297 0 51.195-22.898 51.195-51.195V545.347h0.1zM506.5 0l443.356 255.975v511.95L506.501 1023.9 63.144 767.925v-511.95L506.5 0z</Geometry>
<Geometry x:Key="Icon.LFS">M169.984 470.016l0 84.010667 86.016 0 0-84.010667-86.016 0zM86.016 598.016l0-171.989333 852.010667 0 0 171.989333-852.010667 0zM256 297.984l0-84.010667-86.016 0 0 84.010667 86.016 0zM86.016 169.984l852.010667 0 0 171.989333-852.010667 0 0-171.989333zM169.984 726.016l0 84.010667 86.016 0 0-84.010667-86.016 0zM86.016 854.016l0-171.989333 852.010667 0 0 171.989333-852.010667 0z</Geometry>
<Geometry x:Key="Icon.ScrollLeft">M753.613 996.727L269.38 511.505 754.602 27.272z</Geometry> <Geometry x:Key="Icon.ScrollLeft">M753.613 996.727L269.38 511.505 754.602 27.272z</Geometry>
<Geometry x:Key="Icon.ScrollRight">M270.387 27.273L754.62 512.495 269.398 996.728z</Geometry> <Geometry x:Key="Icon.ScrollRight">M270.387 27.273L754.62 512.495 269.398 996.728z</Geometry>

View file

@ -368,6 +368,12 @@ namespace SourceGit.UI {
switch (node.CommitObject.Kind) { switch (node.CommitObject.Kind) {
case Git.Commit.Object.Type.Blob: case Git.Commit.Object.Type.Blob:
if (repo.IsLFSFiltered(node.FilePath)) {
var obj = repo.GetLFSObject(commit.SHA, node.FilePath);
maskRevision.Visibility = Visibility.Visible;
iconPreviewRevision.Data = FindResource("Icon.LFS") as Geometry;
txtPreviewRevision.Content = $"LFS SIZE: {obj.Size} Bytes";
} else {
await Task.Run(() => { await Task.Run(() => {
var isBinary = false; var isBinary = false;
var data = commit.GetTextFileContent(repo, node.FilePath, out isBinary); var data = commit.GetTextFileContent(repo, node.FilePath, out isBinary);
@ -378,6 +384,7 @@ namespace SourceGit.UI {
Dispatcher.Invoke(() => filePreview.Text = data); Dispatcher.Invoke(() => filePreview.Text = data);
} }
}); });
}
break; break;
case Git.Commit.Object.Type.Tag: case Git.Commit.Object.Type.Tag:
maskRevision.Visibility = Visibility.Visible; maskRevision.Visibility = Visibility.Visible;

View file

@ -164,7 +164,7 @@
<Border x:Name="sizeChange" Grid.Row="1" ClipToBounds="True" Background="{StaticResource Brush.BG3}" Visibility="Collapsed"> <Border x:Name="sizeChange" Grid.Row="1" ClipToBounds="True" Background="{StaticResource Brush.BG3}" Visibility="Collapsed">
<StackPanel Orientation="Vertical" VerticalAlignment="Center"> <StackPanel Orientation="Vertical" VerticalAlignment="Center">
<Label Content="BINARY DIFF" Margin="0,0,0,32" FontSize="18" FontWeight="UltraBold" Foreground="{StaticResource Brush.FG2}" HorizontalAlignment="Center"/> <Label x:Name="txtSizeChangeTitle" Content="BINARY DIFF" Margin="0,0,0,32" FontSize="18" FontWeight="UltraBold" Foreground="{StaticResource Brush.FG2}" HorizontalAlignment="Center"/>
<Path Width="64" Height="64" Style="{StaticResource Style.Icon}" Data="{StaticResource Icon.Binary}" Fill="{StaticResource Brush.FG2}"/> <Path Width="64" Height="64" Style="{StaticResource Style.Icon}" Data="{StaticResource Icon.Binary}" Fill="{StaticResource Brush.FG2}"/>
<Grid Margin="0,16,0,0" HorizontalAlignment="Center" TextElement.FontSize="18" TextElement.FontWeight="UltraBold"> <Grid Margin="0,16,0,0" HorizontalAlignment="Center" TextElement.FontSize="18" TextElement.FontWeight="UltraBold">
<Grid.RowDefinitions> <Grid.RowDefinitions>

View file

@ -64,11 +64,22 @@ namespace SourceGit.UI {
if (!string.IsNullOrEmpty(opts.OrgPath)) args += $"\"{opts.OrgPath}\" "; if (!string.IsNullOrEmpty(opts.OrgPath)) args += $"\"{opts.OrgPath}\" ";
args += $"\"{opts.Path}\""; args += $"\"{opts.Path}\"";
var rs = Git.Diff.Run(repo, args); if (repo.IsLFSFiltered(opts.Path)) {
var lc = Git.Diff.GetLFSChange(repo, args);
if (lc.IsValid) {
SetLFSChange(lc);
} else {
SetSame();
}
return;
}
var rs = Git.Diff.GetTextChange(repo, args);
if (rs.IsBinary) { if (rs.IsBinary) {
SetSizeChangeData(Git.Diff.GetSizeChange(repo, opts.RevisionRange, opts.Path, opts.OrgPath)); SetBinaryChange(Git.Diff.GetSizeChange(repo, opts.RevisionRange, opts.Path, opts.OrgPath));
} else if (rs.Blocks.Count > 0) { } else if (rs.Blocks.Count > 0) {
SetData(rs); SetTextChange(rs);
} else { } else {
SetSame(); SetSame();
} }
@ -91,36 +102,11 @@ namespace SourceGit.UI {
} }
} }
/// <summary>
/// Show size changes.
/// </summary>
/// <param name="bc"></param>
private void SetSizeChangeData(Git.Diff.BinaryChange bc) {
Dispatcher.Invoke(() => {
loading.Visibility = Visibility.Collapsed;
sizeChange.Visibility = Visibility.Visible;
diffNavigation.Visibility = Visibility.Collapsed;
txtNewSize.Content = $"{bc.Size} Bytes";
txtOldSize.Content = $"{bc.PreSize} Bytes";
});
}
/// <summary>
/// Show no changes or only EOL changes.
/// </summary>
private void SetSame() {
Dispatcher.Invoke(() => {
loading.Visibility = Visibility.Collapsed;
noChange.Visibility = Visibility.Visible;
diffNavigation.Visibility = Visibility.Collapsed;
});
}
/// <summary> /// <summary>
/// Show diff content. /// Show diff content.
/// </summary> /// </summary>
/// <param name="rs"></param> /// <param name="rs"></param>
private void SetData(Git.Diff.Result rs) { private void SetTextChange(Git.Diff.TextChange rs) {
Dispatcher.Invoke(() => { Dispatcher.Invoke(() => {
loading.Visibility = Visibility.Collapsed; loading.Visibility = Visibility.Collapsed;
textChange.Visibility = Visibility.Visible; textChange.Visibility = Visibility.Visible;
@ -141,6 +127,50 @@ namespace SourceGit.UI {
}); });
} }
/// <summary>
/// Show size changes.
/// </summary>
/// <param name="bc"></param>
private void SetBinaryChange(Git.Diff.BinaryChange bc) {
Dispatcher.Invoke(() => {
loading.Visibility = Visibility.Collapsed;
sizeChange.Visibility = Visibility.Visible;
diffNavigation.Visibility = Visibility.Collapsed;
txtSizeChangeTitle.Content = "BINARY DIFF";
txtNewSize.Content = $"{bc.Size} Bytes";
txtOldSize.Content = $"{bc.PreSize} Bytes";
});
}
/// <summary>
/// Show size changes.
/// </summary>
/// <param name="lc"></param>
private void SetLFSChange(Git.Diff.LFSChange lc) {
Dispatcher.Invoke(() => {
var oldSize = lc.Old == null ? 0 : lc.Old.Size;
var newSize = lc.New == null ? 0 : lc.New.Size;
loading.Visibility = Visibility.Collapsed;
sizeChange.Visibility = Visibility.Visible;
diffNavigation.Visibility = Visibility.Collapsed;
txtSizeChangeTitle.Content = "LFS OBJECT CHANGE";
txtNewSize.Content = $"{newSize} Bytes";
txtOldSize.Content = $"{oldSize} Bytes";
});
}
/// <summary>
/// Show no changes or only EOL changes.
/// </summary>
private void SetSame() {
Dispatcher.Invoke(() => {
loading.Visibility = Visibility.Collapsed;
noChange.Visibility = Visibility.Visible;
diffNavigation.Visibility = Visibility.Collapsed;
});
}
/// <summary> /// <summary>
/// Make paragraph. /// Make paragraph.
/// </summary> /// </summary>