Skip to content

Commit e481781

Browse files
armcknightAndrew McKnighttib
authored
feat: add parameter to checkout to specify a remote tracking branch (#16)
* add parameter to allow specifying a remote tracking branch * add test * fix tests --------- Co-authored-by: Andrew McKnight <[email protected]> Co-authored-by: Tibor Bödecs <[email protected]>
1 parent 0fc931a commit e481781

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

Sources/GitKit/Git.swift

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,12 @@ public final class Git: Shell {
2020
case writeConfig(name: String, value: String)
2121
case readConfig(name: String)
2222
case clone(url: String)
23-
case checkout(branch: String, create: Bool = false)
23+
24+
/// - parameter branch the name of the branch to checkout
25+
/// - parameter create whether to create a new branch or checkout an existing one
26+
/// - parameter tracking when creating a new branch, the name of the remote branch it should track
27+
case checkout(branch: String, create: Bool = false, tracking: String? = nil)
28+
2429
case log(numberOfCommits: Int? = nil, options: [String]? = nil, revisions: String? = nil)
2530
case push(remote: String? = nil, branch: String? = nil)
2631
case pull(remote: String? = nil, branch: String? = nil, rebase: Bool = false)
@@ -69,12 +74,15 @@ public final class Git: Shell {
6974
}
7075
case .clone(let url):
7176
params = [Command.clone.rawValue, url]
72-
case .checkout(let branch, let create):
77+
case .checkout(let branch, let create, let tracking):
7378
params = [Command.checkout.rawValue]
7479
if create {
7580
params.append("-b")
7681
}
7782
params.append(branch)
83+
if let tracking {
84+
params.append(tracking)
85+
}
7886
case .log(let numberOfCommits, let options, let revisions):
7987
params = [Command.log.rawValue]
8088
if let numberOfCommits = numberOfCommits {
@@ -149,7 +157,7 @@ public final class Git: Shell {
149157
params = [Command.config.rawValue, "--add", name, value]
150158
case .readConfig(let name):
151159
params = [Command.config.rawValue, "--get", name]
152-
case .revList(let branch, let count, let revisions):
160+
case .revList(_, let count, let revisions):
153161
params = [Command.revList.rawValue]
154162
if count {
155163
params.append("--count")

Tests/GitKitTests/GitKitTests.swift

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ final class GitKitTests: XCTestCase {
2525
("testLog", testLog),
2626
("testCommandWithArgs", testCommandWithArgs),
2727
("testClone", testClone),
28+
("testCheckoutRemoteTracking", testCheckoutRemoteTracking),
2829
("testRevParse", testRevParse),
2930
]
3031

@@ -105,9 +106,26 @@ final class GitKitTests: XCTestCase {
105106
self.assert(type: "output", result: statusOutput, expected: expectation)
106107
}
107108

108-
func testRevParse() throws {
109+
func testCheckoutRemoteTracking() throws {
109110
let path = self.currentPath()
111+
try self.clean(path: path)
112+
let git = Git(path: path)
110113

114+
try git.run(.clone(url: "https://github.com/binarybirds/shell-kit.git"))
115+
116+
let repoPath = "\(path)/shell-kit"
117+
let repoGit = Git(path: repoPath)
118+
119+
try repoGit.run(.checkout(branch: "feature-branch", create: true, tracking: "origin/main"))
120+
let branchOutput = try repoGit.run(.raw("branch -vv"))
121+
try self.clean(path: path)
122+
123+
XCTAssertTrue(branchOutput.contains("feature-branch"), "New branch should be created")
124+
XCTAssertTrue(branchOutput.contains("origin/main"), "Branch should track origin/main")
125+
}
126+
127+
func testRevParse() throws {
128+
let path = self.currentPath()
111129
try self.clean(path: path)
112130
let git = Git(path: path)
113131

0 commit comments

Comments
 (0)