From 19e24dd7d8d68bbce648d91f9380e30344da4b55 Mon Sep 17 00:00:00 2001 From: Matt Rubin Date: Sun, 14 Apr 2019 17:42:37 -0400 Subject: [PATCH 1/6] Replace antitypical/Result with the standard library Result type --- .gitmodules | 3 --- Cartfile | 1 - Cartfile.resolved | 1 - Carthage/Checkouts/Result | 1 - SwiftGit2.xcodeproj/project.pbxproj | 14 ++++++-------- SwiftGit2.xcworkspace/contents.xcworkspacedata | 3 --- SwiftGit2/CommitIterator.swift | 2 +- SwiftGit2/Diffs.swift | 2 +- SwiftGit2/OID.swift | 1 - SwiftGit2/Objects.swift | 1 - SwiftGit2/Repository.swift | 1 - SwiftGit2/ResultShims.swift | 15 +++++++++++++++ SwiftGit2Tests/Fixtures/Fixtures.swift | 1 - SwiftGit2Tests/OIDSpec.swift | 1 - SwiftGit2Tests/ObjectsSpec.swift | 1 - SwiftGit2Tests/ReferencesSpec.swift | 1 - SwiftGit2Tests/RemotesSpec.swift | 1 - SwiftGit2Tests/RepositorySpec.swift | 1 - 18 files changed, 23 insertions(+), 28 deletions(-) delete mode 100644 Cartfile delete mode 160000 Carthage/Checkouts/Result create mode 100644 SwiftGit2/ResultShims.swift diff --git a/.gitmodules b/.gitmodules index 18f48a9..cff0fbd 100644 --- a/.gitmodules +++ b/.gitmodules @@ -10,9 +10,6 @@ [submodule "Carthage/Checkouts/xcconfigs"] path = Carthage/Checkouts/xcconfigs url = https://github.com/jspahrsummers/xcconfigs.git -[submodule "Carthage/Checkouts/Result"] - path = Carthage/Checkouts/Result - url = https://github.com/antitypical/Result.git [submodule "Carthage/Checkouts/ZipArchive"] path = Carthage/Checkouts/ZipArchive url = https://github.com/ZipArchive/ZipArchive.git diff --git a/Cartfile b/Cartfile deleted file mode 100644 index e9bc3ad..0000000 --- a/Cartfile +++ /dev/null @@ -1 +0,0 @@ -github "antitypical/Result" ~> 4.0 diff --git a/Cartfile.resolved b/Cartfile.resolved index f85cfe8..975e3b3 100644 --- a/Cartfile.resolved +++ b/Cartfile.resolved @@ -1,5 +1,4 @@ github "Quick/Nimble" "v8.0.1" github "Quick/Quick" "v2.0.0" github "ZipArchive/ZipArchive" "v2.1.5" -github "antitypical/Result" "4.1.0" github "jspahrsummers/xcconfigs" "1.0" diff --git a/Carthage/Checkouts/Result b/Carthage/Checkouts/Result deleted file mode 160000 index 2ca499b..0000000 --- a/Carthage/Checkouts/Result +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 2ca499ba456795616fbc471561ff1d963e6ae160 diff --git a/SwiftGit2.xcodeproj/project.pbxproj b/SwiftGit2.xcodeproj/project.pbxproj index 1a48d4b..188c4ff 100644 --- a/SwiftGit2.xcodeproj/project.pbxproj +++ b/SwiftGit2.xcodeproj/project.pbxproj @@ -50,7 +50,6 @@ 621E66C81C72958D00A0F352 /* detached-head.zip in Resources */ = {isa = PBXBuildFile; fileRef = BE0B1C5C1A9978890004726D /* detached-head.zip */; }; 621E66C91C72958D00A0F352 /* Mantle.zip in Resources */ = {isa = PBXBuildFile; fileRef = BE0991F61A578FB1007D4E6A /* Mantle.zip */; }; 621E66CA1C72958D00A0F352 /* simple-repository.zip in Resources */ = {isa = PBXBuildFile; fileRef = BE14AA561A198C6E0015B439 /* simple-repository.zip */; }; - 621E66D91C72989A00A0F352 /* Result.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 621E66D81C72989900A0F352 /* Result.framework */; }; 621E66E61C729D9600A0F352 /* SwiftGit2.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 621E66B41C72958800A0F352 /* SwiftGit2.framework */; }; 621E66FE1C72A5FF00A0F352 /* libiconv.tbd in Frameworks */ = {isa = PBXBuildFile; fileRef = 621E66FD1C72A5FF00A0F352 /* libiconv.tbd */; }; 621E67001C72A60B00A0F352 /* libz.tbd in Frameworks */ = {isa = PBXBuildFile; fileRef = 621E66FF1C72A60B00A0F352 /* libz.tbd */; }; @@ -81,7 +80,8 @@ BECB5F6C1A56F1B400999413 /* ReferencesSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = BECB5F6B1A56F1B400999413 /* ReferencesSpec.swift */; }; BECB5F6E1A57284700999413 /* Remotes.swift in Sources */ = {isa = PBXBuildFile; fileRef = BECB5F6D1A57284700999413 /* Remotes.swift */; }; BECB5F701A57286200999413 /* RemotesSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = BECB5F6F1A57286200999413 /* RemotesSpec.swift */; }; - BEE591C71ADF470500534F14 /* Result.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = BEE591C61ADF470500534F14 /* Result.framework */; }; + C98A2BA02263E017007A4E3A /* ResultShims.swift in Sources */ = {isa = PBXBuildFile; fileRef = C98A2B9F2263E017007A4E3A /* ResultShims.swift */; }; + C98A2BA12263E017007A4E3A /* ResultShims.swift in Sources */ = {isa = PBXBuildFile; fileRef = C98A2B9F2263E017007A4E3A /* ResultShims.swift */; }; DA5023A01A969F1A004175D7 /* Nimble.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = DA50239F1A969F1A004175D7 /* Nimble.framework */; }; DA5914761A94579000AED74C /* Errors.swift in Sources */ = {isa = PBXBuildFile; fileRef = DA5914751A94579000AED74C /* Errors.swift */; }; DAC8143D1A99749D0063D88C /* Quick.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = DAC8143C1A99749D0063D88C /* Quick.framework */; }; @@ -138,7 +138,6 @@ 25499A996CA7BD416620A397 /* CommitIterator.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CommitIterator.swift; sourceTree = ""; }; 621E66B41C72958800A0F352 /* SwiftGit2.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = SwiftGit2.framework; sourceTree = BUILT_PRODUCTS_DIR; }; 621E66CE1C72958D00A0F352 /* SwiftGit2-iOSTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = "SwiftGit2-iOSTests.xctest"; sourceTree = BUILT_PRODUCTS_DIR; }; - 621E66D81C72989900A0F352 /* Result.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Result.framework; path = "Carthage/Checkouts/Result/build/Debug-iphoneos/Result.framework"; sourceTree = ""; }; 621E66E11C729CE500A0F352 /* libgit2.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libgit2.a; sourceTree = BUILT_PRODUCTS_DIR; }; 621E66EC1C729EB800A0F352 /* libssl.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libssl.a; sourceTree = BUILT_PRODUCTS_DIR; }; 621E66F21C729EBB00A0F352 /* libssh2.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libssh2.a; sourceTree = BUILT_PRODUCTS_DIR; }; @@ -191,7 +190,7 @@ BECB5F6B1A56F1B400999413 /* ReferencesSpec.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ReferencesSpec.swift; sourceTree = ""; }; BECB5F6D1A57284700999413 /* Remotes.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Remotes.swift; sourceTree = ""; }; BECB5F6F1A57286200999413 /* RemotesSpec.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RemotesSpec.swift; sourceTree = ""; }; - BEE591C61ADF470500534F14 /* Result.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Result.framework; path = "../../Library/Developer/Xcode/DerivedData/SwiftGit2-cdzquzvlxdewyddxgdgxjagqvjba/Build/Products/Debug/Result.framework"; sourceTree = ""; }; + C98A2B9F2263E017007A4E3A /* ResultShims.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ResultShims.swift; sourceTree = ""; }; DA50239F1A969F1A004175D7 /* Nimble.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; path = Nimble.framework; sourceTree = BUILT_PRODUCTS_DIR; }; DA5914751A94579000AED74C /* Errors.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Errors.swift; sourceTree = ""; }; DAC8143C1A99749D0063D88C /* Quick.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Quick.framework; path = "../../../../Library/Developer/Xcode/DerivedData/SwiftGit2-ezqqkevntxbroughwcioyxqriijk/Build/Products/Debug/Quick.framework"; sourceTree = ""; }; @@ -204,7 +203,6 @@ files = ( 621E67001C72A60B00A0F352 /* libz.tbd in Frameworks */, 621E66FE1C72A5FF00A0F352 /* libiconv.tbd in Frameworks */, - 621E66D91C72989A00A0F352 /* Result.framework in Frameworks */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -223,7 +221,6 @@ isa = PBXFrameworksBuildPhase; buildActionMask = 2147483647; files = ( - BEE591C71ADF470500534F14 /* Result.framework in Frameworks */, BEB31FA01A0E595600F525B9 /* libz.dylib in Frameworks */, BEB31F9E1A0E595100F525B9 /* libiconv.dylib in Frameworks */, ); @@ -246,7 +243,6 @@ 621E66D41C72965C00A0F352 /* Mac */ = { isa = PBXGroup; children = ( - BEE591C61ADF470500534F14 /* Result.framework */, BEB31F9D1A0E595100F525B9 /* libiconv.dylib */, BEB31F9F1A0E595600F525B9 /* libz.dylib */, ); @@ -256,7 +252,6 @@ 621E66D51C72966000A0F352 /* iOS */ = { isa = PBXGroup; children = ( - 621E66D81C72989900A0F352 /* Result.framework */, 621E66FD1C72A5FF00A0F352 /* libiconv.tbd */, 621E66FF1C72A60B00A0F352 /* libz.tbd */, ); @@ -320,6 +315,7 @@ BECB5F691A56F19900999413 /* References.swift */, BECB5F6D1A57284700999413 /* Remotes.swift */, 25499A996CA7BD416620A397 /* CommitIterator.swift */, + C98A2B9F2263E017007A4E3A /* ResultShims.swift */, ); path = SwiftGit2; sourceTree = ""; @@ -757,6 +753,7 @@ 232861451F4A3A2E00276D65 /* Diffs.swift in Sources */, 621E66A51C72958800A0F352 /* References.swift in Sources */, 621E66A61C72958800A0F352 /* Libgit2.swift in Sources */, + C98A2BA12263E017007A4E3A /* ResultShims.swift in Sources */, 621E66A71C72958800A0F352 /* Pointers.swift in Sources */, 621E66A81C72958800A0F352 /* Errors.swift in Sources */, 621E66A91C72958800A0F352 /* SwiftGit2.m in Sources */, @@ -791,6 +788,7 @@ 232861431F4A3A2E00276D65 /* Diffs.swift in Sources */, BECB5F6A1A56F19900999413 /* References.swift in Sources */, BE36354C1A632C9700D37EC8 /* Libgit2.swift in Sources */, + C98A2BA02263E017007A4E3A /* ResultShims.swift in Sources */, BE7A753F1A4A2BCC002DA7E3 /* Pointers.swift in Sources */, DA5914761A94579000AED74C /* Errors.swift in Sources */, BE14AA501A1974010015B439 /* SwiftGit2.m in Sources */, diff --git a/SwiftGit2.xcworkspace/contents.xcworkspacedata b/SwiftGit2.xcworkspace/contents.xcworkspacedata index bb82c2c..ee8622b 100644 --- a/SwiftGit2.xcworkspace/contents.xcworkspacedata +++ b/SwiftGit2.xcworkspace/contents.xcworkspacedata @@ -4,9 +4,6 @@ - - diff --git a/SwiftGit2/CommitIterator.swift b/SwiftGit2/CommitIterator.swift index 0974da7..4f63ecf 100644 --- a/SwiftGit2/CommitIterator.swift +++ b/SwiftGit2/CommitIterator.swift @@ -3,7 +3,7 @@ // Copyright (c) 2017 GitHub, Inc. All rights reserved. // -import Result +import Foundation import libgit2 public class CommitIterator: IteratorProtocol, Sequence { diff --git a/SwiftGit2/Diffs.swift b/SwiftGit2/Diffs.swift index 8e3b3df..7b00bdf 100644 --- a/SwiftGit2/Diffs.swift +++ b/SwiftGit2/Diffs.swift @@ -5,7 +5,7 @@ // Created by Jake Van Alstyne on 8/20/17. // Copyright © 2017 GitHub, Inc. All rights reserved. // -import Foundation + import libgit2 public struct StatusEntry { diff --git a/SwiftGit2/OID.swift b/SwiftGit2/OID.swift index 9e85350..e3a3e01 100644 --- a/SwiftGit2/OID.swift +++ b/SwiftGit2/OID.swift @@ -6,7 +6,6 @@ // Copyright (c) 2014 GitHub, Inc. All rights reserved. // -import Foundation import libgit2 /// An identifier for a Git object. diff --git a/SwiftGit2/Objects.swift b/SwiftGit2/Objects.swift index 0157045..4b2c037 100644 --- a/SwiftGit2/Objects.swift +++ b/SwiftGit2/Objects.swift @@ -8,7 +8,6 @@ import Foundation import libgit2 -import Result /// A git object. public protocol ObjectType { diff --git a/SwiftGit2/Repository.swift b/SwiftGit2/Repository.swift index e677409..e5e082e 100644 --- a/SwiftGit2/Repository.swift +++ b/SwiftGit2/Repository.swift @@ -7,7 +7,6 @@ // import Foundation -import Result import libgit2 public typealias CheckoutProgressBlock = (String?, Int, Int) -> Void diff --git a/SwiftGit2/ResultShims.swift b/SwiftGit2/ResultShims.swift new file mode 100644 index 0000000..0c600e2 --- /dev/null +++ b/SwiftGit2/ResultShims.swift @@ -0,0 +1,15 @@ +public extension Result { + var value: Success? { + guard case .success(let value) = self else { + return nil + } + return value + } + + var error: Failure? { + guard case .failure(let error) = self else { + return nil + } + return error + } +} diff --git a/SwiftGit2Tests/Fixtures/Fixtures.swift b/SwiftGit2Tests/Fixtures/Fixtures.swift index bb1b0a6..fdb24e4 100644 --- a/SwiftGit2Tests/Fixtures/Fixtures.swift +++ b/SwiftGit2Tests/Fixtures/Fixtures.swift @@ -6,7 +6,6 @@ // Copyright (c) 2014 GitHub, Inc. All rights reserved. // -import Foundation import SwiftGit2 import ZipArchive diff --git a/SwiftGit2Tests/OIDSpec.swift b/SwiftGit2Tests/OIDSpec.swift index 1b8c993..90b2621 100644 --- a/SwiftGit2Tests/OIDSpec.swift +++ b/SwiftGit2Tests/OIDSpec.swift @@ -6,7 +6,6 @@ // Copyright (c) 2014 GitHub, Inc. All rights reserved. // -import Result import SwiftGit2 import Nimble import Quick diff --git a/SwiftGit2Tests/ObjectsSpec.swift b/SwiftGit2Tests/ObjectsSpec.swift index f04832c..9d6bb8c 100644 --- a/SwiftGit2Tests/ObjectsSpec.swift +++ b/SwiftGit2Tests/ObjectsSpec.swift @@ -6,7 +6,6 @@ // Copyright (c) 2014 GitHub, Inc. All rights reserved. // -import Result import SwiftGit2 import Nimble import Quick diff --git a/SwiftGit2Tests/ReferencesSpec.swift b/SwiftGit2Tests/ReferencesSpec.swift index 058340f..5409980 100644 --- a/SwiftGit2Tests/ReferencesSpec.swift +++ b/SwiftGit2Tests/ReferencesSpec.swift @@ -6,7 +6,6 @@ // Copyright (c) 2015 GitHub, Inc. All rights reserved. // -import Result import SwiftGit2 import Nimble import Quick diff --git a/SwiftGit2Tests/RemotesSpec.swift b/SwiftGit2Tests/RemotesSpec.swift index 1dc4ceb..86970ae 100644 --- a/SwiftGit2Tests/RemotesSpec.swift +++ b/SwiftGit2Tests/RemotesSpec.swift @@ -6,7 +6,6 @@ // Copyright (c) 2015 GitHub, Inc. All rights reserved. // -import Result import SwiftGit2 import Nimble import Quick diff --git a/SwiftGit2Tests/RepositorySpec.swift b/SwiftGit2Tests/RepositorySpec.swift index 335188e..07f4a07 100644 --- a/SwiftGit2Tests/RepositorySpec.swift +++ b/SwiftGit2Tests/RepositorySpec.swift @@ -6,7 +6,6 @@ // Copyright (c) 2014 GitHub, Inc. All rights reserved. // -import Result import SwiftGit2 import Nimble import Quick From 690f6c9370ffd998f6fd5de7649e65e700383a03 Mon Sep 17 00:00:00 2001 From: Matt Rubin Date: Sun, 14 Apr 2019 18:20:36 -0400 Subject: [PATCH 2/6] Refactor to reduce Repository's use of Result shims --- SwiftGit2/Repository.swift | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/SwiftGit2/Repository.swift b/SwiftGit2/Repository.swift index e5e082e..cc345aa 100644 --- a/SwiftGit2/Repository.swift +++ b/SwiftGit2/Repository.swift @@ -712,20 +712,22 @@ public final class Repository { var mergeDiff: OpaquePointer? = nil defer { git_object_free(mergeDiff) } for parent in commit.parents { - let error = self.diff(from: parent.oid, to: commit.oid) { (diff: Result) -> NSError? in - guard diff.error == nil else { - return diff.error! - } + let error = self.diff(from: parent.oid, to: commit.oid) { + switch $0 { + case .failure(let error): + return error - if mergeDiff == nil { - mergeDiff = diff.value! - } else { - let mergeResult = git_diff_merge(mergeDiff, diff.value) - guard mergeResult == GIT_OK.rawValue else { - return NSError(gitError: mergeResult, pointOfFailure: "git_diff_merge") + case .success(let newDiff): + if mergeDiff == nil { + mergeDiff = newDiff + } else { + let mergeResult = git_diff_merge(mergeDiff, newDiff) + guard mergeResult == GIT_OK.rawValue else { + return NSError(gitError: mergeResult, pointOfFailure: "git_diff_merge") + } } + return nil } - return nil } if error != nil { From 9becf1cab0679f9e425b615d7ff470f2ae5c051a Mon Sep 17 00:00:00 2001 From: Matt Rubin Date: Sun, 14 Apr 2019 18:28:28 -0400 Subject: [PATCH 3/6] Refactor to reduce Repository's use of Result shims --- SwiftGit2/Repository.swift | 42 ++++++++++++++++++++------------------ 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/SwiftGit2/Repository.swift b/SwiftGit2/Repository.swift index cc345aa..35a5f12 100644 --- a/SwiftGit2/Repository.swift +++ b/SwiftGit2/Repository.swift @@ -744,23 +744,23 @@ public final class Repository { var oldTree: OpaquePointer? = nil defer { git_object_free(oldTree) } if let oid = oldCommitOid { - let result = unsafeTreeForCommitId(oid) - guard result.error == nil else { - return transform(Result.failure(result.error!)) + switch unsafeTreeForCommitId(oid) { + case .failure(let error): + return transform(.failure(error)) + case .success(let value): + oldTree = value } - - oldTree = result.value } var newTree: OpaquePointer? = nil defer { git_object_free(newTree) } if let oid = newCommitOid { - let result = unsafeTreeForCommitId(oid) - guard result.error == nil else { - return transform(Result.failure(result.error!)) + switch unsafeTreeForCommitId(oid) { + case .failure(let error): + return transform(.failure(error)) + case .success(let value): + newTree = value } - - newTree = result.value } var diff: OpaquePointer? = nil @@ -783,21 +783,23 @@ public final class Repository { assert(oldCommitOid != nil || newCommitOid != nil, "It is an error to pass nil for both the oldOid and newOid") var oldTree: Tree? = nil - if oldCommitOid != nil { - let result = safeTreeForCommitId(oldCommitOid!) - guard result.error == nil else { - return Result.failure(result.error!) + if let oldCommitOid = oldCommitOid { + switch safeTreeForCommitId(oldCommitOid) { + case .failure(let error): + return .failure(error) + case .success(let value): + oldTree = value } - oldTree = result.value } var newTree: Tree? = nil - if newCommitOid != nil { - let result = self.safeTreeForCommitId(newCommitOid!) - guard result.error == nil else { - return Result.failure(result.error!) + if let newCommitOid = newCommitOid { + switch safeTreeForCommitId(newCommitOid) { + case .failure(let error): + return .failure(error) + case .success(let value): + newTree = value } - newTree = result.value! } if oldTree != nil && newTree != nil { From 71e9b51a522f5a822b89873a8a529fb27eca09e6 Mon Sep 17 00:00:00 2001 From: Matt Rubin Date: Sun, 14 Apr 2019 18:36:16 -0400 Subject: [PATCH 4/6] Remove unnecessary failure mapping --- SwiftGit2/Repository.swift | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/SwiftGit2/Repository.swift b/SwiftGit2/Repository.swift index 35a5f12..9aa0fc8 100644 --- a/SwiftGit2/Repository.swift +++ b/SwiftGit2/Repository.swift @@ -867,11 +867,7 @@ public final class Repository { private func safeTreeForCommitId(_ oid: OID) -> Result { return withGitObject(oid, type: GIT_OBJ_COMMIT) { commit in let treeId = git_commit_tree_id(commit) - let tree = self.tree(OID(treeId!.pointee)) - guard tree.error == nil else { - return .failure(tree.error!) - } - return tree + return tree(OID(treeId!.pointee)) } } From cd3735cd71dfcb14aa09a112ba49f562684915b6 Mon Sep 17 00:00:00 2001 From: Matt Rubin Date: Sun, 14 Apr 2019 19:45:52 -0400 Subject: [PATCH 5/6] Fix error handling when aggregating an array of Results The previous implementation never handled failure properly and would crash when attempting to force-unwrap a nonexistent success value. --- SwiftGit2/Repository.swift | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/SwiftGit2/Repository.swift b/SwiftGit2/Repository.swift index 9aa0fc8..11feb25 100644 --- a/SwiftGit2/Repository.swift +++ b/SwiftGit2/Repository.swift @@ -361,11 +361,7 @@ public final class Repository { git_strarray_free(pointer) pointer.deallocate() - let error = remotes.reduce(nil) { $0 == nil ? $0 : $1.error } - if let error = error { - return Result.failure(error) - } - return Result.success(remotes.map { $0.value! }) + return remotes.aggregateResult() } private func remoteLookup(named name: String, _ callback: (Result) -> A) -> A { @@ -431,11 +427,7 @@ public final class Repository { git_strarray_free(pointer) pointer.deallocate() - let error = references.reduce(nil) { $0 == nil ? $0 : $1.error } - if let error = error { - return Result.failure(error) - } - return Result.success(references.map { $0.value! }) + return references.aggregateResult() } /// Load the reference with the given long name (e.g. "refs/heads/master") @@ -951,3 +943,18 @@ public final class Repository { } } } + +private extension Array { + func aggregateResult() -> Result<[Value], Error> where Element == Result { + var values: [Value] = [] + for result in self { + switch result { + case .success(let value): + values.append(value) + case .failure(let error): + return .failure(error) + } + } + return .success(values) + } +} From 80f4e600718ada7db3a610799a447b4bf8a8fc9d Mon Sep 17 00:00:00 2001 From: Matt Rubin Date: Sun, 14 Apr 2019 19:53:00 -0400 Subject: [PATCH 6/6] Move the Result shims out of the library and into the test target --- SwiftGit2.xcodeproj/project.pbxproj | 10 +++++----- {SwiftGit2 => SwiftGit2Tests}/ResultShims.swift | 3 ++- 2 files changed, 7 insertions(+), 6 deletions(-) rename {SwiftGit2 => SwiftGit2Tests}/ResultShims.swift (63%) diff --git a/SwiftGit2.xcodeproj/project.pbxproj b/SwiftGit2.xcodeproj/project.pbxproj index 188c4ff..58d6584 100644 --- a/SwiftGit2.xcodeproj/project.pbxproj +++ b/SwiftGit2.xcodeproj/project.pbxproj @@ -80,8 +80,8 @@ BECB5F6C1A56F1B400999413 /* ReferencesSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = BECB5F6B1A56F1B400999413 /* ReferencesSpec.swift */; }; BECB5F6E1A57284700999413 /* Remotes.swift in Sources */ = {isa = PBXBuildFile; fileRef = BECB5F6D1A57284700999413 /* Remotes.swift */; }; BECB5F701A57286200999413 /* RemotesSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = BECB5F6F1A57286200999413 /* RemotesSpec.swift */; }; - C98A2BA02263E017007A4E3A /* ResultShims.swift in Sources */ = {isa = PBXBuildFile; fileRef = C98A2B9F2263E017007A4E3A /* ResultShims.swift */; }; - C98A2BA12263E017007A4E3A /* ResultShims.swift in Sources */ = {isa = PBXBuildFile; fileRef = C98A2B9F2263E017007A4E3A /* ResultShims.swift */; }; + C98A2BA22263FDB9007A4E3A /* ResultShims.swift in Sources */ = {isa = PBXBuildFile; fileRef = C98A2B9F2263E017007A4E3A /* ResultShims.swift */; }; + C98A2BA32263FDBA007A4E3A /* ResultShims.swift in Sources */ = {isa = PBXBuildFile; fileRef = C98A2B9F2263E017007A4E3A /* ResultShims.swift */; }; DA5023A01A969F1A004175D7 /* Nimble.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = DA50239F1A969F1A004175D7 /* Nimble.framework */; }; DA5914761A94579000AED74C /* Errors.swift in Sources */ = {isa = PBXBuildFile; fileRef = DA5914751A94579000AED74C /* Errors.swift */; }; DAC8143D1A99749D0063D88C /* Quick.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = DAC8143C1A99749D0063D88C /* Quick.framework */; }; @@ -315,7 +315,6 @@ BECB5F691A56F19900999413 /* References.swift */, BECB5F6D1A57284700999413 /* Remotes.swift */, 25499A996CA7BD416620A397 /* CommitIterator.swift */, - C98A2B9F2263E017007A4E3A /* ResultShims.swift */, ); path = SwiftGit2; sourceTree = ""; @@ -339,6 +338,7 @@ BECB5F6B1A56F1B400999413 /* ReferencesSpec.swift */, BECB5F6F1A57286200999413 /* RemotesSpec.swift */, BE14AA581A1996B70015B439 /* FixturesSpec.swift */, + C98A2B9F2263E017007A4E3A /* ResultShims.swift */, BEB31F331A0D6F7A00F525B9 /* Supporting Files */, ); path = SwiftGit2Tests; @@ -753,7 +753,6 @@ 232861451F4A3A2E00276D65 /* Diffs.swift in Sources */, 621E66A51C72958800A0F352 /* References.swift in Sources */, 621E66A61C72958800A0F352 /* Libgit2.swift in Sources */, - C98A2BA12263E017007A4E3A /* ResultShims.swift in Sources */, 621E66A71C72958800A0F352 /* Pointers.swift in Sources */, 621E66A81C72958800A0F352 /* Errors.swift in Sources */, 621E66A91C72958800A0F352 /* SwiftGit2.m in Sources */, @@ -772,6 +771,7 @@ 621E66BE1C72958D00A0F352 /* Fixtures.swift in Sources */, 621E66BF1C72958D00A0F352 /* ReferencesSpec.swift in Sources */, 621E66C01C72958D00A0F352 /* OIDSpec.swift in Sources */, + C98A2BA32263FDBA007A4E3A /* ResultShims.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -788,7 +788,6 @@ 232861431F4A3A2E00276D65 /* Diffs.swift in Sources */, BECB5F6A1A56F19900999413 /* References.swift in Sources */, BE36354C1A632C9700D37EC8 /* Libgit2.swift in Sources */, - C98A2BA02263E017007A4E3A /* ResultShims.swift in Sources */, BE7A753F1A4A2BCC002DA7E3 /* Pointers.swift in Sources */, DA5914761A94579000AED74C /* Errors.swift in Sources */, BE14AA501A1974010015B439 /* SwiftGit2.m in Sources */, @@ -807,6 +806,7 @@ BE14AA551A1984550015B439 /* Fixtures.swift in Sources */, BECB5F6C1A56F1B400999413 /* ReferencesSpec.swift in Sources */, BE70B3E71A1ACB37002C3F4E /* OIDSpec.swift in Sources */, + C98A2BA22263FDB9007A4E3A /* ResultShims.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/SwiftGit2/ResultShims.swift b/SwiftGit2Tests/ResultShims.swift similarity index 63% rename from SwiftGit2/ResultShims.swift rename to SwiftGit2Tests/ResultShims.swift index 0c600e2..4095a7a 100644 --- a/SwiftGit2/ResultShims.swift +++ b/SwiftGit2Tests/ResultShims.swift @@ -1,4 +1,5 @@ -public extension Result { +// Once Nimble adds matchers for the Result type, remove these shims and refactor the tests that use them. +extension Result { var value: Success? { guard case .success(let value) = self else { return nil