2022-01-08 05:00:36 +02:00
|
|
|
package git_commands
|
2020-09-29 12:03:39 +02:00
|
|
|
|
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"
|
2022-01-02 01:34:33 +02:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/common"
|
2021-12-30 08:19:01 +02:00
|
|
|
)
|
2020-09-29 12:03:39 +02:00
|
|
|
|
2022-01-02 01:34:33 +02:00
|
|
|
type StashCommands struct {
|
|
|
|
*common.Common
|
|
|
|
|
|
|
|
cmd oscommands.ICmdObjBuilder
|
|
|
|
fileLoader *loaders.FileLoader
|
|
|
|
osCommand *oscommands.OSCommand
|
|
|
|
workingTree *WorkingTreeCommands
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewStashCommands(
|
|
|
|
common *common.Common,
|
|
|
|
cmd oscommands.ICmdObjBuilder,
|
|
|
|
osCommand *oscommands.OSCommand,
|
|
|
|
fileLoader *loaders.FileLoader,
|
|
|
|
workingTree *WorkingTreeCommands,
|
|
|
|
) *StashCommands {
|
|
|
|
return &StashCommands{
|
|
|
|
Common: common,
|
|
|
|
cmd: cmd,
|
|
|
|
fileLoader: fileLoader,
|
|
|
|
osCommand: osCommand,
|
|
|
|
workingTree: workingTree,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *StashCommands) Drop(index int) error {
|
|
|
|
return self.cmd.New(fmt.Sprintf("git stash drop stash@{%d}", index)).Run()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *StashCommands) Pop(index int) error {
|
|
|
|
return self.cmd.New(fmt.Sprintf("git stash pop stash@{%d}", index)).Run()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *StashCommands) Apply(index int) error {
|
|
|
|
return self.cmd.New(fmt.Sprintf("git stash apply stash@{%d}", index)).Run()
|
2020-09-29 12:03:39 +02:00
|
|
|
}
|
|
|
|
|
2022-01-02 01:34:33 +02:00
|
|
|
// Save save stash
|
2020-09-29 12:03:39 +02:00
|
|
|
// TODO: before calling this, check if there is anything to save
|
2022-01-02 01:34:33 +02:00
|
|
|
func (self *StashCommands) Save(message string) error {
|
|
|
|
return self.cmd.New("git stash save " + self.cmd.Quote(message)).Run()
|
2020-09-29 12:03:39 +02:00
|
|
|
}
|
|
|
|
|
2022-01-02 01:34:33 +02:00
|
|
|
func (self *StashCommands) ShowStashEntryCmdObj(index int) oscommands.ICmdObj {
|
|
|
|
cmdStr := fmt.Sprintf("git stash show -p --stat --color=%s --unified=%d stash@{%d}", self.UserConfig.Git.Paging.ColorArg, self.UserConfig.Git.DiffContextSize, index)
|
2022-01-05 02:57:32 +02:00
|
|
|
|
2022-01-02 01:34:33 +02:00
|
|
|
return self.cmd.New(cmdStr).DontLog()
|
2020-09-29 12:03:39 +02:00
|
|
|
}
|
|
|
|
|
2022-01-02 01:34:33 +02:00
|
|
|
// SaveStagedChanges stashes only the currently staged changes. This takes a few steps
|
2020-09-29 12:03:39 +02:00
|
|
|
// shoutouts to Joe on https://stackoverflow.com/questions/14759748/stashing-only-staged-changes-in-git-is-it-possible
|
2022-01-02 01:34:33 +02:00
|
|
|
func (self *StashCommands) SaveStagedChanges(message string) error {
|
2021-04-10 08:01:46 +02:00
|
|
|
// wrap in 'writing', which uses a mutex
|
2022-01-02 01:34:33 +02:00
|
|
|
if err := self.cmd.New("git stash --keep-index").Run(); err != nil {
|
2020-09-29 12:03:39 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-01-02 01:34:33 +02:00
|
|
|
if err := self.Save(message); err != nil {
|
2020-09-29 12:03:39 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-01-02 01:34:33 +02:00
|
|
|
if err := self.cmd.New("git stash apply stash@{1}").Run(); err != nil {
|
2020-09-29 12:03:39 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-01-02 01:34:33 +02:00
|
|
|
if err := self.osCommand.PipeCommands("git stash show -p", "git apply -R"); err != nil {
|
2020-09-29 12:03:39 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-01-02 01:34:33 +02:00
|
|
|
if err := self.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.
|
2022-01-02 01:34:33 +02:00
|
|
|
files := self.fileLoader.
|
2021-12-30 08:19:01 +02:00
|
|
|
GetStatusFiles(loaders.GetStatusFileOptions{})
|
|
|
|
|
2020-09-29 12:03:39 +02:00
|
|
|
for _, file := range files {
|
|
|
|
if file.ShortStatus == "AD" {
|
2022-01-02 01:34:33 +02:00
|
|
|
if err := self.workingTree.UnStageFile(file.Names(), false); err != nil {
|
2020-09-29 12:03:39 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|