switching everything over to []byte, abandoning merkletree for now
This commit is contained in:
@@ -24,7 +24,7 @@ import (
|
||||
// it might be nice to inform the user when diffs build up
|
||||
func manageFileDiffing(ctx context.Context, target, diffobject, commitHashPath string, diffChannel chan database.DiffObject, wg *sync.WaitGroup) error {
|
||||
|
||||
var targetHash, diffobjectHash [16]byte
|
||||
var targetHash, diffobjectHash []byte
|
||||
var err error
|
||||
if targetHash, err = UniqueFileHash(target); err != nil {
|
||||
return err
|
||||
|
||||
@@ -1,14 +1,18 @@
|
||||
package engine
|
||||
|
||||
import (
|
||||
"github.com/kalafut/imohash"
|
||||
"crypto/sha256"
|
||||
"io/ioutil"
|
||||
)
|
||||
|
||||
// UniqueFileHash creats a fast hash of a file. It's not bullet proof (could cause a collision, but in practice unlikely) but its fast
|
||||
func UniqueFileHash(src string) ([16]byte, error) {
|
||||
hash, err := imohash.SumFile(src)
|
||||
// UniqueFileHash uses SHA256 to create a hash of the file
|
||||
func UniqueFileHash(src string) ([]byte, error) {
|
||||
file, err := ioutil.ReadFile(src)
|
||||
if err != nil {
|
||||
return [16]byte{}, err
|
||||
return []byte{}, err
|
||||
}
|
||||
hasher := sha256.New()
|
||||
hasher.Write(file)
|
||||
hash := hasher.Sum(nil)
|
||||
return hash, nil
|
||||
}
|
||||
|
||||
@@ -3,12 +3,15 @@ package engine
|
||||
import (
|
||||
"bytes"
|
||||
"compress/gzip"
|
||||
"crypto/sha256"
|
||||
"encoding/binary"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
clientconfig "github.com/deranjer/gvc/client/clientconfig"
|
||||
"github.com/deranjer/gvc/common/database"
|
||||
)
|
||||
|
||||
// CompressIntArray compresses an array of integers into a buffer
|
||||
@@ -89,8 +92,29 @@ func InitiateDirectory(directory string) {
|
||||
}
|
||||
|
||||
// CreateInitialCommit copies the files over and compresses them if they are not in the NoCompress struct
|
||||
func CreateInitialCommit(conf *clientconfig.Gvcconfig) {
|
||||
func CreateInitialCommit(fileList []database.File, commitMessage string) error { // ONLY HAPPENS FOR MASTER I THINK, SO NO BRANCH NEEDED
|
||||
//Need to deduplicate so we aren't storing duplicates of files, storing all the files in one folder won't work, will need something like git
|
||||
//For initial commit no changes are made to files, so don't store anything, just save the list so you can send to server
|
||||
var initialCommit database.Commit
|
||||
initialCommit.Branch = "master"
|
||||
|
||||
//var hashList [][]byte
|
||||
hasher := sha256.New()
|
||||
for _, file := range fileList {
|
||||
var err error
|
||||
err = file.CalculateHash()
|
||||
if err != nil {
|
||||
return fmt.Errorf("unable to calculate hash for file: %s with error: %s", file.Path, err)
|
||||
}
|
||||
hasher.Write(file.Hash[:])
|
||||
}
|
||||
time := time.Now() // Adding the metadata to the hash
|
||||
hasher.Write([]byte(commitMessage + time.String()))
|
||||
hashBytes := hasher.Sum(nil) // Getting the hash bytes
|
||||
fullHash := hex.EncodeToString(hashBytes)
|
||||
fmt.Println("Commit hash: ", fullHash)
|
||||
initialCommit.CommitHash = hashBytes
|
||||
return nil
|
||||
}
|
||||
|
||||
func IsDirectory(path string) (bool, error) {
|
||||
|
||||
Reference in New Issue
Block a user