Skip to content

Commit

Permalink
✨ Add loading and saving of cancelled commits
Browse files Browse the repository at this point in the history
Add capability to load commits that were previously cancelled. Both new
and amend commits are supported and the saved state will be loaded into
either the new or amend save bank. This allows for toggling between the
two types without losing contents.

To aid in unit testing new commits, a fixed date was required. This
field was added to the main model which can be passed down to component
models.
  • Loading branch information
mikelorant committed Feb 3, 2023
1 parent 58a8e56 commit d12e887
Show file tree
Hide file tree
Showing 56 changed files with 422 additions and 60 deletions.
6 changes: 4 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,16 @@ func NewRootCmd(a App) *cobra.Command {
}

var (
defaultDryRun = isDryRun()
defaultConfigFile = "$HOME/.config/committed/config.yaml"
defaultDryRun = isDryRun()
defaultConfigFile = "$HOME/.config/committed/config.yaml"
defaultSnapshotFile = "$HOME/.local/state/committed/snapshot.yaml"
)

cmd.AddCommand(NewVersionCmd())
cmd.SetVersionTemplate(verTmpl)
cmd.Flags().SortFlags = false
cmd.Flags().StringVarP(&a.opts.ConfigFile, "config", "", defaultConfigFile, "Config file location")
cmd.Flags().StringVarP(&a.opts.SnapshotFile, "snapshot", "", defaultSnapshotFile, "Snapshot file location")
cmd.Flags().BoolVarP(&a.opts.DryRun, "dry-run", "", defaultDryRun, "Simulate applying a commit")
cmd.Flags().BoolVarP(&a.opts.Amend, "amend", "a", false, "Replace the tip of the current branch by creating a new commit")

Expand Down
11 changes: 6 additions & 5 deletions cmd/testdata/help_arg.golden
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ Available Commands:
version Print the version information

Flags:
--config string Config file location (default "$HOME/.config/committed/config.yaml")
--dry-run Simulate applying a commit (default true)
-a, --amend Replace the tip of the current branch by creating a new commit
-h, --help help for committed
-v, --version version for committed
--config string Config file location (default "$HOME/.config/committed/config.yaml")
--snapshot string Snapshot file location (default "$HOME/.local/state/committed/snapshot.yaml")
--dry-run Simulate applying a commit (default true)
-a, --amend Replace the tip of the current branch by creating a new commit
-h, --help help for committed
-v, --version version for committed

Use "committed [command] --help" for more information about a command.
11 changes: 6 additions & 5 deletions cmd/testdata/help_flag.golden
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ Available Commands:
version Print the version information

Flags:
--config string Config file location (default "$HOME/.config/committed/config.yaml")
--dry-run Simulate applying a commit (default true)
-a, --amend Replace the tip of the current branch by creating a new commit
-h, --help help for committed
-v, --version version for committed
--config string Config file location (default "$HOME/.config/committed/config.yaml")
--snapshot string Snapshot file location (default "$HOME/.local/state/committed/snapshot.yaml")
--dry-run Simulate applying a commit (default true)
-a, --amend Replace the tip of the current branch by creating a new commit
-h, --help help for committed
-v, --version version for committed

Use "committed [command] --help" for more information about a command.
11 changes: 6 additions & 5 deletions cmd/testdata/invalid.golden
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ Available Commands:
version Print the version information

Flags:
--config string Config file location (default "$HOME/.config/committed/config.yaml")
--dry-run Simulate applying a commit (default true)
-a, --amend Replace the tip of the current branch by creating a new commit
-h, --help help for committed
-v, --version version for committed
--config string Config file location (default "$HOME/.config/committed/config.yaml")
--snapshot string Snapshot file location (default "$HOME/.local/state/committed/snapshot.yaml")
--dry-run Simulate applying a commit (default true)
-a, --amend Replace the tip of the current branch by creating a new commit
-h, --help help for committed
-v, --version version for committed

Use "committed [command] --help" for more information about a command.

4 changes: 2 additions & 2 deletions internal/commit/commit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ func TestApply(t *testing.T) {
},
},
{
name: "save",
name: "snapshot_save",
args: args{
req: &commit.Request{
Emoji: ":art:",
Expand All @@ -526,7 +526,7 @@ func TestApply(t *testing.T) {
},
},
{
name: "snap_save_error",
name: "snapshot_save_error",
args: args{
req: &commit.Request{},
snapSaveErr: errMock,
Expand Down
14 changes: 14 additions & 0 deletions internal/ui/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,17 @@ func defaultAmendSave(st *commit.State) savedState {

return s
}

func (m Model) snapshotToSave() savedState {
s := savedState{
amend: m.state.Snapshot.Amend,
summary: m.state.Snapshot.Summary,
body: m.state.Snapshot.Body,
}

if e := m.state.Emojis.Find(m.state.Snapshot.Emoji); e.Valid {
s.emoji = e.Emoji
}

return s
}
3 changes: 3 additions & 0 deletions internal/ui/info/info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import (
"github.com/stretchr/testify/assert"
)

const dateTimeFormat = "Mon Jan 2 15:04:05 2006 -0700"

func TestModel(t *testing.T) {
type args struct {
state func(c *commit.State)
Expand Down Expand Up @@ -310,6 +312,7 @@ func TestModel(t *testing.T) {
}

m := info.New(&c)
m.Date = time.Date(2022, time.January, 1, 1, 0, 0, 0, time.UTC).Format(dateTimeFormat)

if tt.args.model != nil {
m = tt.args.model(m)
Expand Down
1 change: 1 addition & 0 deletions internal/ui/status/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ func defaultKeyBindings() []shortcut.KeyBinding {
{Modifier: shortcut.ControlModifier, Key: "h", Label: "Help"},
{Modifier: shortcut.AltModifier, Key: "enter", Label: "Commit"},
{Modifier: shortcut.AltModifier, Key: "a", Label: "Amend"},
{Modifier: shortcut.AltModifier, Key: "l", Label: "Load"},
{Modifier: shortcut.AltModifier, Key: "s", Label: "Sign-off"},
}
}
2 changes: 1 addition & 1 deletion internal/ui/status/testdata/default.golden
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Alt + <enter> Commit <a> Amend <s> Sign-off
Alt + <enter> Commit <a> Amend <l> Load <s> Sign-off
Ctrl + <c> Cancel <h> Help
2 changes: 1 addition & 1 deletion internal/ui/status/testdata/help.golden
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Alt + <enter> Commit <a> Amend <s> Sign-off Exit <esc>
Alt + <enter> Commit <a> Amend <l> Load <s> Sign-off Exit <esc>
Ctrl + <c> Cancel <h> Help
2 changes: 1 addition & 1 deletion internal/ui/status/testdata/next.golden
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Alt + <enter> Commit <a> Amend <s> Sign-off next <tab>
Alt + <enter> Commit <a> Amend <l> Load <s> Sign-off next <tab>
Ctrl + <c> Cancel <h> Help
2 changes: 1 addition & 1 deletion internal/ui/status/testdata/next_previous.golden
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Alt + <enter> Commit <a> Amend <s> Sign-off next <tab>
Alt + <enter> Commit <a> Amend <l> Load <s> Sign-off next <tab>
Ctrl + <c> Cancel <h> Help previous <tab> + Shift
2 changes: 1 addition & 1 deletion internal/ui/status/testdata/previous.golden
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Alt + <enter> Commit <a> Amend <s> Sign-off
Alt + <enter> Commit <a> Amend <l> Load <s> Sign-off
Ctrl + <c> Cancel <h> Help previous <tab> + Shift
2 changes: 1 addition & 1 deletion internal/ui/testdata/alt+1.golden
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@ date: Sat Jan 1 01:00:00 2022 +0000
│ ~ │
└──────────────────────────────────────────────────────────────────────────┘

Alt + <enter> Commit <a> Amend <s> Sign-off Emoji <tab>
Alt + <enter> Commit <a> Amend <l> Load <s> Sign-off Emoji <tab>
Ctrl + <c> Cancel <h> Help
2 changes: 1 addition & 1 deletion internal/ui/testdata/alt+1_twice.golden
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@ date: Sat Jan 1 01:00:00 2022 +0000
│ ~ │
└──────────────────────────────────────────────────────────────────────────┘

Alt + <enter> Commit <a> Amend <s> Sign-off Emoji <tab>
Alt + <enter> Commit <a> Amend <l> Load <s> Sign-off Emoji <tab>
Ctrl + <c> Cancel <h> Help
2 changes: 1 addition & 1 deletion internal/ui/testdata/alt+2.golden
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@ date: Sat Jan 1 01:00:00 2022 +0000
│ ~ │
└──────────────────────────────────────────────────────────────────────────┘

Alt + <enter> Commit <a> Amend <s> Sign-off Summary <tab>
Alt + <enter> Commit <a> Amend <l> Load <s> Sign-off Summary <tab>
Ctrl + <c> Cancel <h> Help Author <tab> + Shift
2 changes: 1 addition & 1 deletion internal/ui/testdata/alt+2_twice.golden
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@ date: Sat Jan 1 01:00:00 2022 +0000
│ ~ │
└──────────────────────────────────────────────────────────────────────────┘

Alt + <enter> Commit <a> Amend <s> Sign-off Summary <tab>
Alt + <enter> Commit <a> Amend <l> Load <s> Sign-off Summary <tab>
Ctrl + <c> Cancel <h> Help Author <tab> + Shift
2 changes: 1 addition & 1 deletion internal/ui/testdata/alt+3.golden
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@ date: Sat Jan 1 01:00:00 2022 +0000
│ ~ │
└──────────────────────────────────────────────────────────────────────────┘

Alt + <enter> Commit <a> Amend <s> Sign-off Body <tab>
Alt + <enter> Commit <a> Amend <l> Load <s> Sign-off Body <tab>
Ctrl + <c> Cancel <h> Help Emoji <tab> + Shift
2 changes: 1 addition & 1 deletion internal/ui/testdata/alt+3_twice.golden
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@ date: Sat Jan 1 01:00:00 2022 +0000
│ ~ │
└──────────────────────────────────────────────────────────────────────────┘

Alt + <enter> Commit <a> Amend <s> Sign-off Body <tab>
Alt + <enter> Commit <a> Amend <l> Load <s> Sign-off Body <tab>
Ctrl + <c> Cancel <h> Help Emoji <tab> + Shift
2 changes: 1 addition & 1 deletion internal/ui/testdata/alt+4.golden
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@ date: Sat Jan 1 01:00:00 2022 +0000
│ │
└──────────────────────────────────────────────────────────────────────────┘

Alt + <enter> Commit <a> Amend <s> Sign-off
Alt + <enter> Commit <a> Amend <l> Load <s> Sign-off
Ctrl + <c> Cancel <h> Help Summary <tab> + Shift
2 changes: 1 addition & 1 deletion internal/ui/testdata/alt+4_twice.golden
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@ date: Sat Jan 1 01:00:00 2022 +0000
│ │
└──────────────────────────────────────────────────────────────────────────┘

Alt + <enter> Commit <a> Amend <s> Sign-off
Alt + <enter> Commit <a> Amend <l> Load <s> Sign-off
Ctrl + <c> Cancel <h> Help Summary <tab> + Shift
2 changes: 1 addition & 1 deletion internal/ui/testdata/alt+enter_invalid.golden
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@ date: Sat Jan 1 01:00:00 2022 +0000
│ │
└──────────────────────────────────────────────────────────────────────────┘

Alt + <enter> Commit <a> Amend <s> Sign-off
Alt + <enter> Commit <a> Amend <l> Load <s> Sign-off
Ctrl + <c> Cancel <h> Help Summary <tab> + Shift
2 changes: 1 addition & 1 deletion internal/ui/testdata/alt+s.golden
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@ date: Sat Jan 1 01:00:00 2022 +0000

Signed-off-by: John Doe <john.doe@example.com>

Alt + <enter> Commit <a> Amend <s> Sign-off Summary <tab>
Alt + <enter> Commit <a> Amend <l> Load <s> Sign-off Summary <tab>
Ctrl + <c> Cancel <h> Help Author <tab> + Shift
2 changes: 1 addition & 1 deletion internal/ui/testdata/alt+s_change_author.golden
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@ date: Sat Jan 1 01:00:00 2022 +0000

Signed-off-by: John Doe <jdoe@example.org>

Alt + <enter> Commit <a> Amend <s> Sign-off Summary <tab>
Alt + <enter> Commit <a> Amend <l> Load <s> Sign-off Summary <tab>
Ctrl + <c> Cancel <h> Help Author <tab> + Shift
2 changes: 1 addition & 1 deletion internal/ui/testdata/alt+s_twice.golden
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@ date: Sat Jan 1 01:00:00 2022 +0000
│ ~ │
└──────────────────────────────────────────────────────────────────────────┘

Alt + <enter> Commit <a> Amend <s> Sign-off Summary <tab>
Alt + <enter> Commit <a> Amend <l> Load <s> Sign-off Summary <tab>
Ctrl + <c> Cancel <h> Help Author <tab> + Shift
2 changes: 1 addition & 1 deletion internal/ui/testdata/alt+t.golden
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@ date: Sat Jan 1 01:00:00 2022 +0000
│ ~ │
└──────────────────────────────────────────────────────────────────────────┘

Alt + <enter> Commit <a> Amend <s> Sign-off Summary <tab>
Alt + <enter> Commit <a> Amend <l> Load <s> Sign-off Summary <tab>
Ctrl + <c> Cancel <h> Help Author <tab> + Shift
32 changes: 32 additions & 0 deletions internal/ui/testdata/amend_empty.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
commit (HEAD -> master)
author: John Doe <john.doe@example.com>
date: Sat Jan 1 01:00:00 2022 +0000

┌────┐ ┌─────────────────────────────────────────────────────┐
│ 🎨 │ │ summary │ 10/50 ● Amend
└────┘ └─────────────────────────────────────────────────────┘

┌──────────────────────────────────────────────────────────────────────────┐
│? Choose an emoji: ● │
│❯ 🎨 - Improve structure / format of the code. │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└──────────────────────────────────────────────────────────────────────────┘

┌──────────────────────────────────────────────────────────────────────────┐
│ body │
│ │
│ │
│ │
│ │
│ │
└──────────────────────────────────────────────────────────────────────────┘

Alt + <enter> Commit <a> Amend <l> Load <s> Sign-off Summary <tab>
Ctrl + <c> Cancel <h> Help Author <tab> + Shift
32 changes: 32 additions & 0 deletions internal/ui/testdata/amend_existing.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
commit (HEAD -> master)
author: John Doe <john.doe@example.com>
date: Sat Jan 1 01:00:00 2022 +0000

┌────┐ ┌─────────────────────────────────────────────────────┐
│ 🎨 │ │ summary │ 10/50 ● Amend
└────┘ └─────────────────────────────────────────────────────┘

┌──────────────────────────────────────────────────────────────────────────┐
│ body │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└──────────────────────────────────────────────────────────────────────────┘

Alt + <enter> Commit <a> Amend <l> Load <s> Sign-off
Ctrl + <c> Cancel <h> Help Summary <tab> + Shift
2 changes: 1 addition & 1 deletion internal/ui/testdata/config_author.golden
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@ date: Sat Jan 1 01:00:00 2022 +0000
│ ~ │
└──────────────────────────────────────────────────────────────────────────┘

Alt + <enter> Commit <a> Amend <s> Sign-off Emoji <tab>
Alt + <enter> Commit <a> Amend <l> Load <s> Sign-off Emoji <tab>
Ctrl + <c> Cancel <h> Help
Loading

0 comments on commit d12e887

Please sign in to comment.