Skip to content

Commit

Permalink
fix: diff for tags (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelramos authored Jun 22, 2022
1 parent 5b7fb09 commit 99e6f9c
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 17 deletions.
38 changes: 32 additions & 6 deletions cmd/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func init() {
actionCmd.MarkFlagRequired("url")
}

func getSublimePackages(commitsCount int64) []core.Packages {
func getSublimeBranchPackages(commitsCount int64) []core.Packages {
sublime := core.GetSublime()
pkgs := []core.Packages{}

Expand All @@ -74,14 +74,34 @@ func getSublimePackages(commitsCount int64) []core.Packages {
var output string = ""

if commitsCount >= 2 {
output, _ = utils.GetBeforeAndLastDiff(sublime.Root, pkgName)
output, _ = utils.GetBeforeAndLastDiff(sublime.Root)
} else if commitsCount == 1 {
output, _ = utils.GetBeforeDiff(sublime.Root, pkgName)
output, _ = utils.GetBeforeDiff(sublime.Root)
}

counted := strings.Count(output, "\n")
founded := strings.Contains(output, pkgName)

if counted > 0 {
if founded {
pkgs = append(pkgs, sublime.Packages[key])
}
}

return pkgs
}

func getSublimeTagPackages() []core.Packages {
sublime := core.GetSublime()
pkgs := []core.Packages{}

for key := range sublime.Packages {
pkgName := sublime.Packages[key].Name
var output string = ""

output, _ = utils.GetDiffBetweenTags(sublime.Root)

founded := strings.Contains(output, pkgName)

if founded {
pkgs = append(pkgs, sublime.Packages[key])
}
}
Expand All @@ -103,6 +123,8 @@ func (ctx *ActionCommand) ReleaseArtifact(cmd *cobra.Command) {
color.Info.Println("🥼 Starting Feature Artifacts creation")
sublime := core.GetSublime()

var pkgs = []core.Packages{}

count, _ := utils.GetCommitsCount(sublime.Root)
counter, err := strconv.ParseInt(count, 10, 0)
if err != nil || counter <= 0 {
Expand All @@ -116,7 +138,11 @@ func (ctx *ActionCommand) ReleaseArtifact(cmd *cobra.Command) {
hash, _ := utils.GetShortCommit(sublime.Root, lastCommit)
supabase := clients.NewSupabase(ctx.BaseUrl, ctx.Key, ctx.Environment)

pkgs := getSublimePackages(counter)
if ctx.Kind == "branch" {
pkgs = getSublimeBranchPackages(counter)
} else {
pkgs = getSublimeTagPackages()
}

if len(pkgs) <= 0 {
color.Info.Println("🥼 No packages founded to build artifacts")
Expand Down
63 changes: 52 additions & 11 deletions utils/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,56 +22,97 @@ THE SOFTWARE.
package utils

import (
"fmt"
"os/exec"
"strings"
)

func InitGit(path string) (string, error) {
gitCmd := exec.Command("git", "-C", path, "init")
gitCmd := exec.Command("git", "init")
gitCmd.Dir = path
output, err := gitCmd.Output()

return strings.Replace(string(output), "\n", "", -1), err
}

func GetLastCommit(path string) (string, error) {
gitCmd := exec.Command("git", "-C", path, "rev-parse", "head")
gitCmd := exec.Command("git", "rev-parse", "head")
gitCmd.Dir = path
output, err := gitCmd.Output()

return strings.Replace(string(output), "\n", "", -1), err
}

func GetBeforeLastCommit(path string) (string, error) {
gitCmd := exec.Command("git", "-C", path, "rev-parse", "head^")
gitCmd := exec.Command("git", "rev-parse", "head^")
gitCmd.Dir = path
output, err := gitCmd.Output()

return strings.Replace(string(output), "\n", "", -1), err
}

func GetShortCommit(path string, hash string) (string, error) {
gitCmd := exec.Command("git", "-C", path, "rev-parse", "--short", hash)
gitCmd := exec.Command("git", "rev-parse", "--short", hash)
gitCmd.Dir = path
output, err := gitCmd.Output()

return strings.Replace(string(output), "\n", "", -1), err
}

func GetBeforeAndLastDiff(path string, searchFor string) (string, error) {
gitCmd := exec.Command("git", "-C", path, "--no-pager", "diff", "--name-only", "HEAD^", "HEAD", "--", fmt.Sprintf("'*%s*'", searchFor))
output, err := gitCmd.Output()
func GetBeforeAndLastDiff(path string) (string, error) {
gitCmd := exec.Command("git", "--no-pager", "diff", "--name-only", "HEAD^", "HEAD")
gitCmd.Dir = path
output, err := gitCmd.CombinedOutput()

return string(output), err
}

func GetBeforeDiff(path string, searchFor string) (string, error) {
gitCmd := exec.Command("git", "-C", path, "--no-pager", "diff", "--name-only", "HEAD^", "--", fmt.Sprintf("'*%s*'", searchFor))
func GetBeforeDiff(path string) (string, error) {
gitCmd := exec.Command("git", "--no-pager", "diff", "--name-only", "HEAD^")
gitCmd.Dir = path
output, err := gitCmd.Output()

return string(output), err
}

func GetCommitsCount(path string) (string, error) {
gitCmd := exec.Command("git", "-C", path, "rev-list", "--objects", "--all", "--count")
gitCmd := exec.Command("git", "rev-list", "--objects", "--all", "--count")
gitCmd.Dir = path
output, err := gitCmd.Output()

return strings.Replace(string(output), "\n", "", -1), err
}

func GetDiffBetweenTags(path string) (string, error) {
gitCmd := exec.Command("git", "rev-list", "--tags", "--max-count=1")
gitCmd.Dir = path
lastTagByte, er := gitCmd.Output()

if er != nil {
return "", er
}

gitCmd = exec.Command("git", "rev-list", "--tags", "--skip=1", "--max-count=1")
gitCmd.Dir = path
tagBeforeByte, erro := gitCmd.Output()

if erro != nil {
return "", er
}

lastTag := strings.Replace(string(lastTagByte), "\n", "", -1)
tagBefore := strings.Replace(string(tagBeforeByte), "\n", "", -1)

if tagBefore == "" {
tagBefore = "main"
}

if lastTag == "" {
lastTag = "HEAD"
}

gitCmd = exec.Command("git", "--no-pager", "diff", "--name-only", tagBefore, lastTag)
gitCmd.Dir = path
output, err := gitCmd.Output()

return string(output), err
}

0 comments on commit 99e6f9c

Please sign in to comment.