diff --git a/README.md b/README.md index 5e0b5d5..f405bb1 100644 --- a/README.md +++ b/README.md @@ -14,3 +14,4 @@ This will do the following: 2. Update `opendax/2-6/versions.yaml` component tag 3. Commit and push the updates +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) \ No newline at end of file diff --git a/cmd/goci/changes.go b/cmd/goci/changes.go new file mode 100644 index 0000000..ad6391d --- /dev/null +++ b/cmd/goci/changes.go @@ -0,0 +1,60 @@ +package main + +import ( + "fmt" + "strings" + + "github.com/go-git/go-git/v5" +) + +func actionChanges() error { + repo, err := git.PlainOpen(".") + if err != nil { + return err + } + + ref, err := repo.Head() + if err != nil { + return err + } + + commit, err := repo.CommitObject(ref.Hash()) + if err != nil { + return err + } + + stats, err := commit.Stats() + if err != nil { + return err + } + + var changedFiles []string + + for _, stat := range stats { + changedFiles = append(changedFiles, stat.Name) + } + + if Depth == 0 { + fmt.Printf("%s\n", strings.Join(changedFiles, " ")) + } else { + var res []string + keys := make(map[string]int) + + for _, f := range changedFiles { + if _, ok := keys[f]; !ok { + keys[f] = 1 + filepath := strings.Split(f, "/") + + if len(filepath) >= Depth { + filepath = filepath[0:Depth] + } + + res = append(res, strings.Join(filepath, "/")) + } + } + + fmt.Printf("%s\n", strings.Join(res, " ")) + } + + return nil +} diff --git a/cmd/goci/main.go b/cmd/goci/main.go index 1a5c4b0..bedd7f5 100644 --- a/cmd/goci/main.go +++ b/cmd/goci/main.go @@ -19,6 +19,9 @@ var Component string // Tag of the component var Tag string +// Changed file display depth(dir1/dir2/.../dir*depth*) +var Depth = 0 + func main() { cli := kli.NewCli("goci", "Openware versions cli", Version) @@ -28,6 +31,10 @@ func main() { cli.StringFlag("tag", "Tag to insert into the versions file", &Tag) cli.AddCommand(cmdVersions) + cmdChanges := kli.NewCommand("changes", "List files changed in the last commit").Action(actionChanges) + cli.IntFlag("depth", "Depth of directories changed in the latest git commit", &Depth) + cli.AddCommand(cmdChanges) + if err := cli.Run(); err != nil { fmt.Printf("Error encountered: %v\n", err) os.Exit(1)