mirror of
https://github.com/gosticks/SwiftGit2.git
synced 2025-10-16 11:55:34 +00:00
commit
25f3ecc4e6
@ -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> {
|
||||
|
||||
@ -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") {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user