Skip to content

Commit 8c823e2

Browse files
committed
refactor(core): neutral merge-requests ref API and single prefix source
1 parent b64aa8c commit 8c823e2

5 files changed

Lines changed: 29 additions & 18 deletions

File tree

src/GitVersion.Core/Core/GitPreparer.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -236,8 +236,8 @@ private void EnsureHeadIsAttachedToBranch(string? currentBranchName, Authenticat
236236
var matchingCurrentBranch = !currentBranchName.IsNullOrEmpty()
237237
? localBranchesWhereCommitShaIsHead.SingleOrDefault(b =>
238238
{
239-
if (ReferenceName.TryParseGitLabMergeRequestRef(currentBranchName, out var iid))
240-
return b.Name.Canonical == ReferenceName.LocalBranchPrefix + ReferenceName.GitLabMergeRequestFriendlyName(iid);
239+
if (ReferenceName.TryParseMergeRequestsRef(currentBranchName, out var mergeRequestId))
240+
return b.Name.Canonical == ReferenceName.LocalBranchPrefix + ReferenceName.MergeRequestsRefFriendlyName(mergeRequestId);
241241
return b.Name.Canonical.Replace("/heads/", "/") == currentBranchName.Replace("/heads/", "/");
242242
})
243243
: null;
@@ -385,8 +385,8 @@ public void EnsureLocalBranchExistsForCurrentBranch(IRemote remote, string? curr
385385
const string referencePrefix = "refs/";
386386
var isLocalBranch = currentBranch.StartsWith(ReferenceName.LocalBranchPrefix);
387387
string localCanonicalName;
388-
if (ReferenceName.TryParseGitLabMergeRequestRef(currentBranch, out var gitLabIid))
389-
localCanonicalName = ReferenceName.LocalBranchPrefix + ReferenceName.GitLabMergeRequestFriendlyName(gitLabIid);
388+
if (ReferenceName.TryParseMergeRequestsRef(currentBranch, out var mergeRequestId))
389+
localCanonicalName = ReferenceName.LocalBranchPrefix + ReferenceName.MergeRequestsRefFriendlyName(mergeRequestId);
390390
else
391391
localCanonicalName = !currentBranch.StartsWith(referencePrefix)
392392
? ReferenceName.LocalBranchPrefix + currentBranch

src/GitVersion.Core/Core/RepositoryStore.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,9 @@ public IBranch GetTargetBranch(string? targetBranchName)
7373
if (targetBranchName.IsNullOrEmpty())
7474
return desiredBranch;
7575

76-
if (ReferenceName.TryParseGitLabMergeRequestRef(targetBranchName, out var gitLabIid))
76+
if (ReferenceName.TryParseMergeRequestsRef(targetBranchName, out var mergeRequestId))
7777
{
78-
var prBranch = FindBranch(ReferenceName.GitLabMergeRequestFriendlyName(gitLabIid));
78+
var prBranch = FindBranch(ReferenceName.MergeRequestsRefFriendlyName(mergeRequestId));
7979
if (prBranch != null)
8080
return prBranch;
8181
}

src/GitVersion.Core/Git/ReferenceName.cs

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,12 @@ public class ReferenceName : IEquatable<ReferenceName?>, IComparable<ReferenceNa
2323
"refs/remotes/pull-requests/"
2424
];
2525

26-
private const string GitLabMergeRequestRefPrefix = "refs/merge-requests/";
26+
/// <summary>
27+
/// The sole <see cref="PullRequestPrefixes" /> entry for <c>refs/merge-requests/&lt;id&gt;/head|merge</c>.
28+
/// Adding another prefix that also contains <c>/merge-requests/</c> will fail at type initialization.
29+
/// </summary>
30+
private static readonly string mergeRequestsRefPrefix = PullRequestPrefixes.Single(
31+
p => p.Contains("/merge-requests/", StringComparison.Ordinal));
2732

2833
public ReferenceName(string canonical)
2934
{
@@ -88,23 +93,27 @@ public bool EquivalentTo(string? name) =>
8893
|| WithoutOrigin.Equals(name, StringComparison.OrdinalIgnoreCase);
8994

9095
/// <summary>
91-
/// Detects GitLab MR ref (refs/merge-requests/&lt;iid&gt;/head or /merge) and extracts IID for pull-requests config.
96+
/// Parses canonical refs under refs/merge-requests/&lt;id&gt;/head or /merge (convention used by some Git hosts) and extracts the merge-request id.
9297
/// </summary>
93-
public static bool TryParseGitLabMergeRequestRef(string? canonicalRef, out int iid)
98+
public static bool TryParseMergeRequestsRef(string? canonicalRef, out int mergeRequestId)
9499
{
95-
iid = 0;
96-
if (string.IsNullOrEmpty(canonicalRef) || !canonicalRef.StartsWith(GitLabMergeRequestRefPrefix, StringComparison.Ordinal))
100+
mergeRequestId = 0;
101+
if (string.IsNullOrEmpty(canonicalRef) || !canonicalRef.StartsWith(mergeRequestsRefPrefix, StringComparison.Ordinal))
97102
return false;
98-
var after = canonicalRef.Substring(GitLabMergeRequestRefPrefix.Length);
103+
var after = canonicalRef.Substring(mergeRequestsRefPrefix.Length);
99104
var slash = after.IndexOf('/');
100105
if (slash <= 0 || slash >= after.Length - 1) return false;
101106
var suffix = after[(slash + 1)..];
102107
if (!suffix.Equals("head", StringComparison.OrdinalIgnoreCase) && !suffix.Equals("merge", StringComparison.OrdinalIgnoreCase))
103108
return false;
104-
return int.TryParse(after.Substring(0, slash), System.Globalization.NumberStyles.None, System.Globalization.CultureInfo.InvariantCulture, out iid)
105-
&& iid > 0;
109+
return int.TryParse(after.Substring(0, slash), System.Globalization.NumberStyles.None, System.Globalization.CultureInfo.InvariantCulture, out mergeRequestId);
106110
}
107111

112+
/// <summary>
113+
/// Returns the branch-style name pull-requests/&lt;id&gt; for default pull-request configuration matching.
114+
/// </summary>
115+
public static string MergeRequestsRefFriendlyName(int mergeRequestId) => $"pull-requests/{mergeRequestId}";
116+
108117
private string Shorten()
109118
{
110119
if (IsLocalBranch)
@@ -116,8 +125,8 @@ private string Shorten()
116125
if (IsTag)
117126
return Canonical[TagPrefix.Length..];
118127

119-
if (TryParseGitLabMergeRequestRef(Canonical, out var iid))
120-
return $"pull-requests/{iid}";
128+
if (TryParseMergeRequestsRef(Canonical, out var mergeRequestId))
129+
return MergeRequestsRefFriendlyName(mergeRequestId);
121130

122131
return Canonical;
123132
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
#nullable enable
2+
static GitVersion.Git.ReferenceName.MergeRequestsRefFriendlyName(int mergeRequestId) -> string!
3+
static GitVersion.Git.ReferenceName.TryParseMergeRequestsRef(string? canonicalRef, out int mergeRequestId) -> bool

src/GitVersion.LibGit2Sharp/Git/GitRepository.mutating.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ public void CreateBranchForPullRequestBranch(AuthenticationInfo auth) => Reposit
7070
}
7171
else if (referenceName.IsPullRequest)
7272
{
73-
var fakeBranchName = ReferenceName.TryParseGitLabMergeRequestRef(canonicalName, out var gitLabIid)
74-
? $"{ReferenceName.LocalBranchPrefix}{ReferenceName.GitLabMergeRequestFriendlyName(gitLabIid)}"
73+
var fakeBranchName = ReferenceName.TryParseMergeRequestsRef(canonicalName, out var mergeRequestId)
74+
? $"{ReferenceName.LocalBranchPrefix}{ReferenceName.MergeRequestsRefFriendlyName(mergeRequestId)}"
7575
: canonicalName.Replace("refs/pull/", "refs/heads/pull/").Replace("refs/pull-requests/", "refs/heads/pull-requests/");
7676

7777
this.log.Info($"Creating fake local branch '{fakeBranchName}'.");

0 commit comments

Comments
 (0)