Skip to content

Commit

Permalink
Fix: Display only unique changes (#8)
Browse files Browse the repository at this point in the history
Co-authored-by: Vee <[email protected]>
  • Loading branch information
dpatsora and xenirio authored Jan 10, 2022
1 parent 9fd976b commit f3f7fd2
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 19 deletions.
40 changes: 21 additions & 19 deletions cmd/goci/changes.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,33 +28,35 @@ func actionChanges() error {
return err
}

var changedFiles []string
var paths []string

for _, stat := range stats {
changedFiles = append(changedFiles, stat.Name)
paths = append(paths, stat.Name)
}

if Depth == 0 {
fmt.Printf("%s\n", strings.Join(changedFiles, " "))
} else {
var res []string
keys := make(map[string]int)
if Depth > 0 {
paths = filterUnique(paths, Depth)
}
fmt.Printf("%s\n", strings.Join(paths, " "))

for _, f := range changedFiles {
if _, ok := keys[f]; !ok {
keys[f] = 1
filepath := strings.Split(f, "/")
return nil
}

if len(filepath) >= Depth {
filepath = filepath[0:Depth]
}
func filterUnique(changes []string, depth int) []string {
var paths []string
keys := make(map[string]int)

res = append(res, strings.Join(filepath, "/"))
}
for _, f := range changes {
filepath := strings.Split(f, "/")
if len(filepath) >= depth {
filepath = filepath[0:depth]
}

fmt.Printf("%s\n", strings.Join(res, " "))
path := strings.Join(filepath, "/")
if _, ok := keys[path]; !ok {
keys[path] = 1
paths = append(paths, path)
}
}

return nil
return paths
}
25 changes: 25 additions & 0 deletions cmd/goci/changes_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package main

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestFilterUnique(t *testing.T) {
scenarios := []struct {
name string
input []string
output []string
}{
{"unique values", []string{"example/values.yaml", "example/versions.yaml"}, []string{"example"}},
{"non-unique values", []string{"example1/test.yaml", "example2/test.yaml"}, []string{"example1", "example2"}},
{"unique and non-unique values", []string{"example1", "example1", "example2"}, []string{"example1", "example2"}},
}

for _, test := range scenarios {
t.Run(test.name, func(t *testing.T) {
assert.Equal(t, test.output, filterUnique(test.input, 1))
})
}
}

0 comments on commit f3f7fd2

Please sign in to comment.