Skip to content

Instantly share code, notes, and snippets.

@apackeer
Forked from timhughes/git-unsorted-log.go
Last active August 29, 2015 14:27
Show Gist options
  • Save apackeer/8e3f64148b488ecc1b08 to your computer and use it in GitHub Desktop.
Save apackeer/8e3f64148b488ecc1b08 to your computer and use it in GitHub Desktop.
An example of howto user git2go https://github.com/libgit2/git2go which is a libgit2 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)
}
for oid := range odb.ForEach() {
obj, err := repo.Lookup(oid)
if err != nil {
log.Fatal("Lookup:", 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",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())
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment