Skip to content

Commit 66f2650

Browse files
ckullaboschpanicking
authored andcommitted
refactor: Remove redundant headName field from HeadNameResult
The headName field in HeadNameResult was only used internally for constructing the remoteHeadName. Since remoteHeadName is now always a fully qualified reference (either refs/remotes/<remote>/<name> for branches or the raw name for tags/commits/FETCH_HEAD), the separate headName field is no longer needed. - Remove headName from HeadNameResult constructor and fields - Update callers to use remoteHeadName instead of headName - Update test assertions to verify remoteHeadName directly
1 parent 97a2781 commit 66f2650

2 files changed

Lines changed: 10 additions & 19 deletions

File tree

src/main/java/jenkins/plugins/git/GitSCMFileSystem.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -293,13 +293,11 @@ public boolean supportsDescriptor(SCMSourceDescriptor descriptor) {
293293
}
294294

295295
static class HeadNameResult {
296-
final String headName;
297296
final String remoteHeadName;
298297
final String refspec;
299298
final SCMRevision rev;
300299

301-
private HeadNameResult(String headName, String remoteHeadName, String refspec, SCMRevision rev) {
302-
this.headName = headName;
300+
private HeadNameResult(String remoteHeadName, String refspec, SCMRevision rev) {
303301
this.remoteHeadName = remoteHeadName;
304302
this.refspec = refspec;
305303
this.rev = rev;
@@ -370,7 +368,7 @@ static HeadNameResult calculate(@NonNull BranchSpec branchSpec,
370368
remoteHead = Constants.R_REMOTES + remoteName + "/" + calculatedHeadName;
371369
}
372370

373-
return new HeadNameResult(calculatedHeadName, remoteHead, refspecExpandedName, rev);
371+
return new HeadNameResult(remoteHead, refspecExpandedName, rev);
374372
}
375373
}
376374

@@ -450,7 +448,7 @@ public SCMFileSystem build(@NonNull Item owner, @NonNull SCM scm, @CheckForNull
450448
HeadNameResult headNameResult = HeadNameResult.calculate(branchSpec, rev, config.getRefspec(), env, remoteName);
451449
client.fetch_().prune(true).from(remoteURI, Collections.singletonList(new RefSpec(headNameResult.refspec))).execute();
452450

453-
listener.getLogger().println("Done with " + remoteName + " using " + headNameResult.headName + ".");
451+
listener.getLogger().println("Done with " + remoteName + " using " + headNameResult.remoteHeadName + ".");
454452
return new GitSCMFileSystem(client, remote, headNameResult.remoteHeadName, (AbstractGitSCMSource.SCMRevisionImpl) headNameResult.rev);
455453
} finally {
456454
cacheLock.unlock();

src/test/java/jenkins/plugins/git/GitSCMFileSystemTest.java

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -514,45 +514,42 @@ void filesystem_supports_descriptor() throws Exception {
514514
public void calculate_head_name_with_env() throws Exception {
515515
String remote = "origin";
516516
HeadNameResult result1 = HeadNameResult.calculate(new BranchSpec("${BRANCH}"), null, null, new EnvVars("BRANCH", "master-a"), remote);
517-
assertEquals("master-a", result1.headName);
517+
assertEquals("refs/remotes/origin/master-a", result1.remoteHeadName);
518518
assertTrue(result1.refspec.startsWith("+" + Constants.R_HEADS));
519519

520520
HeadNameResult result2 = HeadNameResult.calculate(new BranchSpec("${BRANCH}"), null, null, new EnvVars("BRANCH", "refs/heads/master-b"), remote);
521-
assertEquals("master-b", result2.headName);
521+
assertEquals("refs/remotes/origin/master-b", result2.remoteHeadName);
522522
assertTrue(result2.refspec.startsWith("+" + Constants.R_HEADS));
523523

524524
HeadNameResult result3 = HeadNameResult.calculate(new BranchSpec("refs/heads/${BRANCH}"), null, null, new EnvVars("BRANCH", "master-c"), remote);
525-
assertEquals("master-c", result3.headName);
525+
assertEquals("refs/remotes/origin/master-c", result3.remoteHeadName);
526526
assertTrue(result3.refspec.startsWith("+" + Constants.R_HEADS));
527527

528528
HeadNameResult result4 = HeadNameResult.calculate(new BranchSpec("${BRANCH}"), null, null, null, remote);
529-
assertEquals("${BRANCH}", result4.headName);
529+
assertEquals("refs/remotes/origin/${BRANCH}", result4.remoteHeadName);
530530
assertTrue(result4.refspec.startsWith("+" + Constants.R_HEADS));
531531

532532
HeadNameResult result5 = HeadNameResult.calculate(new BranchSpec("*/${BRANCH}"), null, null, new EnvVars("BRANCH", "master-d"), remote);
533-
assertEquals("master-d", result5.headName);
533+
assertEquals("refs/remotes/origin/master-d", result5.remoteHeadName);
534534
assertTrue(result5.refspec.startsWith("+" + Constants.R_HEADS));
535535

536536
HeadNameResult result6 = HeadNameResult.calculate(new BranchSpec("*/master-e"), null, null, new EnvVars("BRANCH", "dummy"), remote);
537-
assertEquals("master-e", result6.headName);
537+
assertEquals("refs/remotes/origin/master-e", result6.remoteHeadName);
538538
assertTrue(result6.refspec.startsWith("+" + Constants.R_HEADS));
539539
}
540540

541541
@Test
542542
public void calculate_head_name() throws Exception {
543543
String remote = "origin";
544544
HeadNameResult result1 = HeadNameResult.calculate(new BranchSpec("branch"), null, null, null, remote);
545-
assertEquals("branch", result1.headName);
546545
assertEquals("refs/remotes/origin/branch", result1.remoteHeadName);
547546
assertEquals("+refs/heads/branch:refs/remotes/origin/branch", result1.refspec);
548547

549548
HeadNameResult result2 = HeadNameResult.calculate(new BranchSpec("refs/heads/branch"), null, null, null, remote);
550-
assertEquals("branch", result2.headName);
551549
assertEquals("refs/remotes/origin/branch", result2.remoteHeadName);
552550
assertEquals("+refs/heads/branch:refs/remotes/origin/branch", result2.refspec);
553551

554552
HeadNameResult result3 = HeadNameResult.calculate(new BranchSpec("refs/tags/my-tag"), null, null, null, remote);
555-
assertEquals("my-tag", result3.headName);
556553
assertEquals("my-tag", result3.remoteHeadName);
557554
assertEquals("+refs/tags/my-tag:refs/tags/my-tag", result3.refspec);
558555
}
@@ -563,13 +560,11 @@ public void calculate_head_name_with_refspec_commit() throws Exception {
563560
String commit = "0123456789" + "0123456789" + "0123456789" + "0123456789";
564561
String branch = "branch";
565562
HeadNameResult result1 = HeadNameResult.calculate(new BranchSpec(commit), null, branch, null, remote);
566-
assertEquals(commit, result1.headName);
567563
assertEquals(commit, result1.remoteHeadName);
568564
assertEquals(branch, result1.refspec);
569565

570566
HeadNameResult result2 = HeadNameResult.calculate(new BranchSpec("${BRANCH}"), null, "${REFSPEC}",
571567
new EnvVars("BRANCH", commit, "REFSPEC", branch), remote);
572-
assertEquals(commit, result2.headName);
573568
assertEquals(commit, result2.remoteHeadName);
574569
assertEquals(branch, result2.refspec);
575570
}
@@ -578,13 +573,11 @@ public void calculate_head_name_with_refspec_commit() throws Exception {
578573
public void calculate_head_name_with_refspec_FETCH_HEAD() throws Exception {
579574
String remote = "origin";
580575
HeadNameResult result1 = HeadNameResult.calculate(new BranchSpec(Constants.FETCH_HEAD), null, "refs/changes/1/2/3", null, remote);
581-
assertEquals(Constants.FETCH_HEAD, result1.headName);
582576
assertEquals(Constants.FETCH_HEAD, result1.remoteHeadName);
583577
assertEquals("refs/changes/1/2/3", result1.refspec);
584578

585579
HeadNameResult result2 = HeadNameResult.calculate(new BranchSpec("${BRANCH}"), null, "${REFSPEC}",
586580
new EnvVars("BRANCH", Constants.FETCH_HEAD, "REFSPEC", "refs/changes/1/2/3"), remote);
587-
assertEquals(Constants.FETCH_HEAD, result2.headName);
588581
assertEquals(Constants.FETCH_HEAD, result2.remoteHeadName);
589582
assertEquals("refs/changes/1/2/3", result2.refspec);
590583
}
@@ -600,7 +593,7 @@ void null_pointer_exception() throws Exception {
600593
AbstractGitSCMSource.SCMRevisionImpl rev260 =
601594
new AbstractGitSCMSource.SCMRevisionImpl(new SCMHead("origin"), git260.getName());
602595
HeadNameResult result1 = HeadNameResult.calculate(new BranchSpec("master-f"), rev260, null, null, "origin");
603-
assertEquals("master-f", result1.headName);
596+
assertEquals("refs/remotes/origin/master-f", result1.remoteHeadName);
604597
assertTrue(result1.refspec.startsWith("+" + Constants.R_HEADS));
605598
}
606599
}

0 commit comments

Comments
 (0)