switching everything over to []byte, abandoning merkletree for now

This commit is contained in:
2020-07-01 14:33:01 -04:00
parent e4ac7f70e6
commit 07bbb442ef
12 changed files with 215 additions and 82 deletions

View File

@@ -1,40 +1,13 @@
package clientcmd
import (
"fmt"
"os"
clientconfig "github.com/deranjer/gvc/client/clientconfig"
"github.com/deranjer/gvc/common/database"
"github.com/deranjer/gvc/common/engine"
"github.com/deranjer/gvc/common/manager"
)
// Commit commits the tracked files and changes to the repo
func Commit(conf *clientconfig.Gvcconfig, commitMessage string, m *manager.Manager) error {
trackedFiles, err := m.FetchTrackedFiles()
if err != nil {
return err
}
var filesToDiff []database.File // Contains the list of files that have changed
for _, trackedFile := range trackedFiles {
currentFile, err := os.Stat(trackedFile.Path)
if err != nil {
fmt.Printf("unable to stat tracked file: %s error: %s\n", currentFile.Name(), err)
continue
}
currentFileHash, err := engine.UniqueFileHash(trackedFile.Path)
if err != nil {
fmt.Printf("unable to create hash for file: %s error: %s\n", currentFile.Name(), err)
continue
}
if currentFileHash == trackedFile.CurrentHash {
fmt.Printf("No changes found in file: %s when compared to file: %s\n", currentFile.Name(), trackedFile.Name)
continue
}
filesToDiff = append(filesToDiff, trackedFile)
}
m.BeginCommit(filesToDiff, conf.CurrentBranch)
m.BeginCommit(conf.CurrentBranch)
return nil
}

View File

@@ -1,27 +0,0 @@
package main
import (
"fmt"
"github.com/imdario/mergo"
)
type Foo struct {
Ignore []string
B int64
}
func main() {
src := Foo{
Ignore: []string{"one", "two", "three"},
B: 2,
}
dest := Foo{
Ignore: []string{"one", "two", "four", "seven"},
}
mergo.Merge(&dest, src)
fmt.Println(dest)
// Will print
// {two 2}
}

65
client/test/test.go.old Normal file
View File

@@ -0,0 +1,65 @@
package main
import (
"crypto/sha256"
"log"
"github.com/cbergoon/merkletree"
)
//TestContent implements the Content interface provided by merkletree and represents the content stored in the tree.
type TestContent struct {
x string
}
//CalculateHash hashes the values of a TestContent
func (t TestContent) CalculateHash() ([]byte, error) {
h := sha256.New()
if _, err := h.Write([]byte(t.x)); err != nil {
return nil, err
}
return h.Sum(nil), nil
}
//Equals tests for equality of two Contents
func (t TestContent) Equals(other merkletree.Content) (bool, error) {
return t.x == other.(TestContent).x, nil
}
func main() {
//Build list of Content to build tree
var list []merkletree.Content
list = append(list, TestContent{x: "Hello"})
list = append(list, TestContent{x: "Hi"})
list = append(list, TestContent{x: "Hey"})
list = append(list, TestContent{x: "Hola"})
//Create a new Merkle Tree from the list of Content
t, err := merkletree.NewTree(list)
if err != nil {
log.Fatal(err)
}
//Get the Merkle Root of the tree
mr := t.MerkleRoot()
log.Println(mr)
//Verify the entire tree (hashes for each node) is valid
vt, err := t.VerifyTree()
if err != nil {
log.Fatal(err)
}
log.Println("Verify Tree: ", vt)
//Verify a specific content in in the tree
vc, err := t.VerifyContent(list[0])
if err != nil {
log.Fatal(err)
}
log.Println("Verify Content: ", vc)
//String representation
log.Println(t)
}

40
client/test/test2.go Normal file
View File

@@ -0,0 +1,40 @@
package main
import (
"crypto/sha256"
"encoding/hex"
"fmt"
)
func main() {
sha1 := []byte("32254b975eb8013394f7f7f3cd90e09aebf4b4489e69150a3260be3a5a7a0562")
sha2 := []byte("22254b975eb8013395f7f7f3cd90e09aebf4b4489e69150a3260be3a5a7a0562")
bytes1 := sha256.Sum256(sha1)
bytes2 := sha256.Sum256(sha2)
var shaList [][32]byte
shaList = append(shaList, bytes1)
shaList = append(shaList, bytes2)
//var hashList [][]byte
hasher := sha256.New()
for _, file := range shaList {
hasher.Write(file[:])
}
commitMessage := "This is a commit message2!"
//time := time.Now()
//hasher.Write([]byte(commitMessage + time.String()))
hasher.Write([]byte(commitMessage))
//commitMeta := []byte(commitMessage + time.String()) //TODO add author and other things
//commitMetaHash := sha256.Sum256(commitMeta)
fmt.Println("Hashbytes: ", hasher.Sum(nil))
fullHash := hex.EncodeToString(hasher.Sum(nil))
fmt.Println("Hasher: ", fullHash)
// if _, err := hasher.Write([]byte(commitMessage + time.String())); err != nil { // Create a hash of the message and time
// return err
// }
//hashList = append(hashList, commitMetaHash) // add that to the tree
//testhash := hashList[:]
//commitHash := sha256.Sum256(hashList[:])
}