reworking database, init and add commands

This commit is contained in:
2020-07-02 15:00:30 -04:00
parent 6379c73e38
commit 5af55ed62e
15 changed files with 220 additions and 295 deletions

View File

@@ -13,7 +13,6 @@ import (
"github.com/deranjer/gvc/common/database"
"github.com/rs/zerolog"
"golang.org/x/net/context"
)
// NewManager creates a new manager interface that contains all the needed information to make changes to the repo
@@ -30,9 +29,10 @@ func NewManager(rootDir string, version string, dbPath string, informer chan Ope
ThumbFolder: dirPaths.ThumbFolder,
DiffFolder: dirPaths.ObjectFolder,
}
gvcDB, err := database.OpenOrCreateDB(dbPath, log)
gvcDB, err := database.OpenDB(dbPath, log, version)
if err != nil {
log.Fatal().Msgf("unable to create or open db: %s", err)
log.Err(err).Msgf("unable to create or open db: %s", err)
return nil, err
}
var wg *sync.WaitGroup
m := Manager{
@@ -49,16 +49,16 @@ func NewManager(rootDir string, version string, dbPath string, informer chan Ope
return &m, nil
}
// CheckPaths just checks the .gvc folder structure
func CheckPaths(rootDir string) (filePaths *FilePaths, err error) {
func generatePaths(rootDir string) (filePaths *FilePaths, err error) {
var fullFilePaths FilePaths
// checking for the .gvc folder (the client (but not the server) already checks for the .gvc folder, but this checks all subdirects to make sure they are there)
rootFolder, err := filepath.Abs(rootDir)
if err != nil {
return &FilePaths{}, err
return &fullFilePaths, err
}
path := rootFolder + string(filepath.Separator) + ".gvc"
//path = filepath.Join(rootFolder, filepath.Separator+".gvc")
var fullFilePaths FilePaths
//where private and public keys are kept
fullFilePaths.KeyFolder = filepath.Join(path, "keys")
//where downloaded files start
@@ -73,7 +73,45 @@ func CheckPaths(rootDir string) (filePaths *FilePaths, err error) {
fullFilePaths.LogFolder = filepath.Join(path, "logs")
//where plugins are stored
fullFilePaths.PluginFolder = filepath.Join(path, "plugins")
return &fullFilePaths, nil
}
// CheckPaths just checks the .gvc folder structure
func CheckPaths(rootDir string) (filePaths *FilePaths, err error) {
fullFilePaths, err := generatePaths(rootDir)
if err != nil {
return nil, fmt.Errorf("unable to generate paths, err: %s", err)
}
if _, err := os.Stat(fullFilePaths.DownloadFolder); os.IsNotExist(err) {
return nil, err
}
if _, err := os.Stat(fullFilePaths.KeyFolder); os.IsNotExist(err) {
return nil, err
}
if _, err := os.Stat(fullFilePaths.LogFolder); os.IsNotExist(err) {
return nil, err
}
if _, err := os.Stat(fullFilePaths.ObjectFolder); os.IsNotExist(err) {
return nil, err
}
if _, err := os.Stat(fullFilePaths.PluginFolder); os.IsNotExist(err) {
return nil, err
}
if _, err := os.Stat(fullFilePaths.SyncFolder); os.IsNotExist(err) {
return nil, err
}
if _, err := os.Stat(fullFilePaths.ThumbFolder); os.IsNotExist(err) {
return nil, err
}
return fullFilePaths, nil
}
// CreatePaths creates the paths needed in the .gvc folder
func CreatePaths(rootDir string) error {
fullFilePaths, err := generatePaths(rootDir)
if err != nil {
return fmt.Errorf("unable to generate file paths.. %s", err)
}
InitiateDirectory(fullFilePaths.KeyFolder)
InitiateDirectory(fullFilePaths.DownloadFolder)
InitiateDirectory(fullFilePaths.SyncFolder)
@@ -81,7 +119,7 @@ func CheckPaths(rootDir string) (filePaths *FilePaths, err error) {
InitiateDirectory(fullFilePaths.ThumbFolder)
InitiateDirectory(fullFilePaths.LogFolder)
InitiateDirectory(fullFilePaths.PluginFolder)
return &fullFilePaths, nil
return nil
}
// This adds a file for the watcher to keep an eye on
@@ -164,18 +202,29 @@ func (m *Manager) prepareDatabaseForFile(tmpFile database.File) (int, error) {
}
// BeginCommit starts the commit process
func (m *Manager) BeginCommit(branch string, commitMessage string) error {
trackedFiles, err := m.FetchTrackedFiles()
fmt.Println("Beginning Commit on Branch: ", branch)
trackedFiles, err := m.dB.RetrieveTrackedFiles()
if err != nil {
return err
}
if len(trackedFiles) == 0 {
return fmt.Errorf("no files show as tracked in repo, cannot commit, aborting...")
}
var filesToDiff []database.File // Contains the list of files that have changed
for _, trackedFile := range trackedFiles {
fmt.Println("Working on file: ", trackedFile.Path)
if trackedFile.Path == "" {
fmt.Println("No filepath found for file: ", trackedFile.Name)
continue
}
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 := UniqueFileHash(trackedFile.Path)
if err != nil {
fmt.Printf("unable to create hash for file: %s error: %s\n", currentFile.Name(), err)
@@ -188,18 +237,19 @@ func (m *Manager) BeginCommit(branch string, commitMessage string) error {
}
filesToDiff = append(filesToDiff, trackedFile)
}
diffChannel := make(chan database.DiffObject)
diffContext := context.Background()
m.WaitGroup.Add(2)
//diffChannel := make(chan database.DiffObject)
//diffContext := context.Background()
//m.WaitGroup.Add(2)
commit, err := m.dB.FetchLastCommitOnBranch(branch)
if err != nil {
m.Info().Msgf("unable to fetch last commit on branch, assuming first commit on branch", err)
err := CreateInitialCommit(filesToDiff, commitMessage)
err := m.CreateInitialCommit(filesToDiff, commitMessage)
if err != nil {
m.Err(err).Msgf("unable to create initial commit: %s", err)
return err
}
}
fmt.Println("COMMIT: ", commit.CommitHash)
return nil
}
@@ -207,12 +257,3 @@ func (m *Manager) FetchCommitByNumber(branch string, commitNumber string) error
return nil
}
// FetchTrackedFiles just grabbes all the files currently tracked in the repo
func (m *Manager) FetchTrackedFiles() ([]database.File, error) {
files, err := m.dB.RetrieveTrackedFiles()
if err != nil {
return nil, fmt.Errorf("unable to retrieve tracked files: %s", err)
}
return files, nil
}