Skip to content

Commit 71a743c

Browse files
authored
enhance: use regex to fix branch names (#1510)
1 parent 81fe7af commit 71a743c

File tree

2 files changed

+16
-16
lines changed

2 files changed

+16
-16
lines changed

src/ViewModels/CreateBranch.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
using System;
22
using System.ComponentModel.DataAnnotations;
3+
using System.Text.RegularExpressions;
34
using System.Threading.Tasks;
45

56
namespace SourceGit.ViewModels
67
{
7-
public class CreateBranch : Popup
8+
public partial class CreateBranch : Popup
89
{
910
[Required(ErrorMessage = "Branch name is required!")]
1011
[RegularExpression(@"^[\w \-/\.#\+]+$", ErrorMessage = "Bad branch name format!")]
@@ -101,7 +102,7 @@ public static ValidationResult ValidateBranchName(string name, ValidationContext
101102
{
102103
if (!creator._allowOverwrite)
103104
{
104-
var fixedName = creator.FixName(name);
105+
var fixedName = FixName(name);
105106
foreach (var b in creator._repo.Branches)
106107
{
107108
if (b.FriendlyName == fixedName)
@@ -227,13 +228,12 @@ public override async Task<bool> Sure()
227228
return true;
228229
}
229230

230-
private string FixName(string name)
231-
{
232-
if (!name.Contains(' '))
233-
return name;
231+
[GeneratedRegex(@"\s+")]
232+
private static partial Regex REG_FIX_NAME();
234233

235-
var parts = name.Split(' ', System.StringSplitOptions.RemoveEmptyEntries);
236-
return string.Join("-", parts);
234+
private static string FixName(string name)
235+
{
236+
return REG_FIX_NAME().Replace(name, "-");
237237
}
238238

239239
private readonly Repository _repo = null;

src/ViewModels/RenameBranch.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
using System;
22
using System.ComponentModel.DataAnnotations;
3+
using System.Text.RegularExpressions;
34
using System.Threading.Tasks;
45

56
namespace SourceGit.ViewModels
67
{
7-
public class RenameBranch : Popup
8+
public partial class RenameBranch : Popup
89
{
910
public Models.Branch Target
1011
{
@@ -31,7 +32,7 @@ public static ValidationResult ValidateBranchName(string name, ValidationContext
3132
{
3233
if (ctx.ObjectInstance is RenameBranch rename)
3334
{
34-
var fixedName = rename.FixName(name);
35+
var fixedName = FixName(name);
3536
foreach (var b in rename._repo.Branches)
3637
{
3738
if (b.IsLocal && b != rename.Target && b.Name == fixedName)
@@ -87,13 +88,12 @@ public override async Task<bool> Sure()
8788
return succ;
8889
}
8990

90-
private string FixName(string name)
91-
{
92-
if (!name.Contains(' '))
93-
return name;
91+
[GeneratedRegex(@"\s+")]
92+
private static partial Regex REG_FIX_NAME();
9493

95-
var parts = name.Split(' ', StringSplitOptions.RemoveEmptyEntries);
96-
return string.Join("-", parts);
94+
private static string FixName(string name)
95+
{
96+
return REG_FIX_NAME().Replace(name, "-");
9797
}
9898

9999
private readonly Repository _repo;

0 commit comments

Comments
 (0)