SwiftGitX

repository·main·Indexed 41 days ago

https://github.com/ibrahimcetin/swiftgitx

A modern, high-level Swift wrapper for libgit2 providing an intuitive, async/await-based API for Git operations. It features full Swift Concurrency support, typed error handling via SwiftGitXError, and compatibility with Swift Package Manager (version 0.1.0). The library allows developers to clone repositories, manage branches, create commits, and inspect history without exposing low-level C types.

Tokens
3.6K
Snippets
11
Records
30
Agent score
100%

What's inside SwiftGitX

  1. Overview of SwiftGitX

    main

    SwiftGitX is a modern Swift wrapper for libgit2 designed for easy and efficient Git operations in Swift. It provides a complete Swift experience, avoiding low-level C functions or types, and offers an intuitive API similar to the Git command line interface. Key features include:

    • Swift Concurrency: Full support for async/await for non-blocking operations.
    • Error Handling: Uses throwing functions for graceful error management.
    • SPM Support: Fully compatible with Swift Package Manager.
    • No External Dependencies: Requires only Swift to build and run.
  2. Understand Git object types in SwiftGitX

    main

    SwiftGitX models the core Git object types found in a repository. These objects are the fundamental building blocks of Git data structures. The library provides specific types for each of the following:

    • Commit: Represents a snapshot of the project at a specific point in time, including author, committer, and parent information.
    • Tree: Represents a directory structure, mapping filenames to the OIDs of blobs or other trees.
    • Blob: Represents the content of a file.
    • Tag: Represents an annotated tag pointing to a specific commit or object.
  3. Handle errors using SwiftGitXError and typed throws

    main

    SwiftGitX uses a unified SwiftGitXError type for all Git operations. Instead of generic Error types, functions in SwiftGitX use Swift's typed throws (throws(SwiftGitXError)). This allows you to catch specific error types at compile-time and provides detailed context about the failure.

    When an error occurs, you can inspect the following properties to understand the failure:

    • code: The specific error code (SwiftGitXError.Code).
    • operation: The specific Git operation that failed (SwiftGitXError.Operation).
    • category: The high-level category of the error (SwiftGitXError.Category).
    • message: A human-readable description of the error.
  4. Access collections of Git commands

    main

    SwiftGitX organizes Git operations into logical collections based on the type of Git command being executed. Instead of a single flat API, you interact with specific collections to perform tasks related to branches, configuration, references, remotes, stashes, or tags.

    Available collections include:

    • BranchCollection: For branch management (create, delete, list, etc.).
    • ConfigCollection: For interacting with Git configuration settings.
    • ReferenceCollection: For managing Git references.
    • RemoteCollection: For managing remote repositories.
    • StashCollection: For managing the Git stash.
    • TagCollection: For managing Git tags.
  5. Iterate over Git branches and commits using Sequences

    main

    SwiftGitX provides sequence types that allow you to iterate over Git objects like branches and commits. These sequences are high-level abstractions that conform to Swift's Sequence protocol, making them compatible with standard Swift iteration patterns (like for...in loops or functional methods like map and filter).

    Available sequences:

    • BranchSequence: For iterating over branches.
    • CommitSequence: For iterating over commits.
    // Example usage pattern for sequences
    for branch in branchSequence {
        print(branch.name)
    }
    
    for commit in commitSequence {
        print(commit.hash)
    }
  6. Add SwiftGitX to your project

    main

    You can integrate SwiftGitX into your project using the Xcode interface or by manually editing your Package.swift file.

    Using Xcode

    1. Go to File > Add Package Dependencies...
    2. Enter the repository URL: https://github.com/ibrahimcetin/SwiftGitX.git
    3. Select the dependency rule Up to Next Major with version 0.1.0.

    Using Package.swift

    Add the following to your dependencies array:

    dependencies: [
        .package(url: "https://github.com/ibrahimcetin/SwiftGitX.git", from: "0.1.0"),
    ]