Skip to content

Commit f3f7fd2

Browse files
dpatsoraxenirio
andauthored
Fix: Display only unique changes (#8)
Co-authored-by: Vee <[email protected]>
1 parent 9fd976b commit f3f7fd2

File tree

2 files changed

+46
-19
lines changed

2 files changed

+46
-19
lines changed

cmd/goci/changes.go

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -28,33 +28,35 @@ func actionChanges() error {
2828
return err
2929
}
3030

31-
var changedFiles []string
31+
var paths []string
3232

3333
for _, stat := range stats {
34-
changedFiles = append(changedFiles, stat.Name)
34+
paths = append(paths, stat.Name)
3535
}
3636

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

43-
for _, f := range changedFiles {
44-
if _, ok := keys[f]; !ok {
45-
keys[f] = 1
46-
filepath := strings.Split(f, "/")
42+
return nil
43+
}
4744

48-
if len(filepath) >= Depth {
49-
filepath = filepath[0:Depth]
50-
}
45+
func filterUnique(changes []string, depth int) []string {
46+
var paths []string
47+
keys := make(map[string]int)
5148

52-
res = append(res, strings.Join(filepath, "/"))
53-
}
49+
for _, f := range changes {
50+
filepath := strings.Split(f, "/")
51+
if len(filepath) >= depth {
52+
filepath = filepath[0:depth]
5453
}
5554

56-
fmt.Printf("%s\n", strings.Join(res, " "))
55+
path := strings.Join(filepath, "/")
56+
if _, ok := keys[path]; !ok {
57+
keys[path] = 1
58+
paths = append(paths, path)
59+
}
5760
}
58-
59-
return nil
61+
return paths
6062
}

cmd/goci/changes_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package main
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestFilterUnique(t *testing.T) {
10+
scenarios := []struct {
11+
name string
12+
input []string
13+
output []string
14+
}{
15+
{"unique values", []string{"example/values.yaml", "example/versions.yaml"}, []string{"example"}},
16+
{"non-unique values", []string{"example1/test.yaml", "example2/test.yaml"}, []string{"example1", "example2"}},
17+
{"unique and non-unique values", []string{"example1", "example1", "example2"}, []string{"example1", "example2"}},
18+
}
19+
20+
for _, test := range scenarios {
21+
t.Run(test.name, func(t *testing.T) {
22+
assert.Equal(t, test.output, filterUnique(test.input, 1))
23+
})
24+
}
25+
}

0 commit comments

Comments
 (0)