Skip to content
This repository was archived by the owner on Dec 5, 2024. It is now read-only.

Commit 417fc58

Browse files
authored
Merge pull request #66 from github-for-unity/fixes/new-branch-name-validation
Adding validation to branch creation
2 parents 346eba5 + 673a1b6 commit 417fc58

File tree

6 files changed

+97
-4
lines changed

6 files changed

+97
-4
lines changed

src/GitHub.Api/GitHub.Api.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@
104104
<Compile Include="Application\ApplicationManagerBase.cs" />
105105
<Compile Include="Helpers\Constants.cs" />
106106
<Compile Include="Cache\IBranchCache.cs" />
107+
<Compile Include="Helpers\Validation.cs" />
107108
<Compile Include="Platform\DefaultEnvironment.cs" />
108109
<Compile Include="Extensions\EnvironmentExtensions.cs" />
109110
<Compile Include="Extensions\FileEventExtensions.cs" />

src/GitHub.Api/Helpers/Validation.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System;
2+
using System.Text.RegularExpressions;
3+
4+
namespace GitHub.Unity.Helpers
5+
{
6+
public static class Validation
7+
{
8+
public static bool IsBranchNameValid(string branchName)
9+
{
10+
return !String.IsNullOrEmpty(branchName)
11+
&& !branchName.Equals(".")
12+
&& !branchName.StartsWith("/")
13+
&& !branchName.EndsWith("/")
14+
&& !branchName.StartsWith(".")
15+
&& !branchName.EndsWith(".")
16+
&& !branchName.Contains("//")
17+
&& !branchName.Contains(@"\")
18+
&& !branchName.EndsWith(".lock")
19+
&& BranchNameRegex.IsMatch(branchName);
20+
}
21+
22+
public static readonly Regex BranchNameRegex = new Regex(@"^(?<name>[\.\w\d\/\-_]+)$");
23+
}
24+
}

src/UnityExtension/Assets/Editor/GitHub.Unity/Misc/Utility.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ class Utility : ScriptableObject
2828
public static readonly Regex StatusAheadBehindRegex =
2929
new Regex(
3030
@"\[ahead (?<ahead>\d+), behind (?<behind>\d+)\]|\[ahead (?<ahead>\d+)\]|\[behind (?<behind>\d+>)\]");
31-
public static readonly Regex BranchNameRegex = new Regex(@"^(?<name>[\w\d\/\-\_]+)$");
3231

3332
private static bool ready;
3433
private static Action onReady;

src/UnityExtension/Assets/Editor/GitHub.Unity/UI/BranchesView.cs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq;
4+
using GitHub.Unity.Helpers;
45
using UnityEditor;
56
using UnityEngine;
7+
using Debug = System.Diagnostics.Debug;
68

79
namespace GitHub.Unity
810
{
@@ -17,6 +19,7 @@ class BranchesView : Subview
1719
private const string NewBranchConfirmButton = "Create";
1820
private const string FavoritesSetting = "Favorites";
1921
private const string FavoritesTitle = "Favorites";
22+
private const string CreateBranchTitle = "Create Branch";
2023
private const string LocalTitle = "Local branches";
2124
private const string RemoteTitle = "Remote branches";
2225
private const string CreateBranchButton = "New Branch";
@@ -451,8 +454,7 @@ private void OnCreateGUI()
451454
var cancelCreate = false;
452455
var cannotCreate = selectedNode == null ||
453456
selectedNode.Type == NodeType.Folder ||
454-
newBranchName == null ||
455-
!Utility.BranchNameRegex.IsMatch(newBranchName);
457+
!Validation.IsBranchNameValid(newBranchName);
456458

457459
// Create on return/enter or cancel on escape
458460
var offsetID = GUIUtility.GetControlID(FocusType.Passive);
@@ -498,7 +500,21 @@ private void OnCreateGUI()
498500
if (createBranch)
499501
{
500502
GitClient.CreateBranch(newBranchName, selectedNode.Name)
501-
.FinallyInUI((success, e) => { if (success) Refresh(); })
503+
.FinallyInUI((success, e) => {
504+
if (success)
505+
{
506+
Refresh();
507+
}
508+
else
509+
{
510+
var errorHeader = "fatal: ";
511+
var errorMessage = e.Message.StartsWith(errorHeader) ? e.Message.Remove(0, errorHeader.Length) : e.Message;
512+
513+
EditorUtility.DisplayDialog(CreateBranchTitle,
514+
errorMessage,
515+
Localization.Ok);
516+
}
517+
})
502518
.Start();
503519
}
504520

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
using System;
2+
using System.Linq;
3+
using FluentAssertions;
4+
using GitHub.Unity.Helpers;
5+
using NUnit.Framework;
6+
7+
namespace UnitTests
8+
{
9+
[TestFixture]
10+
public class ValidationTests
11+
{
12+
[TestCase(true, "feature1", TestName = "Branch name is valid")]
13+
[TestCase(true, "feature-1", TestName = "Branch name with hyphen is valid")]
14+
[TestCase(true, "feature.1", TestName = "Branch name can contain dots")]
15+
[TestCase(true, "feature..1", TestName = "Branch name cannot contain consecutive dots")]
16+
[TestCase(false, "feature 1", TestName = "Branch name cannot contain a space")]
17+
[TestCase(false, "feature~1", TestName = "Branch name cannot contain a ~")]
18+
[TestCase(false, "feature^1", TestName = "Branch name cannot contain a ^")]
19+
[TestCase(false, "feature:1", TestName = "Branch name cannot contain a :")]
20+
[TestCase(false, "feature?1", TestName = "Branch name cannot contain a ?")]
21+
[TestCase(false, "feature*1", TestName = "Branch name cannot contain a *")]
22+
[TestCase(false, "feature[1", TestName = "Branch name cannot contain a [")]
23+
[TestCase(false, "/feature1", TestName = "Branch name cannot begin with a slash")]
24+
[TestCase(false, "feature1/", TestName = "Branch name cannot end with a slash")]
25+
[TestCase(false, "feature1.", TestName = "Branch name cannot end with a dot")]
26+
[TestCase(false, "feature1.lock", TestName = "Branch name cannot end with .lock")]
27+
[TestCase(true, "a", TestName = "Single character is valid")]
28+
[TestCase(false, "@", TestName = "Single character cannot be @")]
29+
[TestCase(false, ".", TestName = "Single character cannot be [period]")]
30+
[TestCase(true, "features/feature-1", TestName = "Folder and branch name is valid")]
31+
[TestCase(false, @"features\feature-1", TestName = "Backslash is not a valid character")]
32+
[TestCase(false, ".hidden", TestName = "Branch name is not valid when starting with [period]")]
33+
[TestCase(false, "features//feature-1", TestName = "Multiple consecutive slashes are not valid")]
34+
[TestCase(false, null, TestName = "null string is not valid")]
35+
[TestCase(false, "", TestName = "Empty string is not valid")]
36+
[TestCase(false, "/", TestName = "Single slash is not valid")]
37+
[TestCase(false, "asdf@{", TestName = "Sequence @{ is not valid")]
38+
public void TestFeatureString(bool isValid, string branch)
39+
{
40+
Validation.IsBranchNameValid(branch).Should().Be(isValid);
41+
}
42+
43+
[TestCase(true, 65, 65, 65, TestName = "Can test with ascii values")]
44+
[TestCase(false, 65, 65, 31, TestName = "No individual ASCII value should be < octal(40) or dec(32)")]
45+
[TestCase(false, 65, 65, 127, TestName = "No individual ASCII value should = octal(177) or dec(127)")]
46+
public void TestFeatureStringFromAsciiArray(bool isValid, params int[] asciiValues)
47+
{
48+
var branch = new string(asciiValues.Select(Convert.ToChar).ToArray());
49+
Validation.IsBranchNameValid(branch).Should().Be(isValid);
50+
}
51+
}
52+
}

src/tests/UnitTests/UnitTests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
</ItemGroup>
8282
<ItemGroup>
8383
<Compile Include="Authentication\KeychainTests.cs" />
84+
<Compile Include="Git\ValidationTests.cs" />
8485
<Compile Include="Git\GitConfigTests.cs" />
8586
<Compile Include="IO\BranchListOutputProcessorTests.cs" />
8687
<Compile Include="Extensions\EnvironmentExtensionTests.cs" />

0 commit comments

Comments
 (0)