From f9d7bd3b61d55727b5657c4d4fa97648be1ffc17 Mon Sep 17 00:00:00 2001 From: Yifei Teng Date: Mon, 30 Apr 2018 18:04:53 -0500 Subject: [PATCH 1/3] add support for unsafeIndex and git-add --- SwiftGit2/Repository.swift | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/SwiftGit2/Repository.swift b/SwiftGit2/Repository.swift index e2413582..a43ded60 100644 --- a/SwiftGit2/Repository.swift +++ b/SwiftGit2/Repository.swift @@ -583,6 +583,33 @@ final public class Repository { return iterator } + /// Gets the index for the repo. + public func unsafeIndex() -> Result { + var index: OpaquePointer? = nil + let result = git_repository_index(&index, self.pointer) + guard result == GIT_OK.rawValue && index != nil else { + let err = NSError(gitError: result, pointOfFailure: "git_repository_index") + return .failure(err) + } + return .success(index!) + } + + /// Stage the file(s) under the specified path + public func add(path: String) -> Result<(), NSError> { + let dir = path + var dirPointer = UnsafeMutablePointer(mutating: (dir as NSString).utf8String) + var paths = git_strarray(strings: &dirPointer, count: 1) + return unsafeIndex().flatMap { index in + defer { git_index_free(index) } + let add_result = git_index_add_all(index, &paths, 0, nil, nil) + guard add_result == GIT_OK.rawValue else { + let err = NSError(gitError: add_result, pointOfFailure: "git_index_add_all") + return .failure(err) + } + return .success(()) + } + } + // MARK: - Diffs public func diff(for commit: Commit) -> Result { From b6e94b291c69f86e0f0cee87c0da001cbd387b2a Mon Sep 17 00:00:00 2001 From: Yifei Teng Date: Mon, 30 Apr 2018 19:05:12 -0500 Subject: [PATCH 2/3] add tests for git-add --- SwiftGit2/Repository.swift | 4 +-- SwiftGit2Tests/RepositorySpec.swift | 42 +++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/SwiftGit2/Repository.swift b/SwiftGit2/Repository.swift index a43ded60..bb91fc35 100644 --- a/SwiftGit2/Repository.swift +++ b/SwiftGit2/Repository.swift @@ -583,7 +583,7 @@ final public class Repository { return iterator } - /// Gets the index for the repo. + /// Get the index for the repo. The caller is responsible for freeing the index. public func unsafeIndex() -> Result { var index: OpaquePointer? = nil let result = git_repository_index(&index, self.pointer) @@ -594,7 +594,7 @@ final public class Repository { return .success(index!) } - /// Stage the file(s) under the specified path + /// Stage the file(s) under the specified path. public func add(path: String) -> Result<(), NSError> { let dir = path var dirPointer = UnsafeMutablePointer(mutating: (dir as NSString).utf8String) diff --git a/SwiftGit2Tests/RepositorySpec.swift b/SwiftGit2Tests/RepositorySpec.swift index d1560292..02c4f3ef 100644 --- a/SwiftGit2Tests/RepositorySpec.swift +++ b/SwiftGit2Tests/RepositorySpec.swift @@ -645,6 +645,48 @@ class RepositorySpec: QuickSpec { expect(commitMessages).to(equal(expectedMessages)) } } + + describe("Repository.add") { + it("Should add the modification under a path") { + let repo = Fixtures.simpleRepository + let branch = repo.localBranch(named: "master").value! + expect(repo.checkout(branch, strategy: CheckoutStrategy.None).error).to(beNil()) + + // make a change to README + let readmeURL = repo.directoryURL!.appendingPathComponent("README.md") + let readmeData = try! Data(contentsOf: readmeURL) + defer { try! readmeData.write(to: readmeURL) } + + try! "different".data(using: .utf8)?.write(to: readmeURL) + + let status = repo.status() + expect(status.value?.count).to(equal(1)) + expect(status.value!.first!.status).to(equal(.workTreeModified)) + + expect(repo.add(path: "README.md").error).to(beNil()) + + let newStatus = repo.status() + expect(newStatus.value?.count).to(equal(1)) + expect(newStatus.value!.first!.status).to(equal(.indexModified)) + } + + it("Should add an untracked file under a path") { + let repo = Fixtures.simpleRepository + let branch = repo.localBranch(named: "master").value! + expect(repo.checkout(branch, strategy: CheckoutStrategy.None).error).to(beNil()) + + // make a change to README + let untrackedURL = repo.directoryURL!.appendingPathComponent("untracked") + try! "different".data(using: .utf8)?.write(to: untrackedURL) + defer { try! FileManager.default.removeItem(at: untrackedURL) } + + expect(repo.add(path: ".").error).to(beNil()) + + let newStatus = repo.status() + expect(newStatus.value?.count).to(equal(1)) + expect(newStatus.value!.first!.status).to(equal(.indexNew)) + } + } describe("Repository.status") { it("Should accurately report status for repositories with no status") { From 81d2fb210a83cdaf1414760029cb6679a2fcc171 Mon Sep 17 00:00:00 2001 From: Yifei Teng Date: Tue, 1 May 2018 13:00:12 -0500 Subject: [PATCH 3/3] make unsafe functions internal --- SwiftGit2/Repository.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SwiftGit2/Repository.swift b/SwiftGit2/Repository.swift index bb91fc35..79fb9ea8 100644 --- a/SwiftGit2/Repository.swift +++ b/SwiftGit2/Repository.swift @@ -584,7 +584,7 @@ final public class Repository { } /// Get the index for the repo. The caller is responsible for freeing the index. - public func unsafeIndex() -> Result { + func unsafeIndex() -> Result { var index: OpaquePointer? = nil let result = git_repository_index(&index, self.pointer) guard result == GIT_OK.rawValue && index != nil else {