Skip to content

Commit 4e531e5

Browse files
committed
Fix getRemoteBranches behavior with newer versions of git
At some point, the git CLI did not return "$remoteName/HEAD" when executing "git branch -r" and now it does for some reason. This breaks some tests, so let's filter it out in both clients and add tests to ensure the filtering works. Also, fix a couple of getRemoteBranches tests that were passing by coincidence due to not specifying the correct remote name and therefore getting empty lists. commit-id: I1011dd0b
1 parent 52aa2f9 commit 4e531e5

3 files changed

Lines changed: 65 additions & 6 deletions

File tree

‎git-jaspr/src/main/kotlin/sims/michael/gitjaspr/CliGitClient.kt‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import org.zeroturnaround.exec.ProcessExecutor
55
import org.zeroturnaround.exec.ProcessResult
66
import sims.michael.gitjaspr.RemoteRefEncoding.getRemoteRefParts
77
import java.io.File
8+
import org.eclipse.jgit.lib.Constants
89

910
class CliGitClient(
1011
override val workingDirectory: File,
@@ -133,7 +134,7 @@ class CliGitClient(
133134
.mapNotNull { line ->
134135
val (nameWithRemote, hash) = line.split(GIT_FORMAT_SEPARATOR)
135136
val (thisRemoteName, name) = nameWithRemote.split("/", limit = 2)
136-
if (thisRemoteName == remoteName) {
137+
if (thisRemoteName == remoteName && name != Constants.HEAD) {
137138
RemoteBranch(name, log(hash, 1).single())
138139
} else {
139140
null

‎git-jaspr/src/main/kotlin/sims/michael/gitjaspr/JGitClient.kt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ class JGitClient(
168168
.setListMode(ListBranchCommand.ListMode.REMOTE)
169169
.call()
170170
.filter { it.name.startsWith(Constants.R_REMOTES) }
171+
.filterNot { ref -> ref.name == Constants.R_REMOTES + "$remoteName/HEAD" }
171172
.mapNotNull { ref ->
172173
val r = git.repository
173174
val (thisRemoteName, shortBranchName) =

‎git-jaspr/src/test/kotlin/sims/michael/gitjaspr/CliGitClientTest.kt‎

Lines changed: 62 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import sims.michael.gitjaspr.testing.DEFAULT_COMMITTER
1212
import sims.michael.gitjaspr.testing.toStringWithClickableURI
1313
import java.io.File
1414
import java.nio.file.Files
15+
import org.eclipse.jgit.lib.Constants
1516

1617
class CliGitClientTest {
1718

@@ -367,6 +368,51 @@ class CliGitClientTest {
367368
}
368369
}
369370

371+
@Test
372+
fun `getRemoteBranches should not include HEAD`() {
373+
// If we've pushed to HEAD, ensure that getRemoteBranches does not return it. At some point git changed its
374+
// behavior to include HEAD in the list of remote branches, which we don't want.
375+
withTestSetup {
376+
createCommitsFrom(
377+
testCase {
378+
repository {
379+
commit {
380+
title = "one"
381+
branch {
382+
commit { title = "a" }
383+
commit { title = "b" }
384+
commit { title = "c" }
385+
commit {
386+
title = "d"
387+
localRefs += "development"
388+
}
389+
}
390+
}
391+
commit { title = "two" }
392+
commit {
393+
title = "three"
394+
body = "This is a body"
395+
remoteRefs += listOf(
396+
"main",
397+
Constants.HEAD,
398+
)
399+
}
400+
}
401+
},
402+
)
403+
val cliGit = CliGitClient(localGit.workingDirectory)
404+
val git = JGitClient(localGit.workingDirectory)
405+
assertFalse(
406+
cliGit.getRemoteBranches(remoteName).any { branch -> branch.name.endsWith("/${Constants.HEAD}") },
407+
"List of remote branches should not be empty",
408+
)
409+
assertEquals(
410+
cliGit.getRemoteBranches(remoteName),
411+
git.getRemoteBranches(remoteName),
412+
)
413+
}
414+
}
415+
370416
@Test
371417
fun `compare getRemoteBranches`() {
372418
withTestSetup {
@@ -389,16 +435,23 @@ class CliGitClientTest {
389435
commit {
390436
title = "three"
391437
body = "This is a body"
392-
remoteRefs += "main"
438+
remoteRefs += listOf(
439+
"main",
440+
Constants.HEAD,
441+
)
393442
}
394443
}
395444
},
396445
)
397446
val cliGit = CliGitClient(localGit.workingDirectory)
398447
val git = JGitClient(localGit.workingDirectory)
448+
assertFalse(
449+
cliGit.getRemoteBranches(remoteName).isEmpty(),
450+
"List of remote branches should not be empty",
451+
)
399452
assertEquals(
400-
cliGit.getRemoteBranches(),
401-
git.getRemoteBranches(),
453+
cliGit.getRemoteBranches(remoteName),
454+
git.getRemoteBranches(remoteName),
402455
)
403456
}
404457
}
@@ -444,9 +497,13 @@ class CliGitClientTest {
444497
)
445498
val cliGit = CliGitClient(localGit.workingDirectory)
446499
val git = JGitClient(localGit.workingDirectory)
500+
assertFalse(
501+
cliGit.getRemoteBranchesById(remoteName).isEmpty(),
502+
"Map of remote branches should not be empty",
503+
)
447504
assertEquals(
448-
cliGit.getRemoteBranchesById(),
449-
git.getRemoteBranchesById(),
505+
cliGit.getRemoteBranchesById(remoteName),
506+
git.getRemoteBranchesById(remoteName),
450507
)
451508
}
452509
}

0 commit comments

Comments
 (0)