Skip to content

Commit 3adf7e4

Browse files
authored
Merge pull request SwiftGit2#151 from mattrubin/result
Switch to the standard library Result type
2 parents cb5567d + 80f4e60 commit 3adf7e4

18 files changed

+78
-75
lines changed

.gitmodules

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@
1010
[submodule "Carthage/Checkouts/xcconfigs"]
1111
path = Carthage/Checkouts/xcconfigs
1212
url = https://github.com/jspahrsummers/xcconfigs.git
13-
[submodule "Carthage/Checkouts/Result"]
14-
path = Carthage/Checkouts/Result
15-
url = https://github.com/antitypical/Result.git
1613
[submodule "Carthage/Checkouts/ZipArchive"]
1714
path = Carthage/Checkouts/ZipArchive
1815
url = https://github.com/ZipArchive/ZipArchive.git

Cartfile

Lines changed: 0 additions & 1 deletion
This file was deleted.

Cartfile.resolved

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
github "Quick/Nimble" "v8.0.1"
22
github "Quick/Quick" "v2.0.0"
33
github "ZipArchive/ZipArchive" "v2.1.5"
4-
github "antitypical/Result" "4.1.0"
54
github "jspahrsummers/xcconfigs" "1.0"

Carthage/Checkouts/Result

Lines changed: 0 additions & 1 deletion
This file was deleted.

SwiftGit2.xcodeproj/project.pbxproj

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@
5050
621E66C81C72958D00A0F352 /* detached-head.zip in Resources */ = {isa = PBXBuildFile; fileRef = BE0B1C5C1A9978890004726D /* detached-head.zip */; };
5151
621E66C91C72958D00A0F352 /* Mantle.zip in Resources */ = {isa = PBXBuildFile; fileRef = BE0991F61A578FB1007D4E6A /* Mantle.zip */; };
5252
621E66CA1C72958D00A0F352 /* simple-repository.zip in Resources */ = {isa = PBXBuildFile; fileRef = BE14AA561A198C6E0015B439 /* simple-repository.zip */; };
53-
621E66D91C72989A00A0F352 /* Result.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 621E66D81C72989900A0F352 /* Result.framework */; };
5453
621E66E61C729D9600A0F352 /* SwiftGit2.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 621E66B41C72958800A0F352 /* SwiftGit2.framework */; };
5554
621E66FE1C72A5FF00A0F352 /* libiconv.tbd in Frameworks */ = {isa = PBXBuildFile; fileRef = 621E66FD1C72A5FF00A0F352 /* libiconv.tbd */; };
5655
621E67001C72A60B00A0F352 /* libz.tbd in Frameworks */ = {isa = PBXBuildFile; fileRef = 621E66FF1C72A60B00A0F352 /* libz.tbd */; };
@@ -81,7 +80,8 @@
8180
BECB5F6C1A56F1B400999413 /* ReferencesSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = BECB5F6B1A56F1B400999413 /* ReferencesSpec.swift */; };
8281
BECB5F6E1A57284700999413 /* Remotes.swift in Sources */ = {isa = PBXBuildFile; fileRef = BECB5F6D1A57284700999413 /* Remotes.swift */; };
8382
BECB5F701A57286200999413 /* RemotesSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = BECB5F6F1A57286200999413 /* RemotesSpec.swift */; };
84-
BEE591C71ADF470500534F14 /* Result.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = BEE591C61ADF470500534F14 /* Result.framework */; };
83+
C98A2BA22263FDB9007A4E3A /* ResultShims.swift in Sources */ = {isa = PBXBuildFile; fileRef = C98A2B9F2263E017007A4E3A /* ResultShims.swift */; };
84+
C98A2BA32263FDBA007A4E3A /* ResultShims.swift in Sources */ = {isa = PBXBuildFile; fileRef = C98A2B9F2263E017007A4E3A /* ResultShims.swift */; };
8585
DA5023A01A969F1A004175D7 /* Nimble.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = DA50239F1A969F1A004175D7 /* Nimble.framework */; };
8686
DA5914761A94579000AED74C /* Errors.swift in Sources */ = {isa = PBXBuildFile; fileRef = DA5914751A94579000AED74C /* Errors.swift */; };
8787
DAC8143D1A99749D0063D88C /* Quick.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = DAC8143C1A99749D0063D88C /* Quick.framework */; };
@@ -138,7 +138,6 @@
138138
25499A996CA7BD416620A397 /* CommitIterator.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CommitIterator.swift; sourceTree = "<group>"; };
139139
621E66B41C72958800A0F352 /* SwiftGit2.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = SwiftGit2.framework; sourceTree = BUILT_PRODUCTS_DIR; };
140140
621E66CE1C72958D00A0F352 /* SwiftGit2-iOSTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = "SwiftGit2-iOSTests.xctest"; sourceTree = BUILT_PRODUCTS_DIR; };
141-
621E66D81C72989900A0F352 /* Result.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Result.framework; path = "Carthage/Checkouts/Result/build/Debug-iphoneos/Result.framework"; sourceTree = "<group>"; };
142141
621E66E11C729CE500A0F352 /* libgit2.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libgit2.a; sourceTree = BUILT_PRODUCTS_DIR; };
143142
621E66EC1C729EB800A0F352 /* libssl.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libssl.a; sourceTree = BUILT_PRODUCTS_DIR; };
144143
621E66F21C729EBB00A0F352 /* libssh2.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libssh2.a; sourceTree = BUILT_PRODUCTS_DIR; };
@@ -191,7 +190,7 @@
191190
BECB5F6B1A56F1B400999413 /* ReferencesSpec.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ReferencesSpec.swift; sourceTree = "<group>"; };
192191
BECB5F6D1A57284700999413 /* Remotes.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Remotes.swift; sourceTree = "<group>"; };
193192
BECB5F6F1A57286200999413 /* RemotesSpec.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RemotesSpec.swift; sourceTree = "<group>"; };
194-
BEE591C61ADF470500534F14 /* Result.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Result.framework; path = "../../Library/Developer/Xcode/DerivedData/SwiftGit2-cdzquzvlxdewyddxgdgxjagqvjba/Build/Products/Debug/Result.framework"; sourceTree = "<group>"; };
193+
C98A2B9F2263E017007A4E3A /* ResultShims.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ResultShims.swift; sourceTree = "<group>"; };
195194
DA50239F1A969F1A004175D7 /* Nimble.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; path = Nimble.framework; sourceTree = BUILT_PRODUCTS_DIR; };
196195
DA5914751A94579000AED74C /* Errors.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Errors.swift; sourceTree = "<group>"; };
197196
DAC8143C1A99749D0063D88C /* Quick.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Quick.framework; path = "../../../../Library/Developer/Xcode/DerivedData/SwiftGit2-ezqqkevntxbroughwcioyxqriijk/Build/Products/Debug/Quick.framework"; sourceTree = "<group>"; };
@@ -204,7 +203,6 @@
204203
files = (
205204
621E67001C72A60B00A0F352 /* libz.tbd in Frameworks */,
206205
621E66FE1C72A5FF00A0F352 /* libiconv.tbd in Frameworks */,
207-
621E66D91C72989A00A0F352 /* Result.framework in Frameworks */,
208206
);
209207
runOnlyForDeploymentPostprocessing = 0;
210208
};
@@ -223,7 +221,6 @@
223221
isa = PBXFrameworksBuildPhase;
224222
buildActionMask = 2147483647;
225223
files = (
226-
BEE591C71ADF470500534F14 /* Result.framework in Frameworks */,
227224
BEB31FA01A0E595600F525B9 /* libz.dylib in Frameworks */,
228225
BEB31F9E1A0E595100F525B9 /* libiconv.dylib in Frameworks */,
229226
);
@@ -246,7 +243,6 @@
246243
621E66D41C72965C00A0F352 /* Mac */ = {
247244
isa = PBXGroup;
248245
children = (
249-
BEE591C61ADF470500534F14 /* Result.framework */,
250246
BEB31F9D1A0E595100F525B9 /* libiconv.dylib */,
251247
BEB31F9F1A0E595600F525B9 /* libz.dylib */,
252248
);
@@ -256,7 +252,6 @@
256252
621E66D51C72966000A0F352 /* iOS */ = {
257253
isa = PBXGroup;
258254
children = (
259-
621E66D81C72989900A0F352 /* Result.framework */,
260255
621E66FD1C72A5FF00A0F352 /* libiconv.tbd */,
261256
621E66FF1C72A60B00A0F352 /* libz.tbd */,
262257
);
@@ -343,6 +338,7 @@
343338
BECB5F6B1A56F1B400999413 /* ReferencesSpec.swift */,
344339
BECB5F6F1A57286200999413 /* RemotesSpec.swift */,
345340
BE14AA581A1996B70015B439 /* FixturesSpec.swift */,
341+
C98A2B9F2263E017007A4E3A /* ResultShims.swift */,
346342
BEB31F331A0D6F7A00F525B9 /* Supporting Files */,
347343
);
348344
path = SwiftGit2Tests;
@@ -775,6 +771,7 @@
775771
621E66BE1C72958D00A0F352 /* Fixtures.swift in Sources */,
776772
621E66BF1C72958D00A0F352 /* ReferencesSpec.swift in Sources */,
777773
621E66C01C72958D00A0F352 /* OIDSpec.swift in Sources */,
774+
C98A2BA32263FDBA007A4E3A /* ResultShims.swift in Sources */,
778775
);
779776
runOnlyForDeploymentPostprocessing = 0;
780777
};
@@ -809,6 +806,7 @@
809806
BE14AA551A1984550015B439 /* Fixtures.swift in Sources */,
810807
BECB5F6C1A56F1B400999413 /* ReferencesSpec.swift in Sources */,
811808
BE70B3E71A1ACB37002C3F4E /* OIDSpec.swift in Sources */,
809+
C98A2BA22263FDB9007A4E3A /* ResultShims.swift in Sources */,
812810
);
813811
runOnlyForDeploymentPostprocessing = 0;
814812
};

SwiftGit2.xcworkspace/contents.xcworkspacedata

Lines changed: 0 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

SwiftGit2/CommitIterator.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// Copyright (c) 2017 GitHub, Inc. All rights reserved.
44
//
55

6-
import Result
6+
import Foundation
77
import libgit2
88

99
public class CommitIterator: IteratorProtocol, Sequence {

SwiftGit2/Diffs.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// Created by Jake Van Alstyne on 8/20/17.
66
// Copyright © 2017 GitHub, Inc. All rights reserved.
77
//
8-
import Foundation
8+
99
import libgit2
1010

1111
public struct StatusEntry {

SwiftGit2/OID.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
// Copyright (c) 2014 GitHub, Inc. All rights reserved.
77
//
88

9-
import Foundation
109
import libgit2
1110

1211
/// An identifier for a Git object.

SwiftGit2/Objects.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
import Foundation
1010
import libgit2
11-
import Result
1211

1312
/// A git object.
1413
public protocol ObjectType {

SwiftGit2/Repository.swift

Lines changed: 54 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
//
88

99
import Foundation
10-
import Result
1110
import libgit2
1211

1312
public typealias CheckoutProgressBlock = (String?, Int, Int) -> Void
@@ -362,11 +361,7 @@ public final class Repository {
362361
git_strarray_free(pointer)
363362
pointer.deallocate()
364363

365-
let error = remotes.reduce(nil) { $0 == nil ? $0 : $1.error }
366-
if let error = error {
367-
return Result.failure(error)
368-
}
369-
return Result.success(remotes.map { $0.value! })
364+
return remotes.aggregateResult()
370365
}
371366

372367
private func remoteLookup<A>(named name: String, _ callback: (Result<OpaquePointer, NSError>) -> A) -> A {
@@ -432,11 +427,7 @@ public final class Repository {
432427
git_strarray_free(pointer)
433428
pointer.deallocate()
434429

435-
let error = references.reduce(nil) { $0 == nil ? $0 : $1.error }
436-
if let error = error {
437-
return Result.failure(error)
438-
}
439-
return Result.success(references.map { $0.value! })
430+
return references.aggregateResult()
440431
}
441432

442433
/// Load the reference with the given long name (e.g. "refs/heads/master")
@@ -713,20 +704,22 @@ public final class Repository {
713704
var mergeDiff: OpaquePointer? = nil
714705
defer { git_object_free(mergeDiff) }
715706
for parent in commit.parents {
716-
let error = self.diff(from: parent.oid, to: commit.oid) { (diff: Result<OpaquePointer, NSError>) -> NSError? in
717-
guard diff.error == nil else {
718-
return diff.error!
719-
}
720-
721-
if mergeDiff == nil {
722-
mergeDiff = diff.value!
723-
} else {
724-
let mergeResult = git_diff_merge(mergeDiff, diff.value)
725-
guard mergeResult == GIT_OK.rawValue else {
726-
return NSError(gitError: mergeResult, pointOfFailure: "git_diff_merge")
707+
let error = self.diff(from: parent.oid, to: commit.oid) {
708+
switch $0 {
709+
case .failure(let error):
710+
return error
711+
712+
case .success(let newDiff):
713+
if mergeDiff == nil {
714+
mergeDiff = newDiff
715+
} else {
716+
let mergeResult = git_diff_merge(mergeDiff, newDiff)
717+
guard mergeResult == GIT_OK.rawValue else {
718+
return NSError(gitError: mergeResult, pointOfFailure: "git_diff_merge")
719+
}
727720
}
721+
return nil
728722
}
729-
return nil
730723
}
731724

732725
if error != nil {
@@ -743,23 +736,23 @@ public final class Repository {
743736
var oldTree: OpaquePointer? = nil
744737
defer { git_object_free(oldTree) }
745738
if let oid = oldCommitOid {
746-
let result = unsafeTreeForCommitId(oid)
747-
guard result.error == nil else {
748-
return transform(Result.failure(result.error!))
739+
switch unsafeTreeForCommitId(oid) {
740+
case .failure(let error):
741+
return transform(.failure(error))
742+
case .success(let value):
743+
oldTree = value
749744
}
750-
751-
oldTree = result.value
752745
}
753746

754747
var newTree: OpaquePointer? = nil
755748
defer { git_object_free(newTree) }
756749
if let oid = newCommitOid {
757-
let result = unsafeTreeForCommitId(oid)
758-
guard result.error == nil else {
759-
return transform(Result.failure(result.error!))
750+
switch unsafeTreeForCommitId(oid) {
751+
case .failure(let error):
752+
return transform(.failure(error))
753+
case .success(let value):
754+
newTree = value
760755
}
761-
762-
newTree = result.value
763756
}
764757

765758
var diff: OpaquePointer? = nil
@@ -782,21 +775,23 @@ public final class Repository {
782775
assert(oldCommitOid != nil || newCommitOid != nil, "It is an error to pass nil for both the oldOid and newOid")
783776

784777
var oldTree: Tree? = nil
785-
if oldCommitOid != nil {
786-
let result = safeTreeForCommitId(oldCommitOid!)
787-
guard result.error == nil else {
788-
return Result<Diff, NSError>.failure(result.error!)
778+
if let oldCommitOid = oldCommitOid {
779+
switch safeTreeForCommitId(oldCommitOid) {
780+
case .failure(let error):
781+
return .failure(error)
782+
case .success(let value):
783+
oldTree = value
789784
}
790-
oldTree = result.value
791785
}
792786

793787
var newTree: Tree? = nil
794-
if newCommitOid != nil {
795-
let result = self.safeTreeForCommitId(newCommitOid!)
796-
guard result.error == nil else {
797-
return Result<Diff, NSError>.failure(result.error!)
788+
if let newCommitOid = newCommitOid {
789+
switch safeTreeForCommitId(newCommitOid) {
790+
case .failure(let error):
791+
return .failure(error)
792+
case .success(let value):
793+
newTree = value
798794
}
799-
newTree = result.value!
800795
}
801796

802797
if oldTree != nil && newTree != nil {
@@ -864,11 +859,7 @@ public final class Repository {
864859
private func safeTreeForCommitId(_ oid: OID) -> Result<Tree, NSError> {
865860
return withGitObject(oid, type: GIT_OBJ_COMMIT) { commit in
866861
let treeId = git_commit_tree_id(commit)
867-
let tree = self.tree(OID(treeId!.pointee))
868-
guard tree.error == nil else {
869-
return .failure(tree.error!)
870-
}
871-
return tree
862+
return tree(OID(treeId!.pointee))
872863
}
873864
}
874865

@@ -952,3 +943,18 @@ public final class Repository {
952943
}
953944
}
954945
}
946+
947+
private extension Array {
948+
func aggregateResult<Value, Error>() -> Result<[Value], Error> where Element == Result<Value, Error> {
949+
var values: [Value] = []
950+
for result in self {
951+
switch result {
952+
case .success(let value):
953+
values.append(value)
954+
case .failure(let error):
955+
return .failure(error)
956+
}
957+
}
958+
return .success(values)
959+
}
960+
}

SwiftGit2Tests/Fixtures/Fixtures.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
// Copyright (c) 2014 GitHub, Inc. All rights reserved.
77
//
88

9-
import Foundation
109
import SwiftGit2
1110
import ZipArchive
1211

SwiftGit2Tests/OIDSpec.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
// Copyright (c) 2014 GitHub, Inc. All rights reserved.
77
//
88

9-
import Result
109
import SwiftGit2
1110
import Nimble
1211
import Quick

SwiftGit2Tests/ObjectsSpec.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
// Copyright (c) 2014 GitHub, Inc. All rights reserved.
77
//
88

9-
import Result
109
import SwiftGit2
1110
import Nimble
1211
import Quick

SwiftGit2Tests/ReferencesSpec.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
// Copyright (c) 2015 GitHub, Inc. All rights reserved.
77
//
88

9-
import Result
109
import SwiftGit2
1110
import Nimble
1211
import Quick

SwiftGit2Tests/RemotesSpec.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
// Copyright (c) 2015 GitHub, Inc. All rights reserved.
77
//
88

9-
import Result
109
import SwiftGit2
1110
import Nimble
1211
import Quick

SwiftGit2Tests/RepositorySpec.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
// Copyright (c) 2014 GitHub, Inc. All rights reserved.
77
//
88

9-
import Result
109
import SwiftGit2
1110
import Nimble
1211
import Quick

SwiftGit2Tests/ResultShims.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Once Nimble adds matchers for the Result type, remove these shims and refactor the tests that use them.
2+
extension Result {
3+
var value: Success? {
4+
guard case .success(let value) = self else {
5+
return nil
6+
}
7+
return value
8+
}
9+
10+
var error: Failure? {
11+
guard case .failure(let error) = self else {
12+
return nil
13+
}
14+
return error
15+
}
16+
}

0 commit comments

Comments
 (0)