Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to diff branches. #5

Merged
merged 6 commits into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions .github/workflows/delete-prereleases.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
name: Delete pre-releases

on:
workflow_dispatch:

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: dev-drprasad/[email protected]
with:
delete_prerelease_only: true
delete_tags: true
keep_latest: 0
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
# vertag Changelog

# v0.0.2

* Added feature to compare against last tag on the same branch rather than last tag on main branch.
* Bug fixes.
8 changes: 5 additions & 3 deletions pkg/cmd/vertag/vertag.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@ var (
dryRun bool
vers bool
help bool
branchDiff bool
)

func Apply(repoRoot string, modulesDir string, authorName string, authorEmail string, dryRun bool, remoteUrl string) error {
myFigure := figure.NewFigure("VerTag", "", true)
myFigure.Print()

vt := core.NewVertag(repoRoot, modulesDir, authorName, authorEmail, dryRun, remoteUrl)
vt := core.NewVertag(repoRoot, modulesDir, authorName, authorEmail, dryRun, remoteUrl, branchDiff)
err := vt.Init()
if err != nil {
output.PrintlnError(err)
Expand Down Expand Up @@ -100,9 +101,10 @@ func NewRootCmd(version string, commit string, date string) *cobra.Command {
cmd.Flags().StringVarP(&remoteUrl, "remote-url", "u", "", "CI Remote URL")
cmd.Flags().BoolVarP(&dryRun, "dry-run", "d", false, "Email of the commiter")
cmd.Flags().BoolVarP(&vers, "version", "v", false, "Version")
cmd.Flags().BoolVarP(&shortened, "short", "s", false, "Print just the version number")
cmd.Flags().StringVarP(&outputFmt, "output", "o", "json", "Output format")
cmd.Flags().BoolVarP(&shortened, "version-short", "s", false, "Print just the version number")
cmd.Flags().StringVarP(&outputFmt, "version-output", "o", "json", "Output format")
cmd.Flags().BoolVarP(&help, "help", "h", false, "Version")
cmd.Flags().BoolVarP(&branchDiff, "branch-diff", "b", false, "When on a branch, compare with tags on the branch rather than the default branch")

return cmd
}
7 changes: 0 additions & 7 deletions pkg/core/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,6 @@ func (r *GitRepo) changedFiles(latestTagOrHash string) []string {
fileschanged = append(fileschanged, stat.Name)
}

fps := diff.FilePatches()
for _, fp := range fps {
f, t := fp.Files()
if t == nil {
fileschanged = removeFromSlice(fileschanged, f.Path())
}
}
return fileschanged
}

Expand Down
25 changes: 14 additions & 11 deletions pkg/core/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,20 @@ import (
)

type Vertag struct {
Repo *GitRepo
RepoRoot string
ModulesDir string
ModulesFullPath string
DryRun bool
LatestStableTag string
LatestStableSHA string
LatestTag string
CurrentBranch string
ModulesChanged []string
NextTags []string
Repo *GitRepo
RepoRoot string
ModulesDir string
ModulesFullPath string
DryRun bool
LatestStableTag string
LatestStableSHA string
LatestTag string
CurrentBranch string
ModulesChanged []string
NextTags []string
BranchDiff bool
LatestBranchUnstableSHA string
ComparisonSHA string
}

type GitRepo struct {
Expand Down
96 changes: 78 additions & 18 deletions pkg/core/vertag.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/gofrontier-com/go-utils/output"
)

func NewVertag(repoRoot string, modulesDir string, authorName string, authorEmail string, dryRun bool, remoteUrl string) *Vertag {
func NewVertag(repoRoot string, modulesDir string, authorName string, authorEmail string, dryRun bool, remoteUrl string, branchDiff bool) *Vertag {
r := &GitRepo{
Author: &GitAuthor{
Name: authorName,
Expand All @@ -27,6 +27,7 @@ func NewVertag(repoRoot string, modulesDir string, authorName string, authorEmai
ModulesDir: modulesDir,
ModulesFullPath: path.Join(repoRoot, modulesDir),
DryRun: dryRun,
BranchDiff: branchDiff,
}
}

Expand Down Expand Up @@ -87,6 +88,7 @@ func (v *Vertag) GetLatestStableTag() error {
} else {
v.LatestStableSHA = latestTagCommit.Hash.String()
}
v.ComparisonSHA = v.LatestStableSHA
return nil
}

Expand All @@ -99,7 +101,8 @@ func (v *Vertag) latestTagContains(tagContains string) error {
var latestTagCommit *object.Commit
var latestTagName string
err = tagRefs.ForEach(func(tagRef *plumbing.Reference) error {
if strings.Contains(tagRef.Name().String(), tagContains) {
moduleName := strings.Split(tagRef.Name().String(), "/")[2]
if moduleName == tagContains {
revision := plumbing.Revision(tagRef.Name().String())
tagCommitHash, err := v.Repo.Repo.ResolveRevision(revision)
if err != nil {
Expand Down Expand Up @@ -139,13 +142,66 @@ func (v *Vertag) latestTagContains(tagContains string) error {
return nil
}

func (v *Vertag) GetLatestBranchUnstableTag() error {
tagRefs, err := v.Repo.Repo.Tags()
if err != nil {
return err
}

head, err := v.Repo.Repo.Head()
if err != nil {
return err
}
currentCommit, err := v.Repo.Repo.CommitObject(head.Hash())
if err != nil {
return err
}

tagRefMap := make(map[string]string)
err = tagRefs.ForEach(func(tagRef *plumbing.Reference) error {
tagRefMap[tagRef.Name().String()] = tagRef.Hash().String()
return nil
})
if err != nil {
return err
}

for {
found := false
for tagRefName, tagRefHash := range tagRefMap {
fmt.Println(tagRefName, tagRefHash, currentCommit.Hash.String())
if strings.Contains(tagRefName, "-unstable") {
if tagRefHash == currentCommit.Hash.String() {
found = true
}
}
}
if err != nil {
return err
}

if found {
v.LatestBranchUnstableSHA = currentCommit.Hash.String()
v.ComparisonSHA = v.LatestBranchUnstableSHA
break
}

currentCommit, err = currentCommit.Parents().Next()
if err != nil {
return err
}
}

return nil
}

func (v *Vertag) GetRefs() error {
err := v.getDiffRefs()
if err != nil {
return err
}

output.PrintfInfo("Comparing\n\tCurrent Branch: %s\nto\n\tLatest Tagged SHA: %s\n\n", v.CurrentBranch, v.LatestStableSHA)
output.PrintfInfo("Comparing\n\tCurrent Branch: %s\nto\n\tLatest Tagged SHA: %s\n\n", v.CurrentBranch, v.ComparisonSHA)

return nil
}
Expand All @@ -162,11 +218,15 @@ func (v *Vertag) getDiffRefs() error {
return err
}

if v.BranchDiff {
err = v.GetLatestBranchUnstableTag()
}

return nil
}

func (v *Vertag) GetChanges() error {
fileschanged := v.Repo.changedFiles(v.LatestStableSHA)
fileschanged := v.Repo.changedFiles(v.ComparisonSHA)
dirschanged := changedDirs(fileschanged, v.ModulesDir, v.ModulesFullPath)
output.PrintlnInfo("Modules changed")
for _, d := range dirschanged {
Expand Down Expand Up @@ -220,26 +280,26 @@ func (v *Vertag) WriteTags() error {
return nil
}

if v.Repo.RemoteUrl != "" {
v.Repo.AddRemote("ci", v.Repo.RemoteUrl)
}

for _, tag := range v.NextTags {
if v.DryRun {
output.Println("[Dry run] Would have created tag: ", tag)
} else {
err := v.Repo.CreateTag(tag)
if err != nil {
return err
}
}
}

if !v.DryRun {
if v.Repo.RemoteUrl != "" {
v.Repo.AddRemote("ci", v.Repo.RemoteUrl)
err := v.Repo.PushWithTagsTo("ci")
if err != nil {
return err
if v.Repo.RemoteUrl != "" {
err := v.Repo.PushWithTagsTo("ci")
if err != nil {
return err
}
} else {
err := v.Repo.PushWithTags()
if err != nil {
return err
}
}
} else {
err := v.Repo.PushWithTags()
if err != nil {
return err
}
Expand Down