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
}
baseCommitHashes := []string{}
destCommitHashes := []string{}
var baseCommitHashes []string
var destCommitHashes []string
// Try to find all differences, limit only to five last changes to avoid reading whole repository at once
for {

View File

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

View File

@ -1,6 +1,7 @@
package config
import (
"errors"
"fmt"
"os"
"reflect"
@ -101,9 +102,9 @@ repositories:
`)
_, 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())
}
}
@ -127,7 +128,7 @@ repositories:
}
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())
}
}
@ -151,7 +152,7 @@ repositories:
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)
}
}