Skip to content

feat: add parameter to checkout to specify a remote tracking branch #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 20, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions Sources/GitKit/Git.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ public final class Git: Shell {
case commit(message: String, Bool = false)
case config(name: String, value: String)
case clone(url: String)
case checkout(branch: String, create: Bool = false)

/// - parameter branch the name of the branch to checkout
/// - parameter create whether to create a new branch or checkout an existing one
/// - parameter tracking when creating a new branch, the name of the remote branch it should track
case checkout(branch: String, create: Bool = false, tracking: String? = nil)

case log(numberOfCommits: Int? = nil, options: [String]? = nil, revisions: String? = nil)
case push(remote: String? = nil, branch: String? = nil)
case pull(remote: String? = nil, branch: String? = nil, rebase: Bool = false)
Expand Down Expand Up @@ -59,12 +64,15 @@ public final class Git: Shell {
}
case .clone(let url):
params = [Command.clone.rawValue, url]
case .checkout(let branch, let create):
case .checkout(let branch, let create, let tracking):
params = [Command.checkout.rawValue]
if create {
params.append("-b")
}
params.append(branch)
if let tracking {
params.append(tracking)
}
case .log(let numberOfCommits, let options, let revisions):
params = [Command.log.rawValue]
if let numberOfCommits = numberOfCommits {
Expand Down
20 changes: 20 additions & 0 deletions Tests/GitKitTests/GitKitTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ final class GitKitTests: XCTestCase {
("testLog", testLog),
("testCommandWithArgs", testCommandWithArgs),
("testClone", testClone),
("testCheckoutRemoteTracking", testCheckoutRemoteTracking),
]

// MARK: - helpers
Expand Down Expand Up @@ -104,6 +105,25 @@ final class GitKitTests: XCTestCase {
self.assert(type: "output", result: statusOutput, expected: expectation)
}

func testCheckoutRemoteTracking() throws {
let path = self.currentPath()

try self.clean(path: path)
let git = Git(path: path)

try git.run(.clone(url: "https://github.com/binarybirds/shell-kit.git"))

let repoPath = "\(path)/shell-kit"
let repoGit = Git(path: repoPath)

try repoGit.run(.checkout(branch: "feature-branch", create: true, tracking: "origin/main"))
let branchOutput = try repoGit.run(.raw("branch -vv"))
try self.clean(path: path)

XCTAssertTrue(branchOutput.contains("feature-branch"), "New branch should be created")
XCTAssertTrue(branchOutput.contains("origin/main"), "Branch should track origin/main")
}

#if os(macOS)
func testAsyncRun() throws {
let path = self.currentPath()
Expand Down