-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGit.swift
More file actions
43 lines (28 loc) · 1.28 KB
/
Git.swift
File metadata and controls
43 lines (28 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import Foundation
public protocol GitProtocol {
static func authorName(_ sha: String) -> String?
static func commitTitle(_ sha: String) -> String?
static func commitMessage(_ sha: String) -> String?
static func repoName() -> String?
static func branchName() -> String?
}
public struct Git: GitProtocol {
public static func repoName() -> String? {
guard let repoPath = Bash.run("git", arguments: ["rev-parse", "--show-toplevel"]) else {
return nil
}
return Bash.run("basename", arguments: [repoPath])?.trimmingCharacters(in: .whitespacesAndNewlines)
}
public static func branchName() -> String? {
return Bash.run("git", arguments: ["rev-parse", "--abbrev-ref", "HEAD"])?.trimmingCharacters(in: .whitespacesAndNewlines)
}
public static func authorName(_ sha: String) -> String? {
return Bash.run("git", arguments: ["show", sha, "--quiet", "--pretty=format:%an"])
}
public static func commitTitle(_ sha: String) -> String? {
return Bash.run("git", arguments: ["show", sha, "--oneline", "--quiet"])
}
public static func commitMessage(_ sha: String) -> String? {
return Bash.run("git", arguments: ["show", sha, "--quiet"])
}
}