Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
22 changes: 9 additions & 13 deletions Sources/GitKit/Git.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,11 @@ public final class Git: Shell {
case cmd(Command, String? = nil)
case addAll
case status(short: Bool = false)
case clone(url: String , dirName: String? = nil)
case commit(message: String, allowEmpty: Bool = false, gpgSigned: Bool = false)
case writeConfig(name: String, value: String)
case readConfig(name: String)
case clone(url: String)

/// - 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 checkout(branch: String, create: Bool = false)
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 @@ -69,20 +64,21 @@ public final class Git: Shell {
}
if gpgSigned {
params.append("--gpg-sign")
} else {
}
else {
params.append("--no-gpg-sign")
}
case .clone(let url):
case .clone(let url, let dirname):
params = [Command.clone.rawValue, url]
case .checkout(let branch, let create, let tracking):
if let dirName = dirname {
params.append(dirName)
}
case .checkout(let branch, let create):
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
26 changes: 14 additions & 12 deletions Tests/GitKitTests/GitKitTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ final class GitKitTests: XCTestCase {
("testLog", testLog),
("testCommandWithArgs", testCommandWithArgs),
("testClone", testClone),
("testCheckoutRemoteTracking", testCheckoutRemoteTracking),
("testRevParse", testRevParse),
]

Expand Down Expand Up @@ -106,26 +105,29 @@ final class GitKitTests: XCTestCase {
self.assert(type: "output", result: statusOutput, expected: expectation)
}

func testCheckoutRemoteTracking() throws {

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

let expectation = """
On branch main
Your branch is up to date with 'origin/main'.

nothing to commit, working tree clean
"""

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 git.run(.clone(url: "https://github.com/binarybirds/shell-kit.git", dirName: "MyCustomDirectory"))
let statusOutput = try git.run("cd \(path)/MyCustomDirectory && git status")
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")
self.assert(type: "output", result: statusOutput, expected: expectation)
}

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

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

Expand Down