Skip to content

Commit 0fc931a

Browse files
armcknightAndrew McKnighttib
authored
feat: rev-parse optional abbrev ref (#15)
* fix params; abbrev-ref is a boolean option; add separate param for the actual revision to parse * add test * fix test build * comments --------- Co-authored-by: Andrew McKnight <[email protected]> Co-authored-by: Tibor Bödecs <[email protected]>
1 parent 6bc44a1 commit 0fc931a

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

Sources/GitKit/Git.swift

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,11 @@ public final class Git: Shell {
3333
case submoduleForeach(recursive: Bool = false, command: String)
3434
case renameRemote(oldName: String, newName: String)
3535
case addRemote(name: String, url: String)
36-
case revParse(abbrevRef: String)
36+
37+
/// - parameter abbrevRef whether or not the result should be the abbreviated reference name or the full commit SHA hash
38+
/// - parameter revision the name of the revision to parse. can be symbolic (`@`), human-readable (`origin/HEAD`) or a commit SHA hash
39+
case revParse(abbrevRef: Bool, revision: String)
40+
3741
case revList(branch: String, count: Bool = false, revisions: String? = nil)
3842
case raw(String)
3943
case lsRemote(url: String, limitToHeads: Bool = false)
@@ -143,8 +147,6 @@ public final class Git: Shell {
143147
params.append(command)
144148
case .writeConfig(let name, let value):
145149
params = [Command.config.rawValue, "--add", name, value]
146-
case .revParse(abbrevRef: let abbrevRef):
147-
params = [Command.revParse.rawValue, "--abbrev-ref", abbrevRef]
148150
case .readConfig(let name):
149151
params = [Command.config.rawValue, "--get", name]
150152
case .revList(let branch, let count, let revisions):
@@ -155,6 +157,12 @@ public final class Git: Shell {
155157
if let revisions = revisions {
156158
params.append(revisions)
157159
}
160+
case .revParse(let abbrevRef, let revision):
161+
params = [Command.revParse.rawValue]
162+
if abbrevRef {
163+
params.append("--abbrev-ref")
164+
}
165+
params.append(revision)
158166
case .lsRemote(url: let url, limitToHeads: let limitToHeads):
159167
params = [Command.lsRemote.rawValue]
160168
if limitToHeads {

Tests/GitKitTests/GitKitTests.swift

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ final class GitKitTests: XCTestCase {
2525
("testLog", testLog),
2626
("testCommandWithArgs", testCommandWithArgs),
2727
("testClone", testClone),
28+
("testRevParse", testRevParse),
2829
]
2930

3031
// MARK: - helpers
@@ -104,6 +105,31 @@ final class GitKitTests: XCTestCase {
104105
self.assert(type: "output", result: statusOutput, expected: expectation)
105106
}
106107

108+
func testRevParse() throws {
109+
let path = self.currentPath()
110+
111+
try self.clean(path: path)
112+
let git = Git(path: path)
113+
114+
try git.run(.raw("init"))
115+
try git.run(.commit(message: "initial commit", allowEmpty: true))
116+
117+
let abbrevRef = try git.run(.revParse(abbrevRef: true, revision: "HEAD"))
118+
XCTAssertEqual(abbrevRef, "main", "Should return abbreviated reference name")
119+
120+
let fullSHA = try git.run(.revParse(abbrevRef: false, revision: "HEAD"))
121+
XCTAssertTrue(fullSHA.count == 40, "Should return full 40-character SHA")
122+
XCTAssertTrue(fullSHA.allSatisfy { $0.isHexDigit }, "SHA should contain only hex characters")
123+
124+
let symbolicRef = try git.run(.revParse(abbrevRef: false, revision: "@"))
125+
XCTAssertEqual(symbolicRef, fullSHA, "Symbolic '@' should resolve to same SHA as HEAD")
126+
127+
let currentBranch = try git.run(.revParse(abbrevRef: true, revision: "@"))
128+
XCTAssertEqual(currentBranch, "main", "Should return current branch name")
129+
130+
try self.clean(path: path)
131+
}
132+
107133
#if os(macOS)
108134
func testAsyncRun() throws {
109135
let path = self.currentPath()

0 commit comments

Comments
 (0)