Permalink
Browse files

Commit Lookup implemented

revwalk Push,Next implemented

Basic tests for the above functions
  • Loading branch information...
1 parent 3430a3a commit 411ade436c85afdb03ad5be5a5afe7a927d0ae57 str1ngs committed Mar 19, 2011
Showing with 93 additions and 55 deletions.
  1. +20 −0 .gitignore
  2. +1 −3 Makefile
  3. +18 −8 git.go
  4. +54 −44 git_test.go
View
@@ -0,0 +1,20 @@
+*.[568ao]
+*.ao
+*.so
+*.pyc
+._*
+.nfs.*
+[568a].out
+*~
+*.orig
+*.rej
+*.exe
+.*.swp
+core
+*.cgo*.go
+*.cgo*.c
+_cgo_*
+_obj
+_test
+_testmain.go
+
View
@@ -15,7 +15,7 @@ LDFLAGS=`pkg-config --libs libgit2`
CLEANFILES+=defs.go ./tmp ctest
-.PHONY: libgit2 libgit2clean
+.PHONY: libgit2
include $(GOROOT)/src/Make.pkg
@@ -32,6 +32,4 @@ defs.go: defs.c
format: *.go
gofmt -l -w *.go
-all: test
-
defs.go: defs.c
View
26 git.go
@@ -29,10 +29,6 @@ func (v *Repo) Open(path string) (err os.Error) {
return
}
-func (v *Repo) Lookup(c *Commit, o *Oid, mask int) {
- //C.git_repository_lookup(&c.git_commit, v.git_repo, o.git_oid, (C.git_otype)(mask))
-}
-
func (v *Repo) Free() {
C.git_repository_free(v.git_repo)
}
@@ -46,11 +42,20 @@ func (v *Repo) Init(path string, isbare uint8) (err os.Error) {
return
}
+
// Commit
type Commit struct {
git_commit *C.git_commit
}
+func (c *Commit) Lookup(r *Repo, o *Oid) (err os.Error) {
+ if e := C.git_commit_lookup(&c.git_commit,r.git_repo,o.git_oid); e != GIT_SUCCESS{
+ es := fmt.Sprintf("commit lookup failed CODE %v", path, e)
+ return os.NewError(es)
+ }
+ return err
+}
+
func (c *Commit) Msg() string {
msg := C.git_commit_message(c.git_commit)
return C.GoString(msg)
@@ -102,14 +107,19 @@ func (v *RevWalk) Reset() {
C.git_revwalk_reset(v.git_revwalk)
}
-func (v *RevWalk) Push(c *Commit) {
- C.git_revwalk_push(v.git_revwalk, c.git_commit)
+
+func (v *RevWalk) Push(o *Oid) {
+ C.git_revwalk_push(v.git_revwalk, o.git_oid)
}
-func (v *RevWalk) Next(c *Commit) {
- C.git_revwalk_next(&c.git_commit, v.git_revwalk)
+func (v *RevWalk) Next(o *Oid) (err os.Error){
+ if ecode := C.git_revwalk_next(o.git_oid,v.git_revwalk); ecode != GIT_SUCCESS {
+ return os.NewError(fmt.Sprintf("RevWalk.Next error CODE %v",ecode))
+ }
+ return err
}
+
//TODO: implement this
func (v *RevWalk) Sorting(sm uint) {
}
View
@@ -2,7 +2,7 @@ package git
import (
"exec"
- "io/ioutil"
+ "encoding/line"
"os"
"strings"
"testing"
@@ -13,13 +13,16 @@ var (
revwalk *RevWalk
path string
head string
+ author string
ref = &Reference{}
)
func init() {
path = "./tmp"
+ author = "schizoid29@gmail.com"
}
+// Repo
func TestInitBare(t *testing.T) {
repo = &Repo{}
if err := repo.Init(path, BARE); err != nil {
@@ -53,21 +56,26 @@ func TestOpenNotBare(t *testing.T) {
//FIXME: only fork calls till we have proper boiler plate
func TestCommit(t *testing.T) {
var (
- err os.Error
cmd *exec.Cmd
)
tmpfile := "README"
- err = ioutil.WriteFile("./tmp/"+tmpfile, []byte("test\n"), 0644)
-
+ f, err := os.Open(path+"/"+tmpfile, os.O_CREAT|os.O_RDWR|os.O_APPEND, 0644)
+ _, err = f.WriteString("foo\n")
+ f.Close()
+ if err != nil {
+ println(err.String())
+ os.Exit(1)
+ }
cmd, err = run("git add " + tmpfile)
cmd.Close()
cmd, err = run("git commit -m test")
cmd.Close()
cmd, err = run("git log --pretty=%H")
- buf, _ := ioutil.ReadAll(cmd.Stdout)
- head = strings.Trim(string(buf), "\n")
+ r := line.NewReader(cmd.Stdout, 256)
+ h, _, _ := r.ReadLine()
+ head = (string(h))
println(head)
cmd.Close()
@@ -76,16 +84,16 @@ func TestCommit(t *testing.T) {
}
}
-func run(s string) (cmd *exec.Cmd, err os.Error) {
- wd := "./tmp/"
- args := strings.Split(s, " ", -1)
- bin, err := exec.LookPath(args[0])
-
- cmd, err = exec.Run(bin, args, os.Environ(), wd, exec.DevNull, exec.Pipe, exec.PassThrough)
-
- return
+//FIXME: change this to a bench and not use forks
+func TestManyCommits(t *testing.T) {
+ for i := 0; i < 29; i++ {
+ TestCommit(t)
+ }
}
+//Commit
+
+//Ref
func TestRefLookup(t *testing.T) {
var err os.Error
refpath := "refs/heads/master"
@@ -102,49 +110,51 @@ func TestRefLookup(t *testing.T) {
}
}
-/*
-func TestRefGetOid(t *testing.T) {
- oid := ref.GetOid()
- oid.String()
-}
-*/
-//Important: this must be called after all of the Test functions
-func TestFinal(t *testing.T) {
- if revwalk != nil {
- revwalk.Free()
- }
- if repo != nil {
- repo.Free()
+// RevWalk
+func TestNewRevWalk(t *testing.T) {
+ var err os.Error
+ revwalk, err = NewRevWalk(repo)
+ if err != nil {
+ t.Fatal("Error:", err)
}
}
-/* Not implimented yet
-
-func TestLookup(t *testing.T) {
- c := &Commit{}
- o, _ := NewOid(oid)
- repo.Lookup(c, o, GIT_OBJ_ANY)
- if c.Author() != author {
- t.Fatal("ERROR:",os.NewError("Lookup failed"))
+func TestRevWalkNext(t *testing.T) {
+ o, _ := NewOid(head)
+ revwalk.Push(o)
+ if err := revwalk.Next(o); err != nil {
+ t.Fatal("Error:", err)
+ }
+ if o.String() != head {
+ t.Errorf("oid string should match %v is %v", head, o.String())
}
}
+//Oid
func TestNewOid(t *testing.T) {
- if _, err := NewOid(oid); err != nil {
+ if _, err := NewOid(head); err != nil {
t.Error(err)
}
}
-func TestNewRevWalk(t *testing.T) {
- var err os.Error
- revwalk, err = NewRevWalk(repo)
- if err != nil {
- t.Fatal("ERROR:", err)
+//Important: this must be called after all of the Test functions
+func TestFinal(t *testing.T) {
+ if revwalk != nil {
+ revwalk.Free()
+ }
+ if repo != nil {
+ repo.Free()
}
}
-*/
+//private helper functions
+
+func run(s string) (cmd *exec.Cmd, err os.Error) {
+ wd := "./tmp/"
+ args := strings.Split(s, " ", -1)
+ bin, err := exec.LookPath(args[0])
+
+ cmd, err = exec.Run(bin, args, os.Environ(), wd, exec.DevNull, exec.Pipe, exec.PassThrough)
-func TestTest(t *testing.T) {
- test()
+ return
}

0 comments on commit 411ade4

Please sign in to comment.