Skip to content

Commit

Permalink
makefile: added LD_LIBRARY_PATH for local testing
Browse files Browse the repository at this point in the history
refs: stubbed out varous reference methods

fixed various CStrings that were not being released

test run() better handles exit status
  • Loading branch information
str1ngs committed May 19, 2011
1 parent 4f8f959 commit 62840ae
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 41 deletions.
3 changes: 2 additions & 1 deletion pkg/git/Makefile
@@ -1,10 +1,11 @@
include $(GOROOT)/src/Make.inc

export LD_LIBRARY_PATH:=/usr/local/lib

TARG=git

CGOFILES=git.go
GOFILES=defs.go

CLEANFILES+=./tmp

include $(GOROOT)/src/Make.pkg
Expand Down
2 changes: 2 additions & 0 deletions pkg/git/defs.c
Expand Up @@ -12,6 +12,8 @@ enum
$GIT_OBJ_TAG = GIT_OBJ_TAG,
$GIT_OBJ_TREE = GIT_OBJ_TREE,
$GIT_OBJ__EXT2 = GIT_OBJ__EXT2,

$GIT_REF_SYMBOLIC = GIT_REF_SYMBOLIC,

$GIT_SUCCESS = GIT_SUCCESS,
$GIT_ENOTOID = GIT_ENOTOID
Expand Down
11 changes: 7 additions & 4 deletions pkg/git/defs.go
Expand Up @@ -16,25 +16,28 @@ const (
GIT_OBJ_TAG = 0x4
GIT_OBJ_TREE = 0x2
GIT_OBJ__EXT2 = 0x5
GIT_REF_SYMBOLIC = 0x2
GIT_SUCCESS = 0
GIT_ENOTOID = -0x2
)

// Types

type GitTime struct {
Time int64
Offset int32
Time int64
Offset int32
Pad_godefs_0 [4]byte
}

type IndexEntry struct {
Ctime [12]byte /* git_index_time */
Mtime [12]byte /* git_index_time */
Ctime [16]byte /* git_index_time */
Mtime [16]byte /* git_index_time */
Dev uint32
Ino uint32
Mode uint32
Uid uint32
Gid uint32
Pad_godefs_0 [4]byte
File_size int64
Oid [20]byte /* git_oid */
Flags uint16
Expand Down
45 changes: 41 additions & 4 deletions pkg/git/git.go
Expand Up @@ -117,9 +117,11 @@ type Commit struct {

//TODO: do not use hardcoded update_ref
func CommitCreate(repo *Repo, tree, parent *Oid, author, commiter *Signature, message string) os.Error {
m := C.CString(message)
oid := NewOid()
m := C.CString(message)
defer C.free(unsafe.Pointer(m))
update_ref := C.CString("HEAD")
defer C.free(unsafe.Pointer(update_ref))
ecode := C.git_commit_create(
oid.git_oid,
repo.git_repo,
Expand Down Expand Up @@ -219,7 +221,10 @@ func GetHead(repo *Repo) (*Oid, os.Error) {
if err != nil {
return nil, err
}
head := ref.GetOid()
head, err := ref.GetOid()
if err != nil {
return nil, err
}
return head, nil
}

Expand Down Expand Up @@ -249,11 +254,34 @@ func (v *Reference) Lookup(r *Repo, name string) (err os.Error) {
if ecode < GIT_SUCCESS {
return LastError()
}
if v.git_reference == nil {
//return os.NewError("Reference Lookup: Failed to look up " + name)
}
return
}

func (v *Reference) GetOid() *Oid {
return &Oid{C.git_reference_oid(v.git_reference)}
func (v *Reference) SetTarget(target string) (err os.Error) {
ctarget := C.CString(target)
defer C.free(unsafe.Pointer(ctarget))
ecode := C.git_reference_set_target(v.git_reference, ctarget)
if ecode < GIT_SUCCESS {
return LastError()
}
return nil
}

func (v *Reference) Type() {
if C.git_reference_type(v.git_reference) == GIT_REF_SYMBOLIC {
println("THIS IS A SYMBOLIC REF")
}
}

func (v *Reference) GetOid() (*Oid, os.Error) {
oid := C.git_reference_oid(v.git_reference)
if oid == nil {
//return nil, os.NewError("GetOid Failed: unable to get Oid for reference")
}
return &Oid{oid}, nil
}

//Index
Expand Down Expand Up @@ -301,6 +329,10 @@ type Signature struct {
git_signature *C.git_signature
}

func (s Signature) Free() {
C.git_signature_free(s.git_signature)
}

func NewSignature(name, email string) *Signature {
n := C.CString(name)
e := C.CString(email)
Expand All @@ -320,3 +352,8 @@ func LastError() os.Error {
func printT(i interface{}) {
fmt.Printf("%T = %v\n", i, i)
}

func abort() {
fmt.Println("GOT ****************************** HERE")
os.Exit(0)
}
72 changes: 40 additions & 32 deletions pkg/git/git_test.go
Expand Up @@ -2,6 +2,7 @@ package git

import (
"exec"
"fmt"
"os"
"strings"
"testing"
Expand All @@ -13,7 +14,7 @@ var (
tree *Tree
path string
author string
ref = &Reference{}
ref = new(Reference)
)

func init() {
Expand Down Expand Up @@ -47,44 +48,31 @@ func TestInitNotBare(t *testing.T) {

func TestOpenNotBare(t *testing.T) {
err := repo.Open(path + "/.git")
if err != nil {
t.Fatal("Error:", err)
}
checkFatal(t, err)
}

//FIXME: figure out how to seed an intial HEAD
func TestSeed(t *testing.T) {
var (
cmd *exec.Cmd
)

tmpfile := "README"

f, err := os.Create(path + "/" + tmpfile)
_, 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()

if err != nil {
t.Fatal("Error:", err)
}
checkFatal(t, err)
err = run("git add " + tmpfile)
checkFatal(t, err)
err = run("git commit -m test")
checkFatal(t, err)
}

// Index
// Index
func TestIndexAdd(t *testing.T) {
index := new(Index)
defer index.Free()
err := index.Open(repo)
check(t, err)
tmpfile := "README"
f, err := os.OpenFile(path+"/"+tmpfile, os.O_CREATE|os.O_RDWR|os.O_APPEND, 0644)
check(t, err)
_, err = f.WriteString("foo\n")
f.Close()
err = index.Add(tmpfile)
Expand All @@ -102,14 +90,13 @@ func TestCommit(t *testing.T) {
check(t, err)
tree, err := TreeFromIndex(repo, index)
check(t, err)
head, _ := GetHeadString(repo)
parent, err := NewOidString(head)
head, err := GetHead(repo)
check(t, err)
s := NewSignature("Foo Bar", "foo@bar.com")
err = CommitCreate(repo, tree, parent, s, s, "some stuff here")
defer s.Free()
err = CommitCreate(repo, tree, head, s, s, "some stuff here")
check(t, err)
}

func TestManyCommits(t *testing.T) {
for i := 0; i < 29; i++ {
TestCommit(t)
Expand Down Expand Up @@ -150,7 +137,7 @@ func TestRevWalk(t *testing.T) {
c := new(Commit)
c.Lookup(repo, o)
// Output example
//fmt.Printf("%v %v %v %v\n", o.String(), c.Author(), c.Email(), c.Msg())
fmt.Printf("%v %v %v %v\n", o.String(), c.Author(), c.Email(), c.Msg())
}
}

Expand All @@ -168,6 +155,9 @@ func TestSignature(t *testing.T) {
NewSignature("foo", "bar")
}

func TestTSignature(t *testing.T) {
}

// Tree
func TestTreeFromIndex(t *testing.T) {
index := new(Index)
Expand Down Expand Up @@ -230,9 +220,11 @@ func TestTreeEntryCount(t *testing.T) {
}
}


// Important: this must be called after all of the Test functions
func TestFinal(t *testing.T) {
if tree != nil {
tree.Free()
}
if revwalk != nil {
revwalk.Free()
}
Expand All @@ -242,16 +234,32 @@ func TestFinal(t *testing.T) {
}

// private helper functions
func run(s string) (cmd *exec.Cmd, err os.Error) {
wd := "./tmp/"
func run(s string) (err os.Error) {
wd := path
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)

cmd, err := exec.Run(bin, args, os.Environ(), wd, exec.DevNull, exec.Pipe, exec.PassThrough)
if err != nil {
return err
}
w, err := cmd.Wait(0)
if err != nil {
return err
}
if !w.Exited() || w.ExitStatus() != 0 {
return os.NewError("failed to run " + s)
}
return
}

func checkFatal(t *testing.T, err os.Error) {
if err != nil {
fmt.Printf("Fatal: %T %v\n", t, err)
os.Exit(0)
}
}

func check(t *testing.T, err os.Error) {
if err != nil {
t.Error(err)
Expand Down

0 comments on commit 62840ae

Please sign in to comment.