Skip to content

Commit

Permalink
index: Get method
Browse files Browse the repository at this point in the history
IndexEntry:  added with some Methods to return values
  • Loading branch information
str1ngs committed May 19, 2011
1 parent 7bbc73e commit e7216ed
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 20 deletions.
1 change: 0 additions & 1 deletion pkg/git/defs.c
Expand Up @@ -20,4 +20,3 @@ enum
};

typedef struct git_time $GitTime;
typedef struct git_index_entry $IndexEntry;
16 changes: 0 additions & 16 deletions pkg/git/defs.go
Expand Up @@ -28,19 +28,3 @@ type GitTime struct {
Offset int32
Pad_godefs_0 [4]byte
}

type IndexEntry struct {
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
Flags_extended uint16
Path *int8
}
30 changes: 29 additions & 1 deletion pkg/git/git.go
Expand Up @@ -288,7 +288,7 @@ func (v *Reference) Type() {
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 nil, os.NewError("GetOid Failed: unable to get Oid for reference")
}
return &Oid{oid}, nil
}
Expand Down Expand Up @@ -333,10 +333,38 @@ func (v *Index) EntryCount() int {
return int(C.git_index_entrycount(v.git_index))
}

func (v *Index) Get(n int) (*IndexEntry, os.Error) {
p := C.git_index_get(v.git_index, C.int(n))
if p == nil {
estring := fmt.Sprintf("Index %v not found, total index is %v", n, v.EntryCount())
return nil, os.NewError(estring)
}
//entry := (*IndexEntry)(unsafe.Pointer(p))
return &IndexEntry{p}, nil
}

func (v *Index) Free() {
C.git_index_free(v.git_index)
}

// IndexEntry

type IndexEntry struct {
index_entry *C.git_index_entry
}

func (i *IndexEntry) Oid() *Oid {
return &Oid{&i.index_entry.oid}
}

func (i *IndexEntry) Path() string {
return C.GoString(i.index_entry.path)
}

func (i *IndexEntry) Flags() int {
return int(i.index_entry.flags)
}

//TODO: its possible we can use godef to generate this struct
// Signature
type Signature struct {
Expand Down
24 changes: 22 additions & 2 deletions pkg/git/git_test.go
Expand Up @@ -95,6 +95,27 @@ func TestIndexEntryCount(t *testing.T) {
}
}

func TestIndexGet(t *testing.T) {
path := "README"
flags := 6
index := new(Index)
defer index.Free()
err := index.Open(repo)
check(t, err)
err = index.Read()
check(t, err)
entry, err := index.Get(0)
if err != nil {
t.Fatal(err)
}
if entry.Path() != path {
t.Errorf("Expected Entry Path %v, got %v", path, index.EntryCount())
}
if entry.Flags() != flags {
t.Errorf("Expected Entry Flags %v, got %v", flags, index.EntryCount())
}
}

// Commit
func TestCommit(t *testing.T) {
TestIndexAdd(t)
Expand Down Expand Up @@ -151,7 +172,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 Down Expand Up @@ -218,7 +239,6 @@ func TestTreeEntryByName(t *testing.T) {
}
}


func TestInvalidTreeEntryByName(t *testing.T) {
expected := "README.does-not-exist"
_, err := tree.EntryByName(expected)
Expand Down

0 comments on commit e7216ed

Please sign in to comment.