Skip to content

Commit 37c4545

Browse files
committed
feature: branch name allows spaces and auto replace spaces with dashes (#917)
1 parent ab080b5 commit 37c4545

File tree

6 files changed

+35
-10
lines changed

6 files changed

+35
-10
lines changed

src/Converters/StringConverters.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,5 +78,8 @@ public object ConvertBack(object value, Type targetType, object parameter, Cultu
7878
return v.Substring(13);
7979
return v;
8080
});
81+
82+
public static readonly FuncValueConverter<string, bool> ContainsSpaces =
83+
new FuncValueConverter<string, bool>(v => v != null && v.Contains(' '));
8184
}
8285
}

src/Resources/Locales/en_US.axaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@
201201
<x:String x:Key="Text.CreateBranch.LocalChanges.StashAndReply" xml:space="preserve">Stash &amp; Reapply</x:String>
202202
<x:String x:Key="Text.CreateBranch.Name" xml:space="preserve">New Branch Name:</x:String>
203203
<x:String x:Key="Text.CreateBranch.Name.Placeholder" xml:space="preserve">Enter branch name.</x:String>
204+
<x:String x:Key="Text.CreateBranch.Name.WarnSpace" xml:space="preserve">Spaces will be replaced with dashes.</x:String>
204205
<x:String x:Key="Text.CreateBranch.Title" xml:space="preserve">Create Local Branch</x:String>
205206
<x:String x:Key="Text.CreateTag" xml:space="preserve">Create Tag...</x:String>
206207
<x:String x:Key="Text.CreateTag.BasedOn" xml:space="preserve">New Tag At:</x:String>

src/Resources/Locales/zh_CN.axaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@
204204
<x:String x:Key="Text.CreateBranch.LocalChanges.StashAndReply" xml:space="preserve">贮藏并自动恢复</x:String>
205205
<x:String x:Key="Text.CreateBranch.Name" xml:space="preserve">新分支名 :</x:String>
206206
<x:String x:Key="Text.CreateBranch.Name.Placeholder" xml:space="preserve">填写分支名称。</x:String>
207+
<x:String x:Key="Text.CreateBranch.Name.WarnSpace" xml:space="preserve">空格将被替换为'-'符号</x:String>
207208
<x:String x:Key="Text.CreateBranch.Title" xml:space="preserve">创建本地分支</x:String>
208209
<x:String x:Key="Text.CreateTag" xml:space="preserve">新建标签 ...</x:String>
209210
<x:String x:Key="Text.CreateTag.BasedOn" 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
@@ -204,6 +204,7 @@
204204
<x:String x:Key="Text.CreateBranch.LocalChanges.StashAndReply" xml:space="preserve">擱置變更並自動復原</x:String>
205205
<x:String x:Key="Text.CreateBranch.Name" xml:space="preserve">新分支名稱:</x:String>
206206
<x:String x:Key="Text.CreateBranch.Name.Placeholder" xml:space="preserve">輸入分支名稱。</x:String>
207+
<x:String x:Key="Text.CreateBranch.Name.WarnSpace" xml:space="preserve">空格將以英文破折號取代</x:String>
207208
<x:String x:Key="Text.CreateBranch.Title" xml:space="preserve">建立本機分支</x:String>
208209
<x:String x:Key="Text.CreateTag" xml:space="preserve">新增標籤...</x:String>
209210
<x:String x:Key="Text.CreateTag.BasedOn" xml:space="preserve">標籤位於:</x:String>

src/ViewModels/CreateBranch.cs

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace SourceGit.ViewModels
66
public class CreateBranch : Popup
77
{
88
[Required(ErrorMessage = "Branch name is required!")]
9-
[RegularExpression(@"^[\w\-/\.#]+$", ErrorMessage = "Bad branch name format!")]
9+
[RegularExpression(@"^[\w \-/\.#]+$", ErrorMessage = "Bad branch name format!")]
1010
[CustomValidation(typeof(CreateBranch), nameof(ValidateBranchName))]
1111
public string Name
1212
{
@@ -74,9 +74,10 @@ public static ValidationResult ValidateBranchName(string name, ValidationContext
7474
if (creator == null)
7575
return new ValidationResult("Missing runtime context to create branch!");
7676

77+
var fixedName = creator.FixName(name);
7778
foreach (var b in creator._repo.Branches)
7879
{
79-
if (b.FriendlyName == name)
80+
if (b.FriendlyName == fixedName)
8081
return new ValidationResult("A branch with same name already exists!");
8182
}
8283

@@ -86,6 +87,8 @@ public static ValidationResult ValidateBranchName(string name, ValidationContext
8687
public override Task<bool> Sure()
8788
{
8889
_repo.SetWatcherEnabled(false);
90+
91+
var fixedName = FixName(_name);
8992
return Task.Run(() =>
9093
{
9194
var succ = false;
@@ -114,8 +117,8 @@ public override Task<bool> Sure()
114117
}
115118
}
116119

117-
SetProgressDescription($"Create new branch '{_name}'");
118-
succ = new Commands.Checkout(_repo.FullPath).Branch(_name, _baseOnRevision, SetProgressDescription);
120+
SetProgressDescription($"Create new branch '{fixedName}'");
121+
succ = new Commands.Checkout(_repo.FullPath).Branch(fixedName, _baseOnRevision, SetProgressDescription);
119122

120123
if (needPopStash)
121124
{
@@ -125,15 +128,15 @@ public override Task<bool> Sure()
125128
}
126129
else
127130
{
128-
SetProgressDescription($"Create new branch '{_name}'");
129-
succ = Commands.Branch.Create(_repo.FullPath, _name, _baseOnRevision);
131+
SetProgressDescription($"Create new branch '{fixedName}'");
132+
succ = Commands.Branch.Create(_repo.FullPath, fixedName, _baseOnRevision);
130133
}
131134

132135
CallUIThread(() =>
133136
{
134137
if (succ && CheckoutAfterCreated)
135138
{
136-
var fake = new Models.Branch() { IsLocal = true, FullName = $"refs/heads/{_name}" };
139+
var fake = new Models.Branch() { IsLocal = true, FullName = $"refs/heads/{fixedName}" };
137140
if (BasedOn is Models.Branch based && !based.IsLocal)
138141
fake.Upstream = based.FullName;
139142

@@ -153,6 +156,15 @@ public override Task<bool> Sure()
153156
});
154157
}
155158

159+
private string FixName(string name)
160+
{
161+
if (!name.Contains(' '))
162+
return name;
163+
164+
var parts = name.Split(' ', System.StringSplitOptions.RemoveEmptyEntries);
165+
return string.Join("-", parts);
166+
}
167+
156168
private readonly Repository _repo = null;
157169
private string _name = null;
158170
private readonly string _baseOnRevision = null;

src/Views/CreateBranch.axaml

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
<RowDefinition Height="32"/>
2121
<RowDefinition Height="Auto"/>
2222
<RowDefinition Height="Auto"/>
23+
<RowDefinition Height="Auto"/>
2324
</Grid.RowDefinitions>
2425

2526
<TextBlock Grid.Row="0" Grid.Column="0"
@@ -63,13 +64,19 @@
6364
Text="{Binding Name, Mode=TwoWay}"
6465
Watermark="{DynamicResource Text.CreateBranch.Name.Placeholder}"
6566
v:AutoFocusBehaviour.IsEnabled="True"/>
67+
<StackPanel Grid.Row="2" Grid.Column="1"
68+
Orientation="Horizontal"
69+
IsVisible="{Binding Name, Converter={x:Static c:StringConverters.ContainsSpaces}}">
70+
<Path Width="10" Height="10" Data="{StaticResource Icons.Error}" Fill="DarkOrange"/>
71+
<TextBlock Classes="small" Text="{DynamicResource Text.CreateBranch.Name.WarnSpace}" Margin="4,0,0,0"/>
72+
</StackPanel>
6673

67-
<TextBlock Grid.Row="2" Grid.Column="0"
74+
<TextBlock Grid.Row="3" Grid.Column="0"
6875
HorizontalAlignment="Right" VerticalAlignment="Center"
6976
Margin="0,0,8,0"
7077
Text="{DynamicResource Text.CreateBranch.LocalChanges}"
7178
IsVisible="{Binding !IsBareRepository}"/>
72-
<Border Grid.Row="2" Grid.Column="1" MinHeight="32" IsVisible="{Binding !IsBareRepository}">
79+
<Border Grid.Row="3" Grid.Column="1" MinHeight="32" IsVisible="{Binding !IsBareRepository}">
7380
<WrapPanel Orientation="Horizontal" VerticalAlignment="Center">
7481
<RadioButton Content="{DynamicResource Text.CreateBranch.LocalChanges.DoNothing}"
7582
x:Name="RadioDoNothing"
@@ -88,7 +95,7 @@
8895
</WrapPanel>
8996
</Border>
9097

91-
<CheckBox Grid.Row="3" Grid.Column="1"
98+
<CheckBox Grid.Row="4" Grid.Column="1"
9299
Content="{DynamicResource Text.CreateBranch.Checkout}"
93100
IsChecked="{Binding CheckoutAfterCreated, Mode=TwoWay}"
94101
IsVisible="{Binding !IsBareRepository}"/>

0 commit comments

Comments
 (0)