-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#3 use arrowKt to check for git and process errors, reorder git comma…
…nd package structure
- Loading branch information
1 parent
c59fa61
commit daef554
Showing
15 changed files
with
256 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
plugin/git-plugin/src/main/kotlin/com/github/simonhauck/git/BaseGitTask.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.github.simonhauck.git | ||
|
||
import com.github.simonhauck.git.process.ProcessConfig | ||
import com.github.simonhauck.git.wrapper.GitCommandApi | ||
import com.github.simonhauck.git.wrapper.GitCommandProcessWrapper | ||
import org.gradle.api.DefaultTask | ||
import org.gradle.api.provider.Property | ||
import org.gradle.api.tasks.Input | ||
import org.gradle.api.tasks.Optional | ||
|
||
abstract class BaseGitTask : DefaultTask() { | ||
|
||
@get:Input @get:Optional abstract val processConfig: Property<ProcessConfig> | ||
|
||
init { | ||
group = "git" | ||
} | ||
|
||
fun getGitCommandApi(): GitCommandApi { | ||
return GitCommandProcessWrapper(config = processConfig.getOrElse(ProcessConfig())) | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
plugin/git-plugin/src/main/kotlin/com/github/simonhauck/git/CreateBranchTask.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.github.simonhauck.git | ||
|
||
import org.gradle.api.provider.Property | ||
import org.gradle.api.tasks.Input | ||
import org.gradle.api.tasks.TaskAction | ||
|
||
abstract class CreateBranchTask : BaseGitTask() { | ||
|
||
@get:Input abstract val branchName: Property<String> | ||
|
||
@TaskAction | ||
fun action() { | ||
getGitCommandApi().createBranch(branchName.get()) | ||
} | ||
} |
19 changes: 0 additions & 19 deletions
19
plugin/git-plugin/src/main/kotlin/com/github/simonhauck/git/GitCommandWrapper.kt
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 9 additions & 14 deletions
23
plugin/git-plugin/src/main/kotlin/com/github/simonhauck/git/process/ProcessResult.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,14 @@ | ||
package com.github.simonhauck.git.process | ||
|
||
sealed interface ProcessResult { | ||
import arrow.core.Either | ||
|
||
val exitCode: Int? | ||
internal typealias ProcessResult = Either<ProcessError, ProcessSuccess> | ||
|
||
data class OK(override val exitCode: Int) : ProcessResult | ||
internal data class ProcessSuccess(val exitCode: Int, val output: List<String>) | ||
|
||
data class Error(override val exitCode: Int?, val message: String, val throwable: Throwable?) : | ||
ProcessResult | ||
|
||
companion object { | ||
fun fromExitCode(exitCode: Int): ProcessResult { | ||
if (exitCode == 0) return OK(exitCode) | ||
|
||
return Error(exitCode, "Process failed with exit code $exitCode", null) | ||
} | ||
} | ||
} | ||
internal data class ProcessError( | ||
val exitCode: Int?, | ||
val output: List<String>, | ||
val error: Throwable, | ||
val message: String | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
plugin/git-plugin/src/main/kotlin/com/github/simonhauck/git/wrapper/GitCommandApi.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.github.simonhauck.git.wrapper | ||
|
||
interface GitCommandApi { | ||
fun gitInit(branchName: String): GitVoidResult | ||
|
||
fun gitStatus(): GitVoidResult | ||
|
||
fun createBranch(branchName: String): GitVoidResult | ||
|
||
fun gitAdd(filePattern: String): GitVoidResult | ||
|
||
fun gitCommit(message: String): GitVoidResult | ||
|
||
fun gitLog(): GitResult<List<GitLogEntry>> | ||
|
||
fun getLocalBranchNames(): GitResult<List<String>> | ||
} |
57 changes: 57 additions & 0 deletions
57
.../git-plugin/src/main/kotlin/com/github/simonhauck/git/wrapper/GitCommandProcessWrapper.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package com.github.simonhauck.git.wrapper | ||
|
||
import arrow.core.Either | ||
import com.github.simonhauck.git.process.ProcessConfig | ||
import com.github.simonhauck.git.process.ProcessSuccess | ||
import com.github.simonhauck.git.process.ProcessWrapper | ||
|
||
internal class GitCommandProcessWrapper( | ||
private val processWrapper: ProcessWrapper = ProcessWrapper(), | ||
private val config: ProcessConfig = ProcessConfig() | ||
) : GitCommandApi { | ||
|
||
override fun gitInit(branchName: String): GitVoidResult { | ||
return gitVoidCommand(listOf("init", "--initial-branch=$branchName")) | ||
} | ||
|
||
override fun gitStatus(): GitVoidResult { | ||
return gitVoidCommand(listOf("status")) | ||
} | ||
|
||
override fun gitAdd(filePattern: String): GitVoidResult { | ||
return gitVoidCommand(listOf("add", filePattern)) | ||
} | ||
|
||
override fun gitCommit(message: String): GitVoidResult { | ||
return gitVoidCommand(listOf("commit", "-m", message)) | ||
} | ||
|
||
override fun gitLog(): GitResult<List<GitLogEntry>> { | ||
return gitCommand(listOf("log", "--pretty=oneline")).map { processSuccess -> | ||
processSuccess.output.map { line -> | ||
val split = line.split(" ") | ||
GitLogEntry(split[0], split.drop(1).joinToString(" ")) | ||
} | ||
} | ||
} | ||
|
||
override fun createBranch(branchName: String): GitVoidResult { | ||
return gitVoidCommand(listOf("branch", branchName)) | ||
} | ||
|
||
override fun getLocalBranchNames(): GitResult<List<String>> { | ||
return gitCommand(listOf("--no-pager", "branch")).map { processSuccess -> | ||
processSuccess.output.map { it.trim() } | ||
} | ||
} | ||
|
||
private fun gitVoidCommand(command: List<String>): Either<GitError, GitOk> { | ||
val runCommand = gitCommand(command) | ||
return runCommand.map { GitOk } | ||
} | ||
|
||
private fun gitCommand(command: List<String>): Either<GitError, ProcessSuccess> { | ||
val runCommand = processWrapper.runCommand(listOf("git").plus(command), config) | ||
return runCommand.mapLeft { GitError(it.message, it.error) } | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
plugin/git-plugin/src/main/kotlin/com/github/simonhauck/git/wrapper/GitCommandResult.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.github.simonhauck.git.wrapper | ||
|
||
import arrow.core.Either | ||
|
||
typealias GitVoidResult = Either<GitError, GitOk> | ||
|
||
typealias GitResult<T> = Either<GitError, T> | ||
|
||
fun Either<GitError, *>.isOk(): Boolean = isRight() | ||
|
||
object GitOk | ||
|
||
data class GitError(val message: String, val throwable: Throwable? = null) | ||
|
||
data class GitLogEntry( | ||
val hash: String, | ||
val message: String, | ||
) |
36 changes: 0 additions & 36 deletions
36
plugin/git-plugin/src/test/kotlin/com/github/simonhauck/git/GitCommandWrapperTest.kt
This file was deleted.
Oops, something went wrong.
76 changes: 76 additions & 0 deletions
76
plugin/git-plugin/src/test/kotlin/com/github/simonhauck/git/wrapper/GitCommandApiTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package com.github.simonhauck.git.wrapper | ||
|
||
import arrow.core.Either | ||
import com.github.simonhauck.git.process.ProcessConfig | ||
import java.io.File | ||
import org.assertj.core.api.Assertions.assertThat | ||
import org.junit.jupiter.api.BeforeEach | ||
import org.junit.jupiter.api.Test | ||
import org.junit.jupiter.api.io.TempDir | ||
|
||
class GitCommandProcessWrapperTest { | ||
|
||
@TempDir lateinit var tempDir: File | ||
private lateinit var gitCommandApi: GitCommandApi | ||
|
||
@BeforeEach | ||
fun setup() { | ||
gitCommandApi = GitCommandProcessWrapper(config = ProcessConfig(workingDir = tempDir)) | ||
} | ||
|
||
@Test | ||
fun `can create a git repository and correctly return the status`() { | ||
val actualInit = gitCommandApi.gitInit("main") | ||
assertThat(actualInit.isOk()).isTrue() | ||
|
||
val actualStatus = gitCommandApi.gitStatus() | ||
assertThat(actualStatus.isOk()).isTrue() | ||
} | ||
|
||
@Test | ||
fun `check that the git command fails if no git repository is available`() { | ||
val actual = gitCommandApi.gitStatus() | ||
|
||
assertThat(actual.isOk()).isFalse() | ||
} | ||
|
||
@Test | ||
fun `should be able to create a git repository and commit a file`() { | ||
gitCommandApi.gitInit("main") | ||
|
||
val file = File(tempDir, "file.txt") | ||
file.writeText("Hello World") | ||
|
||
val actualAdd = gitCommandApi.gitAdd("file.txt") | ||
assertThat(actualAdd.isOk()).isTrue() | ||
|
||
val actualCommit = gitCommandApi.gitCommit("Initial commit") | ||
assertThat(actualCommit.isOk()).isTrue() | ||
|
||
val actual = gitCommandApi.gitLog().get().map { it.message } | ||
assertThat(actual).containsExactly("Initial commit") | ||
} | ||
|
||
@Test | ||
fun `should contain the names of all local created branches`() { | ||
gitCommandApi.gitInit("main") | ||
|
||
File("$tempDir/file.txt").writeText("Hello World") | ||
gitCommandApi.gitAdd("file.txt") | ||
gitCommandApi.gitCommit("Initial commit") | ||
|
||
gitCommandApi.createBranch("feature-1") | ||
gitCommandApi.createBranch("feature-2") | ||
|
||
val actual = gitCommandApi.getLocalBranchNames() | ||
assertThat(actual.isOk()).isTrue() | ||
assertThat(actual.get()).containsExactly("main", "feature-1", "feature-2") | ||
} | ||
} | ||
|
||
fun <T, E> Either<T, E>.get(): E { | ||
return when (this) { | ||
is Either.Left -> throw IllegalStateException("Expected Right but got Left") | ||
is Either.Right -> this.value | ||
} | ||
} |
Oops, something went wrong.