Пример #1
0
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 = repository.EmptyRef() // return no error because there were no repositories saved
			return
		}
		bucket = dbtx.Bucket(append([]byte("refs"), []byte(name)...))
		if bucket == nil {
			h = repository.EmptyRef() // return no error because there were no repositories saved
			return
		}
		h = bucket.Get([]byte(ref))
		if h == nil {
			h = repository.EmptyRef()
		}
	})
	return
}
Пример #2
0
func TestPutGetRef(t *testing.T) {

	db, err := NewDB("test.db")
	defer os.Remove("test.db")

	if err != nil {
		t.Errorf("error opening database: %v", err)
	}

	repo := repository.NewRepository("myrepo", repository.ACTIVE, types.EmptyHash())
	err = db.PutRepository(repo)
	if err != nil {
		t.Errorf("error putting repository: %v", err)
	}

	// before the ref is set...
	ref0, err := db.GetRef("myrepo", "refs/heads/master")
	if err != nil {
		t.Errorf("error getting repository ref: %v", err)
	}
	assert.True(t, ref0.Equals(repository.EmptyRef()))

	ref := util.SHA160([]byte("random"))
	err = db.PutRef("myrepo", "refs/heads/master", ref)
	if err != nil {
		t.Errorf("error putting repository ref: %v", err)
	}
	ref1, err := db.GetRef("myrepo", "refs/heads/master")
	if err != nil {
		t.Errorf("error getting repository ref: %v", err)
	}
	if ref1 == nil {
		t.Errorf("error getting repository ref `refs/heads/master'")
	}
	assert.True(t, bytes.Compare(ref, ref1) == 0)
}