From a360428cbe8b36cb89f923a8689e3cf0f0e4ea1d Mon Sep 17 00:00:00 2001 From: Andrew McKnight Date: Thu, 3 Jul 2025 15:31:37 -0800 Subject: [PATCH 1/3] add parameter to allow specifying a remote tracking branch --- Sources/GitKit/Git.swift | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/Sources/GitKit/Git.swift b/Sources/GitKit/Git.swift index 04ea514..5255778 100644 --- a/Sources/GitKit/Git.swift +++ b/Sources/GitKit/Git.swift @@ -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) @@ -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 { From 146679cc837c98aec790e0bc1069a1eaa3650a91 Mon Sep 17 00:00:00 2001 From: Andrew McKnight Date: Sat, 19 Jul 2025 19:03:14 -0800 Subject: [PATCH 2/3] add test --- Tests/GitKitTests/GitKitTests.swift | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/Tests/GitKitTests/GitKitTests.swift b/Tests/GitKitTests/GitKitTests.swift index 8eabc00..f733355 100644 --- a/Tests/GitKitTests/GitKitTests.swift +++ b/Tests/GitKitTests/GitKitTests.swift @@ -25,6 +25,7 @@ final class GitKitTests: XCTestCase { ("testLog", testLog), ("testCommandWithArgs", testCommandWithArgs), ("testClone", testClone), + ("testCheckoutRemoteTracking", testCheckoutRemoteTracking), ] // MARK: - helpers @@ -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() From bf81355cc4b798c39b6615952d19c1be056c6a32 Mon Sep 17 00:00:00 2001 From: Tibor Bodecs Date: Sun, 20 Jul 2025 13:18:29 +0200 Subject: [PATCH 3/3] fix tests --- Sources/GitKit/Git.swift | 2 +- Tests/GitKitTests/GitKitTests.swift | 8 +++++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/Sources/GitKit/Git.swift b/Sources/GitKit/Git.swift index f2834da..68cad62 100644 --- a/Sources/GitKit/Git.swift +++ b/Sources/GitKit/Git.swift @@ -157,7 +157,7 @@ public final class Git: Shell { params = [Command.config.rawValue, "--add", name, value] case .readConfig(let name): params = [Command.config.rawValue, "--get", name] - case .revList(let branch, let count, let revisions): + case .revList(_, let count, let revisions): params = [Command.revList.rawValue] if count { params.append("--count") diff --git a/Tests/GitKitTests/GitKitTests.swift b/Tests/GitKitTests/GitKitTests.swift index 533204b..d268e30 100644 --- a/Tests/GitKitTests/GitKitTests.swift +++ b/Tests/GitKitTests/GitKitTests.swift @@ -107,7 +107,11 @@ final class GitKitTests: XCTestCase { } func testCheckoutRemoteTracking() throws { - try git.run(.clone(url: "https://github.com/binarybirds/shell-kit.git")) + 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) @@ -121,9 +125,7 @@ final class GitKitTests: XCTestCase { } func testRevParse() throws { - let path = self.currentPath() - try self.clean(path: path) let git = Git(path: path)