diff options
author | Ryooooooga <eial5q265e5@gmail.com> | 2022-10-15 11:15:31 +0900 |
---|---|---|
committer | Ryooooooga <eial5q265e5@gmail.com> | 2022-10-16 09:12:43 +0900 |
commit | eceb3a5aa6de864f31c52016e6fd497b9b6a1214 (patch) | |
tree | a2240f25bf60b1d4277ee85e9c5603f05a3ed6aa /pkg | |
parent | 8a9eefa4d275f925e149be2c05c5c6958da86f5b (diff) |
chore: refactor rename stash
Diffstat (limited to 'pkg')
-rw-r--r-- | pkg/commands/git_commands/stash.go | 25 | ||||
-rw-r--r-- | pkg/commands/git_commands/stash_test.go | 43 | ||||
-rw-r--r-- | pkg/gui/controllers/stash_controller.go | 17 |
3 files changed, 70 insertions, 15 deletions
diff --git a/pkg/commands/git_commands/stash.go b/pkg/commands/git_commands/stash.go index 841765e81..176a67e52 100644 --- a/pkg/commands/git_commands/stash.go +++ b/pkg/commands/git_commands/stash.go @@ -1,7 +1,9 @@ package git_commands import ( + "errors" "fmt" + "regexp" "strings" "github.com/jesseduffield/lazygit/pkg/commands/loaders" @@ -119,3 +121,26 @@ func (self *StashCommands) SaveStagedChanges(message string) error { return nil } + +func (self *StashCommands) Rename(index int, message string) error { + output, err := self.Drop(index) + if err != nil { + return err + } + + // `output` is in the following format: + // Dropped refs/stash@{0} (f0d0f20f2f61ffd6d6bfe0752deffa38845a3edd) + stashShaPattern := regexp.MustCompile(`\(([0-9a-f]+)\)`) + matches := stashShaPattern.FindStringSubmatch(output) + if len(matches) <= 1 { + return errors.New("Output of `git stash drop` is invalid") // Usually this error does not occur + } + stashSha := matches[1] + + err = self.Store(stashSha, message) + if err != nil { + return err + } + + return nil +} diff --git a/pkg/commands/git_commands/stash_test.go b/pkg/commands/git_commands/stash_test.go index e5ce39181..41efc07db 100644 --- a/pkg/commands/git_commands/stash_test.go +++ b/pkg/commands/git_commands/stash_test.go @@ -123,3 +123,46 @@ func TestStashStashEntryCmdObj(t *testing.T) { }) } } + +func TestStashRename(t *testing.T) { + type scenario struct { + testName string + index int + message string + expectedDropCmd []string + dropResult string + expectedStoreCmd []string + } + + scenarios := []scenario{ + { + testName: "Default case", + index: 3, + message: "New message", + expectedDropCmd: []string{"stash", "drop", "stash@{3}"}, + dropResult: "Dropped refs/stash@{3} (f0d0f20f2f61ffd6d6bfe0752deffa38845a3edd)\n", + expectedStoreCmd: []string{"stash", "store", "f0d0f20f2f61ffd6d6bfe0752deffa38845a3edd", "-m", "New message"}, + }, + { + testName: "Empty message", + index: 4, + message: "", + expectedDropCmd: []string{"stash", "drop", "stash@{4}"}, + dropResult: "Dropped refs/stash@{4} (f0d0f20f2f61ffd6d6bfe0752deffa38845a3edd)\n", + expectedStoreCmd: []string{"stash", "store", "f0d0f20f2f61ffd6d6bfe0752deffa38845a3edd"}, + }, + } + + for _, s := range scenarios { + s := s + t.Run(s.testName, func(t *testing.T) { + runner := oscommands.NewFakeRunner(t). + ExpectGitArgs(s.expectedDropCmd, s.dropResult, nil). + ExpectGitArgs(s.expectedStoreCmd, "", nil) + instance := buildStashCommands(commonDeps{runner: runner}) + + err := instance.Rename(s.index, s.message) + assert.NoError(t, err) + }) + } +} diff --git a/pkg/gui/controllers/stash_controller.go b/pkg/gui/controllers/stash_controller.go index 6cd06c6d4..1e4137777 100644 --- a/pkg/gui/controllers/stash_controller.go +++ b/pkg/gui/controllers/stash_controller.go @@ -1,8 +1,6 @@ package controllers import ( - "regexp" - "github.com/jesseduffield/lazygit/pkg/commands/models" "github.com/jesseduffield/lazygit/pkg/gui/context" "github.com/jesseduffield/lazygit/pkg/gui/types" @@ -161,23 +159,12 @@ func (self *StashController) handleRenameStashEntry(stashEntry *models.StashEntr InitialContent: stashEntry.Name, HandleConfirm: func(response string) error { self.c.LogAction(self.c.Tr.Actions.RenameStash) - output, err := self.git.Stash.Drop(stashEntry.Index) - if err != nil { - return err - } - - stashShaPattern := regexp.MustCompile(`\(([0-9a-f]+)\)`) - matches := stashShaPattern.FindStringSubmatch(output) - stashSha := "" - if len(matches) > 1 { - stashSha = matches[1] - } - - err = self.git.Stash.Store(stashSha, response) + err := self.git.Stash.Rename(stashEntry.Index, response) _ = self.c.Refresh(types.RefreshOptions{Scope: []types.RefreshableView{types.STASH}}) if err != nil { return err } + self.context().SetSelectedLineIdx(0) // Select the renamed stash return nil }, }) |