Skip to content

Commit

Permalink
Add ability to update a branch without checking it out
Browse files Browse the repository at this point in the history
  • Loading branch information
LouisCAD committed Aug 3, 2021
1 parent 8e661b8 commit 0faf543
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ private object Git : Vcs {
"git fetch".executeAndPrint()
}

override fun fetch(repository: String, sourceBranch: String, destinationBranch: String) {
"git fetch $repository $sourceBranch:$destinationBranch".executeAndPrint()
}

override fun pull(repository: String) {
"git pull $repository".executeAndPrint()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ interface Vcs {
fun checkoutBranch(branchName: String)
fun checkoutAndTrackRemoteBranch(remoteName: String, branchName: String)
fun fetch()
fun fetch(repository: String, sourceBranch: String, destinationBranch: String)
fun pull(repository: String)
fun push(repository: String, withTags: Boolean = false, setUpstream: Boolean = false, branchName: String? = null)
fun getRemoteUrl(repository: String): String
Expand All @@ -35,6 +36,18 @@ interface Vcs {
fun Vcs.isOnMainBranch() = isOnBranch(expectedBranchName = "main")
fun Vcs.checkoutMain() = checkoutBranch(branchName = "main")
fun Vcs.pullFromOrigin() = pull(repository = "origin")

/**
* Allows updating a branch (like a fast-forwarding `git pull` would do), without checking it out.
*
* Executes `git fetch origin targetBranch:targetBranch` under the hood.
*/
fun Vcs.updateBranchFromOrigin(targetBranch: String) = fetch(
repository = "origin",
sourceBranch = targetBranch,
destinationBranch = targetBranch
)

fun Vcs.pushToOrigin(withTags: Boolean = false) = push(repository = "origin", withTags = withTags)
fun Vcs.mergeMainIntoCurrent() = mergeBranchIntoCurrent(sourceBranch = "main")
fun Vcs.hasBranch(branchName: String): Boolean = branchName in getBranches()
Expand Down

0 comments on commit 0faf543

Please sign in to comment.