func TestListRefs(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) } ref := util.SHA160([]byte("random")) err = db.PutRef("myrepo", "refs/heads/master", ref) if err != nil { t.Errorf("error putting repository ref: %v", err) } err = db.PutRef("myrepo", "refs/heads/next", ref) if err != nil { t.Errorf("error putting repository ref: %v", err) } refs, err := db.ListRefs("myrepo") if err != nil { t.Errorf("error listing repository refs: %v", err) } assert.Equal(t, refs, []string{"refs/heads/master", "refs/heads/next"}) }
func TestReferenceUpdate(t *testing.T) { txn := NewReferenceUpdate("my-repository", "refs/heads/master", util.SHA160([]byte("random")), util.SHA160([]byte("not random"))) assert.True(t, txn.Valid()) txn2 := txn txn2.Version = 100 assert.False(t, txn2.Valid()) txn2 = txn txn2.Repository = "" assert.False(t, txn2.Valid()) txn2 = txn txn2.Ref = "" assert.False(t, txn2.Valid()) }
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) }
func (o *Commit) Hash() []byte { return util.SHA160(ObjectToBytes(o)) }
func (o *Blob) Hash() []byte { return util.SHA160(ObjectToBytes(o)) }
func TestReferenceUpdateEncodingDecoding(t *testing.T) { txn := NewReferenceUpdate("my-repository", "refs/heads/master", util.SHA160([]byte("random")), util.SHA160([]byte("not random"))) testTransactionEncodingDecoding(t, txn) }
func ReadPackfile(r io.Reader) (*Packfile, error) { buffer, err := ioutil.ReadAll(r) contentChecksum := util.SHA160(buffer[0 : len(buffer)-20]) r = bytes.NewBuffer(buffer) magic := make([]byte, 4) r.Read(magic) if bytes.Compare(magic, []byte("PACK")) != 0 { return nil, errors.New("not a packfile") } packfile := &Packfile{offsets: make(map[int]int), hashes: make(map[string]int)} var objects uint32 binary.Read(r, binary.BigEndian, &packfile.Version) binary.Read(r, binary.BigEndian, &objects) content, err := ioutil.ReadAll(r) if err != nil { return nil, err } offset := 12 for i := 0; i < int(objects); i++ { peReader := &packEntryReader{reader: bytes.NewBuffer(content)} err := readEntry(packfile, peReader, offset) if err != nil { return packfile, err } packfile.offsets[offset] = len(packfile.Objects) - 1 offset += peReader.Counter + 4 content = content[peReader.Counter+4:] } var unresolvedDeltas []Delta for i := range packfile.Deltas { ref := packfile.ObjectByHash(packfile.Deltas[i].Hash) if ref == nil { unresolvedDeltas = append(unresolvedDeltas, packfile.Deltas[i]) } else { patched := PatchDelta(ref.Bytes(), packfile.Deltas[i].Delta) newObject := ref.New() err = newObject.SetBytes(patched) if err != nil { return packfile, err } packfile.Objects = append(packfile.Objects, newObject) } } packfile.Deltas = unresolvedDeltas packfile.Checksum = make([]byte, 20) bytes.NewBuffer(content).Read(packfile.Checksum) if bytes.Compare(contentChecksum, packfile.Checksum) != 0 { return packfile, errors.New(fmt.Sprintf("checksum mismatch: expected %x got %x", packfile.Checksum, contentChecksum)) } return packfile, nil }