Skip to content

Instantly share code, notes, and snippets.

@Narven
Forked from michael-k/git-unsorted-log.go
Created May 19, 2021 17:14
Show Gist options
  • Save Narven/ff30008be4b2747c7616afba2b311723 to your computer and use it in GitHub Desktop.
Save Narven/ff30008be4b2747c7616afba2b311723 to your computer and use it in GitHub Desktop.
An example of howto use git2go (https://github.com/libgit2/git2go) which is a libgit2 (https://libgit2.github.com/) bindings package for golang.
/*
requires libgit2
*/
package main
import (
"github.com/libgit2/git2go"
"fmt"
"log"
"flag"
"strings"
)
type Blob struct {
*git.Blob
id *git.Oid
}
func main(){
repoPath := flag.String("repo", "~/git/testrepo", "path to the git repository")
flag.Parse()
repo, err := git.OpenRepository(*repoPath)
if err != nil {
log.Fatal(err)
}
fmt.Println(repo)
odb, err := repo.Odb()
if err != nil {
log.Fatal(err)
}
err = odb.ForEach(func(oid *git.Oid) error {
obj, err := repo.Lookup(oid)
if err != nil {
return err
}
switch obj := obj.(type) {
default:
case *git.Blob:
break
fmt.Printf("==================================\n")
fmt.Printf("obj: %s\n", obj)
fmt.Printf("Type: %s\n", obj.Type())
fmt.Printf("Id: %s\n", obj.Id())
fmt.Printf("Size: %s\n", obj.Size())
case *git.Commit:
fmt.Printf("==================================\n")
fmt.Printf("obj: %s\n", obj)
fmt.Printf("Type: %s\n", obj.Type())
fmt.Printf("Id: %s\n", obj.Id())
author := obj.Author()
fmt.Printf(" Author:\n Name: %s\n Email: %s\n Date: %s\n", author.Name, author.Email, author.When)
committer := obj.Committer()
fmt.Printf(" Committer:\n Name: %s\n Email: %s\n Date: %s\n", committer.Name, committer.Email, committer.When)
fmt.Printf(" ParentCount: %s\n", int(obj.ParentCount()))
fmt.Printf(" TreeId: %s\n", obj.TreeId())
fmt.Printf(" Message:\n\n %s\n\n", strings.Replace(obj.Message(),"\n","\n ", -1))
//fmt.Printf("obj.Parent: %s\n", obj.Parent())
//fmt.Printf("obj.ParentId: %s\n", obj.ParentId())
//fmt.Printf("obj.Tree: %s\n", obj.Tree())
case *git.Tree:
break
fmt.Printf("==================================\n")
fmt.Printf("obj: %s\n", obj)
fmt.Printf("Type: %s\n", obj.Type())
fmt.Printf("Id: %s\n", obj.Id())
fmt.Printf(" EntryCount: %s\n", obj.EntryCount())
}
return nil
})
if err != nil {
log.Fatal("Lookup:", err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment