@@ -4,6 +4,10 @@ import Model
44extension GitClient {
55 /// No overview available.
66 public static let liveValue : GitClient = GitClient { logType in
7+ // Check if we're in a git repository
8+ guard FileManager . default. fileExists ( atPath: " .git " ) else {
9+ throw GitError . repositoryNotFound
10+ }
711 let arguments : [ String ]
812 switch logType {
913 case let . branch( targetBranch) :
@@ -25,29 +29,42 @@ extension GitClient {
2529 ]
2630 }
2731
28- let log = shell (
32+ let ( output , exitCode ) = shell (
2933 command: " git " ,
3034 arguments: arguments
3135 )
36+
37+ guard exitCode == 0 else {
38+ throw GitError . gitCommandFailed ( " git \( arguments. joined ( separator: " " ) ) " , exitCode: exitCode)
39+ }
3240
33- return log . components ( separatedBy: " -@-@-@-@-@-@-@-@ " )
41+ return output . components ( separatedBy: " -@-@-@-@-@-@-@-@ " )
3442 . map { $0. trimmingCharacters ( in: . whitespacesAndNewlines) }
3543 . filter { $0. isEmpty == false }
3644 . compactMap { GitCommit ( $0) }
3745 } tag: {
38- let tag = shell (
46+ // Check if we're in a git repository
47+ guard FileManager . default. fileExists ( atPath: " .git " ) else {
48+ throw GitError . repositoryNotFound
49+ }
50+
51+ let ( output, exitCode) = shell (
3952 command: " git tag --merged " ,
4053 arguments: [ ]
4154 )
55+
56+ guard exitCode == 0 else {
57+ throw GitError . gitCommandFailed ( " git tag --merged " , exitCode: exitCode)
58+ }
4259
43- return tag . split ( separator: " \n " ) . map { String ( $0) }
60+ return output . split ( separator: " \n " ) . map { String ( $0) }
4461 }
4562}
4663
4764private func shell(
4865 command: String ,
4966 arguments: [ String ]
50- ) -> String {
67+ ) -> ( output : String , exitCode : Int ) {
5168 let script = " \( command) \( arguments. joined ( separator: " " ) ) "
5269
5370 let task = Process ( )
@@ -62,8 +79,11 @@ private func shell(
6279 task. standardError = errorPipe
6380
6481 try ? task. run ( )
82+ task. waitUntilExit ( )
6583
6684 let data = pipe. fileHandleForReading. readDataToEndOfFile ( )
67- return ( String ( data: data, encoding: . utf8) ?? " " )
85+ let output = ( String ( data: data, encoding: . utf8) ?? " " )
6886 . trimmingCharacters ( in: . whitespacesAndNewlines)
87+
88+ return ( output: output, exitCode: Int ( task. terminationStatus) )
6989}
0 commit comments