Skip to content

Commit 76a197a

Browse files
committed
feature: supports to overwrite existing branch while creating new branch (#1349)
Signed-off-by: leo <[email protected]>
1 parent 594ffc0 commit 76a197a

File tree

9 files changed

+73
-35
lines changed

9 files changed

+73
-35
lines changed

src/Commands/Branch.cs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
namespace SourceGit.Commands
1+
using System.Text;
2+
3+
namespace SourceGit.Commands
24
{
35
public static class Branch
46
{
@@ -11,12 +13,20 @@ public static string ShowCurrent(string repo)
1113
return cmd.ReadToEnd().StdOut.Trim();
1214
}
1315

14-
public static bool Create(string repo, string name, string basedOn, Models.ICommandLog log)
16+
public static bool Create(string repo, string name, string basedOn, bool force, Models.ICommandLog log)
1517
{
18+
var builder = new StringBuilder();
19+
builder.Append("branch ");
20+
if (force)
21+
builder.Append("-f ");
22+
builder.Append(name);
23+
builder.Append(" ");
24+
builder.Append(basedOn);
25+
1626
var cmd = new Command();
1727
cmd.WorkingDirectory = repo;
1828
cmd.Context = repo;
19-
cmd.Args = $"branch {name} {basedOn}";
29+
cmd.Args = builder.ToString();
2030
cmd.Log = log;
2131
return cmd.Exec();
2232
}
@@ -31,16 +41,6 @@ public static bool Rename(string repo, string name, string to, Models.ICommandLo
3141
return cmd.Exec();
3242
}
3343

34-
public static bool ResetWithoutCheckout(string repo, string name, string to, Models.ICommandLog log)
35-
{
36-
var cmd = new Command();
37-
cmd.WorkingDirectory = repo;
38-
cmd.Context = repo;
39-
cmd.Args = $"branch -f {name} {to}";
40-
cmd.Log = log;
41-
return cmd.Exec();
42-
}
43-
4444
public static bool SetUpstream(string repo, string name, string upstream, Models.ICommandLog log)
4545
{
4646
var cmd = new Command();

src/Commands/Checkout.cs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,28 @@ public Checkout(string repo)
1313

1414
public bool Branch(string branch, bool force)
1515
{
16-
var option = force ? "--force" : string.Empty;
17-
Args = $"checkout {option} --progress {branch}";
16+
var builder = new StringBuilder();
17+
builder.Append("checkout --progress ");
18+
if (force)
19+
builder.Append("--force ");
20+
builder.Append(branch);
21+
22+
Args = builder.ToString();
1823
return Exec();
1924
}
2025

21-
public bool Branch(string branch, string basedOn, bool force)
26+
public bool Branch(string branch, string basedOn, bool force, bool allowOverwrite)
2227
{
23-
var option = force ? "--force" : string.Empty;
24-
Args = $"checkout --progress -b {branch} {basedOn}";
28+
var builder = new StringBuilder();
29+
builder.Append("checkout --progress ");
30+
builder.Append(allowOverwrite ? "-B " : "-b ");
31+
if (force)
32+
builder.Append("--force ");
33+
builder.Append(branch);
34+
builder.Append(" ");
35+
builder.Append(basedOn);
36+
37+
Args = builder.ToString();
2538
return Exec();
2639
}
2740

src/Resources/Locales/en_US.axaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@
217217
<x:String x:Key="Text.CreateBranch.Name.Placeholder" xml:space="preserve">Enter branch name.</x:String>
218218
<x:String x:Key="Text.CreateBranch.Name.WarnSpace" xml:space="preserve">Spaces will be replaced with dashes.</x:String>
219219
<x:String x:Key="Text.CreateBranch.Title" xml:space="preserve">Create Local Branch</x:String>
220+
<x:String x:Key="Text.CreateBranch.OverwriteExisting" xml:space="preserve">Overwrite existing branch</x:String>
220221
<x:String x:Key="Text.CreateTag" xml:space="preserve">Create Tag...</x:String>
221222
<x:String x:Key="Text.CreateTag.BasedOn" xml:space="preserve">New Tag At:</x:String>
222223
<x:String x:Key="Text.CreateTag.GPGSign" xml:space="preserve">GPG signing</x:String>

src/Resources/Locales/zh_CN.axaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@
221221
<x:String x:Key="Text.CreateBranch.Name.Placeholder" xml:space="preserve">填写分支名称。</x:String>
222222
<x:String x:Key="Text.CreateBranch.Name.WarnSpace" xml:space="preserve">空格将被替换为'-'符号</x:String>
223223
<x:String x:Key="Text.CreateBranch.Title" xml:space="preserve">创建本地分支</x:String>
224+
<x:String x:Key="Text.CreateBranch.OverwriteExisting" xml:space="preserve">允许重置已存在的分支</x:String>
224225
<x:String x:Key="Text.CreateTag" xml:space="preserve">新建标签 ...</x:String>
225226
<x:String x:Key="Text.CreateTag.BasedOn" xml:space="preserve">标签位于 :</x:String>
226227
<x:String x:Key="Text.CreateTag.GPGSign" xml:space="preserve">使用GPG签名</x:String>

src/Resources/Locales/zh_TW.axaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@
221221
<x:String x:Key="Text.CreateBranch.Name.Placeholder" xml:space="preserve">輸入分支名稱。</x:String>
222222
<x:String x:Key="Text.CreateBranch.Name.WarnSpace" xml:space="preserve">空格將以英文破折號取代</x:String>
223223
<x:String x:Key="Text.CreateBranch.Title" xml:space="preserve">建立本機分支</x:String>
224+
<x:String x:Key="Text.CreateBranch.OverwriteExisting" xml:space="preserve">允許覆寫現有分支</x:String>
224225
<x:String x:Key="Text.CreateTag" xml:space="preserve">新增標籤...</x:String>
225226
<x:String x:Key="Text.CreateTag.BasedOn" xml:space="preserve">標籤位於:</x:String>
226227
<x:String x:Key="Text.CreateTag.GPGSign" xml:space="preserve">使用 GPG 簽章</x:String>

src/ViewModels/CreateBranch.cs

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,16 @@ public bool IsBareRepository
4343
get => _repo.IsBare;
4444
}
4545

46+
public bool AllowOverwrite
47+
{
48+
get => _allowOverwrite;
49+
set
50+
{
51+
if (SetProperty(ref _allowOverwrite, value))
52+
ValidateProperty(_name, nameof(Name));
53+
}
54+
}
55+
4656
public bool IsRecurseSubmoduleVisible
4757
{
4858
get => _repo.Submodules.Count > 0;
@@ -88,18 +98,24 @@ public CreateBranch(Repository repo, Models.Tag tag)
8898

8999
public static ValidationResult ValidateBranchName(string name, ValidationContext ctx)
90100
{
91-
var creator = ctx.ObjectInstance as CreateBranch;
92-
if (creator == null)
93-
return new ValidationResult("Missing runtime context to create branch!");
101+
if (ctx.ObjectInstance is CreateBranch creator)
102+
{
103+
if (!creator._allowOverwrite)
104+
{
105+
var fixedName = creator.FixName(name);
106+
foreach (var b in creator._repo.Branches)
107+
{
108+
if (b.FriendlyName == fixedName)
109+
return new ValidationResult("A branch with same name already exists!");
110+
}
111+
}
94112

95-
var fixedName = creator.FixName(name);
96-
foreach (var b in creator._repo.Branches)
113+
return ValidationResult.Success;
114+
}
115+
else
97116
{
98-
if (b.FriendlyName == fixedName)
99-
return new ValidationResult("A branch with same name already exists!");
117+
return new ValidationResult("Missing runtime context to create branch!");
100118
}
101-
102-
return ValidationResult.Success;
103119
}
104120

105121
public override Task<bool> Sure()
@@ -119,7 +135,7 @@ public override Task<bool> Sure()
119135
var needPopStash = false;
120136
if (DiscardLocalChanges)
121137
{
122-
succ = new Commands.Checkout(_repo.FullPath).Use(log).Branch(fixedName, _baseOnRevision, true);
138+
succ = new Commands.Checkout(_repo.FullPath).Use(log).Branch(fixedName, _baseOnRevision, true, _allowOverwrite);
123139
}
124140
else
125141
{
@@ -137,7 +153,7 @@ public override Task<bool> Sure()
137153
needPopStash = true;
138154
}
139155

140-
succ = new Commands.Checkout(_repo.FullPath).Use(log).Branch(fixedName, _baseOnRevision, false);
156+
succ = new Commands.Checkout(_repo.FullPath).Use(log).Branch(fixedName, _baseOnRevision, false, _allowOverwrite);
141157
}
142158

143159
if (succ)
@@ -155,7 +171,7 @@ public override Task<bool> Sure()
155171
}
156172
else
157173
{
158-
succ = Commands.Branch.Create(_repo.FullPath, fixedName, _baseOnRevision, log);
174+
succ = Commands.Branch.Create(_repo.FullPath, fixedName, _baseOnRevision, _allowOverwrite, log);
159175
}
160176

161177
log.Complete();
@@ -201,5 +217,6 @@ private string FixName(string name)
201217
private readonly Repository _repo = null;
202218
private string _name = null;
203219
private readonly string _baseOnRevision = null;
220+
private bool _allowOverwrite = false;
204221
}
205222
}

src/ViewModels/InitGitFlow.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ public override Task<bool> Sure()
116116
var masterBranch = _repo.Branches.Find(x => x.IsLocal && x.Name.Equals(_master, StringComparison.Ordinal));
117117
if (masterBranch == null)
118118
{
119-
succ = Commands.Branch.Create(_repo.FullPath, _master, current.Head, log);
119+
succ = Commands.Branch.Create(_repo.FullPath, _master, current.Head, true, log);
120120
if (!succ)
121121
{
122122
log.Complete();
@@ -128,7 +128,7 @@ public override Task<bool> Sure()
128128
var developBranch = _repo.Branches.Find(x => x.IsLocal && x.Name.Equals(_develop, StringComparison.Ordinal));
129129
if (developBranch == null)
130130
{
131-
succ = Commands.Branch.Create(_repo.FullPath, _develop, current.Head, log);
131+
succ = Commands.Branch.Create(_repo.FullPath, _develop, current.Head, true, log);
132132
if (!succ)
133133
{
134134
log.Complete();

src/ViewModels/ResetWithoutCheckout.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public override Task<bool> Sure()
4040

4141
return Task.Run(() =>
4242
{
43-
var succ = Commands.Branch.ResetWithoutCheckout(_repo.FullPath, Target.Name, _revision, log);
43+
var succ = Commands.Branch.Create(_repo.FullPath, Target.Name, _revision, true, log);
4444
log.Complete();
4545
CallUIThread(() => _repo.SetWatcherEnabled(true));
4646
return succ;

src/Views/CreateBranch.axaml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<TextBlock FontSize="18"
1515
Classes="bold"
1616
Text="{DynamicResource Text.CreateBranch.Title}"/>
17-
<Grid Margin="0,16,0,0" RowDefinitions="32,32,Auto,Auto,Auto,Auto" ColumnDefinitions="140,*">
17+
<Grid Margin="0,16,0,0" RowDefinitions="32,32,Auto,32,Auto,Auto,Auto" ColumnDefinitions="140,*">
1818
<TextBlock Grid.Row="0" Grid.Column="0"
1919
HorizontalAlignment="Right" VerticalAlignment="Center"
2020
Margin="0,0,8,0"
@@ -80,11 +80,16 @@
8080
</Border>
8181

8282
<CheckBox Grid.Row="4" Grid.Column="1"
83+
Content="{DynamicResource Text.CreateBranch.OverwriteExisting}"
84+
IsChecked="{Binding AllowOverwrite, Mode=TwoWay}"
85+
ToolTip.Tip="checkout -B or branch -f"/>
86+
87+
<CheckBox Grid.Row="5" Grid.Column="1"
8388
Content="{DynamicResource Text.CreateBranch.Checkout}"
8489
IsChecked="{Binding CheckoutAfterCreated, Mode=TwoWay}"
8590
IsVisible="{Binding !IsBareRepository}"/>
8691

87-
<CheckBox Grid.Row="5" Grid.Column="1"
92+
<CheckBox Grid.Row="6" Grid.Column="1"
8893
Content="{DynamicResource Text.Checkout.RecurseSubmodules}"
8994
IsChecked="{Binding RecurseSubmodules, Mode=TwoWay}"
9095
ToolTip.Tip="--recurse-submodules">

0 commit comments

Comments
 (0)