Skip to content

Commit 4b18753

Browse files
Enrique Raso BarberoEnrique Raso Barbero
authored andcommitted
Refactor ReferenceName class
1 parent a4bf971 commit 4b18753

File tree

1 file changed

+23
-22
lines changed

1 file changed

+23
-22
lines changed

src/GitVersion.Core/Git/ReferenceName.cs

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,36 +11,37 @@ public class ReferenceName : IEquatable<ReferenceName?>, IComparable<ReferenceNa
1111
private const string LocalBranchPrefix = "refs/heads/";
1212
private const string RemoteTrackingBranchPrefix = "refs/remotes/";
1313
private const string TagPrefix = "refs/tags/";
14-
private const string PullRequestPrefix1 = "refs/pull/";
15-
private const string PullRequestPrefix2 = "refs/pull-requests/";
16-
private const string RemotePullRequestPrefix1 = "refs/remotes/pull/";
17-
private const string RemotePullRequestPrefix2 = "refs/remotes/pull-requests/";
14+
private static readonly string[] PullRequestPrefixes =
15+
{
16+
"refs/pull/",
17+
"refs/pull-requests/",
18+
"refs/remotes/pull/",
19+
"refs/remotes/pull-requests/"
20+
};
1821

1922
public ReferenceName(string canonical)
2023
{
2124
Canonical = canonical.NotNull();
22-
Friendly = Shorten();
23-
WithoutRemote = RemoveRemote();
2425

2526
IsBranch = IsPrefixedBy(Canonical, LocalBranchPrefix);
2627
IsRemoteBranch = IsPrefixedBy(Canonical, RemoteTrackingBranchPrefix);
2728
IsTag = IsPrefixedBy(Canonical, TagPrefix);
28-
IsPullRequest = IsPrefixedBy(Canonical, PullRequestPrefix1)
29-
|| IsPrefixedBy(Canonical, PullRequestPrefix2)
30-
|| IsPrefixedBy(Canonical, RemotePullRequestPrefix1)
31-
|| IsPrefixedBy(Canonical, RemotePullRequestPrefix2);
29+
IsPullRequest = IsPrefixedBy(Canonical, PullRequestPrefixes);
30+
31+
Friendly = Shorten();
32+
WithoutRemote = RemoveRemote();
3233
}
3334

3435
public static ReferenceName Parse(string canonicalName)
3536
{
3637
if (IsPrefixedBy(canonicalName, LocalBranchPrefix)
3738
|| IsPrefixedBy(canonicalName, RemoteTrackingBranchPrefix)
3839
|| IsPrefixedBy(canonicalName, TagPrefix)
39-
|| IsPrefixedBy(canonicalName, PullRequestPrefix1)
40-
|| IsPrefixedBy(canonicalName, PullRequestPrefix2))
40+
|| IsPrefixedBy(canonicalName, PullRequestPrefixes))
4141
{
4242
return new ReferenceName(canonicalName);
4343
}
44+
4445
throw new ArgumentException($"The {nameof(canonicalName)} is not a Canonical name");
4546
}
4647

@@ -67,30 +68,30 @@ public bool EquivalentTo(string? name) =>
6768

6869
private string Shorten()
6970
{
70-
if (IsPrefixedBy(Canonical, LocalBranchPrefix))
71+
if (IsBranch)
7172
return Canonical.Substring(LocalBranchPrefix.Length);
72-
if (IsPrefixedBy(Canonical, RemoteTrackingBranchPrefix))
73+
74+
if (IsRemoteBranch)
7375
return Canonical.Substring(RemoteTrackingBranchPrefix.Length);
74-
if (IsPrefixedBy(Canonical, TagPrefix))
76+
77+
if (IsTag)
7578
return Canonical.Substring(TagPrefix.Length);
79+
7680
return Canonical;
7781
}
7882

7983
private string RemoveRemote()
8084
{
81-
var isRemote = IsPrefixedBy(Canonical, RemoteTrackingBranchPrefix);
82-
83-
if (isRemote)
85+
if (IsRemoteBranch)
8486
{
85-
var isPullRequest = IsPrefixedBy(Canonical, RemotePullRequestPrefix1)
86-
|| IsPrefixedBy(Canonical, RemotePullRequestPrefix2);
87-
88-
if (!isPullRequest)
87+
if (!IsPullRequest)
8988
return Friendly.Substring(Friendly.IndexOf("/", StringComparison.Ordinal) + 1);
9089
}
9190

9291
return Friendly;
9392
}
9493

9594
private static bool IsPrefixedBy(string input, string prefix) => input.StartsWith(prefix, StringComparison.Ordinal);
95+
96+
private static bool IsPrefixedBy(string input, string[] prefixes) => prefixes.Any(prefix => IsPrefixedBy(input, prefix));
9697
}

0 commit comments

Comments
 (0)