feature: supports reset branch to selected commit without checkout (#1247) (#1318)

Signed-off-by: leo <longshuang@msn.cn>
This commit is contained in:
leo
2025-05-20 12:24:07 +08:00
parent 12d2b7721c
commit f9b6116a76
20 changed files with 144 additions and 97 deletions

View File

@@ -1,47 +0,0 @@
using System.Threading.Tasks;
namespace SourceGit.ViewModels
{
public class FastForwardWithoutCheckout : Popup
{
public Models.Branch Local
{
get;
}
public Models.Branch To
{
get;
}
public FastForwardWithoutCheckout(Repository repo, Models.Branch local, Models.Branch upstream)
{
_repo = repo;
Local = local;
To = upstream;
}
public override Task<bool> Sure()
{
_repo.SetWatcherEnabled(false);
ProgressDescription = "Fast-Forward ...";
var log = _repo.CreateLog("Fast-Forward (No checkout)");
Use(log);
return Task.Run(() =>
{
new Commands.UpdateRef(_repo.FullPath, Local.FullName, To.FullName).Use(log).Exec();
log.Complete();
CallUIThread(() =>
{
_repo.NavigateToCommit(To.Head);
_repo.SetWatcherEnabled(true);
});
return true;
});
}
private readonly Repository _repo = null;
}
}

View File

@@ -1770,9 +1770,25 @@ namespace SourceGit.ViewModels
fastForward.Click += (_, e) =>
{
if (CanCreatePopup())
ShowAndStartPopup(new FastForwardWithoutCheckout(this, branch, upstream));
ShowAndStartPopup(new ResetWithoutCheckout(this, branch, upstream));
e.Handled = true;
};
menu.Items.Add(fastForward);
var selectedCommit = (_histories?.DetailContext as CommitDetail)?.Commit;
if (selectedCommit != null && !selectedCommit.SHA.Equals(branch.Head, StringComparison.Ordinal))
{
var move = new MenuItem();
move.Header = App.Text("BranchCM.ResetToSelectedCommit", branch.Name, selectedCommit.SHA.Substring(0, 10));
move.Icon = App.CreateMenuIcon("Icons.Reset");
move.Click += (_, e) =>
{
if (CanCreatePopup())
ShowPopup(new ResetWithoutCheckout(this, branch, selectedCommit));
e.Handled = true;
};
menu.Items.Add(move);
}
var fetchInto = new MenuItem();
fetchInto.Header = App.Text("BranchCM.FetchInto", upstream.FriendlyName, branch.Name);
@@ -1785,7 +1801,6 @@ namespace SourceGit.ViewModels
e.Handled = true;
};
menu.Items.Add(fastForward);
menu.Items.Add(new MenuItem() { Header = "-" });
menu.Items.Add(fetchInto);
}

View File

@@ -0,0 +1,53 @@
using System.Threading.Tasks;
namespace SourceGit.ViewModels
{
public class ResetWithoutCheckout : Popup
{
public Models.Branch Target
{
get;
}
public object To
{
get;
}
public ResetWithoutCheckout(Repository repo, Models.Branch target, Models.Branch to)
{
_repo = repo;
_revision = to.Head;
Target = target;
To = to;
}
public ResetWithoutCheckout(Repository repo, Models.Branch target, Models.Commit to)
{
_repo = repo;
_revision = to.SHA;
Target = target;
To = to;
}
public override Task<bool> Sure()
{
_repo.SetWatcherEnabled(false);
ProgressDescription = $"Reset {Target.Name} to {_revision} ...";
var log = _repo.CreateLog($"Reset '{Target.Name}' to '{_revision}'");
Use(log);
return Task.Run(() =>
{
var succ = Commands.Branch.ResetWithoutCheckout(_repo.FullPath, Target.Name, _revision, log);
log.Complete();
CallUIThread(() => _repo.SetWatcherEnabled(true));
return succ;
});
}
private readonly Repository _repo = null;
private readonly string _revision = string.Empty;
}
}