Skip to content

feat: rev-parse optional abbrev ref #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 20, 2025
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
14 changes: 11 additions & 3 deletions Sources/GitKit/Git.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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):
Expand All @@ -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 {
Expand Down
26 changes: 26 additions & 0 deletions Tests/GitKitTests/GitKitTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ final class GitKitTests: XCTestCase {
("testLog", testLog),
("testCommandWithArgs", testCommandWithArgs),
("testClone", testClone),
("testRevParse", testRevParse),
]

// MARK: - helpers
Expand Down Expand Up @@ -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()
Expand Down