forked from sourcegit-scm/sourcegit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDiffContext.cs
More file actions
178 lines (154 loc) · 5.82 KB
/
Copy pathDiffContext.cs
File metadata and controls
178 lines (154 loc) · 5.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using Avalonia;
using Avalonia.Media.Imaging;
using Avalonia.Threading;
using CommunityToolkit.Mvvm.ComponentModel;
namespace SourceGit.ViewModels
{
public class DiffContext : ObservableObject
{
public string RepositoryPath
{
get => _repo;
}
public Models.Change WorkingCopyChange
{
get => _option.WorkingCopyChange;
}
public bool IsUnstaged
{
get => _option.IsUnstaged;
}
public string FilePath
{
get => _option.Path;
}
public bool IsOrgFilePathVisible
{
get => !string.IsNullOrWhiteSpace(_option.OrgPath) && _option.OrgPath != "/dev/null";
}
public string OrgFilePath
{
get => _option.OrgPath;
}
public bool IsLoading
{
get => _isLoading;
private set => SetProperty(ref _isLoading, value);
}
public bool IsTextDiff
{
get => _isTextDiff;
private set => SetProperty(ref _isTextDiff, value);
}
public object Content
{
get => _content;
private set => SetProperty(ref _content, value);
}
public Vector SyncScrollOffset
{
get => _syncScrollOffset;
set => SetProperty(ref _syncScrollOffset, value);
}
public DiffContext(string repo, Models.DiffOption option, DiffContext previous = null)
{
_repo = repo;
_option = option;
if (previous != null)
{
_isTextDiff = previous._isTextDiff;
_content = previous._content;
}
OnPropertyChanged(nameof(FilePath));
OnPropertyChanged(nameof(IsOrgFilePathVisible));
OnPropertyChanged(nameof(OrgFilePath));
Task.Run(() =>
{
var latest = new Commands.Diff(repo, option).Result();
var rs = null as object;
if (latest.TextDiff != null)
{
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(oldPath);
if (IMG_EXTS.Contains(ext))
{
var imgDiff = new Models.ImageDiff();
if (option.Revisions.Count == 2)
{
imgDiff.Old = Commands.GetImageFileAsBitmap.Run(repo, option.Revisions[0], oldPath);
imgDiff.New = Commands.GetImageFileAsBitmap.Run(repo, option.Revisions[1], oldPath);
}
else
{
var fullPath = Path.Combine(repo, _option.Path);
imgDiff.Old = Commands.GetImageFileAsBitmap.Run(repo, "HEAD", oldPath);
imgDiff.New = File.Exists(fullPath) ? new Bitmap(fullPath) : null;
}
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();
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(() =>
{
Content = rs;
IsTextDiff = latest.TextDiff != null;
IsLoading = false;
});
});
}
public async void OpenExternalMergeTool()
{
var type = Preference.Instance.ExternalMergeToolType;
var exec = Preference.Instance.ExternalMergeToolPath;
var tool = Models.ExternalMergeTools.Supported.Find(x => x.Type == type);
if (tool == null || !File.Exists(exec))
{
App.RaiseException(_repo, "Invalid merge tool in preference setting!");
return;
}
var args = tool.Type != 0 ? tool.DiffCmd : Preference.Instance.ExternalMergeToolDiffCmd;
await Task.Run(() => Commands.MergeTool.OpenForDiff(_repo, exec, args, _option));
}
private static readonly HashSet<string> IMG_EXTS = new HashSet<string>()
{
".ico", ".bmp", ".jpg", ".png", ".jpeg"
};
private readonly string _repo = string.Empty;
private readonly Models.DiffOption _option = null;
private bool _isLoading = true;
private bool _isTextDiff = false;
private object _content = null;
private Vector _syncScrollOffset = Vector.Zero;
}
}