/SwiftGit2

Swift bindings to libgit2

Primary LanguageSwiftMIT LicenseMIT

SwiftGit2

Build Status Carthage compatible GitHub release Swift 5.3.x

Swift bindings to libgit2.

let URL: URL = ...
let result = Repository.at(URL)
switch result {
case let .success(repo):
    let latestCommit = repo
        .HEAD()
        .flatMap {
            repo.commit($0.oid)
        }

    switch latestCommit {
    case let .success(commit):
        print("Latest Commit: \(commit.message) by \(commit.author.name)")

    case let .failure(error):
        print("Could not get commit: \(error)")
    }

case let .failure(error):
    print("Could not open repository: \(error)")
}

See our iOS example app project to see the code in action.

Design

SwiftGit2 uses value objects wherever possible. That means using Swift's structs and enums without holding references to libgit2 objects. This has a number of advantages:

  1. Values can be used concurrently.
  2. Consuming values won't result in disk access.
  3. Disk access can be contained to a smaller number of APIs.

This vastly simplifies the design of long-lived applications, which are the most common use case with Swift. Consequently, SwiftGit2 APIs don't necessarily map 1-to-1 with libgit2 APIs.

All methods for reading from or writing to a repository are on SwiftGit2's only class: Repository. This highlights the failability and mutation of these methods, while freeing up all other instances to be immutable structs and enums.

Adding SwiftGit2 to your Project

The easiest way to add SwiftGit2 to your project is to add our repository as a Swift package.

Note that you need to choose spm branch of our fork https://github.com/light-tech/SwiftGit2 which was configured to be compatible with Swift package manager. NEITHER the original repository https://github.com/SwiftGit2/SwiftGit2 nor other branch of our fork support Swift package manager as of this writing.

You also need to add libz.tbd and libiconv.tbd to the app target's Frameworks, Libraries and Embedded Content.

Before using any of the SwiftGit2 API, add

Repository.initialize_libgit2()

to your app initialization method.

Check out our iOS example app project for a starting point.

Contributions

We ❤️ to receive pull requests! GitHub makes it easy:

  1. Fork the repository
  2. Create a branch with your changes
  3. Send a Pull Request

All contributions should match GitHub's Swift Style Guide.

License

SwiftGit2 is available under the MIT license.