Show file size changes for binary diff

This commit is contained in:
leo 2020-07-13 16:50:15 +08:00
parent e8ef47f33d
commit e08b7024fc
9 changed files with 163 additions and 27 deletions

View file

@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
@ -30,6 +31,14 @@ namespace SourceGit.Git {
Both,
}
/// <summary>
/// Binary change.
/// </summary>
public class BinaryChange {
public long Size = 0;
public long PreSize = 0;
}
/// <summary>
/// Block
/// </summary>
@ -215,11 +224,7 @@ namespace SourceGit.Git {
rs.Fit();
if (rs.IsBinary) {
var b = new Block();
b.Mode = LineMode.Indicator;
b.Append("BINARY FILES NOT SUPPORTED!!!");
rs.Blocks.Clear();
rs.Blocks.Add(b);
} else if (rs.Blocks.Count == 0) {
var b = new Block();
b.Mode = LineMode.Indicator;
@ -229,5 +234,38 @@ namespace SourceGit.Git {
return rs;
}
/// <summary>
/// Get file size changes for binary file.
/// </summary>
/// <param name="repo"></param>
/// <param name="revisions"></param>
/// <param name="path"></param>
/// <param name="orgPath"></param>
/// <returns></returns>
public static BinaryChange GetSizeChange(Repository repo, string[] revisions, string path, string orgPath = null) {
var change = new BinaryChange();
if (revisions.Length == 0) { // Compare working copy with HEAD
change.Size = new FileInfo(Path.Combine(repo.Path, path)).Length;
change.PreSize = repo.GetFileSize("HEAD", path);
} else if (revisions.Length == 1) { // Compare HEAD with given revision.
change.Size = repo.GetFileSize("HEAD", path);
if (!string.IsNullOrEmpty(orgPath)) {
change.PreSize = repo.GetFileSize(revisions[0], orgPath);
} else {
change.PreSize = repo.GetFileSize(revisions[0], path);
}
} else {
change.Size = repo.GetFileSize(revisions[1], path);
if (!string.IsNullOrEmpty(orgPath)) {
change.PreSize = repo.GetFileSize(revisions[0], orgPath);
} else {
change.PreSize = repo.GetFileSize(revisions[0], path);
}
}
return change;
}
}
}

View file

@ -4,6 +4,7 @@ using System.Diagnostics;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using System.Windows.Threading;
using System.Xml.Serialization;
@ -876,6 +877,20 @@ namespace SourceGit.Git {
if (errs != null) App.RaiseError(errs);
return blame;
}
/// <summary>
/// Get file size.
/// </summary>
/// <param name="sha"></param>
/// <param name="path"></param>
/// <returns></returns>
public long GetFileSize(string sha, string path) {
long size = 0;
RunCommand($"cat-file -s {sha}:\"{path}\"", line => {
if (!long.TryParse(line, out size)) size = 0;
});
return size;
}
#endregion
#region METHOD_GITFLOW

View file

@ -32,6 +32,7 @@
<Geometry x:Key="Icon.File">M958.656 320H960v639.936A64 64 0 0 1 896.128 1024H191.936A63.872 63.872 0 0 1 128 959.936V64.064A64 64 0 0 1 191.936 0H640v320.96h319.616L958.656 320zM320 544c0 17.152 14.464 32 32.192 32h383.552A32.384 32.384 0 0 0 768 544c0-17.152-14.464-32-32.256-32H352.192A32.448 32.448 0 0 0 320 544z m0 128c0 17.152 14.464 32 32.192 32h383.552a32.384 32.384 0 0 0 32.256-32c0-17.152-14.464-32-32.256-32H352.192a32.448 32.448 0 0 0-32.192 32z m0 128c0 17.152 14.464 32 32.192 32h383.552a32.384 32.384 0 0 0 32.256-32c0-17.152-14.464-32-32.256-32H352.192a32.448 32.448 0 0 0-32.192 32z</Geometry>
<Geometry x:Key="Icon.Diff">M854.2 306.6L611.3 72.9c-6-5.7-13.9-8.9-22.2-8.9H296c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8h277l219 210.6V824c0 4.4 3.6 8 8 8h56c4.4 0 8-3.6 8-8V329.6c0-8.7-3.5-17-9.8-23zM553.4 201.4c-6-6-14.1-9.4-22.6-9.4H192c-17.7 0-32 14.3-32 32v704c0 17.7 14.3 32 32 32h512c17.7 0 32-14.3 32-32V397.3c0-8.5-3.4-16.6-9.4-22.6L553.4 201.4zM568 753c0 3.8-3.4 7-7.5 7h-225c-4.1 0-7.5-3.2-7.5-7v-42c0-3.8 3.4-7 7.5-7h225c4.1 0 7.5 3.2 7.5 7v42z m0-220c0 3.8-3.4 7-7.5 7H476v84.9c0 3.9-3.1 7.1-7 7.1h-42c-3.8 0-7-3.2-7-7.1V540h-84.5c-4.1 0-7.5-3.2-7.5-7v-42c0-3.9 3.4-7 7.5-7H420v-84.9c0-3.9 3.2-7.1 7-7.1h42c3.9 0 7 3.2 7 7.1V484h84.5c4.1 0 7.5 3.1 7.5 7v42z</Geometry>
<Geometry x:Key="Icon.Filter">M599.22969 424.769286 599.22969 657.383158 424.769286 831.844585 424.769286 424.769286 192.155415 192.155415 831.844585 192.155415Z</Geometry>
<Geometry x:Key="Icon.Binary">M71.111111 1024V0h661.333333L952.888889 219.420444V1024H71.111111z m808.305778-731.420444l-220.444445-219.448889H144.583111V950.897778h734.833778V292.579556zM438.528 512h-220.444444V219.420444h220.444444V512z m-73.500444-219.420444H291.555556v146.289777h73.472v-146.289777z m0 512h73.500444v73.130666h-220.444444v-73.130666H291.555556v-146.289778H218.083556V585.102222h146.944v219.448889z m293.944888-365.710223h73.472V512H512v-73.130667h73.472v-146.289777H512V219.420444h146.972444v219.448889z m73.472 438.840889H512V585.130667h220.444444v292.579555z m-73.472-219.420444h-73.500444v146.289778h73.500444v-146.289778z</Geometry>
<Geometry x:Key="Icon.Vertical">M1024 1024H0V0h1024v1024z m-64-64V320H320V256h640V64H64v896h192V64h64v896z</Geometry>
<Geometry x:Key="Icon.Horizontal">M81.92 81.92v860.16h860.16V81.92H81.92z m802.304 57.856V322.56H139.776V139.776h744.448z m-744.448 240.64H322.56v503.808H139.776V380.416z m240.128 503.808V380.416h504.32v503.808H379.904z</Geometry>

View file

@ -4,7 +4,6 @@ using System.IO;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Navigation;
@ -197,7 +196,11 @@ namespace SourceGit.UI {
start = "4b825dc642cb6eb9a060e54bf8d69288fbee4904";
}
diffViewer.Diff(repo, $"{start} {commit.SHA}", node.FilePath, node.OriginalPath);
diffViewer.Diff(repo, new DiffViewer.Option() {
RevisionRange = new string[] { start, commit.SHA },
Path = node.FilePath,
OrgPath = node.OriginalPath
});
}
private void ChangeListSelectionChanged(object sender, SelectionChangedEventArgs e) {
@ -211,7 +214,11 @@ namespace SourceGit.UI {
start = "4b825dc642cb6eb9a060e54bf8d69288fbee4904";
}
diffViewer.Diff(repo, $"{start} {commit.SHA}", change.Path, change.OriginalPath);
diffViewer.Diff(repo, new DiffViewer.Option() {
RevisionRange = new string[] { start, commit.SHA },
Path = change.Path,
OrgPath = change.OriginalPath
});
}
private void ChangeListContextMenuOpening(object sender, ContextMenuEventArgs e) {

View file

@ -128,6 +128,29 @@
</Grid>
</Grid>
<Border x:Name="sizer" Grid.Row="1" ClipToBounds="True" Background="{StaticResource Brush.BG3}" Visibility="Collapsed">
<StackPanel Orientation="Vertical" VerticalAlignment="Center">
<Label 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}"/>
<Grid Margin="0,16,0,0" HorizontalAlignment="Center" TextElement.FontSize="18" TextElement.FontWeight="UltraBold">
<Grid.RowDefinitions>
<RowDefinition Height="32"/>
<RowDefinition Height="32"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="64"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Label Grid.Row="0" Grid.Column="0" Content="OLD :" Foreground="{StaticResource Brush.FG2}"/>
<Label Grid.Row="0" Grid.Column="1" x:Name="txtOldSize" Foreground="{StaticResource Brush.FG2}" HorizontalAlignment="Right"/>
<Label Grid.Row="1" Grid.Column="0" Content="NEW :" Foreground="{StaticResource Brush.FG2}"/>
<Label Grid.Row="1" Grid.Column="1" x:Name="txtNewSize" Foreground="{StaticResource Brush.FG2}" HorizontalAlignment="Right"/>
</Grid>
</StackPanel>
</Border>
<Border x:Name="mask" Grid.RowSpan="2" Background="{StaticResource Brush.BG3}" Visibility="Collapsed">
<StackPanel Orientation="Vertical" VerticalAlignment="Center" Opacity=".2">
<Path Width="64" Height="64" Style="{StaticResource Style.Icon}" Data="{StaticResource Icon.Diff}"/>

View file

@ -15,6 +15,16 @@ namespace SourceGit.UI {
public partial class DiffViewer : UserControl {
private double minWidth = 0;
/// <summary>
/// Diff options.
/// </summary>
public class Option {
public string[] RevisionRange = new string[] { };
public string Path = "";
public string OrgPath = null;
public string ExtraArgs = "";
}
/// <summary>
/// Constructor
/// </summary>
@ -28,26 +38,33 @@ namespace SourceGit.UI {
/// </summary>
public void Reset() {
mask.Visibility = Visibility.Visible;
sizer.Visibility = Visibility.Collapsed;
}
/// <summary>
/// Diff with options.
/// </summary>
/// <param name="repo"></param>
/// <param name="options"></param>
/// <param name="path"></param>
/// <param name="orgPath"></param>
public void Diff(Git.Repository repo, string options, string path, string orgPath = null) {
SetTitle(path, orgPath);
/// <param name="opts"></param>
public void Diff(Git.Repository repo, Option opts) {
SetTitle(opts.Path, opts.OrgPath);
loading.Visibility = Visibility.Visible;
sizer.Visibility = Visibility.Collapsed;
Task.Run(() => {
var args = $"{options} -- ";
if (!string.IsNullOrEmpty(orgPath)) args += $"{orgPath} ";
args += $"\"{path}\"";
var args = $"{opts.ExtraArgs} ";
if (opts.RevisionRange.Length > 0) args += $"{opts.RevisionRange[0]} ";
if (opts.RevisionRange.Length > 1) args += $"{opts.RevisionRange[1]} -- ";
if (!string.IsNullOrEmpty(opts.OrgPath)) args += $"\"{opts.OrgPath}\" ";
args += $"\"{opts.Path}\"";
var rs = Git.Diff.Run(repo, args);
if (rs.IsBinary) {
SetSizeChangeData(Git.Diff.GetSizeChange(repo, opts.RevisionRange, opts.Path, opts.OrgPath));
} else {
SetData(rs);
}
});
}
@ -67,6 +84,20 @@ 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;
mask.Visibility = Visibility.Collapsed;
sizer.Visibility = Visibility.Visible;
txtNewSize.Content = $"{bc.Size} Bytes";
txtOldSize.Content = $"{bc.PreSize} Bytes";
});
}
/// <summary>
/// Show diff content.
/// </summary>

View file

@ -102,7 +102,10 @@ namespace SourceGit.UI {
var start = $"{commit.SHA}^";
if (commit.Parents.Count == 0) start = "4b825dc642cb6eb9a060e54bf8d69288fbee4904";
diff.Diff(repo, $"{start} {commit.SHA}", file);
diff.Diff(repo, new DiffViewer.Option() {
RevisionRange = new string[] { start, commit.SHA },
Path = file
});
}
/// <summary>

View file

@ -82,7 +82,11 @@ namespace SourceGit.UI {
var change = e.AddedItems[0] as Git.Change;
if (change == null) return;
diff.Diff(repo, $"{selectedStash}^ {selectedStash}", change.Path, change.OriginalPath);
diff.Diff(repo, new DiffViewer.Option() {
RevisionRange = new string[] { $"{selectedStash}^", selectedStash },
Path = change.Path,
OrgPath = change.OriginalPath
});
}
/// <summary>

View file

@ -133,15 +133,18 @@ namespace SourceGit.UI {
return;
}
DiffViewer.Option opt;
switch (node.Change.WorkTree) {
case Git.Change.Status.Added:
case Git.Change.Status.Untracked:
diffViewer.Diff(Repo, "--no-index", node.FilePath, "/dev/null");
opt = new DiffViewer.Option() { ExtraArgs = "--no-index", Path = node.FilePath, OrgPath = "/dev/null" };
break;
default:
diffViewer.Diff(Repo, "", node.FilePath, node.Change.OriginalPath);
opt = new DiffViewer.Option() { Path = node.FilePath, OrgPath = node.Change.OriginalPath };
break;
}
diffViewer.Diff(Repo, opt);
}
private void UnstagedListSelectionChanged(object sender, SelectionChangedEventArgs e) {
@ -161,15 +164,18 @@ namespace SourceGit.UI {
return;
}
DiffViewer.Option opt;
switch (change.WorkTree) {
case Git.Change.Status.Added:
case Git.Change.Status.Untracked:
diffViewer.Diff(Repo, "--no-index", change.Path, "/dev/null");
opt = new DiffViewer.Option() { ExtraArgs = "--no-index", Path = change.Path, OrgPath = "/dev/null" };
break;
default:
diffViewer.Diff(Repo, "", change.Path, change.OriginalPath);
opt = new DiffViewer.Option() { Path = change.Path, OrgPath = change.OriginalPath };
break;
}
diffViewer.Diff(Repo, opt);
}
private void SaveAsPatchFromUnstagedChanges(string path, List<Git.Change> changes) {
@ -507,7 +513,11 @@ namespace SourceGit.UI {
if (!node.IsFile) return;
mergePanel.Visibility = Visibility.Collapsed;
diffViewer.Diff(Repo, "--cached", node.FilePath, node.Change.OriginalPath);
diffViewer.Diff(Repo, new DiffViewer.Option() {
ExtraArgs = "--cached",
Path = node.FilePath,
OrgPath = node.Change.OriginalPath
});
e.Handled = true;
}
@ -524,7 +534,11 @@ namespace SourceGit.UI {
var change = selected[0] as Git.Change;
mergePanel.Visibility = Visibility.Collapsed;
diffViewer.Diff(Repo, "--cached", change.Path, change.OriginalPath);
diffViewer.Diff(Repo, new DiffViewer.Option() {
ExtraArgs = "--cached",
Path = change.Path,
OrgPath = change.OriginalPath
});
e.Handled = true;
}