Skip to content

Commit e430e84

Browse files
committed
enhance: auto convert spaces with dashes while renaming a branch (#1088)
Signed-off-by: leo <[email protected]>
1 parent 7331167 commit e430e84

File tree

3 files changed

+27
-16
lines changed

3 files changed

+27
-16
lines changed

src/ViewModels/RenameBranch.cs

+16-5
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public Models.Branch Target
1212
}
1313

1414
[Required(ErrorMessage = "Branch name is required!!!")]
15-
[RegularExpression(@"^[\w\-/\.#]+$", ErrorMessage = "Bad branch name format!")]
15+
[RegularExpression(@"^[\w \-/\.#]+$", ErrorMessage = "Bad branch name format!")]
1616
[CustomValidation(typeof(RenameBranch), nameof(ValidateBranchName))]
1717
public string Name
1818
{
@@ -32,9 +32,10 @@ public static ValidationResult ValidateBranchName(string name, ValidationContext
3232
{
3333
if (ctx.ObjectInstance is RenameBranch rename)
3434
{
35+
var fixedName = rename.FixName(name);
3536
foreach (var b in rename._repo.Branches)
3637
{
37-
if (b.IsLocal && b != rename.Target && b.Name == name)
38+
if (b.IsLocal && b != rename.Target && b.Name == fixedName)
3839
{
3940
return new ValidationResult("A branch with same name already exists!!!");
4041
}
@@ -46,7 +47,8 @@ public static ValidationResult ValidateBranchName(string name, ValidationContext
4647

4748
public override Task<bool> Sure()
4849
{
49-
if (_name == Target.Name)
50+
var fixedName = FixName(_name);
51+
if (fixedName == Target.Name)
5052
return null;
5153

5254
_repo.SetWatcherEnabled(false);
@@ -55,7 +57,7 @@ public override Task<bool> Sure()
5557
return Task.Run(() =>
5658
{
5759
var oldName = Target.FullName;
58-
var succ = Commands.Branch.Rename(_repo.FullPath, Target.Name, _name);
60+
var succ = Commands.Branch.Rename(_repo.FullPath, Target.Name, fixedName);
5961
CallUIThread(() =>
6062
{
6163
if (succ)
@@ -65,7 +67,7 @@ public override Task<bool> Sure()
6567
if (filter.Type == Models.FilterType.LocalBranch &&
6668
filter.Pattern.Equals(oldName, StringComparison.Ordinal))
6769
{
68-
filter.Pattern = $"refs/heads/{_name}";
70+
filter.Pattern = $"refs/heads/{fixedName}";
6971
break;
7072
}
7173
}
@@ -78,6 +80,15 @@ public override Task<bool> Sure()
7880
});
7981
}
8082

83+
private string FixName(string name)
84+
{
85+
if (!name.Contains(' '))
86+
return name;
87+
88+
var parts = name.Split(' ', StringSplitOptions.RemoveEmptyEntries);
89+
return string.Join("-", parts);
90+
}
91+
8192
private readonly Repository _repo;
8293
private string _name;
8394
}

src/Views/CreateBranch.axaml

+2-10
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,7 @@
1414
<TextBlock FontSize="18"
1515
Classes="bold"
1616
Text="{DynamicResource Text.CreateBranch.Title}"/>
17-
<Grid Margin="0,16,0,0" ColumnDefinitions="140,*">
18-
<Grid.RowDefinitions>
19-
<RowDefinition Height="32"/>
20-
<RowDefinition Height="32"/>
21-
<RowDefinition Height="Auto"/>
22-
<RowDefinition Height="Auto"/>
23-
<RowDefinition Height="Auto"/>
24-
</Grid.RowDefinitions>
25-
17+
<Grid Margin="0,16,0,0" RowDefinitions="32,32,Auto,Auto,Auto" ColumnDefinitions="140,*">
2618
<TextBlock Grid.Row="0" Grid.Column="0"
2719
HorizontalAlignment="Right" VerticalAlignment="Center"
2820
Margin="0,0,8,0"
@@ -94,7 +86,7 @@
9486
IsCheckedChanged="OnLocalChangeActionIsCheckedChanged"/>
9587
</WrapPanel>
9688
</Border>
97-
89+
9890
<CheckBox Grid.Row="4" Grid.Column="1"
9991
Content="{DynamicResource Text.CreateBranch.Checkout}"
10092
IsChecked="{Binding CheckoutAfterCreated, Mode=TwoWay}"

src/Views/RenameBranch.axaml

+9-1
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@
44
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
55
xmlns:vm="using:SourceGit.ViewModels"
66
xmlns:v="using:SourceGit.Views"
7+
xmlns:c="using:SourceGit.Converters"
78
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
89
x:Class="SourceGit.Views.RenameBranch"
910
x:DataType="vm:RenameBranch">
1011
<StackPanel Orientation="Vertical" Margin="8,0">
1112
<TextBlock FontSize="18"
1213
Classes="bold"
1314
Text="{DynamicResource Text.RenameBranch}"/>
14-
<Grid Margin="0,16,0,0" RowDefinitions="32,32" ColumnDefinitions="120,*">
15+
<Grid Margin="0,16,0,0" RowDefinitions="32,32,Auto" ColumnDefinitions="120,*">
1516
<TextBlock Grid.Row="0" Grid.Column="0"
1617
HorizontalAlignment="Right" VerticalAlignment="Center"
1718
Margin="0,0,8,0"
@@ -32,6 +33,13 @@
3233
Text="{Binding Name, Mode=TwoWay}"
3334
Watermark="{DynamicResource Text.RenameBranch.Name.Placeholder}"
3435
v:AutoFocusBehaviour.IsEnabled="True"/>
36+
37+
<StackPanel Grid.Row="2" Grid.Column="1"
38+
Orientation="Horizontal"
39+
IsVisible="{Binding Name, Converter={x:Static c:StringConverters.ContainsSpaces}}">
40+
<Path Width="10" Height="10" Data="{StaticResource Icons.Error}" Fill="DarkOrange"/>
41+
<TextBlock Classes="small" Text="{DynamicResource Text.CreateBranch.Name.WarnSpace}" Margin="4,0,0,0"/>
42+
</StackPanel>
3543
</Grid>
3644
</StackPanel>
3745
</UserControl>

0 commit comments

Comments
 (0)