address some PR comments

This commit is contained in:
Jake Van Alstyne 🎩 2017-11-17 16:22:37 -07:00
parent e7b6e4509f
commit a9f79a3b80
2 changed files with 31 additions and 43 deletions

View File

@ -16,11 +16,11 @@ public struct StatusEntry {
self.status = Diff.Status(rawValue: statusEntry.status.rawValue) self.status = Diff.Status(rawValue: statusEntry.status.rawValue)
if let htoi = statusEntry.head_to_index { if let htoi = statusEntry.head_to_index {
self.headToIndex = Diff.Delta(from: htoi.pointee) self.headToIndex = Diff.Delta(_: htoi.pointee)
} }
if let itow = statusEntry.index_to_workdir { if let itow = statusEntry.index_to_workdir {
self.indexToWorkDir = Diff.Delta(from: itow.pointee) self.indexToWorkDir = Diff.Delta(_: itow.pointee)
} }
} }
} }
@ -32,7 +32,7 @@ public struct Diff {
public var size: Int64 public var size: Int64
public var flags: Flags public var flags: Flags
public init(from diffFile: git_diff_file) { public init(_ diffFile: git_diff_file) {
self.oid = OID(diffFile.id) self.oid = OID(diffFile.id)
let path = diffFile.path let path = diffFile.path
self.path = path.map(String.init(cString:))! self.path = path.map(String.init(cString:))!
@ -85,11 +85,11 @@ public struct Diff {
public var oldFile: File? public var oldFile: File?
public var newFile: File? public var newFile: File?
public init(from delta: git_diff_delta) { public init(_ delta: git_diff_delta) {
self.status = Status(rawValue: delta.status.rawValue) self.status = Status(rawValue: delta.status.rawValue)
self.flags = Flags(rawValue: delta.flags) self.flags = Flags(rawValue: delta.flags)
self.oldFile = File(from: delta.old_file) self.oldFile = File(_: delta.old_file)
self.newFile = File(from: delta.new_file) self.newFile = File(_: delta.new_file)
} }
} }
} }

View File

@ -549,23 +549,25 @@ final public class Repository {
.flatMap { baseTree in .flatMap { baseTree in
guard !commit.parents.isEmpty else { guard !commit.parents.isEmpty else {
// Initial commit in a repository // Initial commit in a repository
return self.diff(withOldTree: nil, andNewTree: baseTree) return self.diff(fromTree: nil, toTree: baseTree)
} }
var mergeDiff: Result<OpaquePointer?, NSError> = .success(nil) let mergeDiff: Result<OpaquePointer?, NSError> = .success(nil)
for parent in commit.parents { for parent in commit.parents {
mergeDiff = mergeDiff let diff = self
.flatMap { mergeDiff in .commit(with: parent.oid)
return self
.parentCommit(from: parent)
.flatMap { commit in .flatMap { commit in
return self.tree(from: commit) return self.tree(from: commit)
} }
.flatMap { tree in .flatMap { tree in
return self.diff(withOldTree: tree, andNewTree: baseTree) return self.diff(fromTree: tree, toTree: baseTree)
}
return mergeDiff
.fanout(diff)
.flatMap { (mergeDiff, diff) in
guard let mergeDiff = mergeDiff else {
return .success(diff)
} }
.flatMap { diff in
guard let mergeDiff = mergeDiff else { return .success(diff) }
let mergeResult = git_diff_merge(mergeDiff, diff) let mergeResult = git_diff_merge(mergeDiff, diff)
guard mergeResult == GIT_OK.rawValue else { guard mergeResult == GIT_OK.rawValue else {
return .failure(NSError(gitError: mergeResult, pointOfFailure: "git_diff_merge")) return .failure(NSError(gitError: mergeResult, pointOfFailure: "git_diff_merge"))
@ -573,7 +575,6 @@ final public class Repository {
return .success(mergeDiff) return .success(mergeDiff)
} }
} }
}
return .success(mergeDiff.value!!) return .success(mergeDiff.value!!)
} }
.flatMap { diffResult in .flatMap { diffResult in
@ -594,8 +595,8 @@ final public class Repository {
return Result.success(unwrapBaseCommit) return Result.success(unwrapBaseCommit)
} }
private func diff(withOldTree oldTree: OpaquePointer?, private func diff(fromTree oldTree: OpaquePointer?,
andNewTree newTree: OpaquePointer?) -> Result<OpaquePointer, NSError> { toTree newTree: OpaquePointer?) -> Result<OpaquePointer, NSError> {
var unsafeDiff: OpaquePointer? = nil var unsafeDiff: OpaquePointer? = nil
let diffResult = git_diff_tree_to_tree(&unsafeDiff, self.pointer, oldTree, newTree, nil) let diffResult = git_diff_tree_to_tree(&unsafeDiff, self.pointer, oldTree, newTree, nil)
guard diffResult == GIT_OK.rawValue, let unwrapDiffResult = unsafeDiff else { guard diffResult == GIT_OK.rawValue, let unwrapDiffResult = unsafeDiff else {
@ -605,19 +606,6 @@ final public class Repository {
return Result.success(unwrapDiffResult) return Result.success(unwrapDiffResult)
} }
private func parentCommit(from parent: PointerTo<Commit>) -> Result<OpaquePointer, NSError> {
var unsafeParentCommit: OpaquePointer? = nil
let unsafeParentOid = UnsafeMutablePointer<git_oid>.allocate(capacity: 1)
git_oid_fromstr(unsafeParentOid, parent.oid.description)
let lookupParentGitResult = git_commit_lookup(&unsafeParentCommit, self.pointer, unsafeParentOid)
guard lookupParentGitResult == GIT_OK.rawValue, let unwrapParentCommit = unsafeParentCommit else {
return Result.failure(NSError(gitError: lookupParentGitResult, pointOfFailure: "git_commit_lookup"))
}
git_commit_free(unsafeParentCommit)
return Result.success(unwrapParentCommit)
}
private func tree(from commit: OpaquePointer) -> Result<OpaquePointer, NSError> { private func tree(from commit: OpaquePointer) -> Result<OpaquePointer, NSError> {
var unsafeTree: OpaquePointer? = nil var unsafeTree: OpaquePointer? = nil
let treeResult = git_commit_tree(&unsafeTree, commit) let treeResult = git_commit_tree(&unsafeTree, commit)
@ -637,7 +625,7 @@ final public class Repository {
for i in 0..<count { for i in 0..<count {
let delta = git_diff_get_delta(diffResult, i) let delta = git_diff_get_delta(diffResult, i)
let gitDiffDelta = Diff.Delta(from: (delta?.pointee)!) let gitDiffDelta = Diff.Delta(_: (delta?.pointee)!)
returnDict.append(gitDiffDelta) returnDict.append(gitDiffDelta)