mirror of
https://github.com/gosticks/SwiftGit2.git
synced 2025-10-16 11:55:34 +00:00
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
This commit is contained in:
parent
046d67ed97
commit
8730bb2d8d
@ -65,56 +65,12 @@ public struct DiffDelta {
|
|||||||
public var newFile: DiffFile?
|
public var newFile: DiffFile?
|
||||||
|
|
||||||
public init(from diffDelta: git_diff_delta) {
|
public init(from diffDelta: git_diff_delta) {
|
||||||
let emptyOid = OID(string: "0000000000000000000000000000000000000000")
|
self.status = Status(rawValue: diffDelta.status.rawValue)
|
||||||
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.flags = DiffFlag(rawValue: diffDelta.flags)
|
self.flags = DiffFlag(rawValue: diffDelta.flags)
|
||||||
self.oldFile = self.convertDiffFile(diffDelta.old_file)
|
self.oldFile = self.convertDiffFile(diffDelta.old_file)
|
||||||
self.newFile = self.convertDiffFile(diffDelta.new_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 {
|
private func convertDiffFile(_ file: git_diff_file) -> DiffFile {
|
||||||
let path = file.path
|
let path = file.path
|
||||||
let newFile = DiffFile(oid: OID(file.id),
|
let newFile = DiffFile(oid: OID(file.id),
|
||||||
|
|||||||
@ -657,17 +657,16 @@ final public class Repository {
|
|||||||
var headToIndex: DiffDelta? = nil
|
var headToIndex: DiffDelta? = nil
|
||||||
var indexToWorkDir: DiffDelta? = nil
|
var indexToWorkDir: DiffDelta? = nil
|
||||||
|
|
||||||
// Delta status
|
|
||||||
if let statusValue = s?.pointee.status.rawValue {
|
if let statusValue = s?.pointee.status.rawValue {
|
||||||
status = DiffDelta.convertStatus(statusValue)
|
status = Status(rawValue: statusValue)
|
||||||
}
|
}
|
||||||
|
|
||||||
if s?.pointee.head_to_index != nil {
|
if let htoi = s?.pointee.head_to_index {
|
||||||
headToIndex = DiffDelta(from: (s?.pointee.head_to_index.pointee)!)
|
headToIndex = DiffDelta(from: htoi.pointee)
|
||||||
}
|
}
|
||||||
|
|
||||||
if s?.pointee.index_to_workdir != nil {
|
if let itow = s?.pointee.index_to_workdir {
|
||||||
indexToWorkDir = DiffDelta(from: (s?.pointee.index_to_workdir.pointee)!)
|
indexToWorkDir = DiffDelta(from: itow.pointee)
|
||||||
}
|
}
|
||||||
|
|
||||||
let statusEntry = StatusEntry(status: status, headToIndex: headToIndex, indexToWorkDir: indexToWorkDir)
|
let statusEntry = StatusEntry(status: status, headToIndex: headToIndex, indexToWorkDir: indexToWorkDir)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user