Remove brittle interactive rebase shortcuts

This commit is contained in:
Joe Tretter
2026-05-18 18:37:10 -05:00
parent 06b56010d0
commit d4e2c8714c
8 changed files with 1172 additions and 45 deletions

View File

@@ -515,28 +515,12 @@ object GitSandboxEngine {
private fun rebase(repo: RepoState, arguments: List<String>): Pair<RepoState, List<String>> {
val interactive = "-i" in arguments || "--interactive" in arguments
val originalCommits = repo.commits
val commits = if (interactive && repo.commits.size > 2) {
repo.commits
.filterNot { it.message.contains("squash this commit", ignoreCase = true) }
.map { if (it.message == "First coommit") it.copy(message = "First commit") else it }
.let { ordered ->
if (ordered.map { it.message }.containsAll(listOf("First commit", "Second commit", "Third commit"))) {
ordered.sortedBy { commit ->
when (commit.message) {
"First commit" -> 1
"Second commit" -> 2
"Third commit" -> 3
else -> 0
}
}
} else {
ordered
}
}
} else {
repo.commits
val target = arguments.lastOrNull { it != "-i" && it != "--interactive" && !it.startsWith("-") }
if (interactive && target == null) {
return repo to listOf("fatal: No rebase upstream specified")
}
val originalCommits = repo.commits
val commits = repo.commits
val updatedBranches = when {
"--onto" in arguments -> repo.branches + (repo.headBranch to (repo.branches["master"] ?: 0) + 1)
arguments.isNotEmpty() -> repo.branches + (repo.headBranch to maxOf(repo.branches[repo.headBranch] ?: 0, repo.branches[arguments.last()] ?: 0))
@@ -547,21 +531,15 @@ object GitSandboxEngine {
} else {
repo.maintenanceActions
}
val output = if (interactive) interactiveRebaseConsoleLines(originalCommits, commits, arguments, repo.headBranch) else emptyList()
val output = if (interactive) interactiveRebaseConsoleLines(originalCommits, arguments, repo.headBranch) else emptyList()
return repo.copy(commits = commits, branches = updatedBranches, maintenanceActions = maintenanceActions) to output
}
private fun interactiveRebaseConsoleLines(originalCommits: List<CommitNode>, rebasedCommits: List<CommitNode>, arguments: List<String>, branch: String): List<String> {
private fun interactiveRebaseConsoleLines(originalCommits: List<CommitNode>, arguments: List<String>, branch: String): List<String> {
val target = arguments.lastOrNull { it != "-i" && it != "--interactive" }.orEmpty()
val rebasedIds = rebasedCommits.map { it.id }.toSet()
return buildList {
originalCommits.forEach { commit ->
val action = when {
commit.id !in rebasedIds -> "squash"
commit.message == "First coommit" -> "reword"
else -> "pick"
}
add("$action ${commit.id} ${commit.message}")
add("pick ${commit.id} ${commit.message}")
}
add("")
add("# Rebase ${target.ifBlank { "HEAD" }} in progress; onto HEAD")