mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-03-29 22:07:13 +02:00
chore: refactor rename stash
This commit is contained in:
parent
8a9eefa4d2
commit
eceb3a5aa6
pkg
@ -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
|
||||
}
|
||||
|
@ -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)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
},
|
||||
})
|
||||
|
Loading…
x
Reference in New Issue
Block a user