1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2024-12-12 11:15:00 +02:00
lazygit/pkg/commands/stash_entries.go

68 lines
2.0 KiB
Go
Raw Normal View History

2020-09-29 12:03:39 +02:00
package commands
2021-12-30 08:19:01 +02:00
import (
"fmt"
"github.com/jesseduffield/lazygit/pkg/commands/loaders"
2022-01-05 02:57:32 +02:00
"github.com/jesseduffield/lazygit/pkg/commands/oscommands"
2021-12-30 08:19:01 +02:00
)
2020-09-29 12:03:39 +02:00
// StashDo modify stash
func (c *GitCommand) StashDo(index int, method string) error {
2021-12-29 05:33:38 +02:00
return c.Cmd.New(fmt.Sprintf("git stash %s stash@{%d}", method, index)).Run()
2020-09-29 12:03:39 +02:00
}
// StashSave save stash
// TODO: before calling this, check if there is anything to save
func (c *GitCommand) StashSave(message string) error {
2021-12-29 05:33:38 +02:00
return c.Cmd.New("git stash save " + c.OSCommand.Quote(message)).Run()
2020-09-29 12:03:39 +02:00
}
2022-01-05 02:57:32 +02:00
func (c *GitCommand) ShowStashEntryCmdObj(index int) oscommands.ICmdObj {
cmdStr := fmt.Sprintf("git stash show -p --stat --color=%s --unified=%d stash@{%d}", c.colorArg(), c.UserConfig.Git.DiffContextSize, index)
return c.Cmd.New(cmdStr).DontLog()
2020-09-29 12:03:39 +02:00
}
// StashSaveStagedChanges stashes only the currently staged changes. This takes a few steps
// shoutouts to Joe on https://stackoverflow.com/questions/14759748/stashing-only-staged-changes-in-git-is-it-possible
func (c *GitCommand) StashSaveStagedChanges(message string) error {
// wrap in 'writing', which uses a mutex
2021-12-29 05:33:38 +02:00
if err := c.Cmd.New("git stash --keep-index").Run(); err != nil {
2020-09-29 12:03:39 +02:00
return err
}
if err := c.StashSave(message); err != nil {
return err
}
2021-12-29 05:33:38 +02:00
if err := c.Cmd.New("git stash apply stash@{1}").Run(); err != nil {
2020-09-29 12:03:39 +02:00
return err
}
if err := c.OSCommand.PipeCommands("git stash show -p", "git apply -R"); err != nil {
return err
}
2021-12-29 05:33:38 +02:00
if err := c.Cmd.New("git stash drop stash@{1}").Run(); err != nil {
2020-09-29 12:03:39 +02:00
return err
}
// if you had staged an untracked file, that will now appear as 'AD' in git status
// meaning it's deleted in your working tree but added in your index. Given that it's
// now safely stashed, we need to remove it.
2021-12-30 08:19:01 +02:00
files := loaders.
NewFileLoader(c.Common, c.Cmd, c.GitConfig).
GetStatusFiles(loaders.GetStatusFileOptions{})
2020-09-29 12:03:39 +02:00
for _, file := range files {
if file.ShortStatus == "AD" {
2021-03-20 23:41:06 +02:00
if err := c.UnStageFile(file.Names(), false); err != nil {
2020-09-29 12:03:39 +02:00
return err
}
}
}
return nil
}