Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
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 Aug 17, 2017
82e90d3
stub out status testing
m3ta4a Aug 17, 2017
c4388f0
stub out getting the status of a commit compared with its parent, inc…
m3ta4a Aug 18, 2017
b582e1e
Add diff structs for parsing git_diff_deltas into Swift, more testing
m3ta4a Aug 20, 2017
2173a68
these properties need to be made explicitly public
m3ta4a Aug 20, 2017
6fc4a0c
improve tests, make clear some of the status/diff data is optional, c…
m3ta4a Aug 22, 2017
f422571
fix whitespace indentation issue
m3ta4a Aug 22, 2017
95c7da4
remove 'git' from type names, convert types that should be an optionset
m3ta4a Sep 1, 2017
253cf53
no need to have three different private functions doing nearly the sa…
m3ta4a Sep 1, 2017
edb1906
validate success of git_status_list_new
m3ta4a Sep 1, 2017
99b6381
simpler function name for getting diffs
m3ta4a Sep 1, 2017
4261987
simpler function name for getting status
m3ta4a Sep 1, 2017
bf2bc2e
also should check success of git_status_init_options
m3ta4a Sep 1, 2017
48b8048
refactor how we initialize a DiffDelta using an initializer
m3ta4a Sep 1, 2017
046d67e
use the DiffFlag struct instead of a UInt32
m3ta4a Sep 1, 2017
8730bb2
refactor how we initialize Status, let the consumer of this library d…
m3ta4a Sep 1, 2017
2cff52a
create an initializer for DiffFile
m3ta4a Sep 1, 2017
0d56e1b
these evidently need to be explicitly public since they are internal …
m3ta4a Sep 1, 2017
3b9beac
simplify things by extracting difficult to read libgit2 interactions …
m3ta4a Sep 8, 2017
5f4043b
On second thought, I believe StatusEntry should not be a sub struct o…
m3ta4a Sep 8, 2017
d7154e6
init a StatusEntry with an initializer that takes in a git_status_entry
m3ta4a Sep 8, 2017
8bb6e30
refactor the repository diff to rely on flatmaps to propogate errors
m3ta4a Sep 16, 2017
6a59e65
split out diff tests into a separate describe, rename status describe
m3ta4a Sep 16, 2017
efe1d6e
update for swift 4.0 nuances
m3ta4a Nov 10, 2017
e7b6e45
for some reason the wrong commit of result got used
m3ta4a Nov 17, 2017
a9f79a3
address some PR comments
m3ta4a Nov 17, 2017
db0a955
clean up various xcode warnings
m3ta4a Nov 17, 2017
d8ee830
Merge branch 'master' into develop
m3ta4a Dec 15, 2017
f98664e
Merge branch 'master' into develop
m3ta4a Dec 16, 2017
ecb5263
fixes to tests that used guanaco
m3ta4a Dec 16, 2017
26f1bb7
Fix for spec failing because only the diff for the first parent was c…
m3ta4a Dec 17, 2017
9a9b7a9
More detailed tests
m3ta4a Dec 17, 2017
87af7fc
Address lint in method calls
m3ta4a Jan 6, 2018
1a369b7
Refactor method for calculating diffs to read more cleanly and be saf…
m3ta4a Jan 6, 2018
0397b29
refactor diff calls to be memory safe
m3ta4a Jan 9, 2018
0dab27d
Remove the necessity of the caller of diff to clean up memory and add…
m3ta4a Jan 12, 2018
6f59ce5
Factor out common diff processing code for simplicity
m3ta4a Feb 2, 2018
118ba43
Move deferred memory releases to avoid leaks in case of an error
m3ta4a Feb 8, 2018
1f23feb
move one more defer
m3ta4a Feb 8, 2018
6e5703d
another deferred release
m3ta4a Feb 8, 2018
97ee009
Defer release status list
m3ta4a Feb 9, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion SwiftGit2.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
/* End PBXAggregateTarget section */

/* Begin PBXBuildFile section */
232861431F4A3A2E00276D65 /* Diffs.swift in Sources */ = {isa = PBXBuildFile; fileRef = 232861421F4A3A2E00276D65 /* Diffs.swift */; };
232861451F4A3A2E00276D65 /* Diffs.swift in Sources */ = {isa = PBXBuildFile; fileRef = 232861421F4A3A2E00276D65 /* Diffs.swift */; };
237731C71F46542B0020A3FE /* repository-with-status.zip in Resources */ = {isa = PBXBuildFile; fileRef = 237731C61F46542B0020A3FE /* repository-with-status.zip */; };
2549921B34FFC36AF8C9CD6D /* CommitIterator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 25499A996CA7BD416620A397 /* CommitIterator.swift */; };
25499D325997CAB9BEFFCA4D /* CommitIterator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 25499A996CA7BD416620A397 /* CommitIterator.swift */; };
621E66A01C72958800A0F352 /* OID.swift in Sources */ = {isa = PBXBuildFile; fileRef = BE70B3E41A1ACB1A002C3F4E /* OID.swift */; };
Expand Down Expand Up @@ -129,6 +132,8 @@
/* End PBXContainerItemProxy section */

/* Begin PBXFileReference section */
232861421F4A3A2E00276D65 /* Diffs.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Diffs.swift; sourceTree = "<group>"; };
237731C61F46542B0020A3FE /* repository-with-status.zip */ = {isa = PBXFileReference; lastKnownFileType = archive.zip; path = "repository-with-status.zip"; sourceTree = "<group>"; };
25499A996CA7BD416620A397 /* CommitIterator.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CommitIterator.swift; sourceTree = "<group>"; };
621E66B41C72958800A0F352 /* SwiftGit2.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = SwiftGit2.framework; sourceTree = BUILT_PRODUCTS_DIR; };
621E66CE1C72958D00A0F352 /* SwiftGit2-iOSTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = "SwiftGit2-iOSTests.xctest"; sourceTree = BUILT_PRODUCTS_DIR; };
Expand Down Expand Up @@ -260,6 +265,7 @@
BE14AA531A1983520015B439 /* Fixtures */ = {
isa = PBXGroup;
children = (
237731C61F46542B0020A3FE /* repository-with-status.zip */,
BE0B1C5C1A9978890004726D /* detached-head.zip */,
BE14AA541A1984550015B439 /* Fixtures.swift */,
BE0991F61A578FB1007D4E6A /* Mantle.zip */,
Expand All @@ -272,6 +278,7 @@
isa = PBXGroup;
children = (
BEB31F251A0D6F7A00F525B9 /* SwiftGit2 */,
BEB31F261A0D6F7A00F525B9 /* Supporting Files */,
BEB31F321A0D6F7A00F525B9 /* SwiftGit2Tests */,
BEB31FA11A0E63C100F525B9 /* Libraries */,
BEB31F411A0D75EE00F525B9 /* Configuration */,
Expand Down Expand Up @@ -305,12 +312,12 @@
DA5914751A94579000AED74C /* Errors.swift */,
BE36354B1A632C9700D37EC8 /* Libgit2.swift */,
BE2E3BE51A31261300C67092 /* Objects.swift */,
232861421F4A3A2E00276D65 /* Diffs.swift */,
BE70B3E41A1ACB1A002C3F4E /* OID.swift */,
BE7A753E1A4A2BCC002DA7E3 /* Pointers.swift */,
BEB31F6C1A0D78F300F525B9 /* Repository.swift */,
BECB5F691A56F19900999413 /* References.swift */,
BECB5F6D1A57284700999413 /* Remotes.swift */,
BEB31F261A0D6F7A00F525B9 /* Supporting Files */,
25499A996CA7BD416620A397 /* CommitIterator.swift */,
);
path = SwiftGit2;
Expand All @@ -322,6 +329,7 @@
BEB31F271A0D6F7A00F525B9 /* Info.plist */,
);
name = "Supporting Files";
path = SwiftGit2;
sourceTree = "<group>";
};
BEB31F321A0D6F7A00F525B9 /* SwiftGit2Tests */ = {
Expand Down Expand Up @@ -655,6 +663,7 @@
buildActionMask = 2147483647;
files = (
BE0B1C5D1A9978890004726D /* detached-head.zip in Resources */,
237731C71F46542B0020A3FE /* repository-with-status.zip in Resources */,
BE0991F71A578FB1007D4E6A /* Mantle.zip in Resources */,
BE14AA571A198C6E0015B439 /* simple-repository.zip in Resources */,
);
Expand Down Expand Up @@ -742,6 +751,7 @@
622726351C84E52500C53D17 /* Credentials.swift in Sources */,
621E66A31C72958800A0F352 /* Repository.swift in Sources */,
621E66A41C72958800A0F352 /* Objects.swift in Sources */,
232861451F4A3A2E00276D65 /* Diffs.swift in Sources */,
621E66A51C72958800A0F352 /* References.swift in Sources */,
621E66A61C72958800A0F352 /* Libgit2.swift in Sources */,
621E66A71C72958800A0F352 /* Pointers.swift in Sources */,
Expand Down Expand Up @@ -775,6 +785,7 @@
622726341C84E52500C53D17 /* Credentials.swift in Sources */,
BEB31F6D1A0D78F300F525B9 /* Repository.swift in Sources */,
BE2E3BE61A31261300C67092 /* Objects.swift in Sources */,
232861431F4A3A2E00276D65 /* Diffs.swift in Sources */,
BECB5F6A1A56F19900999413 /* References.swift in Sources */,
BE36354C1A632C9700D37EC8 /* Libgit2.swift in Sources */,
BE7A753F1A4A2BCC002DA7E3 /* Pointers.swift in Sources */,
Expand Down Expand Up @@ -1008,6 +1019,7 @@
PRODUCT_BUNDLE_IDENTIFIER = "org.libgit2.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = SwiftGit2;
SWIFT_INCLUDE_PATHS = "$(SRCROOT)/libgit2";
SWIFT_VERSION = 4.0;
};
name = Debug;
};
Expand Down Expand Up @@ -1036,6 +1048,7 @@
PRODUCT_BUNDLE_IDENTIFIER = "org.libgit2.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = SwiftGit2;
SWIFT_INCLUDE_PATHS = "$(SRCROOT)/libgit2";
SWIFT_VERSION = 4.0;
};
name = Release;
};
Expand Down
16 changes: 8 additions & 8 deletions SwiftGit2/CommitIterator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@ public class CommitIterator: IteratorProtocol, Sequence {
public typealias Iterator = CommitIterator
public typealias Element = Result<Commit, NSError>
let repo: Repository
private var revisionWalker: OpaquePointer? = nil
private var revisionWalker: OpaquePointer?

private enum Next {
case over
case ok
case okay
case error(NSError)

init(_ result: Int32, name: String) {
switch result {
case GIT_ITEROVER.rawValue:
self = .over
case GIT_OK.rawValue:
self = .ok
self = .okay
default:
self = .error(NSError(gitError: result, pointOfFailure: name))
}
Expand Down Expand Up @@ -55,7 +55,7 @@ public class CommitIterator: IteratorProtocol, Sequence {
return Result.failure(error)
case .over:
return nil
case .ok:
case .okay:
var unsafeCommit: OpaquePointer? = nil
let lookupGitResult = git_commit_lookup(&unsafeCommit, repo.pointer, &oid)
guard lookupGitResult == GIT_OK.rawValue,
Expand All @@ -76,7 +76,7 @@ public class CommitIterator: IteratorProtocol, Sequence {
public func map<T>(_ transform: (Result<Commit, NSError>) throws -> T) rethrows -> [T] {
var new: [T] = []
for item in self {
new = new + [try transform(item)]
new += [try transform(item)]
}
return new
}
Expand All @@ -85,7 +85,7 @@ public class CommitIterator: IteratorProtocol, Sequence {
var new: [Result<Commit, NSError>] = []
for item in self {
if try isIncluded(item) {
new = new + [item]
new += [item]
}
}
return new
Expand All @@ -104,12 +104,12 @@ public class CommitIterator: IteratorProtocol, Sequence {
self.repo = repo
}

public func dropFirst(_ n: Int) -> AnySequence<Iterator.Element> {
public func dropFirst(_ num: Int) -> AnySequence<Iterator.Element> {
notImplemented(functionName: self.dropFirst)
return AnySequence<Iterator.Element> { return CommitIterator(repo: self.repo) }
}

public func dropLast(_ n: Int) -> AnySequence<Iterator.Element> {
public func dropLast(_ num: Int) -> AnySequence<Iterator.Element> {
notImplemented(functionName: self.dropLast)
return AnySequence<Iterator.Element> { return CommitIterator(repo: self.repo) }
}
Expand Down
111 changes: 111 additions & 0 deletions SwiftGit2/Diffs.swift
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))
}
}
}
}
Copy link
Contributor

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?

public struct Diff {
   public struct File {  }

   public struct Flag: OptionSet { ... }

   public struct Delta {
     public var status: Status
     public var flags: DiffFlag
     public var oldFile: File
     public var newFile: File
   }

   let deltas: [Delta]
}

Copy link
Contributor Author

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

Loading