feature: branch name allows spaces and auto replace spaces with dashes (#917)

This commit is contained in:
leo 2025-01-24 12:10:56 +08:00
parent ab080b53b1
commit 37c4545875
No known key found for this signature in database
6 changed files with 35 additions and 10 deletions

View file

@ -78,5 +78,8 @@ namespace SourceGit.Converters
return v.Substring(13);
return v;
});
public static readonly FuncValueConverter<string, bool> ContainsSpaces =
new FuncValueConverter<string, bool>(v => v != null && v.Contains(' '));
}
}

View file

@ -201,6 +201,7 @@
<x:String x:Key="Text.CreateBranch.LocalChanges.StashAndReply" xml:space="preserve">Stash &amp; Reapply</x:String>
<x:String x:Key="Text.CreateBranch.Name" xml:space="preserve">New Branch Name:</x:String>
<x:String x:Key="Text.CreateBranch.Name.Placeholder" xml:space="preserve">Enter branch name.</x:String>
<x:String x:Key="Text.CreateBranch.Name.WarnSpace" xml:space="preserve">Spaces will be replaced with dashes.</x:String>
<x:String x:Key="Text.CreateBranch.Title" xml:space="preserve">Create Local Branch</x:String>
<x:String x:Key="Text.CreateTag" xml:space="preserve">Create Tag...</x:String>
<x:String x:Key="Text.CreateTag.BasedOn" xml:space="preserve">New Tag At:</x:String>

View file

@ -204,6 +204,7 @@
<x:String x:Key="Text.CreateBranch.LocalChanges.StashAndReply" xml:space="preserve">贮藏并自动恢复</x:String>
<x:String x:Key="Text.CreateBranch.Name" xml:space="preserve">新分支名 </x:String>
<x:String x:Key="Text.CreateBranch.Name.Placeholder" xml:space="preserve">填写分支名称。</x:String>
<x:String x:Key="Text.CreateBranch.Name.WarnSpace" xml:space="preserve">空格将被替换为'-'符号</x:String>
<x:String x:Key="Text.CreateBranch.Title" xml:space="preserve">创建本地分支</x:String>
<x:String x:Key="Text.CreateTag" xml:space="preserve">新建标签 ...</x:String>
<x:String x:Key="Text.CreateTag.BasedOn" xml:space="preserve">标签位于 </x:String>

View file

@ -204,6 +204,7 @@
<x:String x:Key="Text.CreateBranch.LocalChanges.StashAndReply" xml:space="preserve">擱置變更並自動復原</x:String>
<x:String x:Key="Text.CreateBranch.Name" xml:space="preserve">新分支名稱:</x:String>
<x:String x:Key="Text.CreateBranch.Name.Placeholder" xml:space="preserve">輸入分支名稱。</x:String>
<x:String x:Key="Text.CreateBranch.Name.WarnSpace" xml:space="preserve">空格將以英文破折號取代</x:String>
<x:String x:Key="Text.CreateBranch.Title" xml:space="preserve">建立本機分支</x:String>
<x:String x:Key="Text.CreateTag" xml:space="preserve">新增標籤...</x:String>
<x:String x:Key="Text.CreateTag.BasedOn" xml:space="preserve">標籤位於:</x:String>

View file

@ -6,7 +6,7 @@ namespace SourceGit.ViewModels
public class CreateBranch : Popup
{
[Required(ErrorMessage = "Branch name is required!")]
[RegularExpression(@"^[\w\-/\.#]+$", ErrorMessage = "Bad branch name format!")]
[RegularExpression(@"^[\w \-/\.#]+$", ErrorMessage = "Bad branch name format!")]
[CustomValidation(typeof(CreateBranch), nameof(ValidateBranchName))]
public string Name
{
@ -74,9 +74,10 @@ namespace SourceGit.ViewModels
if (creator == null)
return new ValidationResult("Missing runtime context to create branch!");
var fixedName = creator.FixName(name);
foreach (var b in creator._repo.Branches)
{
if (b.FriendlyName == name)
if (b.FriendlyName == fixedName)
return new ValidationResult("A branch with same name already exists!");
}
@ -86,6 +87,8 @@ namespace SourceGit.ViewModels
public override Task<bool> Sure()
{
_repo.SetWatcherEnabled(false);
var fixedName = FixName(_name);
return Task.Run(() =>
{
var succ = false;
@ -114,8 +117,8 @@ namespace SourceGit.ViewModels
}
}
SetProgressDescription($"Create new branch '{_name}'");
succ = new Commands.Checkout(_repo.FullPath).Branch(_name, _baseOnRevision, SetProgressDescription);
SetProgressDescription($"Create new branch '{fixedName}'");
succ = new Commands.Checkout(_repo.FullPath).Branch(fixedName, _baseOnRevision, SetProgressDescription);
if (needPopStash)
{
@ -125,15 +128,15 @@ namespace SourceGit.ViewModels
}
else
{
SetProgressDescription($"Create new branch '{_name}'");
succ = Commands.Branch.Create(_repo.FullPath, _name, _baseOnRevision);
SetProgressDescription($"Create new branch '{fixedName}'");
succ = Commands.Branch.Create(_repo.FullPath, fixedName, _baseOnRevision);
}
CallUIThread(() =>
{
if (succ && CheckoutAfterCreated)
{
var fake = new Models.Branch() { IsLocal = true, FullName = $"refs/heads/{_name}" };
var fake = new Models.Branch() { IsLocal = true, FullName = $"refs/heads/{fixedName}" };
if (BasedOn is Models.Branch based && !based.IsLocal)
fake.Upstream = based.FullName;
@ -153,6 +156,15 @@ namespace SourceGit.ViewModels
});
}
private string FixName(string name)
{
if (!name.Contains(' '))
return name;
var parts = name.Split(' ', System.StringSplitOptions.RemoveEmptyEntries);
return string.Join("-", parts);
}
private readonly Repository _repo = null;
private string _name = null;
private readonly string _baseOnRevision = null;

View file

@ -20,6 +20,7 @@
<RowDefinition Height="32"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Grid.Column="0"
@ -63,13 +64,19 @@
Text="{Binding Name, Mode=TwoWay}"
Watermark="{DynamicResource Text.CreateBranch.Name.Placeholder}"
v:AutoFocusBehaviour.IsEnabled="True"/>
<StackPanel Grid.Row="2" Grid.Column="1"
Orientation="Horizontal"
IsVisible="{Binding Name, Converter={x:Static c:StringConverters.ContainsSpaces}}">
<Path Width="10" Height="10" Data="{StaticResource Icons.Error}" Fill="DarkOrange"/>
<TextBlock Classes="small" Text="{DynamicResource Text.CreateBranch.Name.WarnSpace}" Margin="4,0,0,0"/>
</StackPanel>
<TextBlock Grid.Row="2" Grid.Column="0"
<TextBlock Grid.Row="3" Grid.Column="0"
HorizontalAlignment="Right" VerticalAlignment="Center"
Margin="0,0,8,0"
Text="{DynamicResource Text.CreateBranch.LocalChanges}"
IsVisible="{Binding !IsBareRepository}"/>
<Border Grid.Row="2" Grid.Column="1" MinHeight="32" IsVisible="{Binding !IsBareRepository}">
<Border Grid.Row="3" Grid.Column="1" MinHeight="32" IsVisible="{Binding !IsBareRepository}">
<WrapPanel Orientation="Horizontal" VerticalAlignment="Center">
<RadioButton Content="{DynamicResource Text.CreateBranch.LocalChanges.DoNothing}"
x:Name="RadioDoNothing"
@ -88,7 +95,7 @@
</WrapPanel>
</Border>
<CheckBox Grid.Row="3" Grid.Column="1"
<CheckBox Grid.Row="4" Grid.Column="1"
Content="{DynamicResource Text.CreateBranch.Checkout}"
IsChecked="{Binding CheckoutAfterCreated, Mode=TwoWay}"
IsVisible="{Binding !IsBareRepository}"/>