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)")
}SwiftGit2 uses value types wherever possible. That means using Swift’s structs and enums without holding references to libgit2 objects. This has a number of advantages:
- Values can be used concurrently.
- Consuming values won’t result in disk access.
- 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 SwiftGit’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.
To build SwiftGit2, you need to install libgit2 on your system.
Make sure to have Homebrew installed, then run
brew install libgit2
On Debian/Ubuntu-based distributions, run
apt install libgit2-dev
The easiest way to add SwiftGit2 to your project is to use SwiftPM. Simply add the following line to your Package.swift's dependencies:
.package(url: "https://github.com/SwiftGit2/SwiftGit2.git", .branch("master"))If you want to build a copy of SwiftGit2, e.g. for development:
- Clone SwiftGit2
- Run
swift build - Optionally run
swift testto run the tests
We ❤️ to receive pull requests! GitHub makes it easy:
- Fork the repository
- Create a branch with your changes
- Send a Pull Request
All contributions should match GitHub’s Swift Style Guide.
SwiftGit2 is available under the MIT license.