working on the commit system
This commit is contained in:
		@@ -36,6 +36,7 @@ func (m *Manager) CreateInitialCommit(fileList []database.File, branch string, c
 | 
			
		||||
	for _, file := range fileList {
 | 
			
		||||
		filename, err := ConvertFileForStorage(&file, folder)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			fmt.Println("Failure converting file for storage! ", file)
 | 
			
		||||
			// TODO: roll back commit on error (folders/files created need to be deleted)
 | 
			
		||||
			return err
 | 
			
		||||
		}
 | 
			
		||||
@@ -51,7 +52,7 @@ func (m *Manager) CreateInitialCommit(fileList []database.File, branch string, c
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// CreateCommit creates a new commit by running diffs on files, compressing, etc, as needed
 | 
			
		||||
func (m *Manager) CreateCommit(changedFiles []database.File, trackedFiles []database.File, branch string, commitMessage string, commitNumber int) error {
 | 
			
		||||
func (m *Manager) CreateCommit(changedFiles, trackedFiles, newFiles []database.File, branch string, commitMessage string, commitNumber int) error {
 | 
			
		||||
	m.Info().Msgf("Starting commit number: %d on branch: %s", commitNumber, branch)
 | 
			
		||||
	//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
 | 
			
		||||
@@ -63,7 +64,7 @@ func (m *Manager) CreateCommit(changedFiles []database.File, trackedFiles []data
 | 
			
		||||
	}
 | 
			
		||||
	currentTime := time.Now()
 | 
			
		||||
	newCommit.CommitHash = hashBytes
 | 
			
		||||
	newCommit.Number = 1
 | 
			
		||||
	newCommit.Number = commitNumber
 | 
			
		||||
	newCommit.TrackedFiles = trackedFiles
 | 
			
		||||
	newCommit.Date = currentTime.String()
 | 
			
		||||
	folder := m.FilePaths.ObjectFolder + string(filepath.Separator) + hex.EncodeToString(hashBytes)
 | 
			
		||||
@@ -71,13 +72,20 @@ func (m *Manager) CreateCommit(changedFiles []database.File, trackedFiles []data
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return fmt.Errorf("unable to create commit directory in object dir: %s err: %s", folder, err)
 | 
			
		||||
	}
 | 
			
		||||
	// TODO: Do a full file copy every 5 commits
 | 
			
		||||
	m.Info().Msgf("Total number of files to commit: %d", len(trackedFiles))
 | 
			
		||||
	fmt.Println("Total number of files to commit: ", len(trackedFiles))
 | 
			
		||||
	// TODO: CHeck for NEW Files and convert to starage, not changed files
 | 
			
		||||
	for _, file := range changedFiles {
 | 
			
		||||
		m.Info().Msgf("Converting changed fi")
 | 
			
		||||
	// Find NEW files that need to be converted for storage
 | 
			
		||||
	var changedandNewFiles []database.File
 | 
			
		||||
	changedandNewFiles = append(changedFiles, newFiles...)
 | 
			
		||||
 | 
			
		||||
	for _, file := range changedandNewFiles {
 | 
			
		||||
		m.Info().Msgf("Converting changed file: %s", file.Name)
 | 
			
		||||
		filename, err := ConvertFileForStorage(&file, folder)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			fmt.Println("Error converting changed file! ", file.Name)
 | 
			
		||||
			m.Info().Msgf("error converting file for storage: %s err: %s", file.Name, err)
 | 
			
		||||
			// TODO: roll back commit on error (folders/files created need to be deleted)
 | 
			
		||||
			return err
 | 
			
		||||
		}
 | 
			
		||||
 
 | 
			
		||||
@@ -19,12 +19,12 @@ type SomeStruct struct {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// CompressFile uses pgzip to compress a file for storage
 | 
			
		||||
func CompressFile(path string) error {
 | 
			
		||||
func CompressFile(path string, newPath string) error {
 | 
			
		||||
	fileBytes, err := ioutil.ReadFile(path)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return fmt.Errorf("unable to read file: %s", path)
 | 
			
		||||
	}
 | 
			
		||||
	file, err := os.Create(path)
 | 
			
		||||
	file, err := os.Create(newPath)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
 
 | 
			
		||||
@@ -69,8 +69,8 @@ func ExpandToIntArray(length int64, arry []byte, intArray *[]int64) error {
 | 
			
		||||
 | 
			
		||||
// ConvertFileForStorage opens the original file and dumps the bytes to pgzip to compress it for storage
 | 
			
		||||
func ConvertFileForStorage(file *database.File, folder string) (fileName string, err error) {
 | 
			
		||||
	filename := folder + string(filepath.Separator) + file.Name //fileName is under the object folder then the hash, then the filename
 | 
			
		||||
	err = CompressFile(filename)
 | 
			
		||||
	filename := folder + string(filepath.Separator) + file.Name //fileName (for compressed object) is under the object folder then the hash, then the filename
 | 
			
		||||
	err = CompressFile(file.Path, filename)                     // Path to the original file so we can compress, and the expected new compressed file
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return filename, err
 | 
			
		||||
	}
 | 
			
		||||
 
 | 
			
		||||
@@ -210,11 +210,11 @@ func (m *Manager) BeginCommit(branch string, commitMessage string) error {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
	if len(trackedFiles) == 0 {
 | 
			
		||||
		return fmt.Errorf("no files show as tracked in repo, cannot commit, aborting...")
 | 
			
		||||
		return fmt.Errorf("no files show as tracked in repo, cannot commit, aborting")
 | 
			
		||||
	}
 | 
			
		||||
	commit, err := m.dB.FetchLastCommitOnBranch(branch) //Check for previous commits
 | 
			
		||||
	lastCommit, err := m.dB.FetchLastCommitOnBranch(branch) //Check for previous commits
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		m.Info().Msgf("unable to fetch last commit on branch, assuming first commit on branch", err)
 | 
			
		||||
		m.Info().Msgf("unable to fetch last commit on branch, assuming first commit on branch: %s", err)
 | 
			
		||||
		err := m.CreateInitialCommit(trackedFiles, branch, commitMessage) // Create the initial commit
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			m.Err(err).Msgf("unable to create initial commit: %s", err)
 | 
			
		||||
@@ -223,7 +223,7 @@ func (m *Manager) BeginCommit(branch string, commitMessage string) error {
 | 
			
		||||
		m.Info().Msgf("Initial Commit Created, returning...")
 | 
			
		||||
		return nil
 | 
			
		||||
	}
 | 
			
		||||
	var filesToDiff []database.File // Contains the list of files that have changed since the initial commit
 | 
			
		||||
	var changedFiles []database.File // Contains the list of files that have changed since the initial commit
 | 
			
		||||
	for _, trackedFile := range trackedFiles {
 | 
			
		||||
		fmt.Println("Working on file: ", trackedFile.Path)
 | 
			
		||||
		if trackedFile.Path == "" {
 | 
			
		||||
@@ -245,19 +245,32 @@ func (m *Manager) BeginCommit(branch string, commitMessage string) error {
 | 
			
		||||
			fmt.Printf("No changes found in file: %s when compared to file: %s\n", currentFile.Name(), trackedFile.Name)
 | 
			
		||||
			continue
 | 
			
		||||
		}
 | 
			
		||||
		filesToDiff = append(filesToDiff, trackedFile)
 | 
			
		||||
		changedFiles = append(changedFiles, trackedFile)
 | 
			
		||||
	}
 | 
			
		||||
	var newFiles []database.File // Comparing old tracked files with current to find new tracked files
 | 
			
		||||
	for _, oldFile := range lastCommit.TrackedFiles {
 | 
			
		||||
		for _, newFile := range trackedFiles {
 | 
			
		||||
			if oldFile.Path == newFile.Path {
 | 
			
		||||
				continue
 | 
			
		||||
			} else {
 | 
			
		||||
				newFiles = append(newFiles, newFile)
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	m.Info().Msgf("Number of new Files: %d", len(newFiles))
 | 
			
		||||
	//diffChannel := make(chan database.DiffObject)
 | 
			
		||||
	//diffContext := context.Background()
 | 
			
		||||
	//m.WaitGroup.Add(2)
 | 
			
		||||
	fmt.Println("Changed Files: ", filesToDiff)
 | 
			
		||||
	if len(filesToDiff) == 0 {
 | 
			
		||||
	for _, changedFile := range changedFiles {
 | 
			
		||||
		fmt.Println("Changed File: ", changedFile.Path)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if len(changedFiles) == 0 {
 | 
			
		||||
		m.Info().Msgf("No changed files found to commit on branch: %s", branch)
 | 
			
		||||
		return fmt.Errorf("no changed files, cannot commit on branch: %s", branch)
 | 
			
		||||
	}
 | 
			
		||||
	fmt.Println("COMMIT: ", hex.EncodeToString(commit.CommitHash))
 | 
			
		||||
	newCommitNumber := commit.Number + 1
 | 
			
		||||
	err = m.CreateCommit(filesToDiff, trackedFiles, branch, commitMessage, newCommitNumber)
 | 
			
		||||
	newCommitNumber := lastCommit.Number + 1
 | 
			
		||||
	err = m.CreateCommit(changedFiles, trackedFiles, newFiles, branch, commitMessage, newCommitNumber)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user