Skip to content

Commit

Permalink
Adapt GitAPITest for CLI git 2.48.0
Browse files Browse the repository at this point in the history
Fetch from CLI git 2.48.0 and later return origin/HEAD

https://github.blog/open-source/git/highlights-from-git-2-48/ says:

With Git 2.48, if the remote has a default branch but refs/remotes/origin/HEAD
is missing locally, then a fetch will update it.

This test was unintentionally testing the behavior of CLI git before
2.48.0.  Other tests in GitClientFetchTest were testing that origin/HEAD
was reported as a branch.
  • Loading branch information
MarkEWaite committed Jan 14, 2025
1 parent 5bb9304 commit 65ad746
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 25 deletions.
11 changes: 0 additions & 11 deletions src/main/java/org/jenkinsci/plugins/gitclient/CliGitAPIImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.lib.RepositoryBuilder;
import org.eclipse.jgit.lib.SymbolicRef;
import org.eclipse.jgit.transport.RefSpec;
import org.eclipse.jgit.transport.RemoteConfig;
import org.eclipse.jgit.transport.URIish;
Expand Down Expand Up @@ -3031,16 +3030,6 @@ public Set<Branch> getRemoteBranches() throws GitException, InterruptedException

for (Ref candidate : refs) {
if (candidate.getName().startsWith(Constants.R_REMOTES)) {
if (candidate instanceof SymbolicRef && candidate.getName().endsWith("/" + Constants.HEAD)) {
/* Skip HEAD symbolic ref returned as a remote ref in CLI git 2.48.0 and later.
*
* https://github.blog/open-source/git/highlights-from-git-2-48/ says:
*
* With Git 2.48, if the remote has a default branch but refs/remotes/origin/HEAD
* is missing locally, then a fetch will update it.
*/
continue;
}
Branch buildBranch = new Branch(candidate);
if (!GitClient.quietRemoteBranches) {
listener.getLogger().println("Seen branch in repository " + buildBranch.getName());
Expand Down
13 changes: 0 additions & 13 deletions src/main/java/org/jenkinsci/plugins/gitclient/JGitAPIImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.lib.RepositoryBuilder;
import org.eclipse.jgit.lib.StoredConfig;
import org.eclipse.jgit.lib.SymbolicRef;
import org.eclipse.jgit.merge.MergeStrategy;
import org.eclipse.jgit.notes.Note;
import org.eclipse.jgit.revwalk.RevCommit;
Expand Down Expand Up @@ -656,18 +655,6 @@ public Set<Branch> getBranchesInternal(ListBranchCommand.ListMode mode) throws G
List<Ref> refs = git(repo).branchList().setListMode(mode).call();
Set<Branch> branches = new HashSet<>(refs.size());
for (Ref ref : refs) {
if (ref instanceof SymbolicRef
&& mode.equals(ListBranchCommand.ListMode.REMOTE)
&& ref.getName().endsWith("/" + Constants.HEAD)) {
/* Skip HEAD symbolic ref returned as a remote ref in CLI git 2.48.0 and later
*
* https://github.blog/open-source/git/highlights-from-git-2-48/ says:
*
* With Git 2.48, if the remote has a default branch but refs/remotes/origin/HEAD
* is missing locally, then a fetch will update it.
*/
continue;
}
branches.add(new Branch(ref));
}
return branches;
Expand Down
21 changes: 20 additions & 1 deletion src/test/java/org/jenkinsci/plugins/gitclient/GitAPITest.java
Original file line number Diff line number Diff line change
Expand Up @@ -758,7 +758,26 @@ public void testListRemoteBranches() throws Exception {
workspace.launchCommand("git", "fetch", "origin");
Set<Branch> branches = testGitClient.getRemoteBranches();
assertBranchesExist(branches, "origin/" + defaultBranchName, "origin/test", "origin/another");
assertEquals("Wrong branch count, found " + branches.size() + " branches: " + branches, 3, branches.size());
int branchCount = 3;
if (workspace.cgit().isAtLeastVersion(2, 48, 0, 0)) {
/* Fetch from CLI git 2.48.0 and later return origin/HEAD
*
* https://github.blog/open-source/git/highlights-from-git-2-48/ says:
*
* With Git 2.48, if the remote has a default branch but refs/remotes/origin/HEAD
* is missing locally, then a fetch will update it.
*
* This test was unintentionally testing the behavior of CLI git before
* 2.48.0. Other tests in GitClientFetchTest were testing that origin/HEAD
* was reported as a branch.
*/
assertBranchesExist(branches, "origin/HEAD");
branchCount = 4;
}
assertEquals(
"Wrong branch count, found " + branches.size() + " branches: " + branches,
branchCount,
branches.size());
}

@Test
Expand Down

0 comments on commit 65ad746

Please sign in to comment.