Skip to content

Commit

Permalink
Merge pull request SwiftGit2#127 from Maaimusic/git-add
Browse files Browse the repository at this point in the history
Git Add
  • Loading branch information
mdiep authored May 2, 2018
2 parents 280ab5b + 81d2fb2 commit 25f3ecc
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
27 changes: 27 additions & 0 deletions SwiftGit2/Repository.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand Down
42 changes: 42 additions & 0 deletions SwiftGit2Tests/RepositorySpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand Down

0 comments on commit 25f3ecc

Please sign in to comment.