Convert Figma logo to code with AI

libgit2 logogit2go

Git to Go; bindings for libgit2. Like McDonald's but tastier.

1,927
314
1,927
83

Top Related Projects

4,907

Project has been moved to: https://github.com/go-git/go-git

44,038

Git with a cup of tea! Painless self-hosted all-in-one software development service, including Git hosting, code review, team collaboration, package registry and CI/CD

Package githubv4 is a client library for accessing GitHub GraphQL API v4 (https://docs.github.com/en/graphql).

Quick Overview

Git2Go is a Go binding for libgit2, providing native speed and ease of use for Git operations in Go applications. It allows developers to interact with Git repositories programmatically, offering a wide range of Git functionalities without relying on external Git commands.

Pros

  • High performance due to native implementation
  • Comprehensive Git functionality support
  • Thread-safe and memory-safe
  • Well-maintained and actively developed

Cons

  • Requires libgit2 to be installed on the system
  • Learning curve for developers unfamiliar with libgit2
  • May have version compatibility issues between Git2Go and libgit2

Code Examples

  1. Opening a repository:
repo, err := git.OpenRepository("/path/to/repo")
if err != nil {
    log.Fatal(err)
}
defer repo.Free()
  1. Creating a commit:
signature := &git.Signature{
    Name:  "John Doe",
    Email: "john@example.com",
    When:  time.Now(),
}

treeId, err := repo.Index().WriteTree()
if err != nil {
    log.Fatal(err)
}

tree, err := repo.LookupTree(treeId)
if err != nil {
    log.Fatal(err)
}

commitId, err := repo.CreateCommit("HEAD", signature, signature, "Commit message", tree)
if err != nil {
    log.Fatal(err)
}
  1. Listing branches:
branches, err := repo.Branches()
if err != nil {
    log.Fatal(err)
}
defer branches.Free()

branches.ForEach(func(branch *git.Branch, branchType git.BranchType) error {
    name, err := branch.Name()
    if err != nil {
        return err
    }
    fmt.Printf("Branch: %s\n", name)
    return nil
})

Getting Started

  1. Install libgit2 on your system (version 1.3 or later recommended).
  2. Install Git2Go:
    go get github.com/libgit2/git2go/v33
    
  3. Import Git2Go in your Go code:
    import "github.com/libgit2/git2go/v33"
    
  4. Start using Git2Go functions in your application:
    repo, err := git.OpenRepository("/path/to/repo")
    if err != nil {
        log.Fatal(err)
    }
    defer repo.Free()
    
    // Perform Git operations using the repo object
    

Competitor Comparisons

4,907

Project has been moved to: https://github.com/go-git/go-git

Pros of go-git

  • Pure Go implementation, no C dependencies
  • Easier to cross-compile and deploy
  • More idiomatic Go API

Cons of go-git

  • Less feature-complete compared to libgit2
  • Potentially slower performance for some operations
  • Newer project with a smaller community

Code Comparison

git2go example:

repo, err := git.OpenRepository("path/to/repo")
if err != nil {
    log.Fatal(err)
}
defer repo.Free()

go-git example:

repo, err := git.PlainOpen("path/to/repo")
if err != nil {
    log.Fatal(err)
}

Both libraries provide similar functionality for opening a Git repository, but go-git's API is more idiomatic Go. git2go requires manual memory management with defer repo.Free(), while go-git doesn't need this step.

git2go is a wrapper around libgit2, which is written in C, providing excellent performance and feature completeness. However, it requires CGo and can be challenging to cross-compile.

go-git, being a pure Go implementation, is easier to work with in Go projects and doesn't require CGo. It's more straightforward to cross-compile and deploy, but may lack some advanced features and could be slower for certain operations.

44,038

Git with a cup of tea! Painless self-hosted all-in-one software development service, including Git hosting, code review, team collaboration, package registry and CI/CD

Pros of Gitea

  • Full-featured Git hosting solution with web interface, issue tracking, and more
  • Self-hosted and open-source, offering greater control and customization
  • Active community and regular updates

Cons of Gitea

  • Larger codebase and more complex setup compared to git2go
  • May be overkill for projects only needing Git operations without a full hosting platform

Code Comparison

git2go (C bindings for libgit2):

repo, err := git.OpenRepository("path/to/repo")
if err != nil {
    // Handle error
}
defer repo.Free()

Gitea (Git operations using go-git):

repo, err := git.PlainOpen("path/to/repo")
if err != nil {
    // Handle error
}

Summary

git2go is a Go binding for libgit2, providing low-level Git operations. It's lightweight and suitable for projects needing direct Git functionality. Gitea, on the other hand, is a complete Git hosting solution with a web interface and additional features. It uses go-git for Git operations and offers a more comprehensive platform for managing Git repositories, issues, and collaboration. The choice between the two depends on whether you need a full hosting solution or just Git operations in your Go project.

Package githubv4 is a client library for accessing GitHub GraphQL API v4 (https://docs.github.com/en/graphql).

Pros of githubv4

  • Focuses specifically on GitHub API v4, providing a more tailored and efficient interface for GitHub interactions
  • Utilizes GraphQL, allowing for more flexible and precise data fetching
  • Offers strong type safety through code generation based on the GitHub GraphQL schema

Cons of githubv4

  • Limited to GitHub-specific operations, unlike git2go which provides general Git functionality
  • Requires understanding of GraphQL for effective use, potentially steeper learning curve
  • Dependent on GitHub's API availability and rate limits

Code Comparison

githubv4:

client := githubv4.NewClient(httpClient)
var query struct {
    Repository struct {
        Issue struct {
            Title string
        } `graphql:"issue(number: $issueNumber)"`
    } `graphql:"repository(owner: $owner, name: $name)"`
}

git2go:

repo, err := git.OpenRepository("/path/to/repo")
if err != nil {
    return err
}
commit, err := repo.LookupCommit(oid)

The githubv4 example demonstrates querying a specific issue using GraphQL, while the git2go example shows opening a local repository and looking up a commit. githubv4 is more focused on remote GitHub operations, while git2go provides lower-level Git functionality.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

git2go

GoDoc Build Status

Go bindings for libgit2.

Which Go version to use

Due to the fact that Go 1.11 module versions have semantic meaning and don't necessarily align with libgit2's release schedule, please consult the following table for a mapping between libgit2 and git2go module versions:

libgit2git2go
main(will be v35)
1.5v34
1.3v33
1.2v32
1.1v31
1.0v30
0.99v29
0.28v28
0.27v27

You can import them in your project with the version's major number as a suffix. For example, if you have libgit2 v1.2 installed, you'd import git2go v34 with:

go get github.com/libgit2/git2go/v34
import "github.com/libgit2/git2go/v34"

which will ensure there are no sudden changes to the API.

The main branch follows the tip of libgit2 itself (with some lag) and as such has no guarantees on the stability of libgit2's API. Thus this only supports statically linking against libgit2.

Which branch to send Pull requests to

If there's something version-specific that you'd want to contribute to, you can send them to the release-${MAJOR}.${MINOR} branches, which follow libgit2's releases.

Installing

This project wraps the functionality provided by libgit2. It thus needs it in order to perform the work.

This project wraps the functionality provided by libgit2. If you're using a versioned branch, install it to your system via your system's package manager and then install git2go.

Versioned branch, dynamic linking

When linking dynamically against a released version of libgit2, install it via your system's package manager. CGo will take care of finding its pkg-config file and set up the linking. Import via Go modules, e.g. to work against libgit2 v1.2

import "github.com/libgit2/git2go/v34"

Versioned branch, static linking

Follow the instructions for Versioned branch, dynamic linking, but pass the -tags static,system_libgit2 flag to all go commands that build any binaries. For instance:

go build -tags static,system_libgit2 github.com/my/project/...
go test -tags static,system_libgit2 github.com/my/project/...
go install -tags static,system_libgit2 github.com/my/project/...

main branch, or vendored static linking

If using main or building a branch with the vendored libgit2 statically, we need to build libgit2 first. In order to build it, you need cmake, pkg-config and a C compiler. You will also need the development packages for OpenSSL (outside of Windows or macOS) and LibSSH2 installed if you want libgit2 to support HTTPS and SSH respectively. Note that even if libgit2 is included in the resulting binary, its dependencies will not be.

Run go get -d github.com/libgit2/git2go to download the code and go to your $GOPATH/src/github.com/libgit2/git2go directory. From there, we need to build the C code and put it into the resulting go binary.

git submodule update --init # get libgit2
make install-static

will compile libgit2, link it into git2go and install it. The main branch is set up to follow the specific libgit2 version that is vendored, so trying dynamic linking may or may not work depending on the exact versions involved.

In order to let Go pass the correct flags to pkg-config, -tags static needs to be passed to all go commands that build any binaries. For instance:

go build -tags static github.com/my/project/...
go test -tags static github.com/my/project/...
go install -tags static github.com/my/project/...

One thing to take into account is that since Go expects the pkg-config file to be within the same directory where make install-static was called, so the go.mod file may need to have a replace directive so that the correct setup is achieved. So if git2go is checked out at $GOPATH/src/github.com/libgit2/git2go and your project at $GOPATH/src/github.com/my/project, the go.mod file of github.com/my/project might need to have a line like

replace github.com/libgit2/git2go/v34 => ../../libgit2/git2go

Parallelism and network operations

libgit2 may use OpenSSL and LibSSH2 for performing encrypted network connections. For now, git2go asks libgit2 to set locking for OpenSSL. This makes HTTPS connections thread-safe, but it is fragile and will likely stop doing it soon. This may also make SSH connections thread-safe if your copy of libssh2 is linked against OpenSSL. Check libgit2's THREADSAFE.md for more information.

Running the tests

For the stable version, go test will work as usual. For the main branch, similarly to installing, running the tests requires building a local libgit2 library, so the Makefile provides a wrapper that makes sure it's built

make test-static

Alternatively, you can build the library manually first and then run the tests

make install-static
go test -v -tags static ./...

License

M to the I to the T. See the LICENSE file if you've never seen an MIT license before.

Authors

  • Carlos Martín (@carlosmn)
  • Vicent Martí (@vmg)