Merge pull request #127 from Maaimusic/git-add

Git Add
This commit is contained in:
Matt Diephouse 2018-05-02 08:23:49 -04:00 committed by GitHub
commit 25f3ecc4e6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 69 additions and 0 deletions

View File

@ -583,6 +583,33 @@ final public class Repository {
return iterator
}
/// Get the index for the repo. The caller is responsible for freeing the index.
func unsafeIndex() -> Result<OpaquePointer, NSError> {
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<Int8>(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<Diff, NSError> {

View File

@ -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") {