Skip to content

Commit aba413d

Browse files
authored
feat: use the same rules as git for branch and tag name validation (#2626)
* feat: use the same rules as git for branch, tag and remote name validation * fix: keep previous validation for remote names
1 parent f4a6217 commit aba413d

7 files changed

Lines changed: 103 additions & 8 deletions

File tree

‎src/Models/RefName.cs‎

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
using System;
2+
3+
namespace SourceGit.Models
4+
{
5+
/// <summary>
6+
/// Validates branch and tag names using the same rules that git itself enforces,
7+
/// mirroring `git check-ref-format --allow-onelevel`. Rules are taken from the git-check-ref-format documentation.
8+
/// Rule 2 (a refname must contain at least one slash) is intentionally waived, matching `--allow-onelevel`.
9+
/// </summary>
10+
public static class RefName
11+
{
12+
public static bool IsValidBranchName(string name)
13+
{
14+
if (string.Equals(name, "HEAD", StringComparison.Ordinal))
15+
return false;
16+
17+
return IsValidRefName(name);
18+
}
19+
20+
public static bool IsValidTagName(string name) => IsValidRefName(name);
21+
22+
private static bool IsValidRefName(string name)
23+
{
24+
if (string.IsNullOrEmpty(name))
25+
return false;
26+
27+
// Anything starting with '-' is treated as a CLI option.
28+
if (name.StartsWith('-'))
29+
return false;
30+
31+
// Rule 9: cannot be the single character '@'.
32+
if (name.Equals("@", StringComparison.Ordinal))
33+
return false;
34+
35+
// Rule 6: cannot begin or end with '/', or contain consecutive slashes.
36+
if (name[0] == '/' || name[^1] == '/' || name.Contains("//", StringComparison.Ordinal))
37+
return false;
38+
39+
// Rule 7: cannot end with a dot.
40+
if (name[^1] == '.')
41+
return false;
42+
43+
// Rule 3: cannot contain two consecutive dots.
44+
if (name.Contains("..", StringComparison.Ordinal))
45+
return false;
46+
47+
// Rule 8: cannot contain the sequence '@{'.
48+
if (name.Contains("@{", StringComparison.Ordinal))
49+
return false;
50+
51+
// Rules 4, 5 & 10: no control chars, DEL, space, or ~ ^ : ? * [ \.
52+
foreach (var ch in name)
53+
{
54+
if (ch is < ' ' or '\x7f')
55+
return false;
56+
57+
switch (ch)
58+
{
59+
case ' ':
60+
case '~':
61+
case '^':
62+
case ':':
63+
case '?':
64+
case '*':
65+
case '[':
66+
case '\\':
67+
return false;
68+
}
69+
}
70+
71+
// Rule 1: no slash-separated component may begin with a dot or end with ".lock".
72+
foreach (var component in name.Split('/'))
73+
{
74+
if (component[0] == '.')
75+
return false;
76+
77+
if (component.EndsWith(".lock", StringComparison.Ordinal))
78+
return false;
79+
}
80+
81+
return true;
82+
}
83+
}
84+
}

‎src/ViewModels/CheckoutBranchFromStash.cs‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ public Models.Stash Target
1212
}
1313

1414
[Required(ErrorMessage = "Branch name is required!")]
15-
[RegularExpression(@"^[\w\-/\.#\+]+$", ErrorMessage = "Bad branch name format!")]
1615
[CustomValidation(typeof(CheckoutBranchFromStash), nameof(ValidateBranchName))]
1716
public string BranchName
1817
{
@@ -30,6 +29,9 @@ public static ValidationResult ValidateBranchName(string name, ValidationContext
3029
{
3130
if (ctx.ObjectInstance is CheckoutBranchFromStash caller)
3231
{
32+
if (!Models.RefName.IsValidBranchName(name))
33+
return new ValidationResult("Bad branch name format!");
34+
3335
foreach (var b in caller._repo.Branches)
3436
{
3537
if (b.FriendlyName.Equals(name, StringComparison.Ordinal))

‎src/ViewModels/CreateBranch.cs‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ namespace SourceGit.ViewModels
77
public class CreateBranch : Popup
88
{
99
[Required(ErrorMessage = "Branch name is required!")]
10-
[RegularExpression(@"^[\w\-/\.#\+]+$", ErrorMessage = "Bad branch name format!")]
1110
[CustomValidation(typeof(CreateBranch), nameof(ValidateBranchName))]
1211
public string Name
1312
{
@@ -115,6 +114,9 @@ public static ValidationResult ValidateBranchName(string name, ValidationContext
115114
{
116115
if (ctx.ObjectInstance is CreateBranch creator)
117116
{
117+
if (!Models.RefName.IsValidBranchName(name))
118+
return new ValidationResult("Bad branch name format!");
119+
118120
if (!creator._allowOverwrite)
119121
{
120122
foreach (var b in creator._repo.Branches)

‎src/ViewModels/CreateTag.cs‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ public object BasedOn
1313
}
1414

1515
[Required(ErrorMessage = "Tag name is required!")]
16-
[RegularExpression(@"^(?!\.)(?!/)(?!.*\.$)(?!.*/$)(?!.*\.\.)[\w\-\+\./]+$", ErrorMessage = "Bad tag name format!")]
1716
[CustomValidation(typeof(CreateTag), nameof(ValidateTagName))]
1817
public string TagName
1918
{
@@ -74,6 +73,9 @@ public static ValidationResult ValidateTagName(string name, ValidationContext ct
7473
{
7574
if (ctx.ObjectInstance is CreateTag creator)
7675
{
76+
if (!Models.RefName.IsValidTagName(name))
77+
return new ValidationResult("Bad tag name format!");
78+
7779
var found = creator._repo.Tags.Find(x => x.Name == name);
7880
if (found != null)
7981
return new ValidationResult("A tag with same name already exists!");

‎src/ViewModels/GitFlowStart.cs‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ public string Prefix
3232
}
3333

3434
[Required(ErrorMessage = "Name is required!!!")]
35-
[RegularExpression(@"^[\w\-/\.#]+$", ErrorMessage = "Bad branch name format!")]
3635
[CustomValidation(typeof(GitFlowStart), nameof(ValidateBranchName))]
3736
public string Name
3837
{
@@ -67,6 +66,9 @@ public static ValidationResult ValidateBranchName(string name, ValidationContext
6766
{
6867
if (ctx.ObjectInstance is GitFlowStart starter)
6968
{
69+
if (!Models.RefName.IsValidBranchName($"{starter.Prefix}{name}"))
70+
return new ValidationResult("Bad branch name format!");
71+
7072
var check = $"{starter.Prefix}{name}";
7173
foreach (var b in starter._repo.Branches)
7274
{

‎src/ViewModels/InitGitFlow.cs‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ public partial class InitGitFlow : Popup
1212
private static partial Regex REG_TAG_PREFIX();
1313

1414
[Required(ErrorMessage = "Production branch name is required!!!")]
15-
[RegularExpression(@"^[\w\-/\.]+$", ErrorMessage = "Bad branch name format!")]
1615
[CustomValidation(typeof(InitGitFlow), nameof(ValidateBaseBranch))]
1716
public string Production
1817
{
@@ -21,7 +20,6 @@ public string Production
2120
}
2221

2322
[Required(ErrorMessage = "Development branch name is required!!!")]
24-
[RegularExpression(@"^[\w\-/\.]+$", ErrorMessage = "Bad branch name format!")]
2523
[CustomValidation(typeof(InitGitFlow), nameof(ValidateBaseBranch))]
2624
public string Develop
2725
{
@@ -81,10 +79,13 @@ public InitGitFlow(Repository repo)
8179
_production = "main";
8280
}
8381

84-
public static ValidationResult ValidateBaseBranch(string _, ValidationContext ctx)
82+
public static ValidationResult ValidateBaseBranch(string name, ValidationContext ctx)
8583
{
8684
if (ctx.ObjectInstance is InitGitFlow initializer)
8785
{
86+
if (!Models.RefName.IsValidBranchName(name))
87+
return new ValidationResult("Bad branch name format!");
88+
8889
if (initializer._production == initializer._develop)
8990
return new ValidationResult("Develop branch has the same name with production branch!");
9091
}

‎src/ViewModels/RenameBranch.cs‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ public Models.Branch Target
1212
}
1313

1414
[Required(ErrorMessage = "Branch name is required!!!")]
15-
[RegularExpression(@"^[\w\-/\.#\+]+$", ErrorMessage = "Bad branch name format!")]
1615
[CustomValidation(typeof(RenameBranch), nameof(ValidateBranchName))]
1716
public string Name
1817
{
@@ -31,6 +30,9 @@ public static ValidationResult ValidateBranchName(string name, ValidationContext
3130
{
3231
if (ctx.ObjectInstance is RenameBranch rename)
3332
{
33+
if (!Models.RefName.IsValidBranchName(name))
34+
return new ValidationResult("Bad branch name format!");
35+
3436
foreach (var b in rename._repo.Branches)
3537
{
3638
if (b.IsLocal && b != rename.Target && b.Name.Equals(name, StringComparison.Ordinal))

0 commit comments

Comments
 (0)