Skip to content

Commit

Permalink
Feature: Ability to display changed files from the latest commit (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
vshatravenko authored Jun 4, 2021
1 parent 28277f6 commit ecfba94
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
60 changes: 60 additions & 0 deletions cmd/goci/changes.go
Original file line number Diff line number Diff line change
@@ -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
}
7 changes: 7 additions & 0 deletions cmd/goci/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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)
Expand Down

0 comments on commit ecfba94

Please sign in to comment.