Skip to content

Commit ecfba94

Browse files
authored
Feature: Ability to display changed files from the latest commit (#7)
1 parent 28277f6 commit ecfba94

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@ This will do the following:
1414
2. Update `opendax/2-6/versions.yaml` component tag
1515
3. Commit and push the updates
1616

17+
To display all files updated in the latest Git commit, run `goci -depth 1 changes`, where depth is the depth of directories you'd like to use(dir1/dir2/...dirn)

cmd/goci/changes.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
7+
"github.com/go-git/go-git/v5"
8+
)
9+
10+
func actionChanges() error {
11+
repo, err := git.PlainOpen(".")
12+
if err != nil {
13+
return err
14+
}
15+
16+
ref, err := repo.Head()
17+
if err != nil {
18+
return err
19+
}
20+
21+
commit, err := repo.CommitObject(ref.Hash())
22+
if err != nil {
23+
return err
24+
}
25+
26+
stats, err := commit.Stats()
27+
if err != nil {
28+
return err
29+
}
30+
31+
var changedFiles []string
32+
33+
for _, stat := range stats {
34+
changedFiles = append(changedFiles, stat.Name)
35+
}
36+
37+
if Depth == 0 {
38+
fmt.Printf("%s\n", strings.Join(changedFiles, " "))
39+
} else {
40+
var res []string
41+
keys := make(map[string]int)
42+
43+
for _, f := range changedFiles {
44+
if _, ok := keys[f]; !ok {
45+
keys[f] = 1
46+
filepath := strings.Split(f, "/")
47+
48+
if len(filepath) >= Depth {
49+
filepath = filepath[0:Depth]
50+
}
51+
52+
res = append(res, strings.Join(filepath, "/"))
53+
}
54+
}
55+
56+
fmt.Printf("%s\n", strings.Join(res, " "))
57+
}
58+
59+
return nil
60+
}

cmd/goci/main.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ var Component string
1919
// Tag of the component
2020
var Tag string
2121

22+
// Changed file display depth(dir1/dir2/.../dir*depth*)
23+
var Depth = 0
24+
2225
func main() {
2326
cli := kli.NewCli("goci", "Openware versions cli", Version)
2427

@@ -28,6 +31,10 @@ func main() {
2831
cli.StringFlag("tag", "Tag to insert into the versions file", &Tag)
2932
cli.AddCommand(cmdVersions)
3033

34+
cmdChanges := kli.NewCommand("changes", "List files changed in the last commit").Action(actionChanges)
35+
cli.IntFlag("depth", "Depth of directories changed in the latest git commit", &Depth)
36+
cli.AddCommand(cmdChanges)
37+
3138
if err := cli.Run(); err != nil {
3239
fmt.Printf("Error encountered: %v\n", err)
3340
os.Exit(1)

0 commit comments

Comments
 (0)