Ejemplo n.º 1
0
// saveTree saves a tree of fake files in the repo and returns the ID.
func saveTree(t testing.TB, repo *repository.Repository, seed int64) backend.ID {
	rnd := rand.NewSource(seed)
	numNodes := int(rnd.Int63() % 64)
	t.Logf("create %v nodes", numNodes)

	var tree Tree
	for i := 0; i < numNodes; i++ {
		seed := rnd.Int63() % maxSeed
		size := rnd.Int63() % maxFileSize

		node := &Node{
			Name: fmt.Sprintf("file-%v", seed),
			Type: "file",
			Mode: 0644,
			Size: uint64(size),
		}

		node.Content = saveFile(t, repo, fakeFile(t, seed, size))
		tree.Nodes = append(tree.Nodes, node)
	}

	id, err := repo.SaveJSON(pack.Tree, tree)
	if err != nil {
		t.Fatal(err)
	}

	return id
}
Ejemplo n.º 2
0
// saveTreeJSON stores a tree in the repository.
func saveTreeJSON(repo *repository.Repository, item interface{}) (backend.ID, error) {
	data, err := json.Marshal(item)
	if err != nil {
		return backend.ID{}, err
	}
	data = append(data, '\n')

	// check if tree has been saved before
	id := backend.Hash(data)
	if repo.Index().Has(id) {
		return id, nil
	}

	return repo.SaveJSON(pack.Tree, item)
}