diff --git a/Sources/GitKit/Git.swift b/Sources/GitKit/Git.swift index 1447fd0..66fb3af 100644 --- a/Sources/GitKit/Git.swift +++ b/Sources/GitKit/Git.swift @@ -33,7 +33,11 @@ public final class Git: Shell { case submoduleForeach(recursive: Bool = false, command: String) case renameRemote(oldName: String, newName: String) case addRemote(name: String, url: String) - case revParse(abbrevRef: String) + + /// - parameter abbrevRef whether or not the result should be the abbreviated reference name or the full commit SHA hash + /// - parameter revision the name of the revision to parse. can be symbolic (`@`), human-readable (`origin/HEAD`) or a commit SHA hash + case revParse(abbrevRef: Bool, revision: String) + case revList(branch: String, count: Bool = false, revisions: String? = nil) case raw(String) case lsRemote(url: String, limitToHeads: Bool = false) @@ -143,8 +147,6 @@ public final class Git: Shell { params.append(command) case .writeConfig(let name, let value): params = [Command.config.rawValue, "--add", name, value] - case .revParse(abbrevRef: let abbrevRef): - params = [Command.revParse.rawValue, "--abbrev-ref", abbrevRef] case .readConfig(let name): params = [Command.config.rawValue, "--get", name] case .revList(let branch, let count, let revisions): @@ -155,6 +157,12 @@ public final class Git: Shell { if let revisions = revisions { params.append(revisions) } + case .revParse(let abbrevRef, let revision): + params = [Command.revParse.rawValue] + if abbrevRef { + params.append("--abbrev-ref") + } + params.append(revision) case .lsRemote(url: let url, limitToHeads: let limitToHeads): params = [Command.lsRemote.rawValue] if limitToHeads { diff --git a/Tests/GitKitTests/GitKitTests.swift b/Tests/GitKitTests/GitKitTests.swift index 4699cc7..55622da 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), + ("testRevParse", testRevParse), ] // MARK: - helpers @@ -104,6 +105,31 @@ final class GitKitTests: XCTestCase { 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) + + try git.run(.raw("init")) + try git.run(.commit(message: "initial commit", allowEmpty: true)) + + let abbrevRef = try git.run(.revParse(abbrevRef: true, revision: "HEAD")) + XCTAssertEqual(abbrevRef, "main", "Should return abbreviated reference name") + + let fullSHA = try git.run(.revParse(abbrevRef: false, revision: "HEAD")) + XCTAssertTrue(fullSHA.count == 40, "Should return full 40-character SHA") + XCTAssertTrue(fullSHA.allSatisfy { $0.isHexDigit }, "SHA should contain only hex characters") + + let symbolicRef = try git.run(.revParse(abbrevRef: false, revision: "@")) + XCTAssertEqual(symbolicRef, fullSHA, "Symbolic '@' should resolve to same SHA as HEAD") + + let currentBranch = try git.run(.revParse(abbrevRef: true, revision: "@")) + XCTAssertEqual(currentBranch, "main", "Should return current branch name") + + try self.clean(path: path) + } + #if os(macOS) func testAsyncRun() throws { let path = self.currentPath()