Skip to content

Commit 8aa1690

Browse files
committed
Adding checkout commit menu and dialog
1 parent b0c0034 commit 8aa1690

File tree

7 files changed

+181
-0
lines changed

7 files changed

+181
-0
lines changed

src/Resources/Locales/en_US.axaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,11 @@
5353
<x:String x:Key="Text.ChangeDisplayMode.List" xml:space="preserve">Show as List</x:String>
5454
<x:String x:Key="Text.ChangeDisplayMode.Tree" xml:space="preserve">Show as Tree</x:String>
5555
<x:String x:Key="Text.Checkout" xml:space="preserve">Checkout Branch</x:String>
56+
<x:String x:Key="Text.Checkout.Commit" xml:space="preserve">Checkout Commit</x:String>
57+
<x:String x:Key="Text.Checkout.Commit.Warning" xml:space="preserve">Warning: By doing a commit checkout, your Head will be detached</x:String>
5658
<x:String x:Key="Text.Checkout.Target" xml:space="preserve">Branch :</x:String>
59+
<x:String x:Key="Text.Checkout.CommitTarget.Sha" xml:space="preserve">Commit SHA :</x:String>
60+
<x:String x:Key="Text.Checkout.CommitTarget.ShortSha" xml:space="preserve">Commit Short SHA :</x:String>
5761
<x:String x:Key="Text.Checkout.LocalChanges" xml:space="preserve">Local Changes :</x:String>
5862
<x:String x:Key="Text.Checkout.LocalChanges.StashAndReply" xml:space="preserve">Stash &amp; Reapply</x:String>
5963
<x:String x:Key="Text.Checkout.LocalChanges.Discard" xml:space="preserve">Discard</x:String>
@@ -73,6 +77,7 @@
7377
<x:String x:Key="Text.Close" xml:space="preserve">CLOSE</x:String>
7478
<x:String x:Key="Text.CommitCM.CherryPick" xml:space="preserve">Cherry-Pick This Commit</x:String>
7579
<x:String x:Key="Text.CommitCM.CopySHA" xml:space="preserve">Copy SHA</x:String>
80+
<x:String x:Key="Text.CommitCM.Checkout" xml:space="preserve">Checkout commit${0}</x:String>
7681
<x:String x:Key="Text.CommitCM.Rebase" xml:space="preserve">Rebase${0}$to Here</x:String>
7782
<x:String x:Key="Text.CommitCM.Reset" xml:space="preserve">Reset${0}$to Here</x:String>
7883
<x:String x:Key="Text.CommitCM.Revert" xml:space="preserve">Revert Commit</x:String>

src/SourceGit.csproj

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,11 @@
4444
<TrimmerRootAssembly Include="SourceGit" />
4545
<TrimmerRootAssembly Include="Avalonia.Themes.Fluent" />
4646
</ItemGroup>
47+
48+
<ItemGroup>
49+
<Compile Update="Views\CheckoutCommit.axaml.cs">
50+
<DependentUpon>CheckoutCommit.axaml</DependentUpon>
51+
<SubType>Code</SubType>
52+
</Compile>
53+
</ItemGroup>
4754
</Project>

src/ViewModels/CheckoutCommit.cs

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
using System.Threading.Tasks;
2+
3+
namespace SourceGit.ViewModels
4+
{
5+
public class CheckoutCommit: Popup
6+
{
7+
public string Commit
8+
{
9+
get;
10+
private set;
11+
}
12+
13+
public bool HasLocalChanges
14+
{
15+
get => _repo.WorkingCopyChangesCount > 0;
16+
}
17+
18+
public bool AutoStash
19+
{
20+
get => _autoStash;
21+
set => SetProperty(ref _autoStash, value);
22+
}
23+
24+
public CheckoutCommit(Repository repo, string commit)
25+
{
26+
_repo = repo;
27+
Commit = commit;
28+
View = new Views.CheckoutCommit() { DataContext = this };
29+
}
30+
31+
public override Task<bool> Sure()
32+
{
33+
_repo.SetWatcherEnabled(false);
34+
ProgressDescription = $"Checkout Commit '{Commit}' ...";
35+
36+
return Task.Run(() =>
37+
{
38+
var needPopStash = false;
39+
if (HasLocalChanges)
40+
{
41+
if (AutoStash)
42+
{
43+
SetProgressDescription("Adding untracked changes ...");
44+
var succ = new Commands.Add(_repo.FullPath).Exec();
45+
if (succ)
46+
{
47+
SetProgressDescription("Stash local changes ...");
48+
succ = new Commands.Stash(_repo.FullPath).Push("CHECKOUT_AUTO_STASH");
49+
}
50+
51+
if (!succ)
52+
{
53+
CallUIThread(() => _repo.SetWatcherEnabled(true));
54+
return false;
55+
}
56+
57+
needPopStash = true;
58+
}
59+
else
60+
{
61+
SetProgressDescription("Discard local changes ...");
62+
Commands.Discard.All(_repo.FullPath);
63+
}
64+
}
65+
66+
SetProgressDescription("Checkout commit ...");
67+
var rs = new Commands.Checkout(_repo.FullPath).Commit(Commit, SetProgressDescription);
68+
69+
if (needPopStash)
70+
{
71+
SetProgressDescription("Re-apply local changes...");
72+
rs = new Commands.Stash(_repo.FullPath).Apply("stash@{0}");
73+
if (rs)
74+
{
75+
rs = new Commands.Stash(_repo.FullPath).Drop("stash@{0}");
76+
}
77+
}
78+
79+
CallUIThread(() => _repo.SetWatcherEnabled(true));
80+
return rs;
81+
});
82+
}
83+
84+
private readonly Repository _repo = null;
85+
private bool _autoStash = true;
86+
}
87+
}

src/ViewModels/Histories.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
using System;
22
using System.Collections;
33
using System.Collections.Generic;
4+
using System.Globalization;
45
using System.Threading.Tasks;
56

67
using Avalonia.Controls;
8+
using Avalonia.Data.Converters;
79
using Avalonia.Platform.Storage;
810
using Avalonia.Threading;
911

@@ -234,6 +236,20 @@ public ContextMenu MakeContextMenu()
234236
e.Handled = true;
235237
};
236238
menu.Items.Add(reset);
239+
240+
var checkoutCommit = new MenuItem();
241+
242+
var shortSha = Converters.StringConverters.ToShortSHA
243+
.Convert(commit.SHA, typeof(string), null, CultureInfo.CurrentCulture);
244+
245+
checkoutCommit.Header = new Views.NameHighlightedTextBlock("CommitCM.Checkout", shortSha);
246+
checkoutCommit.Icon = App.CreateMenuIcon("Icons.Check");
247+
checkoutCommit.Click += (o, e) =>
248+
{
249+
_repo.CheckoutCommit(commit.SHA);
250+
e.Handled = true;
251+
};
252+
menu.Items.Add(checkoutCommit);
237253
}
238254
else
239255
{

src/ViewModels/Repository.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -722,6 +722,14 @@ public void CheckoutLocalBranch(string branch)
722722
PopupHost.ShowAndStartPopup(new Checkout(this, branch));
723723
}
724724

725+
public void CheckoutCommit(string commit)
726+
{
727+
if (!PopupHost.CanCreatePopup())
728+
return;
729+
730+
PopupHost.ShowPopup(new CheckoutCommit(this, commit));
731+
}
732+
725733
public void CreateNewTag()
726734
{
727735
var current = Branches.Find(x => x.IsCurrent);

src/Views/CheckoutCommit.axaml

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<UserControl xmlns="https://github.com/avaloniaui"
2+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
3+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
4+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
5+
xmlns:vm="using:SourceGit.ViewModels"
6+
xmlns:c="clr-namespace:SourceGit.Converters"
7+
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
8+
x:Class="SourceGit.Views.CheckoutCommit"
9+
x:DataType="vm:CheckoutCommit">
10+
<StackPanel Orientation="Vertical" Margin="8,0">
11+
<TextBlock FontSize="18"
12+
Classes="bold"
13+
Text="{DynamicResource Text.Checkout.Commit}"/>
14+
15+
<TextBlock FontSize="14"
16+
TextWrapping="Wrap"
17+
Classes="italic"
18+
Text="{DynamicResource Text.Checkout.Commit.Warning}"/>
19+
20+
<TextBlock TextWrapping="Wrap" Margin="0,16,0,0">
21+
<TextBlock Classes="bold" Grid.Row="0" Grid.Column="0"
22+
Text="{DynamicResource Text.Checkout.CommitTarget.Sha}"/>
23+
<TextBlock TextWrapping="Wrap" Text="{Binding Commit}"/>
24+
(<TextBlock Classes="italic" TextWrapping="Wrap" Text="{Binding Commit, Converter={x:Static c:StringConverters.ToShortSHA}}"/>)
25+
</TextBlock>
26+
27+
<StackPanel Margin="0, 26, 0, 0" Grid.Row="1" Grid.Column="1" Orientation="Vertical" IsVisible="{Binding HasLocalChanges}">
28+
<TextBlock Grid.Row="1" Grid.Column="0"
29+
HorizontalAlignment="Left" VerticalAlignment="Center"
30+
Margin="0,0,8,0"
31+
Text="{DynamicResource Text.Checkout.LocalChanges}"/>
32+
33+
<StackPanel Margin="0, 13, 0, 0" Grid.Row="1" Grid.Column="1" Orientation="Horizontal">
34+
<RadioButton Content="{DynamicResource Text.Checkout.LocalChanges.StashAndReply}"
35+
GroupName="LocalChanges"
36+
IsChecked="{Binding AutoStash, Mode=TwoWay}"/>
37+
<RadioButton Content="{DynamicResource Text.Checkout.LocalChanges.Discard}"
38+
GroupName="LocalChanges"
39+
Margin="8,0,0,0"/>
40+
</StackPanel>
41+
</StackPanel>
42+
43+
44+
</StackPanel>
45+
</UserControl>

src/Views/CheckoutCommit.axaml.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using Avalonia.Controls;
2+
3+
namespace SourceGit.Views
4+
{
5+
public partial class CheckoutCommit : UserControl
6+
{
7+
public bool HasLocalChanges;
8+
public CheckoutCommit()
9+
{
10+
InitializeComponent();
11+
}
12+
}
13+
}

0 commit comments

Comments
 (0)