Skip to content

Commit

Permalink
GitClient uses repository root instead of workspace root
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaszwawrzyk committed Aug 6, 2023
1 parent 5c2bb9b commit 339d0b0
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ object AppBuilder {
val mavenDataExtractor = MavenDataExtractor(appConfig.workspaceRoot)
val mavenRepository = MavenRepository()
val updateLogic = UpdateLogic()
val gitOperations = GitOperations(appConfig.workspaceRoot, appConfig.baseBranch)
val gitOperations = runBlocking { GitOperations.resolve(appConfig.workspaceRoot, appConfig.baseBranch) }
val gitPlatform = if (github) {
GithubPlatform.create(env, appConfig.baseBranch, appConfig.gitAuthor)
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class CommandRunner {
return runForOutput(command.toList(), directory)
}

suspend fun runForOutput(command: List<String>, directory: Path): String {
suspend fun runForOutput(command: List<String>, directory: Path? = null): String {
val result = run(command, directory)
if (result.isSuccess) {
return result.stdout
Expand All @@ -29,10 +29,13 @@ class CommandRunner {
}
}

suspend fun run(command: List<String>, directory: Path): Result {
suspend fun run(command: List<String>, directory: Path?): Result {
logger.info { command.joinToString(" ") { if (it.contains(" ")) """"$it"""" else it } }
return withContext(Dispatchers.IO) {
val process = ProcessBuilder(command).directory(directory.toFile()).start()
val processBuilder = ProcessBuilder(command).apply {
directory?.let { directory(it.toFile()) }
}
val process = processBuilder.start()
.onExit().await()
val stdout = process.inputStream.bufferedReader().use { it.readText() }
val stderr = process.errorStream.bufferedReader().use { it.readText() }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ import java.nio.file.Path

class GitClient(private val repositoryRoot: Path) {
private val quiet = "--quiet"
private val git = runBlocking { CommandRunner.runForOutput(listOf("sh", "-c", "which git"), repositoryRoot).trim() }

companion object {
private val git = runBlocking { CommandRunner.runForOutput(listOf("sh", "-c", "which git")).trim() }
}

suspend fun checkout(target: String, newBranch: Boolean = false) {
val b = if (newBranch) "-b" else null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import mu.KotlinLogging
import org.virtuslab.bazelsteward.core.GitBranch
import java.lang.RuntimeException
import java.nio.file.Path
import kotlin.io.path.Path
import kotlin.io.path.readText
import kotlin.io.path.writeText

Expand All @@ -21,8 +22,17 @@ data class CommitRequest(

private val logger = KotlinLogging.logger {}

class GitOperations(workspaceRoot: Path, private val baseBranch: String) {
private val git = GitClient(workspaceRoot)
class GitOperations(repositoryRoot: Path, private val baseBranch: String) {

companion object {
suspend fun resolve(workspaceRoot: Path, baseBranch: String): GitOperations {
val initial = GitClient(workspaceRoot)
val repositoryRoot = initial.run("rev-parse", "--show-toplevel").trim().let { Path(it) }
return GitOperations(repositoryRoot, baseBranch)
}
}

private val git = GitClient(repositoryRoot)

suspend fun checkoutBaseBranch() {
git.checkout(baseBranch)
Expand Down

0 comments on commit 339d0b0

Please sign in to comment.