Merge pull request #147 from filipeRmlh/feature/allowing_to_checkout_commit

Feature/allowing to checkout commit
This commit is contained in:
leo 2024-05-26 13:37:59 +08:00 committed by GitHub
commit e00bc4e630
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
15 changed files with 225 additions and 9 deletions

View file

@ -62,6 +62,14 @@ namespace SourceGit.Commands
return Exec();
}
public bool Commit(string commitId, Action<string> onProgress)
{
Args = $"checkout --detach --progress {commitId}";
TraitErrorAsOutput = true;
_outputHandler = onProgress;
return Exec();
}
public bool Files(List<string> files)
{
StringBuilder builder = new StringBuilder();

View file

@ -52,6 +52,11 @@ namespace SourceGit.Commands
if (refName.EndsWith("/HEAD", StringComparison.Ordinal))
return;
if (refName.StartsWith("(HEAD detached at"))
{
branch.isHead = true;
}
if (refName.StartsWith(PREFIX_LOCAL, StringComparison.Ordinal))
{
branch.Name = refName.Substring(PREFIX_LOCAL.Length);

View file

@ -146,6 +146,15 @@ namespace SourceGit.Commands
Name = d.Substring(19).Trim(),
});
}
else if (d.Equals("HEAD"))
{
isHeadOfCurrent = true;
decorators.Add(new Models.Decorator()
{
Type = Models.DecoratorType.CurrentCommitHead,
Name = d.Trim(),
});
}
else if (d.StartsWith("refs/heads/", StringComparison.Ordinal))
{
decorators.Add(new Models.Decorator()

View file

@ -38,6 +38,9 @@ namespace SourceGit.Converters
});
public static readonly FuncValueConverter<Models.DecoratorType, FontWeight> ToFontWeight =
new FuncValueConverter<Models.DecoratorType, FontWeight>(v => v == Models.DecoratorType.CurrentBranchHead ? FontWeight.Bold : FontWeight.Regular);
new FuncValueConverter<Models.DecoratorType, FontWeight>(v =>
v is Models.DecoratorType.CurrentBranchHead or Models.DecoratorType.CurrentCommitHead
? FontWeight.Bold : FontWeight.Regular
);
}
}

View file

@ -10,5 +10,6 @@
public string Upstream { get; set; }
public string UpstreamTrackStatus { get; set; }
public string Remote { get; set; }
public bool isHead { get; set; }
}
}

View file

@ -32,7 +32,7 @@ namespace SourceGit.Models
public bool IsCurrentHead
{
get => Decorators.Find(x => x.Type == DecoratorType.CurrentBranchHead) != null;
get => Decorators.Find(x => x.Type is DecoratorType.CurrentBranchHead or DecoratorType.CurrentCommitHead) != null;
}
public string FullMessage

View file

@ -7,6 +7,7 @@ namespace SourceGit.Models
None,
CurrentBranchHead,
LocalBranchHead,
CurrentCommitHead,
RemoteBranchHead,
Tag,
}

View file

@ -54,7 +54,11 @@
<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.Commit" xml:space="preserve">Checkout Commit</x:String>
<x:String x:Key="Text.Checkout.Commit.Warning" xml:space="preserve">Warning: By doing a commit checkout, your Head will be detached</x:String>
<x:String x:Key="Text.Checkout.Target" xml:space="preserve">Branch :</x:String>
<x:String x:Key="Text.Checkout.CommitTarget.Sha" xml:space="preserve">Commit SHA :</x:String>
<x:String x:Key="Text.Checkout.CommitTarget.ShortSha" xml:space="preserve">Commit Short SHA :</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>
@ -74,6 +78,7 @@
<x:String x:Key="Text.Close" xml:space="preserve">CLOSE</x:String>
<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.Checkout" xml:space="preserve">Checkout commit${0}</x:String>
<x:String x:Key="Text.CommitCM.Rebase" xml:space="preserve">Rebase${0}$to Here</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>

View file

@ -44,4 +44,11 @@
<TrimmerRootAssembly Include="SourceGit" />
<TrimmerRootAssembly Include="Avalonia.Themes.Fluent" />
</ItemGroup>
<ItemGroup>
<Compile Update="Views\CheckoutCommit.axaml.cs">
<DependentUpon>CheckoutCommit.axaml</DependentUpon>
<SubType>Code</SubType>
</Compile>
</ItemGroup>
</Project>

View file

@ -11,6 +11,7 @@ namespace SourceGit.ViewModels
{
public enum BranchTreeNodeType
{
DetachedHead,
Remote,
Folder,
Branch,
@ -52,6 +53,11 @@ namespace SourceGit.ViewModels
get => Type == BranchTreeNodeType.Branch;
}
public bool IsDetachedHead
{
get => Type == BranchTreeNodeType.DetachedHead;
}
public bool IsCurrent
{
get => IsBranch && (Backend as Models.Branch).IsCurrent;
@ -217,7 +223,7 @@ namespace SourceGit.ViewModels
lastFolder.Children.Add(new BranchTreeNode()
{
Name = Path.GetFileName(branch.Name),
Type = BranchTreeNodeType.Branch,
Type = branch.isHead ? BranchTreeNodeType.DetachedHead : BranchTreeNodeType.Branch,
Backend = branch,
IsExpanded = false,
IsFiltered = isFiltered,
@ -228,14 +234,16 @@ namespace SourceGit.ViewModels
{
nodes.Sort((l, r) =>
{
if (l.Type == BranchTreeNodeType.DetachedHead)
{
return -1;
}
if (l.Type == r.Type)
{
return l.Name.CompareTo(r.Name);
}
else
{
return (int)l.Type - (int)r.Type;
}
return (int)l.Type - (int)r.Type;
});
foreach (var node in nodes)

View file

@ -0,0 +1,87 @@
using System.Threading.Tasks;
namespace SourceGit.ViewModels
{
public class CheckoutCommit: Popup
{
public string Commit
{
get;
private set;
}
public bool HasLocalChanges
{
get => _repo.WorkingCopyChangesCount > 0;
}
public bool AutoStash
{
get => _autoStash;
set => SetProperty(ref _autoStash, value);
}
public CheckoutCommit(Repository repo, string commit)
{
_repo = repo;
Commit = commit;
View = new Views.CheckoutCommit() { DataContext = this };
}
public override Task<bool> Sure()
{
_repo.SetWatcherEnabled(false);
ProgressDescription = $"Checkout Commit '{Commit}' ...";
return Task.Run(() =>
{
var needPopStash = false;
if (HasLocalChanges)
{
if (AutoStash)
{
SetProgressDescription("Adding untracked changes ...");
var succ = new Commands.Add(_repo.FullPath).Exec();
if (succ)
{
SetProgressDescription("Stash local changes ...");
succ = new Commands.Stash(_repo.FullPath).Push("CHECKOUT_AUTO_STASH");
}
if (!succ)
{
CallUIThread(() => _repo.SetWatcherEnabled(true));
return false;
}
needPopStash = true;
}
else
{
SetProgressDescription("Discard local changes ...");
Commands.Discard.All(_repo.FullPath);
}
}
SetProgressDescription("Checkout commit ...");
var rs = new Commands.Checkout(_repo.FullPath).Commit(Commit, SetProgressDescription);
if (needPopStash)
{
SetProgressDescription("Re-apply local changes...");
rs = new Commands.Stash(_repo.FullPath).Apply("stash@{0}");
if (rs)
{
rs = new Commands.Stash(_repo.FullPath).Drop("stash@{0}");
}
}
CallUIThread(() => _repo.SetWatcherEnabled(true));
return rs;
});
}
private readonly Repository _repo = null;
private bool _autoStash = true;
}
}

View file

@ -1,9 +1,11 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Threading.Tasks;
using Avalonia.Controls;
using Avalonia.Data.Converters;
using Avalonia.Platform.Storage;
using Avalonia.Threading;
@ -234,6 +236,20 @@ namespace SourceGit.ViewModels
e.Handled = true;
};
menu.Items.Add(reset);
var checkoutCommit = new MenuItem();
var shortSha = Converters.StringConverters.ToShortSHA
.Convert(commit.SHA, typeof(string), null, CultureInfo.CurrentCulture);
checkoutCommit.Header = new Views.NameHighlightedTextBlock("CommitCM.Checkout", shortSha);
checkoutCommit.Icon = App.CreateMenuIcon("Icons.Check");
checkoutCommit.Click += (o, e) =>
{
_repo.CheckoutCommit(commit.SHA);
e.Handled = true;
};
menu.Items.Add(checkoutCommit);
}
else
{

View file

@ -722,6 +722,14 @@ namespace SourceGit.ViewModels
PopupHost.ShowAndStartPopup(new Checkout(this, branch));
}
public void CheckoutCommit(string commit)
{
if (!PopupHost.CanCreatePopup())
return;
PopupHost.ShowPopup(new CheckoutCommit(this, commit));
}
public void DeleteMultipleBranches(List<Models.Branch> branches, bool isLocal)
{
if (PopupHost.CanCreatePopup())

View file

@ -0,0 +1,45 @@
<UserControl xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
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.CheckoutCommit"
x:DataType="vm:CheckoutCommit">
<StackPanel Orientation="Vertical" Margin="8,0">
<TextBlock FontSize="18"
Classes="bold"
Text="{DynamicResource Text.Checkout.Commit}"/>
<TextBlock FontSize="14"
TextWrapping="Wrap"
Classes="italic"
Text="{DynamicResource Text.Checkout.Commit.Warning}"/>
<TextBlock TextWrapping="Wrap" Margin="0,16,0,0">
<TextBlock Classes="bold" Grid.Row="0" Grid.Column="0"
Text="{DynamicResource Text.Checkout.CommitTarget.Sha}"/>
<TextBlock TextWrapping="Wrap" Text="{Binding Commit}"/>
(<TextBlock Classes="italic" TextWrapping="Wrap" Text="{Binding Commit, Converter={x:Static c:StringConverters.ToShortSHA}}"/>)
</TextBlock>
<StackPanel Margin="0, 26, 0, 0" Grid.Row="1" Grid.Column="1" Orientation="Vertical" IsVisible="{Binding HasLocalChanges}">
<TextBlock Grid.Row="1" Grid.Column="0"
HorizontalAlignment="Left" VerticalAlignment="Center"
Margin="0,0,8,0"
Text="{DynamicResource Text.Checkout.LocalChanges}"/>
<StackPanel Margin="0, 13, 0, 0" Grid.Row="1" Grid.Column="1" Orientation="Horizontal">
<RadioButton Content="{DynamicResource Text.Checkout.LocalChanges.StashAndReply}"
GroupName="LocalChanges"
IsChecked="{Binding AutoStash, Mode=TwoWay}"/>
<RadioButton Content="{DynamicResource Text.Checkout.LocalChanges.Discard}"
GroupName="LocalChanges"
Margin="8,0,0,0"/>
</StackPanel>
</StackPanel>
</StackPanel>
</UserControl>

View file

@ -0,0 +1,13 @@
using Avalonia.Controls;
namespace SourceGit.Views
{
public partial class CheckoutCommit : UserControl
{
public bool HasLocalChanges;
public CheckoutCommit()
{
InitializeComponent();
}
}
}