From 8730bb2d8d009547bf59aba2331e6182ec1db76b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jake=20Van=20Alstyne=20=F0=9F=8E=A9?= Date: Fri, 1 Sep 2017 17:22:20 -0600 Subject: [PATCH] refactor how we initialize Status, let the consumer of this library decide what the different statuses returned by libgit2 mean rather than imposing our own assumptions --- SwiftGit2/Diffs.swift | 46 +------------------------------------- SwiftGit2/Repository.swift | 11 +++++---- 2 files changed, 6 insertions(+), 51 deletions(-) diff --git a/SwiftGit2/Diffs.swift b/SwiftGit2/Diffs.swift index d5e46d0..fad5cd0 100644 --- a/SwiftGit2/Diffs.swift +++ b/SwiftGit2/Diffs.swift @@ -65,56 +65,12 @@ public struct DiffDelta { public var newFile: DiffFile? public init(from diffDelta: git_diff_delta) { - let emptyOid = OID(string: "0000000000000000000000000000000000000000") - let oldOid = OID(diffDelta.old_file.id) - let newOid = OID(diffDelta.old_file.id) - - // Because of the way git diffs work, new or deleted files can have confusing statuses - // We're simplifying that here by checking directly - if newOid == emptyOid { - self.status = Status.indexDeleted - } else if oldOid == emptyOid { - self.status = Status.indexNew - } else { - self.status = DiffDelta.convertStatus(diffDelta.status.rawValue) - } + self.status = Status(rawValue: diffDelta.status.rawValue) self.flags = DiffFlag(rawValue: diffDelta.flags) self.oldFile = self.convertDiffFile(diffDelta.old_file) self.newFile = self.convertDiffFile(diffDelta.new_file) } - static func convertStatus(_ statusValue: UInt32) -> Status { - var status: Status? = nil - - // Index status - if (statusValue & GIT_STATUS_INDEX_NEW.rawValue) == GIT_STATUS_INDEX_NEW.rawValue { - status = Status.indexNew - } else if (statusValue & GIT_STATUS_INDEX_MODIFIED.rawValue) == GIT_STATUS_INDEX_MODIFIED.rawValue { - status = Status.indexModified - } else if (statusValue & GIT_STATUS_INDEX_DELETED.rawValue) == GIT_STATUS_INDEX_DELETED.rawValue { - status = Status.indexDeleted - } else if (statusValue & GIT_STATUS_INDEX_RENAMED.rawValue) == GIT_STATUS_INDEX_RENAMED.rawValue { - status = Status.indexRenamed - } else if (statusValue & GIT_STATUS_INDEX_TYPECHANGE.rawValue) == GIT_STATUS_INDEX_TYPECHANGE.rawValue { - status = Status.indexTypeChange - } - - // Worktree status - if (statusValue & GIT_STATUS_WT_NEW.rawValue) == GIT_STATUS_WT_NEW.rawValue { - status = Status(rawValue: status!.rawValue & Status.workTreeNew.rawValue) - } else if (statusValue & GIT_STATUS_WT_MODIFIED.rawValue) == GIT_STATUS_WT_MODIFIED.rawValue { - status = Status(rawValue: status!.rawValue & Status.workTreeModified.rawValue) - } else if (statusValue & GIT_STATUS_WT_DELETED.rawValue) == GIT_STATUS_WT_DELETED.rawValue { - status = Status(rawValue: status!.rawValue & Status.workTreeDeleted.rawValue) - } else if (statusValue & GIT_STATUS_WT_RENAMED.rawValue) == GIT_STATUS_WT_RENAMED.rawValue { - status = Status(rawValue: status!.rawValue & Status.workTreeRenamed.rawValue) - } else if (statusValue & GIT_STATUS_WT_TYPECHANGE.rawValue) == GIT_STATUS_WT_TYPECHANGE.rawValue { - status = Status(rawValue: status!.rawValue & Status.workTreeTypeChange.rawValue) - } - - return status! - } - private func convertDiffFile(_ file: git_diff_file) -> DiffFile { let path = file.path let newFile = DiffFile(oid: OID(file.id), diff --git a/SwiftGit2/Repository.swift b/SwiftGit2/Repository.swift index 7096c87..cef84e2 100644 --- a/SwiftGit2/Repository.swift +++ b/SwiftGit2/Repository.swift @@ -657,17 +657,16 @@ final public class Repository { var headToIndex: DiffDelta? = nil var indexToWorkDir: DiffDelta? = nil - // Delta status if let statusValue = s?.pointee.status.rawValue { - status = DiffDelta.convertStatus(statusValue) + status = Status(rawValue: statusValue) } - if s?.pointee.head_to_index != nil { - headToIndex = DiffDelta(from: (s?.pointee.head_to_index.pointee)!) + if let htoi = s?.pointee.head_to_index { + headToIndex = DiffDelta(from: htoi.pointee) } - if s?.pointee.index_to_workdir != nil { - indexToWorkDir = DiffDelta(from: (s?.pointee.index_to_workdir.pointee)!) + if let itow = s?.pointee.index_to_workdir { + indexToWorkDir = DiffDelta(from: itow.pointee) } let statusEntry = StatusEntry(status: status, headToIndex: headToIndex, indexToWorkDir: indexToWorkDir)