Last active
April 28, 2017 09:51
-
-
Save timhughes/9925692 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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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
Thank you for this example, it helped me a lot. But due to a change in git2go (libgit2/git2go@2594f3f) it's broken. Line 38 should be replaced by somethink like this (+ closing parenthesis and return statement)