Misc Changes
This commit is contained in:
@@ -165,10 +165,13 @@ object GitSandboxEngine {
|
||||
private fun interactiveAdd(repo: RepoState, arguments: List<String>): Pair<RepoState, List<String>> {
|
||||
val targets = arguments.filterNot { it == "-i" || it == "--interactive" || it.startsWith("--") }
|
||||
val target = targets.lastOrNull()
|
||||
val candidates = repo.files.filter { file ->
|
||||
!file.deleted && (target == null || target == "." || file.name == target || file.name.startsWith(target.trimEnd('/') + "/"))
|
||||
}
|
||||
val updated = repo.files.map { file ->
|
||||
if (file.deleted) {
|
||||
file
|
||||
} else if (target == null || target == "." || file.name == target) {
|
||||
} else if (target == null || target == "." || file.name == target || file.name.startsWith(target.trimEnd('/') + "/")) {
|
||||
file.copy(staged = true)
|
||||
} else {
|
||||
file
|
||||
@@ -178,10 +181,32 @@ object GitSandboxEngine {
|
||||
val before = repo.files.firstOrNull { it.name == updatedFile.name }
|
||||
updatedFile.staged && before?.staged != true
|
||||
}
|
||||
return repo.copy(files = updated) to listOf(
|
||||
"Interactive add is handled by GitHug Android.",
|
||||
"Staged ${if (target == null || target == ".") "$stagedCount file(s)" else target}.",
|
||||
)
|
||||
return repo.copy(files = updated) to interactiveAddConsoleLines(candidates, target, stagedCount)
|
||||
}
|
||||
|
||||
private fun interactiveAddConsoleLines(candidates: List<GitFile>, target: String?, stagedCount: Int): List<String> {
|
||||
return buildList {
|
||||
add(" staged unstaged path")
|
||||
candidates.forEachIndexed { index, file ->
|
||||
val staged = if (file.staged) "unchanged" else "+0/-0"
|
||||
val unstaged = when {
|
||||
file.tracked -> "+1/-0"
|
||||
else -> "+0/-0"
|
||||
}
|
||||
add("${index + 1}: ${staged.padEnd(10)} ${unstaged.padEnd(9)} ${file.name}")
|
||||
}
|
||||
if (candidates.isEmpty()) {
|
||||
add("No changes.")
|
||||
}
|
||||
add("*** Commands ***")
|
||||
add(" 1: status 2: update 3: revert 4: add untracked")
|
||||
add(" 5: patch 6: diff 7: quit 8: help")
|
||||
add("What now> update")
|
||||
add("Update>> ${target ?: "*"}")
|
||||
add("updated ${if (target == null || target == ".") "$stagedCount path(s)" else target}")
|
||||
add("What now> quit")
|
||||
add("Bye.")
|
||||
}
|
||||
}
|
||||
|
||||
fun tokenizeCommand(command: String): List<String> {
|
||||
@@ -434,7 +459,9 @@ object GitSandboxEngine {
|
||||
}
|
||||
|
||||
private fun rebase(repo: RepoState, arguments: List<String>): Pair<RepoState, List<String>> {
|
||||
val commits = if ("-i" in arguments && repo.commits.size > 2) {
|
||||
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 }
|
||||
@@ -465,7 +492,31 @@ object GitSandboxEngine {
|
||||
} else {
|
||||
repo.maintenanceActions
|
||||
}
|
||||
return repo.copy(commits = commits, branches = updatedBranches, maintenanceActions = maintenanceActions) to emptyList()
|
||||
val output = if (interactive) interactiveRebaseConsoleLines(originalCommits, commits, 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> {
|
||||
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("")
|
||||
add("# Rebase ${target.ifBlank { "HEAD" }} in progress; onto HEAD")
|
||||
add("# Commands:")
|
||||
add("# p, pick <commit> = use commit")
|
||||
add("# r, reword <commit> = use commit, but edit the commit message")
|
||||
add("# s, squash <commit> = use commit, but meld into previous commit")
|
||||
add("# d, drop <commit> = remove commit")
|
||||
add("Successfully rebased and updated refs/heads/$branch.")
|
||||
}
|
||||
}
|
||||
|
||||
private fun commit(repo: RepoState, arguments: List<String>): Pair<RepoState, List<String>> {
|
||||
|
||||
Reference in New Issue
Block a user