Skip to content

Commit 7043a22

Browse files
committed
feature: allows to compare selected tag with current HEAD
Signed-off-by: leo <[email protected]>
1 parent bfee290 commit 7043a22

File tree

7 files changed

+71
-33
lines changed

7 files changed

+71
-33
lines changed

src/Resources/Locales/en_US.axaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -853,6 +853,7 @@
853853
<x:String x:Key="Text.Sure" xml:space="preserve">OK</x:String>
854854
<x:String x:Key="Text.Tag.Tagger" xml:space="preserve">TAGGER</x:String>
855855
<x:String x:Key="Text.Tag.Time" xml:space="preserve">TIME</x:String>
856+
<x:String x:Key="Text.TagCM.CompareWithHead" xml:space="preserve">Compare with HEAD</x:String>
856857
<x:String x:Key="Text.TagCM.Copy.Message" xml:space="preserve">Message</x:String>
857858
<x:String x:Key="Text.TagCM.Copy.Name" xml:space="preserve">Name</x:String>
858859
<x:String x:Key="Text.TagCM.Copy.Tagger" xml:space="preserve">Tagger</x:String>

src/Resources/Locales/zh_CN.axaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -857,6 +857,7 @@
857857
<x:String x:Key="Text.Sure" xml:space="preserve">确 定</x:String>
858858
<x:String x:Key="Text.Tag.Tagger" xml:space="preserve">创建者</x:String>
859859
<x:String x:Key="Text.Tag.Time" xml:space="preserve">创建时间</x:String>
860+
<x:String x:Key="Text.TagCM.CompareWithHead" xml:space="preserve">与当前 HEAD 比较</x:String>
860861
<x:String x:Key="Text.TagCM.Copy.Message" xml:space="preserve">标签信息</x:String>
861862
<x:String x:Key="Text.TagCM.Copy.Name" xml:space="preserve">标签名</x:String>
862863
<x:String x:Key="Text.TagCM.Copy.Tagger" xml:space="preserve">创建者</x:String>

src/Resources/Locales/zh_TW.axaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -857,6 +857,7 @@
857857
<x:String x:Key="Text.Sure" xml:space="preserve">確 定</x:String>
858858
<x:String x:Key="Text.Tag.Tagger" xml:space="preserve">建立者</x:String>
859859
<x:String x:Key="Text.Tag.Time" xml:space="preserve">建立時間</x:String>
860+
<x:String x:Key="Text.TagCM.CompareWithHead" xml:space="preserve">與目前 HEAD 比較</x:String>
860861
<x:String x:Key="Text.TagCM.Copy.Message" xml:space="preserve">標籤訊息</x:String>
861862
<x:String x:Key="Text.TagCM.Copy.Name" xml:space="preserve">標籤名稱</x:String>
862863
<x:String x:Key="Text.TagCM.Copy.Tagger" xml:space="preserve">建立者</x:String>

src/ViewModels/BranchCompare.cs

Lines changed: 51 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,22 @@ namespace SourceGit.ViewModels
88
{
99
public class BranchCompare : ObservableObject
1010
{
11-
public string RepositoryPath
12-
{
13-
get => _repo;
14-
}
15-
1611
public bool IsLoading
1712
{
1813
get => _isLoading;
1914
private set => SetProperty(ref _isLoading, value);
2015
}
2116

22-
public Models.Branch Base
17+
public string BaseName
2318
{
24-
get => _based;
25-
private set => SetProperty(ref _based, value);
19+
get => _baseName;
20+
private set => SetProperty(ref _baseName, value);
2621
}
2722

28-
public Models.Branch To
23+
public string ToName
2924
{
30-
get => _to;
31-
private set => SetProperty(ref _to, value);
25+
get => _toName;
26+
private set => SetProperty(ref _toName, value);
3227
}
3328

3429
public Models.Commit BaseHead
@@ -63,7 +58,7 @@ public List<Models.Change> SelectedChanges
6358
if (SetProperty(ref _selectedChanges, value))
6459
{
6560
if (value is { Count: 1 })
66-
DiffContext = new DiffContext(_repo, new Models.DiffOption(_based.Head, _to.Head, value[0]), _diffContext);
61+
DiffContext = new DiffContext(_repo, new Models.DiffOption(_based, _to, value[0]), _diffContext);
6762
else
6863
DiffContext = null;
6964
}
@@ -86,11 +81,13 @@ public DiffContext DiffContext
8681
private set => SetProperty(ref _diffContext, value);
8782
}
8883

89-
public BranchCompare(string repo, Models.Branch baseBranch, Models.Branch toBranch)
84+
public BranchCompare(string repo, object based, object to)
9085
{
9186
_repo = repo;
92-
_based = baseBranch;
93-
_to = toBranch;
87+
_based = GetSHA(based);
88+
_to = GetSHA(to);
89+
_baseName = GetName(based);
90+
_toName = GetName(to);
9491

9592
Refresh();
9693
}
@@ -113,10 +110,8 @@ public void NavigateTo(string commitSHA)
113110

114111
public void Swap()
115112
{
116-
(Base, To) = (_to, _based);
117-
118-
VisibleChanges = [];
119-
SelectedChanges = [];
113+
(_based, _to) = (_to, _based);
114+
(BaseName, ToName) = (_toName, _baseName);
120115

121116
if (_baseHead != null)
122117
(BaseHead, ToHead) = (_toHead, _baseHead);
@@ -134,26 +129,33 @@ public string GetAbsPath(string path)
134129
return Native.OS.GetAbsPath(_repo, path);
135130
}
136131

132+
public void OpenInExternalDiffTool(Models.Change change)
133+
{
134+
new Commands.DiffTool(_repo, new Models.DiffOption(_based, _to, change)).Open();
135+
}
136+
137137
public async Task SaveChangesAsPatchAsync(List<Models.Change> changes, string saveTo)
138138
{
139-
var succ = await Commands.SaveChangesAsPatch.ProcessRevisionCompareChangesAsync(_repo, changes, _based.Head, _to.Head, saveTo);
139+
var succ = await Commands.SaveChangesAsPatch.ProcessRevisionCompareChangesAsync(_repo, changes, _based, _to, saveTo);
140140
if (succ)
141141
App.SendNotification(_repo, App.Text("SaveAsPatchSuccess"));
142142
}
143143

144144
private void Refresh()
145145
{
146146
IsLoading = true;
147+
VisibleChanges = [];
148+
SelectedChanges = [];
147149

148150
Task.Run(async () =>
149151
{
150152
if (_baseHead == null)
151153
{
152-
var baseHead = await new Commands.QuerySingleCommit(_repo, _based.Head)
154+
var baseHead = await new Commands.QuerySingleCommit(_repo, _based)
153155
.GetResultAsync()
154156
.ConfigureAwait(false);
155157

156-
var toHead = await new Commands.QuerySingleCommit(_repo, _to.Head)
158+
var toHead = await new Commands.QuerySingleCommit(_repo, _to)
157159
.GetResultAsync()
158160
.ConfigureAwait(false);
159161

@@ -164,7 +166,7 @@ private void Refresh()
164166
});
165167
}
166168

167-
_changes = await new Commands.CompareRevisions(_repo, _based.Head, _to.Head)
169+
_changes = await new Commands.CompareRevisions(_repo, _based, _to)
168170
.ReadAsync()
169171
.ConfigureAwait(false);
170172

@@ -215,10 +217,34 @@ private void RefreshVisible()
215217
}
216218
}
217219

220+
private string GetName(object obj)
221+
{
222+
return obj switch
223+
{
224+
Models.Branch b => b.FriendlyName,
225+
Models.Tag t => t.Name,
226+
Models.Commit c => c.SHA.Substring(0, 10),
227+
_ => "HEAD",
228+
};
229+
}
230+
231+
private string GetSHA(object obj)
232+
{
233+
return obj switch
234+
{
235+
Models.Branch b => b.Head,
236+
Models.Tag t => t.SHA,
237+
Models.Commit c => c.SHA,
238+
_ => "HEAD",
239+
};
240+
}
241+
218242
private string _repo;
219243
private bool _isLoading = true;
220-
private Models.Branch _based = null;
221-
private Models.Branch _to = null;
244+
private string _based = string.Empty;
245+
private string _to = string.Empty;
246+
private string _baseName = string.Empty;
247+
private string _toName = string.Empty;
222248
private Models.Commit _baseHead = null;
223249
private Models.Commit _toHead = null;
224250
private int _totalChanges = 0;

src/Views/BranchCompare.axaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
User="{Binding BaseHead.Author}"/>
4949
<TextBlock Grid.Column="1" Text="{Binding BaseHead.Author.Name}" Margin="8,0,0,0"/>
5050
<Border Grid.Column="2" Background="{DynamicResource Brush.Accent}" CornerRadius="4">
51-
<TextBlock Text="{Binding Base.FriendlyName}" Margin="4,0" Foreground="#FFDDDDDD"/>
51+
<TextBlock Text="{Binding BaseName}" Margin="4,0" Foreground="#FFDDDDDD"/>
5252
</Border>
5353
<TextBlock Grid.Column="3" Text="{Binding BaseHead.SHA, Converter={x:Static c:StringConverters.ToShortSHA}}" Foreground="DarkOrange" Margin="8,0,0,0" TextDecorations="Underline" Cursor="Hand" PointerPressed="OnPressedSHA"/>
5454
<TextBlock Grid.Column="4" Text="{Binding BaseHead.CommitterTimeStr}" Foreground="{DynamicResource Brush.FG2}" Margin="8,0,0,0"/>
@@ -72,7 +72,7 @@
7272
User="{Binding ToHead.Author}"/>
7373
<TextBlock Grid.Column="1" Text="{Binding ToHead.Author.Name}" Margin="8,0,0,0"/>
7474
<Border Grid.Column="2" Background="{DynamicResource Brush.Accent}" CornerRadius="4">
75-
<TextBlock Text="{Binding To.FriendlyName}" Margin="4,0" Foreground="#FFDDDDDD"/>
75+
<TextBlock Text="{Binding ToName}" Margin="4,0" Foreground="#FFDDDDDD"/>
7676
</Border>
7777
<TextBlock Grid.Column="3" Text="{Binding ToHead.SHA, Converter={x:Static c:StringConverters.ToShortSHA}}" Foreground="DarkOrange" Margin="8,0,0,0" TextDecorations="Underline" Cursor="Hand" PointerPressed="OnPressedSHA"/>
7878
<TextBlock Grid.Column="4" Text="{Binding ToHead.CommitterTimeStr}" Foreground="{DynamicResource Brush.FG2}" Margin="8,0,0,0"/>

src/Views/BranchCompare.axaml.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ private void OnChangeContextRequested(object sender, ContextRequestedEventArgs e
2121
sender is ChangeCollectionView view)
2222
{
2323
var menu = new ContextMenu();
24-
var repo = vm.RepositoryPath;
2524

2625
var patch = new MenuItem();
2726
patch.Header = App.Text("FileCM.SaveAsPatch");
@@ -48,7 +47,7 @@ private void OnChangeContextRequested(object sender, ContextRequestedEventArgs e
4847
}
4948
catch (Exception exception)
5049
{
51-
App.RaiseException(repo, $"Failed to save as patch: {exception.Message}");
50+
App.RaiseException(null, $"Failed to save as patch: {exception.Message}");
5251
}
5352

5453
e.Handled = true;
@@ -63,14 +62,14 @@ private void OnChangeContextRequested(object sender, ContextRequestedEventArgs e
6362
openWithMerger.Tag = OperatingSystem.IsMacOS() ? "⌘+⇧+D" : "Ctrl+Shift+D";
6463
openWithMerger.Click += (_, ev) =>
6564
{
66-
new Commands.DiffTool(repo, new Models.DiffOption(vm.Base.Head, vm.To.Head, change)).Open();
65+
vm.OpenInExternalDiffTool(change);
6766
ev.Handled = true;
6867
};
6968
menu.Items.Add(openWithMerger);
7069

7170
if (change.Index != Models.ChangeState.Deleted)
7271
{
73-
var full = Path.GetFullPath(Path.Combine(repo, change.Path));
72+
var full = vm.GetAbsPath(change.Path);
7473
var explore = new MenuItem();
7574
explore.Header = App.Text("RevealFile");
7675
explore.Icon = App.CreateMenuIcon("Icons.Explore");
@@ -99,7 +98,7 @@ private void OnChangeContextRequested(object sender, ContextRequestedEventArgs e
9998
copyFullPath.Tag = OperatingSystem.IsMacOS() ? "⌘+⇧+C" : "Ctrl+Shift+C";
10099
copyFullPath.Click += async (_, ev) =>
101100
{
102-
await App.CopyTextAsync(Native.OS.GetAbsPath(repo, change.Path));
101+
await App.CopyTextAsync(vm.GetAbsPath(change.Path));
103102
ev.Handled = true;
104103
};
105104

@@ -133,7 +132,7 @@ private void OnChangeContextRequested(object sender, ContextRequestedEventArgs e
133132
{
134133
var builder = new StringBuilder();
135134
foreach (var c in selected)
136-
builder.AppendLine(Native.OS.GetAbsPath(repo, c.Path));
135+
builder.AppendLine(vm.GetAbsPath(c.Path));
137136

138137
await App.CopyTextAsync(builder.ToString());
139138
ev.Handled = true;

src/Views/TagsView.axaml.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,14 @@ private void OnTagsContextMenuRequested(object sender, ContextRequestedEventArgs
257257
ev.Handled = true;
258258
};
259259

260+
var compareWithHead = new MenuItem();
261+
compareWithHead.Header = App.Text("TagCM.CompareWithHead");
262+
compareWithHead.Icon = App.CreateMenuIcon("Icons.Compare");
263+
compareWithHead.Click += (_, _) =>
264+
{
265+
App.ShowWindow(new ViewModels.BranchCompare(repo.FullPath, tag, repo.CurrentBranch));
266+
};
267+
260268
var archive = new MenuItem();
261269
archive.Icon = App.CreateMenuIcon("Icons.Archive");
262270
archive.Header = App.Text("Archive");
@@ -273,6 +281,8 @@ private void OnTagsContextMenuRequested(object sender, ContextRequestedEventArgs
273281
menu.Items.Add(pushTag);
274282
menu.Items.Add(deleteTag);
275283
menu.Items.Add(new MenuItem() { Header = "-" });
284+
menu.Items.Add(compareWithHead);
285+
menu.Items.Add(new MenuItem() { Header = "-" });
276286
menu.Items.Add(archive);
277287
menu.Items.Add(new MenuItem() { Header = "-" });
278288

0 commit comments

Comments
 (0)