Warning
The SwiftGit2 maintainers are in the process of migrating SwiftGit2 to the Swift Package Manager.
This causes the following breaking changes:
- The Carthage configuration and Xcode project are gone and are longer supported.
- SwiftGit2 must now be explicitly initialized by the application by calling
SwiftGit2Init(). Its resources may be cleaned up again usingSwiftGit2Shutdown(). - The dependency libssh2 is not yet integrated. The impact is that SwiftGit2 on iOS cannot interact with remote repositories over SSH. (Remotes that use HTTPS are supported using the HTTP client that ships with libgit2.)
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.
You can add SwiftGit2 to your project using the Swift Package Manager.
In Xcode, go to your project settings, then to Package Dependencies. Add SwiftGit2 using the URL:
https://github.com/SwiftGit2/SwiftGit2.git
If you're developing an SPM-based project, open your Package.swift file and add SwiftGit2 as a dependency:
.package(url: "https://github.com/SwiftGit2/SwiftGit2.git", from: "1.0.0")And don't forget to reference it from your target:
.target(name: "YourProject", dependencies: ["SwiftGit2"]),If you want to build and test SwiftGit2 locally for development purposes:
- Clone SwiftGit2
- Run
git submodule update --initto clone the libgit2 submodule - Run
swift testor open thePackage.swiftfile to develop and test using Xcode
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.