Skip to content

Commit 92ab85b

Browse files
committed
enhance: do not warn when creating branch with current detached HEAD
Signed-off-by: leo <[email protected]>
1 parent f33edb7 commit 92ab85b

File tree

5 files changed

+51
-33
lines changed

5 files changed

+51
-33
lines changed

src/ViewModels/Checkout.cs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,19 @@ public override async Task<bool> Sure()
4545
var succ = false;
4646
var needPopStash = false;
4747

48-
var confirmed = await _repo.ConfirmCheckoutBranchAsync();
49-
if (!confirmed)
48+
if (_repo.CurrentBranch is { IsDetachedHead: true })
5049
{
51-
_repo.SetWatcherEnabled(true);
52-
return true;
50+
var refs = await new Commands.QueryRefsContainsCommit(_repo.FullPath, _repo.CurrentBranch.Head).GetResultAsync();
51+
if (refs.Count == 0)
52+
{
53+
var msg = App.Text("Checkout.WarnLostCommits");
54+
var shouldContinue = await App.AskConfirmAsync(msg, null);
55+
if (!shouldContinue)
56+
{
57+
_repo.SetWatcherEnabled(true);
58+
return true;
59+
}
60+
}
5361
}
5462

5563
if (DiscardLocalChanges)

src/ViewModels/CheckoutAndFastForward.cs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,19 @@ public override async Task<bool> Sure()
5050
var succ = false;
5151
var needPopStash = false;
5252

53-
var confirmed = await _repo.ConfirmCheckoutBranchAsync();
54-
if (!confirmed)
53+
if (_repo.CurrentBranch is { IsDetachedHead: true })
5554
{
56-
_repo.SetWatcherEnabled(true);
57-
return true;
55+
var refs = await new Commands.QueryRefsContainsCommit(_repo.FullPath, _repo.CurrentBranch.Head).GetResultAsync();
56+
if (refs.Count == 0)
57+
{
58+
var msg = App.Text("Checkout.WarnLostCommits");
59+
var shouldContinue = await App.AskConfirmAsync(msg, null);
60+
if (!shouldContinue)
61+
{
62+
_repo.SetWatcherEnabled(true);
63+
return true;
64+
}
65+
}
5866
}
5967

6068
if (DiscardLocalChanges)

src/ViewModels/CheckoutCommit.cs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,19 @@ public override async Task<bool> Sure()
4545
bool succ;
4646
var needPop = false;
4747

48-
var confirmed = await _repo.ConfirmCheckoutBranchAsync();
49-
if (!confirmed)
48+
if (_repo.CurrentBranch is { IsDetachedHead: true })
5049
{
51-
_repo.SetWatcherEnabled(true);
52-
return true;
50+
var refs = await new Commands.QueryRefsContainsCommit(_repo.FullPath, _repo.CurrentBranch.Head).GetResultAsync();
51+
if (refs.Count == 0)
52+
{
53+
var msg = App.Text("Checkout.WarnLostCommits");
54+
var shouldContinue = await App.AskConfirmAsync(msg, null);
55+
if (!shouldContinue)
56+
{
57+
_repo.SetWatcherEnabled(true);
58+
return true;
59+
}
60+
}
5361
}
5462

5563
if (DiscardLocalChanges)

src/ViewModels/CreateBranch.cs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.ComponentModel.DataAnnotations;
1+
using System;
2+
using System.ComponentModel.DataAnnotations;
23
using System.Threading.Tasks;
34

45
namespace SourceGit.ViewModels
@@ -67,7 +68,7 @@ public bool RecurseSubmodules
6768
public CreateBranch(Repository repo, Models.Branch branch)
6869
{
6970
_repo = repo;
70-
_baseOnRevision = branch.IsDetachedHead ? branch.Head : branch.FullName;
71+
_baseOnRevision = branch.Head;
7172

7273
if (!branch.IsLocal && repo.Branches.Find(x => x.IsLocal && x.Name == branch.Name) == null)
7374
Name = branch.Name;
@@ -127,11 +128,19 @@ public override async Task<bool> Sure()
127128

128129
if (CheckoutAfterCreated)
129130
{
130-
var confirmed = await _repo.ConfirmCheckoutBranchAsync();
131-
if (!confirmed)
131+
if (_repo.CurrentBranch is { IsDetachedHead: true } && !_repo.CurrentBranch.Head.Equals(_baseOnRevision, StringComparison.Ordinal))
132132
{
133-
_repo.SetWatcherEnabled(true);
134-
return true;
133+
var refs = await new Commands.QueryRefsContainsCommit(_repo.FullPath, _repo.CurrentBranch.Head).GetResultAsync();
134+
if (refs.Count == 0)
135+
{
136+
var msg = App.Text("Checkout.WarnLostCommits");
137+
var shouldContinue = await App.AskConfirmAsync(msg, null);
138+
if (!shouldContinue)
139+
{
140+
_repo.SetWatcherEnabled(true);
141+
return true;
142+
}
143+
}
135144
}
136145
}
137146

src/ViewModels/Repository.cs

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1337,21 +1337,6 @@ public void CreateNewBranch()
13371337
ShowPopup(new CreateBranch(this, _currentBranch));
13381338
}
13391339

1340-
public async Task<bool> ConfirmCheckoutBranchAsync()
1341-
{
1342-
if (_currentBranch is not { IsDetachedHead: true })
1343-
return true;
1344-
1345-
var refs = await new Commands.QueryRefsContainsCommit(_fullpath, _currentBranch.Head).GetResultAsync();
1346-
if (refs.Count == 0)
1347-
{
1348-
var msg = App.Text("Checkout.WarnLostCommits");
1349-
return await App.AskConfirmAsync(msg, null);
1350-
}
1351-
1352-
return true;
1353-
}
1354-
13551340
public void CheckoutBranch(Models.Branch branch)
13561341
{
13571342
if (branch.IsLocal)

0 commit comments

Comments
 (0)