Skip to content

Commit 9b087b2

Browse files
committed
Allow git checkout command to create new branch if it doesn't exist
1 parent fbd039f commit 9b087b2

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

turtle/src/main/kotlin/com/lordcodes/turtle/GitCommands.kt

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,15 @@ class GitCommands internal constructor(
116116
* @throws [ShellFailedException] There was an issue running the command.
117117
* @throws [ShellRunException] Running the command produced error output.
118118
*/
119-
fun checkout(branch: String) =
120-
gitCommand(listOf("checkout", branch, "--quiet"))
119+
fun checkout(branch: String, createIfNecessary: Boolean = true): String {
120+
val arguments = mutableListOf("checkout")
121+
if (createIfNecessary) {
122+
arguments.add("-B")
123+
}
124+
arguments.add(branch)
125+
arguments.add("--quiet")
126+
return gitCommand(arguments)
127+
}
121128

122129
/**
123130
* Clone a Git repository at a given URL.
@@ -162,8 +169,7 @@ class GitCommands internal constructor(
162169
* @throws [ShellFailedException] There was an issue running the command.
163170
* @throws [ShellRunException] Running the command produced error output.
164171
*/
165-
fun addTag(tagName: String, message: String) =
166-
gitCommand(listOf("tag", "-a", tagName, "-m", message))
172+
fun addTag(tagName: String, message: String) = gitCommand(listOf("tag", "-a", tagName, "-m", message))
167173

168174
/**
169175
* Push the given Git tag.

0 commit comments

Comments
 (0)