Sign in

    libgit2

    MoonBit bindings for libgit2 - Git operations from MoonBit

    git
    libgit2
    ffi
    Download zip
    Author
    Version
    0.1.0
    License
    MIT
    Last updated
    10 months ago
    Downloads
    16K

    Dependencies

    #libgit2.mbt

    MoonBit bindings for libgit2 - Git operations from MoonBit.

    #Features

    • Repository operations (open, init, clone)
    • Commit history traversal
    • Branch and tag management
    • Status and diff operations
    • Tree and blob access
    • Index operations
    • Remote operations
    • And more...

    #Installation

    #Prerequisites

    Install libgit2 on your system:

    macOS (Homebrew):
    brew install libgit2

    Ubuntu/Debian:
    sudo apt-get install libgit2-dev

    Fedora:
    sudo dnf install libgit2-devel

    #Add to your project

    Add to your moon.mod.json:
    { "deps": { "mizchi/libgit2": "0.1.0" } }

    #Usage

    #Basic Example

    fn main {
    // Initialize libgit2
    if not(@libgit2.git_init()) {
    println("Failed to load libgit2")
    return
    }

    // Open repository
    match @libgit2.Repository::open(".") {
    Some(repo) => {
    println("Repository: \{repo.path()}")

    // Get recent commits
    let commits = repo.log(5)
    for commit in commits {
    println("[\{commit.id.substring(0, 7)}] \{commit.summary}")
    }

    repo.close()
    }
    None => println("Failed to open repository")
    }

    @libgit2.git_shutdown()
    }

    #More Examples

    See the examples/ directory for more usage examples:

    • basic - Simple repository operations
    • advanced - Advanced features (branches, remotes, etc.)

    #API Documentation

    Run moon doc to generate API documentation.

    #Core Types

    • Repository - Git repository handle
    • CommitInfo - Commit information
    • BranchInfo - Branch information
    • RemoteInfo - Remote repository information
    • DiffStats - Diff statistics
    • Tree, Blob, Index - Git object types

    #Main Functions

    #Repository Operations

    Repository::open(path: String) -> Repository?
    Repository::init(path: String, is_bare: Bool) -> Repository?
    Repository::close(self: Repository) -> Unit

    #Commit Operations

    Repository::log(self: Repository, max_count: Int) -> Array[CommitInfo]
    Repository::head_commit(self: Repository) -> CommitInfo?

    #Branch Operations

    Repository::branches(self: Repository, include_remote: Bool) -> Array[BranchInfo]
    Repository::current_branch(self: Repository) -> String?

    See lib/run.mbt for the complete API.

    #Testing

    Run tests:
    moon test --target native

    #Architecture

    This library uses a 3-layer architecture:

    MoonBit API (lib/run.mbt) ↓ C Wrapper (lib/stub.c) ↓ libgit2 (dynamic library)

    The C wrapper handles:
    • Dynamic library loading (dlopen/dlsym)
    • Type conversion (MoonBit Bytes ↔ C strings)
    • Memory management
    • Platform-specific paths (macOS, Linux)

    #Platform Support

    • macOS: ✓ (Intel & Apple Silicon)
    • Linux: ✓
    • Windows: Not yet tested

    #License

    MIT License - See LICENSE for details.

    #Contributing

    Contributions welcome! Please open an issue or pull request.

    Blob

    pub struct Blob {
    // private fields
    }

    ブロブ(ファイル内容)

    Blob::close

    fn Blob::close(self : Blob) -> Unit

    ブロブを閉じる

    Blob::content

    fn Blob::content(self : Blob) -> Bytes

    ブロブの内容を取得(バイト列)

    Blob::content_string

    fn Blob::content_string(self : Blob) -> String

    ブロブの内容を文字列として取得

    Blob::is_binary

    fn Blob::is_binary(self : Blob) -> Bool

    バイナリファイルかどうか

    Blob::size

    fn Blob::size(self : Blob) -> Int64

    ブロブのサイズを取得

    BranchInfo

    pub struct BranchInfo {
    name : String
    branch_type : BranchType
    }

    ブランチ情報

    BranchInfo::to_string

    fn BranchInfo::to_string(self : BranchInfo) -> String

    BranchType

    pub enum BranchType {
    Local
    Remote
    }

    ブランチタイプ

    CommitInfo

    pub struct CommitInfo {
    id : String
    message : String
    summary : String
    author_name : String
    author_email : String
    committer_name : String
    committer_email : String
    time : Int64
    }

    コミット情報

    CommitInfo::short_id

    fn CommitInfo::short_id(self : CommitInfo) -> String

    CommitInfo::to_string

    fn CommitInfo::to_string(self : CommitInfo) -> String

    DiffStats

    pub struct DiffStats {
    files_changed : Int
    insertions : Int
    deletions : Int
    }

    差分統計

    DiffStats::to_string

    fn DiffStats::to_string(self : DiffStats) -> String

    GitBlob

    type GitBlob

    GitIndex

    type GitIndex

    GitRepository

    type GitRepository

    GitSignature

    type GitSignature

    GitTree

    type GitTree

    Index

    pub struct Index {
    // private fields
    }

    インデックス(ステージングエリア)

    Index::add

    fn Index::add(self : Index, path : String) -> Bool

    ファイルをステージに追加

    Index::close

    fn Index::close(self : Index) -> Unit

    インデックスを閉じる

    Index::entry_count

    fn Index::entry_count(self : Index) -> Int

    ステージされたエントリ数

    Index::reload

    fn Index::reload(self : Index) -> Bool

    インデックスを再読み込み

    Index::remove

    fn Index::remove(self : Index, path : String) -> Bool

    ファイルをステージから削除

    Index::write

    fn Index::write(self : Index) -> Bool

    インデックスを書き込み

    Index::write_tree

    fn Index::write_tree(self : Index) -> String?

    インデックスからツリーを作成し、ツリー OID を返す

    ObjectType

    pub enum ObjectType {
    Blob
    Tree
    Commit
    Tag
    Unknown
    }

    オブジェクトタイプ

    RemoteInfo

    pub struct RemoteInfo {
    name : String
    url : String
    push_url : String
    }

    リモート情報

    RemoteInfo::to_string

    fn RemoteInfo::to_string(self : RemoteInfo) -> String

    Repository

    pub struct Repository {
    // private fields
    }

    リポジトリ

    Repository::blob

    fn Repository::blob(self : Repository, oid : String) -> Blob?

    OID からブロブを取得

    Repository::branches

    fn Repository::branches(self : Repository, include_remote : Bool) -> Array[BranchInfo]

    ブランチ一覧を取得

    Repository::checkout_head

    fn Repository::checkout_head(self : Repository) -> Bool

    HEAD にチェックアウト

    Repository::close

    fn Repository::close(self : Repository) -> Unit

    リポジトリを閉じる

    Repository::commit

    fn Repository::commit(self : Repository, message : String, author : Signature, committer : Signature, tree_oid : String) -> String?

    コミットを作成(HEAD を親として) 戻り値: 新しいコミットの OID

    Repository::commit_simple

    fn Repository::commit_simple(self : Repository, message : String) -> String?

    簡易コミット(デフォルト署名を使用)

    Repository::config_get

    fn Repository::config_get(self : Repository, name : String) -> String?

    git config の値を取得

    Repository::default_signature

    fn Repository::default_signature(self : Repository) -> Signature?

    リポジトリのデフォルト署名(user.name, user.email から)

    Repository::diff_count

    fn Repository::diff_count(self : Repository) -> Int

    ワーキングツリーの変更ファイル数を取得

    Repository::diff_stats

    fn Repository::diff_stats(self : Repository) -> DiffStats?

    ワーキングツリーの差分統計を取得

    Repository::head_id

    fn Repository::head_id(self : Repository) -> String?

    HEADのコミットIDを取得

    Repository::head_name

    fn Repository::head_name(self : Repository) -> String?

    HEADの参照名を取得

    Repository::head_tree

    fn Repository::head_tree(self : Repository) -> Tree?

    HEAD のツリーを取得

    Repository::index

    fn Repository::index(self : Repository) -> Index?

    インデックスを開く

    Repository::init

    fn Repository::init(path : String) -> Repository?

    新しいリポジトリを初期化

    Repository::log

    fn Repository::log(self : Repository, max_count : Int) -> Array[CommitInfo]

    コミット履歴を取得(最新からn件)

    Repository::open

    fn Repository::open(path : String) -> Repository?

    リポジトリを開く

    Repository::path

    fn Repository::path(self : Repository) -> String

    リポジトリのパスを取得

    Repository::remote

    fn Repository::remote(self : Repository, name : String) -> RemoteInfo?

    リモート情報を取得

    Repository::remote_names

    fn Repository::remote_names(self : Repository) -> Array[String]

    リモート名一覧を取得

    Repository::remotes

    fn Repository::remotes(self : Repository) -> Array[RemoteInfo]

    すべてのリモート情報を取得

    Repository::reset

    fn Repository::reset(self : Repository, commit_oid : String, reset_type : ResetType) -> Bool

    指定したコミットにリセット

    Repository::status_count

    fn Repository::status_count(self : Repository) -> Int

    ワーキングツリーのステータス(変更ファイル数)

    Repository::status_flags

    fn Repository::status_flags(self : Repository) -> Array[StatusFlags]

    ワーキングツリーのステータスフラグ一覧を取得

    Repository::tag_count

    fn Repository::tag_count(self : Repository) -> Int

    タグ数を取得

    Repository::tags

    fn Repository::tags(self : Repository) -> Array[String]

    タグ一覧を取得

    ResetType

    pub(all) enum ResetType {
    Soft
    Mixed
    Hard
    }

    リセットタイプ

    Signature

    pub struct Signature {
    // private fields
    }

    署名(author/committer)

    Signature::close

    fn Signature::close(self : Signature) -> Unit

    署名を解放

    Signature::email

    fn Signature::email(self : Signature) -> String

    署名のメールアドレス

    Signature::name

    fn Signature::name(self : Signature) -> String

    署名の名前

    Signature::now

    fn Signature::now(name : String, email : String) -> Signature?

    現在時刻で署名を作成

    StatusFlags

    pub struct StatusFlags {
    flags : Int
    }

    ステータスフラグ

    StatusFlags::is_deleted

    fn StatusFlags::is_deleted(self : StatusFlags) -> Bool

    StatusFlags::is_modified

    fn StatusFlags::is_modified(self : StatusFlags) -> Bool

    StatusFlags::is_new

    fn StatusFlags::is_new(self : StatusFlags) -> Bool

    StatusFlags::is_staged

    fn StatusFlags::is_staged(self : StatusFlags) -> Bool

    StatusFlags::new

    fn StatusFlags::new(flags : Int) -> StatusFlags

    StatusFlags::to_string

    fn StatusFlags::to_string(self : StatusFlags) -> String

    Tree

    pub struct Tree {
    // private fields
    }

    ツリー

    Tree::close

    fn Tree::close(self : Tree) -> Unit

    Tree::entries

    fn Tree::entries(self : Tree) -> Array[TreeEntry]

    Tree::entry_count

    fn Tree::entry_count(self : Tree) -> Int

    TreeEntry

    pub struct TreeEntry {
    name : String
    id : String
    object_type : ObjectType
    }

    ツリーエントリ情報

    TreeEntry::to_string

    fn TreeEntry::to_string(self : TreeEntry) -> String

    GIT_SORT_NONE

    let GIT_SORT_NONE : Int

    GIT_SORT_REVERSE

    let GIT_SORT_REVERSE : Int

    GIT_SORT_TIME

    let GIT_SORT_TIME : Int

    GIT_SORT_TOPOLOGICAL

    let GIT_SORT_TOPOLOGICAL : Int

    GIT_STATUS_CURRENT

    let GIT_STATUS_CURRENT : Int

    GIT_STATUS_INDEX_DELETED

    let GIT_STATUS_INDEX_DELETED : Int

    GIT_STATUS_INDEX_MODIFIED

    let GIT_STATUS_INDEX_MODIFIED : Int

    GIT_STATUS_INDEX_NEW

    let GIT_STATUS_INDEX_NEW : Int

    GIT_STATUS_WT_DELETED

    let GIT_STATUS_WT_DELETED : Int

    GIT_STATUS_WT_MODIFIED

    let GIT_STATUS_WT_MODIFIED : Int

    GIT_STATUS_WT_NEW

    let GIT_STATUS_WT_NEW : Int

    git_init

    fn git_init() -> Bool

    ライブラリの初期化(ライブラリのロードも行う) 戻り値: 成功時はtrue、失敗時はfalse

    git_shutdown

    fn git_shutdown() -> Unit

    ライブラリのシャットダウン

    is_loaded

    fn is_loaded() -> Bool

    ライブラリがロードされているかチェック

    last_error

    fn last_error() -> String

    最後のエラーメッセージを取得

    Source Files