Skip to content

Commit

Permalink
fix: remove buggy inner loop from git dirty check (#117)
Browse files Browse the repository at this point in the history
This change removes an inner loop introduced in `CheckDirDirty` that was
not correctly skipping over files we should be ignoring when doing dirty
checks of a working directory.
  • Loading branch information
disintegrator authored Apr 23, 2024
1 parent ca00e47 commit 95d7dd9
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 4 deletions.
1 change: 1 addition & 0 deletions internal/git/fixtures/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This folder is the base of git repositories created in tests.
3 changes: 3 additions & 0 deletions internal/git/fixtures/nested/names.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
foo
bar
baz
1 change: 1 addition & 0 deletions internal/git/fixtures/sample.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
another boring file
10 changes: 6 additions & 4 deletions internal/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"path/filepath"
"regexp"
"runtime"
"slices"
"strings"
"time"

Expand Down Expand Up @@ -115,10 +116,11 @@ func (g *Git) CheckDirDirty(dir string, ignoreChangePatterns map[string]string)
filesToIgnore := []string{"gen.yaml", "gen.lock", "workflow.yaml", "workflow.lock"}

for f, s := range status {
for _, fileToIgnore := range filesToIgnore {
if strings.Contains(f, fileToIgnore) {
continue
}
shouldSkip := slices.ContainsFunc(filesToIgnore, func(fileToIgnore string) bool {
return strings.Contains(f, fileToIgnore)
})
if shouldSkip {
continue
}

if strings.HasPrefix(f, cleanedDir) {
Expand Down
107 changes: 107 additions & 0 deletions internal/git/git_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package git

import (
"fmt"
"io"
"io/fs"
"os"
"path/filepath"
"testing"
"time"

"github.com/go-git/go-billy/v5"
"github.com/go-git/go-billy/v5/memfs"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing/object"
"github.com/go-git/go-git/v5/storage/memory"
"github.com/stretchr/testify/require"
)

func newTestRepo(t *testing.T) (*git.Repository, billy.Filesystem) {
t.Helper()

mfs := memfs.New()

err := filepath.WalkDir("./fixtures", func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}

if d.IsDir() {
return nil
}

fixture, err := os.Open(path)
if err != nil {
return err
}
defer fixture.Close()

f, err := mfs.Create(path)
if err != nil {
return err
}
defer f.Close()

_, err = io.Copy(f, fixture)
if err != nil {
return err
}

return nil
})
require.NoError(t, err, "expected to walk the fixture directory")

storage := memory.NewStorage()
repo, err := git.Init(storage, mfs)
require.NoError(t, err, "expected empty repo to be initialized")

wt, err := repo.Worktree()
require.NoError(t, err, "expected to get worktree")

_, err = wt.Add(".")
require.NoError(t, err, "expected to add all files")

_, err = wt.Commit("initial commit", &git.CommitOptions{
Author: &object.Signature{
Name: "Test User",
Email: "test@example.com",
When: time.Unix(0, 0),
},
})
require.NoError(t, err, "expected to commit all files")

return repo, mfs
}

func TestGit_CheckDirDirty(t *testing.T) {
repo, mfs := newTestRepo(t)

f, err := mfs.Create("dirty-file")
require.NoError(t, err, "expected to create a dirty file")
defer f.Close()
fmt.Fprintln(f, "sample content")

g := Git{repo: repo}
dirty, str, err := g.CheckDirDirty(".", map[string]string{})
require.NoError(t, err, "expected to check the directory")

require.Equal(t, `new file found: []string{"dirty-file"}`, str)
require.True(t, dirty, "expected the directory to be dirty")
}

func TestGit_CheckDirDirty_IgnoredFiles(t *testing.T) {
repo, mfs := newTestRepo(t)

f, err := mfs.Create("workflow.lock")
require.NoError(t, err, "expected to create a dirty file")
defer f.Close()
fmt.Fprintln(f, "sample content")

g := Git{repo: repo}
dirty, str, err := g.CheckDirDirty(".", map[string]string{})
require.NoError(t, err, "expected to check the directory")

require.Equal(t, "", str, "expected no dirty files reported")
require.False(t, dirty, "expected the directory to be clean")
}

0 comments on commit 95d7dd9

Please sign in to comment.