Improve code readability #2

+ Improve syntax
This commit is contained in:
Mikołaj Pęczkowski 2024-06-16 15:28:12 +02:00
parent 68636d5e64
commit 29d40969c2
3 changed files with 27 additions and 24 deletions

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
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) {
@ -62,22 +62,24 @@ 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 {
cmdStatus.Changed, err = cloneRepository(destPath, &repoCfg)
cmdStatus.Message = syncCloned
} else if err == nil {
cmdStatus.Changed, err = fetchRepository(repo)
if cmdStatus.Changed {
cmdStatus.Message = syncFetched
if err != nil {
if errors.Is(err, git.ErrRepositoryNotExists) {
cmdStatus.Changed, err = cloneRepository(destPath, &repoCfg)
cmdStatus.Message = syncCloned
} else {
cmdStatus.Message = syncUpToDate
cmdStatus.Error = true
cmdStatus.Message = err.Error()
}
} 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
}
if err != nil {
cmdStatus.Error = true
cmdStatus.Message = err.Error()

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)
}
}