diff --git a/src/Converters/StringConverters.cs b/src/Converters/StringConverters.cs index e6f4237c..5e4608c5 100644 --- a/src/Converters/StringConverters.cs +++ b/src/Converters/StringConverters.cs @@ -78,5 +78,8 @@ namespace SourceGit.Converters return v.Substring(13); return v; }); + + public static readonly FuncValueConverter ContainsSpaces = + new FuncValueConverter(v => v != null && v.Contains(' ')); } } diff --git a/src/Resources/Locales/en_US.axaml b/src/Resources/Locales/en_US.axaml index 3cfff3d0..40409b9e 100644 --- a/src/Resources/Locales/en_US.axaml +++ b/src/Resources/Locales/en_US.axaml @@ -201,6 +201,7 @@ Stash & Reapply New Branch Name: Enter branch name. + Spaces will be replaced with dashes. Create Local Branch Create Tag... New Tag At: diff --git a/src/Resources/Locales/zh_CN.axaml b/src/Resources/Locales/zh_CN.axaml index d12fa07c..cdbdcc94 100644 --- a/src/Resources/Locales/zh_CN.axaml +++ b/src/Resources/Locales/zh_CN.axaml @@ -204,6 +204,7 @@ 贮藏并自动恢复 新分支名 : 填写分支名称。 + 空格将被替换为'-'符号 创建本地分支 新建标签 ... 标签位于 : diff --git a/src/Resources/Locales/zh_TW.axaml b/src/Resources/Locales/zh_TW.axaml index 6be685bb..52f4754f 100644 --- a/src/Resources/Locales/zh_TW.axaml +++ b/src/Resources/Locales/zh_TW.axaml @@ -204,6 +204,7 @@ 擱置變更並自動復原 新分支名稱: 輸入分支名稱。 + 空格將以英文破折號取代 建立本機分支 新增標籤... 標籤位於: diff --git a/src/ViewModels/CreateBranch.cs b/src/ViewModels/CreateBranch.cs index b67a453a..a9698a07 100644 --- a/src/ViewModels/CreateBranch.cs +++ b/src/ViewModels/CreateBranch.cs @@ -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 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; diff --git a/src/Views/CreateBranch.axaml b/src/Views/CreateBranch.axaml index 49bfda8d..dff42516 100644 --- a/src/Views/CreateBranch.axaml +++ b/src/Views/CreateBranch.axaml @@ -20,6 +20,7 @@ + + + + + - - + -