enhance: do not warn when creating branch with current detached HEAD

Signed-off-by: leo <longshuang@msn.cn>
This commit is contained in:
leo
2025-07-03 20:11:21 +08:00
parent f33edb7601
commit 92ab85bb3f
5 changed files with 51 additions and 33 deletions

View File

@@ -45,11 +45,19 @@ namespace SourceGit.ViewModels
var succ = false;
var needPopStash = false;
var confirmed = await _repo.ConfirmCheckoutBranchAsync();
if (!confirmed)
if (_repo.CurrentBranch is { IsDetachedHead: true })
{
_repo.SetWatcherEnabled(true);
return true;
var refs = await new Commands.QueryRefsContainsCommit(_repo.FullPath, _repo.CurrentBranch.Head).GetResultAsync();
if (refs.Count == 0)
{
var msg = App.Text("Checkout.WarnLostCommits");
var shouldContinue = await App.AskConfirmAsync(msg, null);
if (!shouldContinue)
{
_repo.SetWatcherEnabled(true);
return true;
}
}
}
if (DiscardLocalChanges)

View File

@@ -50,11 +50,19 @@ namespace SourceGit.ViewModels
var succ = false;
var needPopStash = false;
var confirmed = await _repo.ConfirmCheckoutBranchAsync();
if (!confirmed)
if (_repo.CurrentBranch is { IsDetachedHead: true })
{
_repo.SetWatcherEnabled(true);
return true;
var refs = await new Commands.QueryRefsContainsCommit(_repo.FullPath, _repo.CurrentBranch.Head).GetResultAsync();
if (refs.Count == 0)
{
var msg = App.Text("Checkout.WarnLostCommits");
var shouldContinue = await App.AskConfirmAsync(msg, null);
if (!shouldContinue)
{
_repo.SetWatcherEnabled(true);
return true;
}
}
}
if (DiscardLocalChanges)

View File

@@ -45,11 +45,19 @@ namespace SourceGit.ViewModels
bool succ;
var needPop = false;
var confirmed = await _repo.ConfirmCheckoutBranchAsync();
if (!confirmed)
if (_repo.CurrentBranch is { IsDetachedHead: true })
{
_repo.SetWatcherEnabled(true);
return true;
var refs = await new Commands.QueryRefsContainsCommit(_repo.FullPath, _repo.CurrentBranch.Head).GetResultAsync();
if (refs.Count == 0)
{
var msg = App.Text("Checkout.WarnLostCommits");
var shouldContinue = await App.AskConfirmAsync(msg, null);
if (!shouldContinue)
{
_repo.SetWatcherEnabled(true);
return true;
}
}
}
if (DiscardLocalChanges)

View File

@@ -1,4 +1,5 @@
using System.ComponentModel.DataAnnotations;
using System;
using System.ComponentModel.DataAnnotations;
using System.Threading.Tasks;
namespace SourceGit.ViewModels
@@ -67,7 +68,7 @@ namespace SourceGit.ViewModels
public CreateBranch(Repository repo, Models.Branch branch)
{
_repo = repo;
_baseOnRevision = branch.IsDetachedHead ? branch.Head : branch.FullName;
_baseOnRevision = branch.Head;
if (!branch.IsLocal && repo.Branches.Find(x => x.IsLocal && x.Name == branch.Name) == null)
Name = branch.Name;
@@ -127,11 +128,19 @@ namespace SourceGit.ViewModels
if (CheckoutAfterCreated)
{
var confirmed = await _repo.ConfirmCheckoutBranchAsync();
if (!confirmed)
if (_repo.CurrentBranch is { IsDetachedHead: true } && !_repo.CurrentBranch.Head.Equals(_baseOnRevision, StringComparison.Ordinal))
{
_repo.SetWatcherEnabled(true);
return true;
var refs = await new Commands.QueryRefsContainsCommit(_repo.FullPath, _repo.CurrentBranch.Head).GetResultAsync();
if (refs.Count == 0)
{
var msg = App.Text("Checkout.WarnLostCommits");
var shouldContinue = await App.AskConfirmAsync(msg, null);
if (!shouldContinue)
{
_repo.SetWatcherEnabled(true);
return true;
}
}
}
}

View File

@@ -1337,21 +1337,6 @@ namespace SourceGit.ViewModels
ShowPopup(new CreateBranch(this, _currentBranch));
}
public async Task<bool> ConfirmCheckoutBranchAsync()
{
if (_currentBranch is not { IsDetachedHead: true })
return true;
var refs = await new Commands.QueryRefsContainsCommit(_fullpath, _currentBranch.Head).GetResultAsync();
if (refs.Count == 0)
{
var msg = App.Text("Checkout.WarnLostCommits");
return await App.AskConfirmAsync(msg, null);
}
return true;
}
public void CheckoutBranch(Models.Branch branch)
{
if (branch.IsLocal)