Improve code readability #1 #3

Merged
mpeczkowski merged 2 commits from readability_refactor_1 into main 2024-06-16 13:59:06 +00:00
3 changed files with 27 additions and 24 deletions
Showing only changes of commit 29d40969c2 - Show all commits

View File

@ -54,8 +54,8 @@ func findNumberOfCommitDiffs(srcCommit *object.Commit, dstCommit *object.Commit)
return diffRange, false return diffRange, false
} }
baseCommitHashes := []string{} var baseCommitHashes []string
destCommitHashes := []string{} var destCommitHashes []string
// Try to find all differences, limit only to five last changes to avoid reading whole repository at once // Try to find all differences, limit only to five last changes to avoid reading whole repository at once
for { for {

View File

@ -1,6 +1,7 @@
package commands package commands
import ( import (
"errors"
"fmt" "fmt"
"gitlab.com/revalus/grm/internal/config" "gitlab.com/revalus/grm/internal/config"
@ -26,15 +27,14 @@ const (
func fetchRepository(repo *git.Repository) (bool, error) { func fetchRepository(repo *git.Repository) (bool, error) {
err := repo.Fetch(&git.FetchOptions{}) err := repo.Fetch(&git.FetchOptions{})
if err == git.NoErrAlreadyUpToDate { switch {
case errors.Is(err, git.NoErrAlreadyUpToDate):
return false, nil return false, nil
case errors.Is(err, git.NoErrAlreadyUpToDate):
return false, nil
default:
return true, nil
} }
if err != nil && err != git.NoErrAlreadyUpToDate {
return false, err
}
return true, nil
} }
func cloneRepository(destPath string, repoCfg *config.RepositoryConfig) (bool, error) { func cloneRepository(destPath string, repoCfg *config.RepositoryConfig) (bool, error) {
@ -62,22 +62,24 @@ func (s Synchronizer) Command(repoCfg config.RepositoryConfig) CommandStatus {
destPath := fmt.Sprintf("%v/%v", s.workspace, repoCfg.Dest) destPath := fmt.Sprintf("%v/%v", s.workspace, repoCfg.Dest)
repo, err := git.PlainOpen(destPath) repo, err := git.PlainOpen(destPath)
if err != nil && err == git.ErrRepositoryNotExists { if err != nil {
cmdStatus.Changed, err = cloneRepository(destPath, &repoCfg) if errors.Is(err, git.ErrRepositoryNotExists) {
cmdStatus.Message = syncCloned cmdStatus.Changed, err = cloneRepository(destPath, &repoCfg)
} else if err == nil { cmdStatus.Message = syncCloned
cmdStatus.Changed, err = fetchRepository(repo)
if cmdStatus.Changed {
cmdStatus.Message = syncFetched
} else { } else {
cmdStatus.Message = syncUpToDate cmdStatus.Error = true
cmdStatus.Message = err.Error()
} }
} else {
cmdStatus.Error = true
cmdStatus.Message = err.Error()
return cmdStatus return cmdStatus
} }
cmdStatus.Changed, err = fetchRepository(repo)
if cmdStatus.Changed {
cmdStatus.Message = syncFetched
} else {
cmdStatus.Message = syncUpToDate
}
if err != nil { if err != nil {
cmdStatus.Error = true cmdStatus.Error = true
cmdStatus.Message = err.Error() cmdStatus.Message = err.Error()

View File

@ -1,6 +1,7 @@
package config package config
import ( import (
"errors"
"fmt" "fmt"
"os" "os"
"reflect" "reflect"
@ -101,9 +102,9 @@ repositories:
`) `)
_, err := GetRepositoryConfig(exampleWrongYamlConfig, "yaml") _, err := GetRepositoryConfig(exampleWrongYamlConfig, "yaml")
expectedError := fmt.Sprintf(errMissingSrcField, 0) expectedError := errors.New(fmt.Sprintf(errMissingSrcField, 0))
if err.Error() != expectedError { if errors.Is(err, expectedError) {
t.Errorf("Expected to get error with value %v, instead of this got: %v", expectedError, err.Error()) t.Errorf("Expected to get error with value %v, instead of this got: %v", expectedError, err.Error())
} }
} }
@ -127,7 +128,7 @@ repositories:
} }
expectedError := getDuplicateFieldError("name", "example2", []int{1, 2}) expectedError := getDuplicateFieldError("name", "example2", []int{1, 2})
if err.Error() != expectedError.Error() { if errors.Is(err, expectedError) {
t.Errorf("Expected to get error with value %v, instead of this got: %v", expectedError.Error(), err.Error()) t.Errorf("Expected to get error with value %v, instead of this got: %v", expectedError.Error(), err.Error())
} }
} }
@ -151,7 +152,7 @@ repositories:
expectedError := getDuplicateFieldError("dest", "example", []int{1, 2}) expectedError := getDuplicateFieldError("dest", "example", []int{1, 2})
if err.Error() != expectedError.Error() { if errors.Is(err, expectedError) {
t.Errorf("Expected to get error with value \"%v\", instead of this got: \"%v\"", expectedError, err) t.Errorf("Expected to get error with value \"%v\", instead of this got: \"%v\"", expectedError, err)
} }
} }