Skip to content
New issue

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

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

Already on GitHub? # to your account

fix: ensure changes to files can optionally be staged #270

Merged
merged 2 commits into from
Oct 7, 2022
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
1 change: 1 addition & 0 deletions cmd/uplift/bump.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ func setupBumpContext(opts bumpOptions, out io.Writer) (*context.Context, error)
ctx.Debug = opts.Debug
ctx.DryRun = opts.DryRun
ctx.NoPush = opts.NoPush
ctx.NoStage = opts.NoStage
ctx.Out = out

// Handle prerelease suffix if one is provided
Expand Down
1 change: 1 addition & 0 deletions cmd/uplift/changelog.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ func setupChangelogContext(opts changelogOptions, out io.Writer) (*context.Conte
ctx.Debug = opts.Debug
ctx.DryRun = opts.DryRun
ctx.NoPush = opts.NoPush
ctx.NoStage = opts.NoStage
ctx.Changelog.DiffOnly = opts.DiffOnly
ctx.Changelog.All = opts.All

Expand Down
1 change: 1 addition & 0 deletions cmd/uplift/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ func setupReleaseContext(opts releaseOptions, out io.Writer) (*context.Context,
ctx.Debug = opts.Debug
ctx.DryRun = opts.DryRun
ctx.NoPush = opts.NoPush
ctx.NoStage = opts.NoStage
ctx.FetchTags = opts.FetchTags
ctx.Out = out
ctx.SkipChangelog = opts.SkipChangelog
Expand Down
2 changes: 2 additions & 0 deletions cmd/uplift/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type globalOptions struct {
DryRun bool
Debug bool
NoPush bool
NoStage bool
Silent bool
IgnoreExistingPrerelease bool
IgnoreDetached bool
Expand Down Expand Up @@ -75,6 +76,7 @@ func newRootCmd(out io.Writer) *rootCommand {
pf.BoolVar(&rootCmd.Opts.DryRun, "dry-run", false, "run without making any changes")
pf.BoolVar(&rootCmd.Opts.Debug, "debug", false, "show me everything that happens")
pf.BoolVar(&rootCmd.Opts.NoPush, "no-push", false, "no changes will be pushed to the git remote")
pf.BoolVar(&rootCmd.Opts.NoStage, "no-stage", false, "no changes will be git staged")
pf.BoolVar(&rootCmd.Opts.Silent, "silent", false, "silence all logging")
pf.BoolVar(&rootCmd.Opts.IgnoreDetached, "ignore-detached", false, "ignore reported git detached HEAD error")
pf.BoolVar(&rootCmd.Opts.IgnoreShallow, "ignore-shallow", false, "ignore reported git shallow clone error")
Expand Down
10 changes: 10 additions & 0 deletions cmd/uplift/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,13 @@ func TestRoot_IgnoreShallowFlag(t *testing.T) {

assert.True(t, rootCmd.Opts.IgnoreShallow)
}

func TestRoot_NoStage(t *testing.T) {
rootCmd := newRootCmd(os.Stdout)

rootCmd.Cmd.SetArgs([]string{"--no-stage"})
err := rootCmd.Cmd.Execute()
require.NoError(t, err)

assert.True(t, rootCmd.Opts.NoStage)
}
1 change: 1 addition & 0 deletions internal/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ type Context struct {
PrintCurrentTag bool
PrintNextTag bool
NoPush bool
NoStage bool
Changelog Changelog
SCM SCM
SkipChangelog bool
Expand Down
22 changes: 10 additions & 12 deletions internal/task/bump/bump.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,14 @@ func (t Task) Run(ctx *context.Context) error {

n := 0
for _, bump := range ctx.Config.Bumps {
var bumped bool
if len(bump.Regex) > 0 {
ok, err := regexBump(ctx, bump.File, bump.Regex)
if err != nil {
return err
}

if ok {
bumped = ok
n++
}
}

Expand All @@ -70,20 +69,19 @@ func (t Task) Run(ctx *context.Context) error {
}

if ok {
bumped = ok
n++
}
}

if bumped {
// Attempt to stage the changed file, if this isn't a dry run
if !ctx.DryRun {
if err := git.Stage(bump.File); err != nil {
return err
}
log.WithField("file", bump.File).Info("successfully staged file")
}
n++
if ctx.NoStage {
log.Info("skip staging of file")
continue
}

if err := git.Stage(bump.File); err != nil {
return err
}
log.WithField("file", bump.File).Info("successfully staged file")
}

if n > 0 {
Expand Down
34 changes: 34 additions & 0 deletions internal/task/bump/bump_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"github.com/gembaadvantage/uplift/internal/git"
"github.com/gembaadvantage/uplift/internal/semver"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestString(t *testing.T) {
Expand Down Expand Up @@ -66,3 +67,36 @@ func TestRun_NotGitRepository(t *testing.T) {
err := Task{}.Run(ctx)
assert.EqualError(t, err, "fatal: not a git repository (or any of the parent directories): .git")
}

func TestRun_NoStage(t *testing.T) {
git.InitRepo(t)
file := WriteFile(t, "version: 0.1.0")

ctx := &context.Context{
NextVersion: semver.Version{
Raw: "0.1.1",
},
Config: config.Uplift{
Bumps: []config.Bump{
{
File: file,
Regex: []config.RegexBump{
{
Pattern: "version: $VERSION",
},
},
},
},
},
NoStage: true,
}

err := Task{}.Run(ctx)
require.NoError(t, err)

actual := ReadFile(t, file)
assert.Equal(t, "version: 0.1.1", actual)

staged, _ := git.Staged()
assert.Empty(t, staged)
}
5 changes: 5 additions & 0 deletions internal/task/changelog/changelog.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,11 @@ func (t Task) Run(ctx *context.Context) error {
return chgErr
}

if ctx.NoStage {
log.Info("skip staging of CHANGELOG.md")
return nil
}

log.Debug("staging CHANGELOG.md")
return git.Stage(MarkdownFile)
}
Expand Down
18 changes: 18 additions & 0 deletions internal/task/changelog/changelog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,24 @@ func TestRun_Staged(t *testing.T) {
assert.Equal(t, "CHANGELOG.md", stg[0])
}

func TestRun_NotStaged(t *testing.T) {
git.InitRepo(t)
git.EmptyCommitsAndTag(t, "1.0.0", "first commit", "second commit")

ctx := &context.Context{
NextVersion: semver.Version{
Raw: "1.0.0",
},
NoStage: true,
}

err := Task{}.Run(ctx)
require.NoError(t, err)

stg, _ := git.Staged()
assert.Len(t, stg, 0)
}

func TestRun_AppendToUnsupportedTemplate(t *testing.T) {
git.InitRepo(t)
git.EmptyCommitsAndTag(t, "1.0.0", "first commit")
Expand Down