Gitnuro/src/main/kotlin/com/jetpackduba/gitnuro/git/branches/CheckoutRefUseCase.kt
2022-09-28 01:56:21 +02:00

25 lines
892 B
Kotlin

package com.jetpackduba.gitnuro.git.branches
import com.jetpackduba.gitnuro.extensions.isBranch
import com.jetpackduba.gitnuro.extensions.simpleName
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import org.eclipse.jgit.api.CreateBranchCommand
import org.eclipse.jgit.api.Git
import org.eclipse.jgit.lib.Ref
import javax.inject.Inject
class CheckoutRefUseCase @Inject constructor() {
suspend operator fun invoke(git: Git, ref: Ref): Unit = withContext(Dispatchers.IO) {
git.checkout().apply {
setName(ref.name)
if (ref.isBranch && ref.name.startsWith("refs/remotes/")) {
setCreateBranch(true)
setName(ref.simpleName)
setStartPoint(ref.objectId.name)
setUpstreamMode(CreateBranchCommand.SetupUpstreamMode.TRACK)
}
call()
}
}
}