Skip to content

Golang library for programmatically working with the git command (via the os/exec package).

License

Notifications You must be signed in to change notification settings

mergestat/gitutils

Repository files navigation

Go Reference CI Test Suite Go Report Card codecov

gitutils

This is a Golang library for programmatically working with the git command (via the os/exec package). In general, the options for working with git repositories in Go are:

  • go-git is a git implementation written in pure Go
  • git2go are the Golang C-bindings to the libgit2 project (requires CGO)
  • Shelling out to the git command (using the os/exec package) and parsing results

This library uses the 3rd option (shelling out to git) and provides an abstraction layer to simplify using the output of various git subcommands.

Examples

Cloning a repo

package main

import (
	"context"
	"log"
	"os"

	"github.com/mergestat/gitutils/clone"
)

func main() {
	err := clone.Exec(context.Background(), "https://github.com/mergestat/gitutils", "some-dir")
	if err != nil {
		log.Fatal(err)
	}
}

Walking the Commit Log

package main

import (
	"context"
	"errors"
	"fmt"
	"io"
	"log"
	"os"

	"github.com/mergestat/gitutils/gitlog"
)

func main() {
	iter, err := gitlog.Exec(context.TODO(), "/path/to/some/local/repo", gitlog.WithStats(false))
	if err != nil {
		log.Fatal(err)
	}

	for {
		if commit, err := iter.Next(); err != nil {
			if errors.Is(err, io.EOF) {
				break
			}
			log.Fatal(err)
		} else {
			fmt.Println(commit.SHA)
		}
	}
}

See more examples in the examples directory.

About

Golang library for programmatically working with the git command (via the os/exec package).

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 3

  •  
  •  
  •