Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add checkout commit (#144) #146

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Commands/Checkout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ public Checkout(string repo)
Context = repo;
}

public bool Branch(string branch, Action<string> onProgress)
public bool Target(string target, Action<string> onProgress)
{
Args = $"checkout --progress {branch}";
Args = $"checkout --progress {target}";
TraitErrorAsOutput = true;
_outputHandler = onProgress;
return Exec();
Expand Down
30 changes: 30 additions & 0 deletions src/Converters/CheckoutTargetTypeConverters.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using Avalonia.Controls;
using Avalonia.Data.Converters;
using Avalonia.Media;
using SourceGit.ViewModels;

namespace SourceGit.Converters
{
public class CheckoutTargetTypeConverters
{
public static readonly FuncValueConverter<CheckoutTargetType, StreamGeometry> ToIcon =
new(x =>
x == CheckoutTargetType.Branch
? App.Current?.FindResource("Icons.Branch") as StreamGeometry
: App.Current?.FindResource("Icons.Commit") as StreamGeometry);

public static readonly FuncValueConverter<CheckoutTargetType, string> ToTitle =
new(x =>
x == CheckoutTargetType.Branch ? App.Text("CheckoutBranch") : App.Text("CheckoutCommit"));

public static readonly FuncValueConverter<CheckoutTargetType, string> ToTarget =
new(x =>
x == CheckoutTargetType.Branch ? App.Text("Checkout.TargetBranch") : App.Text("Checkout.TargetCommit"));

public static readonly FuncValueConverter<CheckoutTargetType, bool> IsBranch =
new(x => x == CheckoutTargetType.Branch);

public static readonly FuncValueConverter<CheckoutTargetType, bool> IsCommit =
new(x => x == CheckoutTargetType.Commit);
}
}
7 changes: 5 additions & 2 deletions src/Resources/Locales/en_US.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,10 @@
<x:String x:Key="Text.ChangeDisplayMode.Grid" xml:space="preserve">Show as Grid</x:String>
<x:String x:Key="Text.ChangeDisplayMode.List" xml:space="preserve">Show as List</x:String>
<x:String x:Key="Text.ChangeDisplayMode.Tree" xml:space="preserve">Show as Tree</x:String>
<x:String x:Key="Text.Checkout" xml:space="preserve">Checkout Branch</x:String>
<x:String x:Key="Text.Checkout.Target" xml:space="preserve">Branch :</x:String>
<x:String x:Key="Text.CheckoutBranch" xml:space="preserve">Checkout Branch</x:String>
<x:String x:Key="Text.CheckoutCommit" xml:space="preserve">Checkout Commit</x:String>
<x:String x:Key="Text.Checkout.TargetBranch" xml:space="preserve">Branch :</x:String>
<x:String x:Key="Text.Checkout.TargetCommit" xml:space="preserve">Commit :</x:String>
<x:String x:Key="Text.Checkout.LocalChanges" xml:space="preserve">Local Changes :</x:String>
<x:String x:Key="Text.Checkout.LocalChanges.StashAndReply" xml:space="preserve">Stash &amp; Reapply</x:String>
<x:String x:Key="Text.Checkout.LocalChanges.Discard" xml:space="preserve">Discard</x:String>
Expand All @@ -75,6 +77,7 @@
<x:String x:Key="Text.CommitCM.CherryPick" xml:space="preserve">Cherry-Pick This Commit</x:String>
<x:String x:Key="Text.CommitCM.CopySHA" xml:space="preserve">Copy SHA</x:String>
<x:String x:Key="Text.CommitCM.Rebase" xml:space="preserve">Rebase${0}$to Here</x:String>
<x:String x:Key="Text.CommitCM.Checkout" xml:space="preserve">Checkout This Commit</x:String>
<x:String x:Key="Text.CommitCM.Reset" xml:space="preserve">Reset${0}$to Here</x:String>
<x:String x:Key="Text.CommitCM.Revert" xml:space="preserve">Revert Commit</x:String>
<x:String x:Key="Text.CommitCM.Reword" xml:space="preserve">Reword</x:String>
Expand Down
7 changes: 5 additions & 2 deletions src/Resources/Locales/zh_CN.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,10 @@
<x:String x:Key="Text.ChangeDisplayMode.Grid" xml:space="preserve">网格模式</x:String>
<x:String x:Key="Text.ChangeDisplayMode.List" xml:space="preserve">列表模式</x:String>
<x:String x:Key="Text.ChangeDisplayMode.Tree" xml:space="preserve">树形模式</x:String>
<x:String x:Key="Text.Checkout" xml:space="preserve">检出(checkout)分支</x:String>
<x:String x:Key="Text.Checkout.Target" xml:space="preserve">目标分支 :</x:String>
<x:String x:Key="Text.CheckoutBranch" xml:space="preserve">检出(checkout)分支</x:String>
<x:String x:Key="Text.CheckoutCommit" xml:space="preserve">检出(checkout)提交</x:String>
<x:String x:Key="Text.Checkout.TargetBranch" xml:space="preserve">目标分支 :</x:String>
<x:String x:Key="Text.Checkout.TargetCommit" xml:space="preserve">目标提交 :</x:String>
<x:String x:Key="Text.Checkout.LocalChanges" xml:space="preserve">未提交更改 :</x:String>
<x:String x:Key="Text.Checkout.LocalChanges.StashAndReply" xml:space="preserve">贮藏(stash)并自动恢复</x:String>
<x:String x:Key="Text.Checkout.LocalChanges.Discard" xml:space="preserve">忽略</x:String>
Expand All @@ -75,6 +77,7 @@
<x:String x:Key="Text.CommitCM.CherryPick" xml:space="preserve">挑选(cherry-pick)此提交</x:String>
<x:String x:Key="Text.CommitCM.CopySHA" xml:space="preserve">复制提交指纹</x:String>
<x:String x:Key="Text.CommitCM.Rebase" xml:space="preserve">变基(rebase)${0}$到此处</x:String>
<x:String x:Key="Text.CommitCM.Checkout" xml:space="preserve">检出(checkout)此提交</x:String>
<x:String x:Key="Text.CommitCM.Reset" xml:space="preserve">重置(reset)${0}$到此处</x:String>
<x:String x:Key="Text.CommitCM.Revert" xml:space="preserve">回滚此提交</x:String>
<x:String x:Key="Text.CommitCM.Reword" xml:space="preserve">编辑提交信息</x:String>
Expand Down
48 changes: 42 additions & 6 deletions src/ViewModels/Checkout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@

namespace SourceGit.ViewModels
{
public enum CheckoutTargetType
{
Branch,
Commit,
}

public class Checkout : Popup
{
public string Branch
public string Target
{
get;
private set;
Expand All @@ -16,17 +22,47 @@ public bool AutoStash
set => SetProperty(ref _autoStash, value);
}

public Checkout(Repository repo, string branch)
public string Subject
{
get;
private set;
}

public CheckoutTargetType TargetType
{
get;
private set;
}

public Checkout(Repository repo, string target)
{
_repo = repo;
Target = target;
TargetType = CheckoutTargetType.Branch;
View = new Views.Checkout() { DataContext = this };
}

public Checkout(Repository repo, Models.Commit commit)
{
_repo = repo;
Target = commit.SHA;
Subject = commit.Subject;
TargetType = CheckoutTargetType.Commit;
View = new Views.Checkout() { DataContext = this };
}

public Checkout(Repository repo, string target, CheckoutTargetType type)
{
_repo = repo;
Branch = branch;
Target = target;
TargetType = type;
View = new Views.Checkout() { DataContext = this };
}

public override Task<bool> Sure()
{
_repo.SetWatcherEnabled(false);
ProgressDescription = $"Checkout '{Branch}' ...";
ProgressDescription = $"Checkout '{Target}' ...";

var hasLocalChanges = _repo.WorkingCopyChangesCount > 0;
return Task.Run(() =>
Expand Down Expand Up @@ -59,8 +95,8 @@ public override Task<bool> Sure()
}
}

SetProgressDescription("Checkout branch ...");
var rs = new Commands.Checkout(_repo.FullPath).Branch(Branch, SetProgressDescription);
SetProgressDescription($"Checkout '{Target}' ...");
var rs = new Commands.Checkout(_repo.FullPath).Target(Target, SetProgressDescription);

if (needPopStash)
{
Expand Down
11 changes: 11 additions & 0 deletions src/ViewModels/Histories.cs
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,17 @@ public ContextMenu MakeContextMenu()

if (current.Head != commit.SHA)
{
var checkout = new MenuItem();
checkout.Header = App.Text("CommitCM.Checkout");
checkout.Icon = App.CreateMenuIcon("Icons.Commit");
checkout.Click += (o, e) =>
{
if (PopupHost.CanCreatePopup())
PopupHost.ShowPopup(new Checkout(_repo, commit));
e.Handled = true;
};
menu.Items.Add(checkout);

var reset = new MenuItem();
reset.Header = new Views.NameHighlightedTextBlock("CommitCM.Reset", current.Name);
reset.Icon = App.CreateMenuIcon("Icons.Reset");
Expand Down
11 changes: 7 additions & 4 deletions src/Views/Checkout.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,25 @@
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:vm="using:SourceGit.ViewModels"
xmlns:c="clr-namespace:SourceGit.Converters"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="SourceGit.Views.Checkout"
x:DataType="vm:Checkout">
<StackPanel Orientation="Vertical" Margin="8,0">
<TextBlock FontSize="18"
Classes="bold"
Text="{DynamicResource Text.Checkout}"/>
Text="{Binding TargetType, Converter={x:Static c:CheckoutTargetTypeConverters.ToTitle}}"/>

<Grid Margin="0,16,0,0" RowDefinitions="32,32" ColumnDefinitions="150,*">
<TextBlock Grid.Row="0" Grid.Column="0"
HorizontalAlignment="Right" VerticalAlignment="Center"
Margin="0,0,8,0"
Text="{DynamicResource Text.Checkout.Target}"/>
Text="{Binding TargetType, Converter={x:Static c:CheckoutTargetTypeConverters.ToTarget}}"/>
<StackPanel Grid.Row="0" Grid.Column="1" Orientation="Horizontal">
<Path Width="14" Height="14" Margin="4,0" Data="{StaticResource Icons.Branch}"/>
<TextBlock Text="{Binding Branch}"/>
<Path Width="14" Height="14" Margin="4,0" Data="{Binding TargetType, Converter={x:Static c:CheckoutTargetTypeConverters.ToIcon}}"/>
<TextBlock Text="{Binding Target}" IsVisible="{Binding TargetType, Converter={x:Static c:CheckoutTargetTypeConverters.IsBranch}}"/>
<TextBlock Text="{Binding Target, Converter={x:Static c:StringConverters.ToShortSHA}}" Foreground="DarkOrange" IsVisible="{Binding TargetType, Converter={x:Static c:CheckoutTargetTypeConverters.IsCommit}}"/>
<TextBlock VerticalAlignment="Center" Text="{Binding Subject}" Margin="4,0,0,0" IsVisible="{Binding TargetType, Converter={x:Static c:CheckoutTargetTypeConverters.IsCommit}}"/>
</StackPanel>

<TextBlock Grid.Row="1" Grid.Column="0"
Expand Down
Loading