Skip to content

Commit

Permalink
Print helpful message when branch already exists (#274)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattprecious authored Feb 25, 2025
1 parent f08604d commit 954a829
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import com.mattprecious.stacker.rendering.branch
import com.mattprecious.stacker.rendering.code
import com.mattprecious.stacker.stack.StackManager
import com.mattprecious.stacker.vc.VersionControl
import com.mattprecious.stacker.vc.VersionControl.BranchCreateResult

fun StackerDeps.branchCreate(
branchName: String,
Expand Down Expand Up @@ -49,7 +50,21 @@ internal class BranchCreate(
abort()
}

vc.createBranchFromCurrent(branchName)
stackManager.trackBranch(branchName, currentBranch.name, vc.getSha(currentBranch.name))
when (vc.createBranchFromCurrent(branchName)) {
BranchCreateResult.Success -> {
stackManager.trackBranch(branchName, currentBranch.name, vc.getSha(currentBranch.name))
}
BranchCreateResult.AlreadyExists -> {
printStaticError(
buildAnnotatedString {
append("Branch ")
branch { append(branchName) }
append(" already exists.")
},
)

abort()
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.github.git2.GIT_CHECKOUT_OPTIONS_VERSION
import com.github.git2.GIT_CREDTYPE_SSH_KEY
import com.github.git2.GIT_EAPPLIED
import com.github.git2.GIT_ECONFLICT
import com.github.git2.GIT_EEXISTS
import com.github.git2.GIT_ENOTFOUND
import com.github.git2.GIT_EUNMERGED
import com.github.git2.GIT_FETCH_OPTIONS_VERSION
Expand Down Expand Up @@ -97,6 +98,8 @@ import com.github.git2.git_signature_default_from_env
import com.github.git2.git_signature_free
import com.github.git2.git_strarray
import com.mattprecious.stacker.shell.Shell
import com.mattprecious.stacker.vc.ReturnCodes.EEXISTS
import com.mattprecious.stacker.vc.VersionControl.BranchCreateResult
import com.mattprecious.stacker.vc.VersionControl.CommitInfo
import kotlinx.cinterop.CPointer
import kotlinx.cinterop.CPointerVar
Expand Down Expand Up @@ -196,10 +199,14 @@ class GitVersionControl(
checkout(branchName, treeish.pointed!!)
}

override fun createBranchFromCurrent(branchName: String) = memScoped {
override fun createBranchFromCurrent(branchName: String): BranchCreateResult = memScoped {
val commit = getCommitForBranch(currentBranchName)
checkError(git_branch_create(allocPointerTo<git_reference>().ptr, repo, branchName, commit.ptr, 0))
val code =
git_branch_create(allocPointerTo<git_reference>().ptr, repo, branchName, commit.ptr, 0)
if (code == EEXISTS) return BranchCreateResult.AlreadyExists
checkError(code)
checkout(branchName, commit.asObject())
return BranchCreateResult.Success
}

override fun renameBranch(branchName: String, newName: String) = memScoped {
Expand Down Expand Up @@ -590,6 +597,7 @@ private fun checkError(result: Int) {
private object ReturnCodes {
val OK = GIT_OK
val ENOTFOUND = GIT_ENOTFOUND
val EEXISTS = GIT_EEXISTS
val EUNMERGED = GIT_EUNMERGED
val ECONFLICT = GIT_ECONFLICT
val EAPPLIED = GIT_EAPPLIED
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@ interface VersionControl : AutoCloseable {

fun checkout(branchName: String)

fun createBranchFromCurrent(branchName: String)
enum class BranchCreateResult {
Success,
AlreadyExists,
}

fun createBranchFromCurrent(branchName: String): BranchCreateResult

fun renameBranch(branchName: String, newName: String)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import com.mattprecious.stacker.db.Branch
import com.mattprecious.stacker.delegates.Optional
import com.mattprecious.stacker.test.util.gitCommit
import com.mattprecious.stacker.test.util.gitCreateAndCheckoutBranch
import com.mattprecious.stacker.test.util.gitCreateBranch
import com.mattprecious.stacker.test.util.gitInit
import com.mattprecious.stacker.test.util.withTestEnvironment
import kotlin.test.Test
Expand Down Expand Up @@ -108,4 +109,25 @@ class BranchCreateTest {
.containsExactly("main")
}
}

@Test
fun duplicateBranch() = withTestEnvironment {
gitInit()
gitCommit("Empty")
testCommand({ repoInit("main", Optional.None) })
gitCreateBranch("change-a")

testCommand({ branchCreate("change-a") }) {
awaitFrame(
static = "Branch change-a already exists.",
output = "",
)
assertThat(awaitResult()).isFalse()
}

withDatabase {
assertThat(it.branchQueries.selectAll().executeAsList().map { it.name })
.containsExactly("main")
}
}
}

0 comments on commit 954a829

Please sign in to comment.