fun collectActions()

in bunch-cli/src/main/kotlin/org/jetbrains/bunches/git/git.kt [276:318]


fun collectActions(git: Git, commit: RevCommit): List<FileAction> {
    val reader = git.repository.newObjectReader()

    val oldTreeIterator =
        if (commit.parentCount != 0) {
            CanonicalTreeParser().apply {
                reset(reader, commit.getParent(0).tree)
            }

        } else {
            EmptyTreeIterator()
        }

    val newTreeIterator = CanonicalTreeParser().apply {
        reset(reader, commit.tree)
    }

    val diffCommand = git.diff().apply {
        setOldTree(oldTreeIterator)
        setNewTree(newTreeIterator)
    }

    return diffCommand.call().flatMap { entry: DiffEntry ->
        when (entry.changeType) {
            DiffEntry.ChangeType.RENAME -> {
                listOf(
                    FileAction(entry.changeType, entry.oldPath, ""),
                    FileAction(entry.changeType, entry.newPath, entry.readNewContent(git))
                )
            }
            DiffEntry.ChangeType.DELETE -> {
                listOf(FileAction(entry.changeType, entry.oldPath, ""))
            }

            DiffEntry.ChangeType.ADD,
            DiffEntry.ChangeType.MODIFY,
            DiffEntry.ChangeType.COPY -> {
                listOf(FileAction(entry.changeType, entry.newPath, entry.readNewContent(git)))
            }
            else -> listOf()
        }
    }
}