-
Notifications
You must be signed in to change notification settings - Fork 179
Implement git status and diffs #102
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 40 commits
Commits
Show all changes
41 commits
Select commit
Hold shift + click to select a range
1af5941
stub out getting repository status
m3ta4a 82e90d3
stub out status testing
m3ta4a c4388f0
stub out getting the status of a commit compared with its parent, inc…
m3ta4a b582e1e
Add diff structs for parsing git_diff_deltas into Swift, more testing
m3ta4a 2173a68
these properties need to be made explicitly public
m3ta4a 6fc4a0c
improve tests, make clear some of the status/diff data is optional, c…
m3ta4a f422571
fix whitespace indentation issue
m3ta4a 95c7da4
remove 'git' from type names, convert types that should be an optionset
m3ta4a 253cf53
no need to have three different private functions doing nearly the sa…
m3ta4a edb1906
validate success of git_status_list_new
m3ta4a 99b6381
simpler function name for getting diffs
m3ta4a 4261987
simpler function name for getting status
m3ta4a bf2bc2e
also should check success of git_status_init_options
m3ta4a 48b8048
refactor how we initialize a DiffDelta using an initializer
m3ta4a 046d67e
use the DiffFlag struct instead of a UInt32
m3ta4a 8730bb2
refactor how we initialize Status, let the consumer of this library d…
m3ta4a 2cff52a
create an initializer for DiffFile
m3ta4a 0d56e1b
these evidently need to be explicitly public since they are internal …
m3ta4a 3b9beac
simplify things by extracting difficult to read libgit2 interactions …
m3ta4a 5f4043b
On second thought, I believe StatusEntry should not be a sub struct o…
m3ta4a d7154e6
init a StatusEntry with an initializer that takes in a git_status_entry
m3ta4a 8bb6e30
refactor the repository diff to rely on flatmaps to propogate errors
m3ta4a 6a59e65
split out diff tests into a separate describe, rename status describe
m3ta4a efe1d6e
update for swift 4.0 nuances
m3ta4a e7b6e45
for some reason the wrong commit of result got used
m3ta4a a9f79a3
address some PR comments
m3ta4a db0a955
clean up various xcode warnings
m3ta4a d8ee830
Merge branch 'master' into develop
m3ta4a f98664e
Merge branch 'master' into develop
m3ta4a ecb5263
fixes to tests that used guanaco
m3ta4a 26f1bb7
Fix for spec failing because only the diff for the first parent was c…
m3ta4a 9a9b7a9
More detailed tests
m3ta4a 87af7fc
Address lint in method calls
m3ta4a 1a369b7
Refactor method for calculating diffs to read more cleanly and be saf…
m3ta4a 0397b29
refactor diff calls to be memory safe
m3ta4a 0dab27d
Remove the necessity of the caller of diff to clean up memory and add…
m3ta4a 6f59ce5
Factor out common diff processing code for simplicity
m3ta4a 118ba43
Move deferred memory releases to avoid leaks in case of an error
m3ta4a 1f23feb
move one more defer
m3ta4a 6e5703d
another deferred release
m3ta4a 97ee009
Defer release status list
m3ta4a File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
// | ||
// Diffs.swift | ||
// SwiftGit2 | ||
// | ||
// Created by Jake Van Alstyne on 8/20/17. | ||
// Copyright © 2017 GitHub, Inc. All rights reserved. | ||
// | ||
import Foundation | ||
import libgit2 | ||
|
||
public struct StatusEntry { | ||
public var status: Diff.Status | ||
public var headToIndex: Diff.Delta? | ||
public var indexToWorkDir: Diff.Delta? | ||
|
||
public init(from statusEntry: git_status_entry) { | ||
self.status = Diff.Status(rawValue: statusEntry.status.rawValue) | ||
|
||
if let htoi = statusEntry.head_to_index { | ||
self.headToIndex = Diff.Delta(htoi.pointee) | ||
} | ||
|
||
if let itow = statusEntry.index_to_workdir { | ||
self.indexToWorkDir = Diff.Delta(itow.pointee) | ||
} | ||
} | ||
} | ||
|
||
public struct Diff { | ||
|
||
/// The set of deltas. | ||
public var deltas = [Delta]() | ||
|
||
public struct Delta { | ||
public static let type = GIT_OBJ_REF_DELTA | ||
|
||
public var status: Status | ||
public var flags: Flags | ||
public var oldFile: File? | ||
public var newFile: File? | ||
|
||
public init(_ delta: git_diff_delta) { | ||
self.status = Status(rawValue: UInt32(git_diff_status_char(delta.status))) | ||
self.flags = Flags(rawValue: delta.flags) | ||
self.oldFile = File(delta.old_file) | ||
self.newFile = File(delta.new_file) | ||
} | ||
} | ||
|
||
public struct File { | ||
public var oid: OID | ||
public var path: String | ||
public var size: Int64 | ||
public var flags: Flags | ||
|
||
public init(_ diffFile: git_diff_file) { | ||
self.oid = OID(diffFile.id) | ||
let path = diffFile.path | ||
self.path = path.map(String.init(cString:))! | ||
self.size = diffFile.size | ||
self.flags = Flags(rawValue: diffFile.flags) | ||
} | ||
} | ||
|
||
public struct Status: OptionSet { | ||
// This appears to be necessary due to bug in Swift | ||
// https://bugs.swift.org/browse/SR-3003 | ||
public init(rawValue: UInt32) { | ||
self.rawValue = rawValue | ||
} | ||
public let rawValue: UInt32 | ||
|
||
public static let current = Status(rawValue: 0) | ||
public static let indexNew = Status(rawValue: 1 << 0) | ||
public static let indexModified = Status(rawValue: 1 << 1) | ||
public static let indexDeleted = Status(rawValue: 1 << 2) | ||
public static let indexRenamed = Status(rawValue: 1 << 3) | ||
public static let indexTypeChange = Status(rawValue: 1 << 4) | ||
public static let workTreeNew = Status(rawValue: 1 << 5) | ||
public static let workTreeModified = Status(rawValue: 1 << 6) | ||
public static let workTreeDeleted = Status(rawValue: 1 << 7) | ||
public static let workTreeTypeChange = Status(rawValue: 1 << 8) | ||
public static let workTreeRenamed = Status(rawValue: 1 << 9) | ||
public static let workTreeUnreadable = Status(rawValue: 1 << 10) | ||
public static let ignored = Status(rawValue: 1 << 11) | ||
public static let conflicted = Status(rawValue: 1 << 12) | ||
} | ||
|
||
public struct Flags: OptionSet { | ||
// This appears to be necessary due to bug in Swift | ||
// https://bugs.swift.org/browse/SR-3003 | ||
public init(rawValue: UInt32) { | ||
self.rawValue = rawValue | ||
} | ||
public let rawValue: UInt32 | ||
|
||
public static let binary = Flags(rawValue: 0) | ||
public static let notBinary = Flags(rawValue: 1 << 0) | ||
public static let validId = Flags(rawValue: 1 << 1) | ||
public static let exists = Flags(rawValue: 1 << 2) | ||
} | ||
|
||
/// Create an instance with a libgit2 `git_diff`. | ||
public init(_ pointer: OpaquePointer) { | ||
for i in 0..<git_diff_num_deltas(pointer) { | ||
if let delta = git_diff_get_delta(pointer, i) { | ||
deltas.append(Diff.Delta(delta.pointee)) | ||
} | ||
} | ||
} | ||
} | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you think of adding a
Diff
type that holds the deltas and then nesting these types inside of it?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The only caveat with this is that to create an array of a nested struct defined that way, you apparently need to typealias it like so
typealias Delta = Diff.Delta; var returnDict = [Delta]()
, but otherwise i like the organization it provides