Git Library for Go
As a Pet Project, I Want to Develop a Note Taking App Using Git as Storage Backend. (I Suspect This Doesn't Exist Yet, Given This Guy's Blog Post: ) Now, I'd...
As a pet project, I want to develop a note taking app using git as storage backend. (I suspect this doesn't exist yet, given this guy's blog post: )
Now, I'd like to take this as an opportunity to play around with Go a bit. However, I cannot seem to find any (not even the tiniest approach to) git library for Go. Is there actually any?
Obviously my knowledge of Go is non-existant, so writing bindings for libgit doesn't seem a fun way to start... (and I would probably resort to ruby, which I don't know either)
7 Answers
Since a few years ago, my team and I, we were coding a pure git implementation in Go, it avoids to have any c/c++ dependency and make it more flexible and easy to extend.
go-git aims to reach the completeness of libgit2 or jgit, nowadays covers the majority of the plumbing read operations and some of the main write operations, but lacks the main porcelain operations such as merges.
What Victor proposed is indeed the "official" way to "script" Git as envisioned by its developers. Git's commands are divided in the two broad groups specifically for this purpose: the "plumbing" commands are low-level and inteneded mostly to be used by other programs; the "porcelain" command are intended to interact with the user, and call plumbing commands to do their work. Look inside the /usr/lib/git-core directory (might be different on your system) to get the idea of how many plumbing commands Git has.
On the other hand, Go supports linking with shared libraries via its cgo facility. You hence could try wrapping libgit2 with it. AFAIK, libgit2 is not yet fully on par with the Git itself, but it is able to read/write Git repositories, do branching etc — supposedly it will be enough for your task.
Okay, after I wrote all that, I scrolled down the "Bindings" entry on the libgit2's site and found go-git...
A search for "git" on GoDoc turns up some projects. There's a libgit2 wrapper, and at the bottom is an unfinished Git implementation in Go.
use git as storage backend
Then the Go library fluxcd/go-git-providers can come in handy.
go-git-providersis a general-purpose Go client for interacting with Git providers' APIs (e.g. GitHub, GitLab, Bitbucket).
Example:
package github_test
import (
"context"
"fmt"
""
""
gogithub ""
)
func ExampleOrgRepositoriesClient_Get() {
// Create a new client
ctx := context.Background()
c, err := github.NewClient()
checkErr(err)
// Parse the URL into an OrgRepositoryRef
ref, err := gitprovider.ParseOrgRepositoryURL("")
checkErr(err)
// Get public information about the flux repository.
repo, err := c.OrgRepositories().Get(ctx, *ref)
checkErr(err)
// Use .Get() to aquire a high-level gitprovider.OrganizationInfo struct
repoInfo := repo.Get()
// Cast the internal object to a *gogithub.Repository to access custom data
internalRepo := repo.APIObject().(*gogithub.Repository)
fmt.Printf("Description: %s. Homepage: %s", *repoInfo.Description, internalRepo.GetHomepage())
// Output: Description: The GitOps Kubernetes operator. Homepage:
}