Skip to content

Instantly share code, notes, and snippets.

@michael-k
Forked from timhughes/git-unsorted-log.go
Last active May 19, 2021 17:14
Show Gist options
  • Save michael-k/31cf86a57ff6da30e479 to your computer and use it in GitHub Desktop.
Save michael-k/31cf86a57ff6da30e479 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