Skip to content

Instantly share code, notes, and snippets.

@SurpSG
Created June 7, 2020 23:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SurpSG/c7b9d95b797f750cac5b37f59282961d to your computer and use it in GitHub Desktop.
Save SurpSG/c7b9d95b797f750cac5b37f59282961d to your computer and use it in GitHub Desktop.
fun main(args: Array<String>) {
val repository: Repository = FileRepositoryBuilder().setGitDir(
File("C:\\Users\\Crulio\\IdeaProjects\\test").resolve(".git")
).build()
Git(repository).use { git ->
DiffFormatter(System.out).apply {
setRepository(repository)
scan(
prepareTreeParser( git.getRepository(), Constants.HEAD ),
FileTreeIterator(repository)
).forEach {
format(it)
}
}
}
}
fun prepareTreeParser(repository: Repository, ref: String): AbstractTreeIterator {
val head = repository.exactRef(ref)
RevWalk(repository).use { walk ->
val commit = walk.parseCommit(head.objectId)
val tree: RevTree = walk.parseTree(commit.tree.id)
val treeParser = CanonicalTreeParser()
repository.newObjectReader().use { reader -> treeParser.reset(reader, tree.id) }
walk.dispose()
return treeParser
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment