Skip to content

Commit 0e22503

Browse files
committed
feature: warn users when checking out new branch with commits not connected to any branches (#1463)
Signed-off-by: leo <[email protected]>
1 parent 67efe76 commit 0e22503

14 files changed

+130
-53
lines changed

src/Resources/Locales/en_US.axaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@
8989
<x:String x:Key="Text.Checkout.LocalChanges.StashAndReply" xml:space="preserve">Stash &amp; Reapply</x:String>
9090
<x:String x:Key="Text.Checkout.RecurseSubmodules" xml:space="preserve">Update all submodules</x:String>
9191
<x:String x:Key="Text.Checkout.Target" xml:space="preserve">Branch:</x:String>
92+
<x:String x:Key="Text.Checkout.WarnLostCommits" xml:space="preserve">Your current HEAD contains commit(s) not connected to any branches/tags! Do you want to continue?</x:String>
9293
<x:String x:Key="Text.Checkout.WithFastForward" xml:space="preserve">Checkout &amp; Fast-Forward</x:String>
9394
<x:String x:Key="Text.Checkout.WithFastForward.Upstream" xml:space="preserve">Fast-Forward to:</x:String>
9495
<x:String x:Key="Text.CherryPick" xml:space="preserve">Cherry Pick</x:String>

src/Resources/Locales/zh_CN.axaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@
9393
<x:String x:Key="Text.Checkout.LocalChanges.StashAndReply" xml:space="preserve">贮藏并自动恢复</x:String>
9494
<x:String x:Key="Text.Checkout.RecurseSubmodules" xml:space="preserve">同时更新所有子模块</x:String>
9595
<x:String x:Key="Text.Checkout.Target" xml:space="preserve">目标分支 :</x:String>
96+
<x:String x:Key="Text.Checkout.WarnLostCommits" xml:space="preserve">您当前游离的HEAD包含未被任何分支及标签引用的提交!是否继续?</x:String>
9697
<x:String x:Key="Text.Checkout.WithFastForward" xml:space="preserve">检出分支并快进</x:String>
9798
<x:String x:Key="Text.Checkout.WithFastForward.Upstream" xml:space="preserve">上游分支 :</x:String>
9899
<x:String x:Key="Text.CherryPick" xml:space="preserve">挑选提交</x:String>

src/Resources/Locales/zh_TW.axaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@
9393
<x:String x:Key="Text.Checkout.LocalChanges.StashAndReply" xml:space="preserve">擱置變更並自動復原</x:String>
9494
<x:String x:Key="Text.Checkout.RecurseSubmodules" xml:space="preserve">同時更新所有子模組</x:String>
9595
<x:String x:Key="Text.Checkout.Target" xml:space="preserve">目標分支:</x:String>
96+
<x:String x:Key="Text.Checkout.WarnLostCommits" xml:space="preserve">您目前的分離的 HEAD 包含與任何分支/標籤無關的提交!您要繼續嗎?</x:String>
9697
<x:String x:Key="Text.Checkout.WithFastForward" xml:space="preserve">簽出分支並快轉</x:String>
9798
<x:String x:Key="Text.Checkout.WithFastForward.Upstream" xml:space="preserve">上游分支 :</x:String>
9899
<x:String x:Key="Text.CherryPick" xml:space="preserve">揀選提交</x:String>

src/ViewModels/Checkout.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@ public override Task<bool> Sure()
4747
var succ = false;
4848
var needPopStash = false;
4949

50+
if (!_repo.ConfirmCheckoutBranch())
51+
{
52+
CallUIThread(() => _repo.SetWatcherEnabled(true));
53+
return true;
54+
}
55+
5056
if (DiscardLocalChanges)
5157
{
5258
succ = new Commands.Checkout(_repo.FullPath).Use(log).Branch(Branch, true);

src/ViewModels/CheckoutAndFastForward.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@ public override Task<bool> Sure()
5252
var succ = false;
5353
var needPopStash = false;
5454

55+
if (!_repo.ConfirmCheckoutBranch())
56+
{
57+
CallUIThread(() => _repo.SetWatcherEnabled(true));
58+
return true;
59+
}
60+
5561
if (DiscardLocalChanges)
5662
{
5763
succ = new Commands.Checkout(_repo.FullPath).Use(log).Branch(LocalBranch.Name, RemoteBranch.Head, true, true);

src/ViewModels/CheckoutCommit.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@ public override Task<bool> Sure()
4747
var succ = false;
4848
var needPop = false;
4949

50+
if (!_repo.ConfirmCheckoutBranch())
51+
{
52+
CallUIThread(() => _repo.SetWatcherEnabled(true));
53+
return true;
54+
}
55+
5056
if (DiscardLocalChanges)
5157
{
5258
succ = new Commands.Checkout(_repo.FullPath).Use(log).Commit(Commit.SHA, true);

src/ViewModels/Confirm.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using System;
2+
3+
namespace SourceGit.ViewModels
4+
{
5+
public class Confirm
6+
{
7+
public string Message
8+
{
9+
get;
10+
private set;
11+
}
12+
13+
public Confirm(string message, Action onSure, Action onCancel = null)
14+
{
15+
Message = message;
16+
_onSure = onSure;
17+
_onCancel = onCancel;
18+
}
19+
20+
public void Done(bool isSure)
21+
{
22+
if (isSure)
23+
_onSure?.Invoke();
24+
else
25+
_onCancel?.Invoke();
26+
}
27+
28+
private Action _onSure;
29+
private Action _onCancel;
30+
}
31+
}

src/ViewModels/ConfirmCommit.cs

Lines changed: 0 additions & 26 deletions
This file was deleted.

src/ViewModels/CreateBranch.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,13 @@ public override Task<bool> Sure()
130130
return Task.Run(() =>
131131
{
132132
bool succ = false;
133+
134+
if (CheckoutAfterCreated && !_repo.ConfirmCheckoutBranch())
135+
{
136+
CallUIThread(() => _repo.SetWatcherEnabled(true));
137+
return true;
138+
}
139+
133140
if (CheckoutAfterCreated && !_repo.IsBare)
134141
{
135142
var needPopStash = false;

src/ViewModels/Repository.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1330,6 +1330,41 @@ public void CreateNewBranch()
13301330
ShowPopup(new CreateBranch(this, _currentBranch));
13311331
}
13321332

1333+
public bool ConfirmCheckoutBranch()
1334+
{
1335+
if (Dispatcher.UIThread.CheckAccess())
1336+
return true;
1337+
1338+
if (_currentBranch is not { IsDetachedHead: true })
1339+
return true;
1340+
1341+
var refs = new Commands.QueryRefsContainsCommit(_fullpath, _currentBranch.Head).Result();
1342+
if (refs.Count == 0)
1343+
{
1344+
var confirmCheckout = false;
1345+
var resetEvent = new AutoResetEvent(false);
1346+
1347+
Dispatcher.UIThread.Invoke(() =>
1348+
{
1349+
var msg = App.Text("Checkout.WarnLostCommits");
1350+
App.ShowWindow(new Confirm(msg, () =>
1351+
{
1352+
confirmCheckout = true;
1353+
resetEvent.Set();
1354+
}, () =>
1355+
{
1356+
confirmCheckout = false;
1357+
resetEvent.Set();
1358+
}), true);
1359+
});
1360+
1361+
resetEvent.WaitOne();
1362+
return confirmCheckout;
1363+
}
1364+
1365+
return true;
1366+
}
1367+
13331368
public void CheckoutBranch(Models.Branch branch)
13341369
{
13351370
if (branch.IsLocal)

0 commit comments

Comments
 (0)