Skip to content

Commit

Permalink
Introduce repository.Ref type
Browse files Browse the repository at this point in the history
  • Loading branch information
yrashk committed May 22, 2014
1 parent b42422f commit 691fd28
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
11 changes: 5 additions & 6 deletions db/repository.go
Expand Up @@ -3,7 +3,6 @@ package db
import (
"github.com/boltdb/bolt"
"github.com/gitchain/gitchain/repository"
"github.com/gitchain/gitchain/types"
)

func (db *T) PutRepository(repo *repository.T) (e error) {
Expand Down Expand Up @@ -94,7 +93,7 @@ func (db *T) ListPendingRepositories() (keys []string) {
return
}

func (db *T) PutRef(name, ref string, new types.Hash) (e error) {
func (db *T) PutRef(name, ref string, new repository.Ref) (e error) {
writable(&e, db, func(dbtx *bolt.Tx) bool {
bucket, e := dbtx.CreateBucketIfNotExists([]byte("repositories"))
if e != nil {
Expand All @@ -111,21 +110,21 @@ func (db *T) PutRef(name, ref string, new types.Hash) (e error) {
return
}

func (db *T) GetRef(name, ref string) (h types.Hash, e error) {
func (db *T) GetRef(name, ref string) (h repository.Ref, e error) {
readable(&e, db, func(dbtx *bolt.Tx) {
bucket := dbtx.Bucket([]byte("repositories"))
if bucket == nil {
h = types.EmptyHash() // return no error because there were no repositories saved
h = repository.EmptyRef() // return no error because there were no repositories saved
return
}
bucket = dbtx.Bucket(append([]byte("refs"), []byte(name)...))
if bucket == nil {
h = types.EmptyHash() // return no error because there were no repositories saved
h = repository.EmptyRef() // return no error because there were no repositories saved
return
}
h = bucket.Get([]byte(ref))
if h == nil {
h = types.EmptyHash()
h = repository.EmptyRef()
}
})
return
Expand Down
2 changes: 1 addition & 1 deletion db/repository_test.go
Expand Up @@ -124,7 +124,7 @@ func TestPutGetRef(t *testing.T) {
if err != nil {
t.Errorf("error getting repository ref: %v", err)
}
assert.True(t, bytes.Compare(ref0, types.EmptyHash()) == 0)
assert.True(t, ref0.Equals(repository.EmptyRef()))

ref := util.SHA160([]byte("random"))
err = db.PutRef("myrepo", "refs/heads/master", ref)
Expand Down
10 changes: 10 additions & 0 deletions repository/repository.go
Expand Up @@ -12,6 +12,8 @@ const (
ACTIVE = 1
)

type Ref []byte

type T struct {
Name string
Status int
Expand All @@ -36,3 +38,11 @@ func Decode(encoded []byte) (*T, error) {
err := enc.Decode(&t)
return &t, err
}

func EmptyRef() Ref {
return make([]byte, 20)
}

func (r Ref) Equals(r1 Ref) bool {
return bytes.Compare(r, r1) == 0
}

0 comments on commit 691fd28

Please sign in to comment.