From 47036818560e17d8708cbd2d03eb32106f3ff7f4 Mon Sep 17 00:00:00 2001 From: Andrew McKnight Date: Thu, 3 Jul 2025 15:32:39 -0800 Subject: [PATCH 1/4] fix params; abbrev-ref is a boolean option; add separate param for the actual revision to parse --- Sources/GitKit/Git.swift | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/Sources/GitKit/Git.swift b/Sources/GitKit/Git.swift index 04ea514..f4a4a7a 100644 --- a/Sources/GitKit/Git.swift +++ b/Sources/GitKit/Git.swift @@ -32,7 +32,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) @@ -137,8 +141,6 @@ public final class Git: Shell { params.append(command) case .config(name: let name, value: let value): params = [Command.config.rawValue, "--add", name, value] - case .revParse(abbrevRef: let abbrevRef): - params = [Command.revParse.rawValue, "--abbrev-ref", abbrevRef] case .revList(let branch, let count, let revisions): params = [Command.revList.rawValue] if count { From ed9c1abe14e90093056c4dac0c4498ee7eda0a4c Mon Sep 17 00:00:00 2001 From: Andrew McKnight Date: Sat, 19 Jul 2025 19:22:02 -0800 Subject: [PATCH 2/4] add test --- Sources/GitKit/Git.swift | 6 ++++++ Tests/GitKitTests/GitKitTests.swift | 32 +++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/Sources/GitKit/Git.swift b/Sources/GitKit/Git.swift index f4a4a7a..82b2cc5 100644 --- a/Sources/GitKit/Git.swift +++ b/Sources/GitKit/Git.swift @@ -149,6 +149,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 8eabc00..3c6f539 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,37 @@ 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) + + // Initialize a repository and make an initial commit + try git.run(.raw("init")) + try git.run(.commit(message: "initial commit", true)) + + // Test 1: Get abbreviated reference name for HEAD + let abbrevRef = try git.run(.revParse(abbrevRef: true, revision: "HEAD")) + XCTAssertEqual(abbrevRef, "main", "Should return abbreviated reference name") + + // Test 2: Get full commit SHA for HEAD + 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") + + // Test 3: Parse symbolic reference (@) + let symbolicRef = try git.run(.revParse(abbrevRef: false, revision: "@")) + XCTAssertEqual(symbolicRef, fullSHA, "Symbolic '@' should resolve to same SHA as HEAD") + + // Test 4: Get abbreviated reference for current branch + let currentBranch = try git.run(.revParse(abbrevRef: true, revision: "@")) + XCTAssertEqual(currentBranch, "main", "Should return current branch name") + + // Clean up + try self.clean(path: path) + } + #if os(macOS) func testAsyncRun() throws { let path = self.currentPath() From fb67284c81f12ad265827724f74d3b01281f2892 Mon Sep 17 00:00:00 2001 From: Andrew McKnight Date: Sat, 19 Jul 2025 19:25:08 -0800 Subject: [PATCH 3/4] fix test build --- Tests/GitKitTests/GitKitTests.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tests/GitKitTests/GitKitTests.swift b/Tests/GitKitTests/GitKitTests.swift index 3c6f539..d86ab41 100644 --- a/Tests/GitKitTests/GitKitTests.swift +++ b/Tests/GitKitTests/GitKitTests.swift @@ -113,7 +113,7 @@ final class GitKitTests: XCTestCase { // Initialize a repository and make an initial commit try git.run(.raw("init")) - try git.run(.commit(message: "initial commit", true)) + try git.run(.commit(message: "initial commit", allowEmpty: true)) // Test 1: Get abbreviated reference name for HEAD let abbrevRef = try git.run(.revParse(abbrevRef: true, revision: "HEAD")) From 824086018a7611c36d63d23f9ad7156e9af53071 Mon Sep 17 00:00:00 2001 From: Andrew McKnight Date: Sat, 19 Jul 2025 19:29:18 -0800 Subject: [PATCH 4/4] comments --- Tests/GitKitTests/GitKitTests.swift | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/Tests/GitKitTests/GitKitTests.swift b/Tests/GitKitTests/GitKitTests.swift index d86ab41..68e55d0 100644 --- a/Tests/GitKitTests/GitKitTests.swift +++ b/Tests/GitKitTests/GitKitTests.swift @@ -110,29 +110,23 @@ final class GitKitTests: XCTestCase { try self.clean(path: path) let git = Git(path: path) - - // Initialize a repository and make an initial commit + try git.run(.raw("init")) try git.run(.commit(message: "initial commit", allowEmpty: true)) - - // Test 1: Get abbreviated reference name for HEAD + let abbrevRef = try git.run(.revParse(abbrevRef: true, revision: "HEAD")) XCTAssertEqual(abbrevRef, "main", "Should return abbreviated reference name") - // Test 2: Get full commit SHA for HEAD 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") - - // Test 3: Parse symbolic reference (@) + let symbolicRef = try git.run(.revParse(abbrevRef: false, revision: "@")) XCTAssertEqual(symbolicRef, fullSHA, "Symbolic '@' should resolve to same SHA as HEAD") - - // Test 4: Get abbreviated reference for current branch + let currentBranch = try git.run(.revParse(abbrevRef: true, revision: "@")) XCTAssertEqual(currentBranch, "main", "Should return current branch name") - // Clean up try self.clean(path: path) }