Permalink
Browse files

initial commit

  • Loading branch information...
0 parents commit 81504fbed0061567bc054f66cd145b707428c614 str1ngs committed Mar 4, 2011
Showing with 946 additions and 0 deletions.
  1. +674 −0 COPYING
  2. +16 −0 LICENSE
  3. +23 −0 Makefile
  4. +119 −0 git.go
  5. +20 −0 git_defs.c
  6. +94 −0 git_test.go
674 COPYING

Large diffs are not rendered by default.

Oops, something went wrong.
16 LICENSE
@@ -0,0 +1,16 @@
+ go-git Go bindings to libgit2
+
+ Copyright (C) 2011 Mike Rosset mike.rosset@gmail.com
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
@@ -0,0 +1,23 @@
+include $(GOROOT)/src/Make.inc
+
+TARG=go-git
+
+CGOFILES=git.go
+
+GOFILES=defs.go
+
+CGO_CFLAGS:=`pkg-config --cflags libgit2`
+
+CGO_LDFLAGS=`pkg-config --libs libgit2`
+
+CLEANFILES+=defs.go
+
+include $(GOROOT)/src/Make.pkg
+
+defs.go: git_defs.c
+ godefs -g git git_defs.c > defs.go
+
+format:
+ gofmt -l -w *.go
+
+all: format test defs.go
119 git.go
@@ -0,0 +1,119 @@
+package git
+
+/*
+#include <git2.h>
+*/
+import "C"
+import (
+ "fmt"
+ "os"
+)
+
+const (
+ NOTBARE = iota
+ BARE
+)
+
+/* Repo */
+type Repo struct {
+ git_repo *C.git_repository
+}
+
+func (v *Repo) Open(path string) (err os.Error) {
+ if C.git_repository_open(&v.git_repo, C.CString(path)) != 0 {
+ err = os.NewError("failed to open " + path)
+ }
+ 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)
+}
+
+func (v *Repo) Init(path string, isbare uint8) (err os.Error) {
+ if i := C.git_repository_init(&v.git_repo, C.CString(path), C.uint(isbare)); i != 0 {
+ e := fmt.Sprintf("failed to init %v CODE %v", path, i)
+ println(e)
+ return os.NewError(e)
+ }
+ return
+}
+
+// Commit
+type Commit struct {
+ git_commit *C.git_commit
+}
+
+func (c *Commit) Msg() string {
+ msg := C.git_commit_message(c.git_commit)
+ return C.GoString(msg)
+}
+
+func (c *Commit) Author() string {
+ p := C.git_commit_author(c.git_commit)
+ return C.GoString(p.name)
+}
+func (c *Commit) Email() string {
+ p := C.git_commit_author(c.git_commit)
+ return C.GoString(p.email)
+}
+
+// Oid
+type Oid struct {
+ git_oid *C.git_oid
+ String string
+}
+
+func NewOid(s string) (*Oid, os.Error) {
+ o := &Oid{new(C.git_oid), s}
+ if C.git_oid_mkstr(o.git_oid, C.CString(s)) == GIT_ENOTOID {
+ return nil, os.NewError("could not create new oid")
+ }
+ return o, nil
+}
+
+// RevWalk
+type RevWalk struct {
+ git_revwalk *C.git_revwalk
+}
+
+func NewRevWalk(repo *Repo) (*RevWalk, os.Error) {
+ r := &RevWalk{}
+ if C.git_revwalk_new(&r.git_revwalk, repo.git_repo) != 0 {
+ return nil, os.NewError("could not create new RevWalk")
+ }
+ return r, nil
+}
+
+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) Next(c *Commit) {
+ C.git_revwalk_next(&c.git_commit, v.git_revwalk)
+}
+
+//TODO: implement this
+func (v *RevWalk) Sorting(sm uint) {
+}
+
+func (v *RevWalk) Free() {
+ C.git_revwalk_free(v.git_revwalk)
+}
+
+func test() {
+ printT(BARE)
+ printT(NOTBARE)
+}
+
+func printT(i interface{}) {
+ fmt.Printf("%T = %v\n", i, i)
+}
@@ -0,0 +1,20 @@
+#include <git2.h>
+
+enum
+{
+ $GIT_OBJ_ANY = GIT_OBJ_ANY,
+ $GIT_OBJ_BAD = GIT_OBJ_BAD,
+ $GIT_OBJ__EXT1 = GIT_OBJ__EXT1,
+ $GIT_OBJ_BLOB = GIT_OBJ_BLOB,
+ $GIT_OBJ_COMMIT = GIT_OBJ_COMMIT,
+ $GIT_OBJ_OFS_DELTA = GIT_OBJ_OFS_DELTA,
+ $GIT_OBJ_REF_DELTA = GIT_OBJ_REF_DELTA,
+ $GIT_OBJ_TAG = GIT_OBJ_TAG,
+ $GIT_OBJ_TREE = GIT_OBJ_TREE,
+ $GIT_OBJ__EXT2 = GIT_OBJ__EXT2,
+
+ $GIT_ENOTOID = GIT_ENOTOID
+};
+
+typedef struct git_signature $Signature;
+typedef struct git_time $GitTime;
@@ -0,0 +1,94 @@
+package git
+
+import "os"
+import "testing"
+
+const (
+ oid = "0b1f85e2daba25df3b2070b554c6464c780d0f9b"
+ author = "str1ngs"
+)
+
+var (
+ repo *Repo
+ revwalk *RevWalk
+ path string
+ fatal bool = false
+ calls int = 0
+)
+
+func init() {
+ path, _ = os.Getwd()
+ path += "/tmp"
+ repo = &Repo{}
+}
+
+func TestInit(t *testing.T) {
+ if fatal {
+ return
+ }
+ if err := repo.Init(path, NOTBARE); err != nil {
+ fatal = true
+ t.Error(err)
+ }
+}
+
+func TestOpen(t *testing.T) {
+ if fatal {
+ return
+ }
+ err := repo.Open(path + "/.git")
+ if err != nil {
+ fatal = true
+ println("We can not test with out a working Repo")
+ println(err.String())
+ }
+}
+
+func TestNewOid(t *testing.T) {
+ if fatal {
+ return
+ }
+ if _, err := NewOid(oid); err != nil {
+ fatal = true
+ t.Error(err)
+ }
+}
+
+func TestLookup(t *testing.T) {
+ if fatal {
+ return
+ }
+ c := &Commit{}
+ o, _ := NewOid(oid)
+ repo.Lookup(c, o, GIT_OBJ_ANY)
+ if c.Author() != author {
+ fatal = true
+ t.Error(os.NewError("Lookup failed"))
+ }
+}
+
+func TestNewRevWalk(t *testing.T) {
+ var err os.Error
+ if fatal {
+ return
+ }
+ revwalk, err = NewRevWalk(repo)
+ if err != nil {
+ fatal = true
+ t.Error(err)
+ }
+}
+
+//Important: this must be called after all of the Test functions
+func TestFinal(t *testing.T) {
+ if fatal {
+ return
+ }
+ revwalk.Free()
+ repo.Free()
+}
+
+func TestTest(t *testing.T) {
+
+ test()
+}

0 comments on commit 81504fb

Please sign in to comment.