Compare commits

..

1 Commits

Author SHA1 Message Date
9e8ccfe78e Improve code readability #1
Improve mostly common_utils_test.go
2024-06-13 21:19:04 +02:00
4 changed files with 25 additions and 28 deletions

View File

@ -54,8 +54,8 @@ func findNumberOfCommitDiffs(srcCommit *object.Commit, dstCommit *object.Commit)
return diffRange, false return diffRange, false
} }
var baseCommitHashes []string baseCommitHashes := []string{}
var destCommitHashes []string 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,7 +1,6 @@
package commands package commands
import ( import (
"errors"
"fmt" "fmt"
"gitlab.com/revalus/grm/internal/config" "gitlab.com/revalus/grm/internal/config"
@ -27,14 +26,15 @@ 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{})
switch { if err == git.NoErrAlreadyUpToDate {
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,23 +62,21 @@ 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 { if err != nil && err == git.ErrRepositoryNotExists {
if errors.Is(err, git.ErrRepositoryNotExists) {
cmdStatus.Changed, err = cloneRepository(destPath, &repoCfg) cmdStatus.Changed, err = cloneRepository(destPath, &repoCfg)
cmdStatus.Message = syncCloned cmdStatus.Message = syncCloned
} else { } else if err == nil {
cmdStatus.Error = true
cmdStatus.Message = err.Error()
}
return cmdStatus
}
cmdStatus.Changed, err = fetchRepository(repo) cmdStatus.Changed, err = fetchRepository(repo)
if cmdStatus.Changed { if cmdStatus.Changed {
cmdStatus.Message = syncFetched cmdStatus.Message = syncFetched
} else { } else {
cmdStatus.Message = syncUpToDate cmdStatus.Message = syncUpToDate
} }
} else {
cmdStatus.Error = true
cmdStatus.Message = err.Error()
return cmdStatus
}
if err != nil { if err != nil {
cmdStatus.Error = true cmdStatus.Error = true

View File

@ -1,7 +1,6 @@
package config package config
import ( import (
"errors"
"fmt" "fmt"
"os" "os"
"reflect" "reflect"
@ -102,9 +101,9 @@ repositories:
`) `)
_, err := GetRepositoryConfig(exampleWrongYamlConfig, "yaml") _, err := GetRepositoryConfig(exampleWrongYamlConfig, "yaml")
expectedError := errors.New(fmt.Sprintf(errMissingSrcField, 0)) expectedError := fmt.Sprintf(errMissingSrcField, 0)
if errors.Is(err, expectedError) { if err.Error() != 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())
} }
} }
@ -128,7 +127,7 @@ repositories:
} }
expectedError := getDuplicateFieldError("name", "example2", []int{1, 2}) expectedError := getDuplicateFieldError("name", "example2", []int{1, 2})
if errors.Is(err, expectedError) { if err.Error() != expectedError.Error() {
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())
} }
} }
@ -152,7 +151,7 @@ repositories:
expectedError := getDuplicateFieldError("dest", "example", []int{1, 2}) expectedError := getDuplicateFieldError("dest", "example", []int{1, 2})
if errors.Is(err, expectedError) { if err.Error() != expectedError.Error() {
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)
} }
} }

View File

@ -29,7 +29,7 @@ func TestGetFileExtension(t *testing.T) {
} }
func TestErrorInGetExtensionFile(t *testing.T) { func TestErrorInGetExcensionFile(t *testing.T) {
result, err := getFileExtension("test") result, err := getFileExtension("test")