refactor: isolate expensive IsDirty() call
Some checks failed
continuous-integration/drone/push Build is failing

See #689
This commit is contained in:
2025-10-03 20:35:09 +02:00
parent 2460dd9438
commit d0ccb805c6
4 changed files with 14 additions and 14 deletions

View File

@ -644,6 +644,9 @@ func (a App) WriteRecipeVersion(version string, dryRun bool) error {
scanner = bufio.NewScanner(file)
)
// NOTE(d1): don't care at this point if there is a git failure
isDirty, _ := a.Recipe.IsDirty()
for scanner.Scan() {
line := scanner.Text()
if !strings.HasPrefix(line, "RECIPE=") && !strings.HasPrefix(line, "TYPE=") {
@ -656,7 +659,7 @@ func (a App) WriteRecipeVersion(version string, dryRun bool) error {
continue
}
if strings.Contains(line, version) && !a.Recipe.Dirty && !strings.HasSuffix(line, config.DIRTY_DEFAULT) {
if strings.Contains(line, version) && !isDirty && !strings.HasSuffix(line, config.DIRTY_DEFAULT) {
skipped = true
lines = append(lines, line)
continue
@ -669,16 +672,16 @@ func (a App) WriteRecipeVersion(version string, dryRun bool) error {
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
return err
}
if a.Recipe.Dirty && dirtyVersion != "" {
if isDirty && dirtyVersion != "" {
version = dirtyVersion
}
if !dryRun {
if err := os.WriteFile(a.Path, []byte(strings.Join(lines, "\n")), os.ModePerm); err != nil {
log.Fatal(err)
return err
}
} else {
log.Debug(i18n.G("skipping writing version %s because dry run", version))

View File

@ -305,6 +305,7 @@ func (r *Recipe) ChaosVersion() (string, error) {
if err != nil {
return "", err
}
if dirty {
return fmt.Sprintf("%s%s", version, config.DIRTY_DEFAULT), nil
}

View File

@ -15,7 +15,12 @@ func TestIsDirty(t *testing.T) {
t.Fatal(err)
}
assert.False(t, r.Dirty)
isDirty, err := r.IsDirty()
if err != nil {
t.Fatal(err)
}
assert.False(t, isDirty)
fpath := filepath.Join(r.Dir, "foo.txt")
f, err := os.Create(fpath)

View File

@ -13,7 +13,6 @@ import (
"strings"
"coopcloud.tech/abra/pkg/i18n"
"github.com/go-git/go-git/v5"
"coopcloud.tech/abra/pkg/catalogue"
"coopcloud.tech/abra/pkg/config"
@ -170,12 +169,6 @@ func Get(name string) Recipe {
AbraShPath: path.Join(dir, "abra.sh"),
}
dirty, err := r.IsDirty()
if err != nil && !errors.Is(err, git.ErrRepositoryNotExists) {
log.Fatal(i18n.G("failed to check git status of %s: %s", r.Name, err))
}
r.Dirty = dirty
return r
}
@ -183,7 +176,6 @@ type Recipe struct {
Name string
EnvVersion string
EnvVersionRaw string
Dirty bool // NOTE(d1): git terminology for unstaged changes
Dir string
GitURL string
SSHURL string
@ -198,7 +190,6 @@ type Recipe struct {
func (r Recipe) String() string {
out := i18n.G("{name: %s, ", r.Name)
out += i18n.G("version : %s, ", r.EnvVersion)
out += i18n.G("dirty: %v, ", r.Dirty)
out += i18n.G("dir: %s, ", r.Dir)
out += i18n.G("git url: %s, ", r.GitURL)
out += i18n.G("ssh url: %s, ", r.SSHURL)