chore: refactor rename stash

This commit is contained in:
Ryooooooga 2022-10-15 11:15:31 +09:00
parent 8a9eefa4d2
commit eceb3a5aa6
No known key found for this signature in database
GPG key ID: 07CF200DFCC20C25
3 changed files with 70 additions and 15 deletions

View file

@ -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
}