Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/main/java/hudson/plugins/git/BranchSpec.java
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,9 @@ private Pattern getPattern(EnvVars env, String repositoryName) {
String regexSubstring = expandedName.substring(1, expandedName.length());
return Pattern.compile(regexSubstring);
}
if (expandedName.startsWith("refs/changes/")) {
return Pattern.compile(Pattern.quote(expandedName));
}
if (repositoryName != null) {
// remove the "refs/.../" stuff from the branch-spec if necessary
String pattern = cutRefs(expandedName)
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/jenkins/plugins/git/GitSCMFileSystem.java
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,9 @@
|| gscm.getBranches().get(0).getName().matches(
"^((\\Q" + Constants.R_TAGS + "\\E.*)|([^/]+)|(\\*/[^/*]+(/[^/*]+)*))$"
)
|| gscm.getBranches().get(0).getName().matches(

Check warning on line 270 in src/main/java/jenkins/plugins/git/GitSCMFileSystem.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 270 is only partially covered, one branch is missing
"^((\\Qrefs/changes/\\E.*)|([^/]+)|(\\*/[^/*]+(/[^/*]+)*))$"
)
);
// we only support where the branch spec is obvious and not a wildcard
}
Expand Down Expand Up @@ -306,6 +309,8 @@
String prefix = Constants.R_HEADS;
if (branchSpecExpandedName.startsWith(Constants.R_TAGS)) {
prefix = Constants.R_TAGS;
} else if (branchSpecExpandedName.startsWith("refs/changes")) {
prefix = "refs/changes/";
}

String headName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@
Tracks/checks out the specified tag.<br/>
E.g. <code>refs/tags/git-2.3.0</code>
</li>
<li> <strong><code>refs/changes/&lt;gerritChange&gt;</code></strong><br/>
Tracks/checks out the specified gerrit change.<br/>
E.g. <code>refs/changes/91/45391/1</code>
</li>
<li> <strong><code>&lt;commitId&gt;</code></strong><br/>
Checks out the specified commit.<br/>
E.g. <code>5062ac843f2b947733e6a3b105977056821bd352</code>, <code>5062ac84</code>, ...
Expand Down
8 changes: 8 additions & 0 deletions src/test/java/hudson/plugins/git/BranchSpecTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,14 @@ void testUsesRefsHeads() {
assertFalse(m.matches("remote/origin/jane"));
}

@Test
public void testMatchesGerritChangeRef() {
BranchSpec spec = new BranchSpec("refs/changes/91/45391/1");

assertTrue(spec.matches("refs/changes/91/45391/1"));
assertFalse(spec.matches("refs/heads/refs/changes/91/45391/1"));
}

@Test
void testUsesJavaPatternDirectlyIfPrefixedWithColon() {
BranchSpec m = new BranchSpec(":^(?!(origin/prefix)).*");
Expand Down
23 changes: 23 additions & 0 deletions src/test/java/jenkins/plugins/git/GitSCMFileSystemTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,29 @@ void create_SCMFileSystem_from_tag() throws Exception {
assertThat(file.contentAsString(), is("modified"));
}

@Test
public void testSupportsGerritChangeRef() throws Exception {
// Create a dummy GitSCM configured with a Gerrit change refspec
GitSCM scm = new GitSCM(
GitSCM.createRepoList("https://example.com/repo.git", null),
Collections.singletonList(new BranchSpec("refs/changes/91/45391/1")),
null, null, Collections.emptyList());

GitSCMFileSystem.BuilderImpl builder = new GitSCMFileSystem.BuilderImpl();
assertTrue(builder.supports(scm), "Builder should support refs/changes/ branch specs");
}

@Test
public void testHeadNameResultCalculateGerritChange() {
BranchSpec spec = new BranchSpec("refs/changes/91/45391/1");

GitSCMFileSystem.BuilderImpl.HeadNameResult result =
GitSCMFileSystem.BuilderImpl.HeadNameResult.calculate(spec, null, null);

assertEquals("refs/changes/", result.prefix);
assertEquals("91/45391/1", result.headName);
}

@Issue("JENKINS-52964")
@Test
void filesystem_supports_descriptor() throws Exception {
Expand Down
Loading