Skip to content

Commit

Permalink
♻️ Refactor applying or cancelling a commit
Browse files Browse the repository at this point in the history
Applying or cancelling a quit now provides the commit state. This is
necessary for adding the ability to store a cancelled commit for reuse.

This requires identifying in the request whether to apply the commit
and exiting early if the commit was cancelled.
  • Loading branch information
mikelorant committed Feb 2, 2023
1 parent a40c8da commit 5efd937
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 12 deletions.
5 changes: 5 additions & 0 deletions internal/commit/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type Options struct {
}

type Request struct {
Apply bool
Emoji string
Summary string
Body string
Expand Down Expand Up @@ -89,6 +90,10 @@ func (c *Commit) Apply(req *Request) error {
Footer: req.Footer,
}

if !req.Apply {
return nil
}

opts := []func(c *repository.Commit){
repository.WithAmend(req.Amend),
repository.WithDryRun(c.Options.DryRun),
Expand Down
9 changes: 9 additions & 0 deletions internal/commit/commit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ func TestApply(t *testing.T) {
author repository.User
amend bool
options commit.Options
apply bool
}

type want struct {
Expand All @@ -239,6 +240,7 @@ func TestApply(t *testing.T) {
{
name: "normal",
args: args{
apply: true,
emoji: ":art:",
summary: "summary",
body: "body",
Expand All @@ -258,6 +260,7 @@ func TestApply(t *testing.T) {
{
name: "dryrun",
args: args{
apply: true,
options: commit.Options{
DryRun: true,
},
Expand All @@ -269,6 +272,7 @@ func TestApply(t *testing.T) {
{
name: "amend",
args: args{
apply: true,
amend: true,
},
want: want{
Expand All @@ -278,6 +282,7 @@ func TestApply(t *testing.T) {
{
name: "amend_dryrun",
args: args{
apply: true,
amend: true,
options: commit.Options{
DryRun: true,
Expand All @@ -290,6 +295,9 @@ func TestApply(t *testing.T) {
},
{
name: "invalid",
args: args{
apply: true,
},
want: want{
err: errMock,
},
Expand All @@ -303,6 +311,7 @@ func TestApply(t *testing.T) {
}

req := &commit.Request{
Apply: tt.args.apply,
Emoji: tt.args.emoji,
Summary: tt.args.summary,
Body: tt.args.body,
Expand Down
42 changes: 30 additions & 12 deletions internal/ui/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type Model struct {
focus focus
previousFocus focus
models Models
quit bool
quit quit
amend bool
signoff bool
err error
Expand Down Expand Up @@ -70,6 +70,14 @@ const (
helpComponent
)

type quit int

const (
unsetQuit quit = iota
applyQuit
cancelQuit
)

const (
bodyDefaultHeight = 19
bodyAuthorHeight = 12
Expand Down Expand Up @@ -178,7 +186,7 @@ func (m Model) View() string {
return ""
}

if m.quit {
if m.quit == applyQuit {
return lipgloss.JoinVertical(lipgloss.Top,
m.models.info.View(),
m.models.message.View(),
Expand Down Expand Up @@ -249,7 +257,7 @@ func (m Model) onKeyPress(msg tea.KeyMsg) keyResponse {
break
}

m = m.commit()
m = m.commit(applyQuit)

return keyResponse{model: m, cmd: tea.Quit, end: true}
case "alt+a":
Expand Down Expand Up @@ -298,6 +306,8 @@ func (m Model) onKeyPress(msg tea.KeyMsg) keyResponse {
m.focus = summaryComponent
}
case "ctrl+c":
m = m.commit(cancelQuit)

return keyResponse{model: m, cmd: tea.Quit, end: true}
}

Expand Down Expand Up @@ -366,8 +376,8 @@ func (m Model) updateModels(msg tea.Msg) (Model, tea.Cmd) {
return m, tea.Batch(cmds...)
}

func (m Model) commit() Model {
m.quit = true
func (m Model) commit(q quit) Model {
m.quit = q

var emoji string

Expand All @@ -378,13 +388,15 @@ func (m Model) commit() Model {
emoji = m.models.header.Emoji.Shortcode
}

m.models.message = message.New(message.State{
Emoji: emoji,
Summary: m.models.header.Summary(),
Body: m.models.body.Value(),
Footer: m.models.footer.Value(),
Theme: m.state.Theme,
})
if m.quit == applyQuit {
m.models.message = message.New(message.State{
Emoji: emoji,
Summary: m.models.header.Summary(),
Body: m.models.body.Value(),
Footer: m.models.footer.Value(),
Theme: m.state.Theme,
})
}

m.Request = &commit.Request{
Author: m.models.info.Author,
Expand All @@ -395,6 +407,12 @@ func (m Model) commit() Model {
Amend: m.amend,
}

if m.quit == applyQuit {
m.Request.Apply = true

return m
}

return m
}

Expand Down
7 changes: 7 additions & 0 deletions internal/ui/ui_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,7 @@ func TestModel(t *testing.T) {
want: want{
model: func(m ui.Model) {
req := commit.Request{
Apply: true,
Summary: "test",
Author: repository.User{
Name: "John Doe",
Expand Down Expand Up @@ -387,6 +388,7 @@ func TestModel(t *testing.T) {
want: want{
model: func(m ui.Model) {
req := commit.Request{
Apply: true,
Emoji: ":art:",
Summary: "test",
Author: repository.User{
Expand Down Expand Up @@ -416,6 +418,7 @@ func TestModel(t *testing.T) {
want: want{
model: func(m ui.Model) {
req := commit.Request{
Apply: true,
Summary: "test",
Body: "test",
Author: repository.User{
Expand Down Expand Up @@ -444,6 +447,7 @@ func TestModel(t *testing.T) {
want: want{
model: func(m ui.Model) {
req := commit.Request{
Apply: true,
Summary: "test",
Footer: "Signed-off-by: John Doe <[email protected]>",
Author: repository.User{
Expand Down Expand Up @@ -474,6 +478,7 @@ func TestModel(t *testing.T) {
want: want{
model: func(m ui.Model) {
req := commit.Request{
Apply: true,
Summary: "test",
Author: repository.User{
Name: "John Doe",
Expand Down Expand Up @@ -577,6 +582,7 @@ func TestModel(t *testing.T) {
want: want{
model: func(m ui.Model) {
req := commit.Request{
Apply: true,
Emoji: ":art:",
Summary: "test",
Author: repository.User{
Expand Down Expand Up @@ -608,6 +614,7 @@ func TestModel(t *testing.T) {
want: want{
model: func(m ui.Model) {
req := commit.Request{
Apply: true,
Emoji: "🎨",
Summary: "test",
Author: repository.User{
Expand Down

0 comments on commit 5efd937

Please sign in to comment.