Call Git cmd_main directly

This commit is contained in:
Joe Tretter
2026-06-24 13:16:35 -05:00
parent a9d7b0cfc0
commit 0554c736e4
26 changed files with 1141 additions and 661 deletions

View File

@@ -409,34 +409,6 @@ class GitSandboxEngineTest {
}
}
@Test
fun nativeInteractiveRebaseRewordUsesEditedTodoMessage() {
val git = testGitBinary()
assumeTrue(git.exists() && git.canExecute())
val root = Files.createTempDirectory("githug-reword-editor").toFile()
try {
val runtime = GitRepositoryRuntime(root, git)
val level = renameCommitLevel()
val repo = runtime.prepareLevel(level)
val invocation = parseGitEditorInvocation("git rebase -i HEAD~2")
?: error("Expected interactive rebase editor invocation")
val todo = runtime.gitEditorInitialContent(level, repo, invocation)
val rewordedTodo = todo.replace(
Regex("(?m)^pick (\\S+) First coommit$"),
"reword $1 First commit",
)
val (updatedRepo, output) = runtime.executeGitEditorCommand(level, repo, invocation, rewordedTodo)
assertFalse(output.any { it.contains("error:", ignoreCase = true) || it.contains("fatal:", ignoreCase = true) })
assertTrue(updatedRepo.commits.any { it.message == "First commit" })
assertFalse(updatedRepo.commits.any { it.message == "First coommit" })
assertTrue(level.validator(updatedRepo, invocation.command))
} finally {
root.deleteRecursively()
}
}
@Test
fun nativeExecutableShortcutRunsScriptThroughShell() {
val git = testGitBinary()

View File

@@ -3,10 +3,7 @@ package solutions.tretter.githugandroid
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Assume.assumeTrue
import org.junit.Test
import java.io.File
import java.nio.file.Files
class InteractiveAddEngineTest {
@Test
@@ -200,108 +197,4 @@ class InteractiveAddEngineTest {
}
}
@Test
fun nativeInteractiveAddSelectionUpdatesGitIndex() {
val git = testGitBinary()
assumeTrue(git.exists() && git.canExecute())
val root = Files.createTempDirectory("githug-interactive-add").toFile()
try {
val runtime = GitRepositoryRuntime(root, git)
val level = level(
id = "interactive-add-test",
title = "Interactive Add Test",
description = "",
hints = emptyList(),
commandSuggestions = emptyList(),
setup = { RepoState(initialized = true, files = listOf(GitFile("README")), branches = mapOf("master" to 0)) },
validator = { _, _ -> false },
)
val repo = runtime.prepareLevel(level)
val (menuRepo, _) = runtime.execute(level, repo, "git stage -i")
val (updateRepo, _) = runtime.execute(level, menuRepo, "2")
val (selectedRepo, _) = runtime.execute(level, updateRepo, "1")
val (quitRepo, _) = runtime.execute(level, selectedRepo, "7")
val (_, statusOutput) = runtime.execute(level, quitRepo, "git status")
assertTrue(selectedRepo.files.single { it.name == "README" }.staged)
assertTrue(quitRepo.interactiveAddSession == null)
assertTrue(statusOutput.any { it.contains("new file:") && it.contains("README") })
} finally {
root.deleteRecursively()
}
}
@Test
fun nativePatchAddSelectionUpdatesGitIndex() {
val git = testGitBinary()
assumeTrue(git.exists() && git.canExecute())
val root = Files.createTempDirectory("githug-patch-add").toFile()
try {
val runtime = GitRepositoryRuntime(root, git)
val level = level(
id = "patch-add-test",
title = "Patch Add Test",
description = "",
hints = emptyList(),
commandSuggestions = emptyList(),
setup = { RepoState(initialized = true, files = listOf(GitFile("README")), branches = mapOf("master" to 0)) },
validator = { _, _ -> false },
)
val repo = runtime.prepareLevel(level)
val (patchRepo, patchOutput) = runtime.execute(level, repo, "git add -p README")
val (selectedRepo, _) = runtime.execute(level, patchRepo, "y")
assertTrue(patchOutput.any { it.contains("Stage this hunk") })
assertTrue(selectedRepo.files.single { it.name == "README" }.staged)
} finally {
root.deleteRecursively()
}
}
@Test
fun nativePatchHunkEditorSaveUpdatesGitIndex() {
val git = testGitBinary()
assumeTrue(git.exists() && git.canExecute())
val root = Files.createTempDirectory("githug-patch-edit").toFile()
try {
val runtime = GitRepositoryRuntime(root, git)
val level = level(
id = "patch-edit-test",
title = "Patch Edit Test",
description = "",
hints = emptyList(),
commandSuggestions = emptyList(),
setup = { RepoState(initialized = true, files = listOf(GitFile("README")), branches = mapOf("master" to 0)) },
validator = { _, _ -> false },
)
val repo = runtime.prepareLevel(level)
val (patchRepo, _) = runtime.execute(level, repo, "git add -p README")
val invocation = GitSandboxEngine.parsePatchHunkEditorInvocation(patchRepo, "e")
?: error("Expected patch editor invocation")
val (selectedRepo, output) = runtime.executeGitEditorCommand(level, patchRepo, invocation, invocation.initialContent)
assertTrue(output.any { it.contains("Applied edited hunk.") })
assertTrue(selectedRepo.files.single { it.name == "README" }.staged)
} finally {
root.deleteRecursively()
}
}
private fun testGitBinary(): File {
System.getenv("GITHUG_TEST_GIT_BINARY")
?.takeIf { it.isNotBlank() }
?.let { File(it) }
?.takeIf { it.exists() && it.canExecute() }
?.let { return it }
val repoHostGit = File(repoRoot(), "build/host-git/libgit.so")
if (repoHostGit.exists() && repoHostGit.canExecute()) return repoHostGit
return File("/usr/bin/git")
}
private fun repoRoot(): File {
val userDir = File(System.getProperty("user.dir") ?: ".")
return if (File(userDir, "app/build.gradle.kts").exists()) userDir else userDir.parentFile ?: userDir
}
}