2020-09-29 12:03:39 +02:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import "fmt"
|
|
|
|
|
|
|
|
// StashDo modify stash
|
|
|
|
func (c *GitCommand) StashDo(index int, method string) error {
|
2021-12-07 12:59:36 +02:00
|
|
|
return c.Run(c.NewCmdObj(fmt.Sprintf("git stash %s stash@{%d}", method, index)))
|
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-07 12:59:36 +02:00
|
|
|
return c.Run(c.NewCmdObj("git stash save " + c.OSCommand.Quote(message)))
|
2020-09-29 12:03:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetStashEntryDiff stash diff
|
|
|
|
func (c *GitCommand) ShowStashEntryCmdStr(index int) string {
|
2021-09-11 00:47:50 +02:00
|
|
|
return fmt.Sprintf("git stash show -p --stat --color=%s --unified=%d stash@{%d}", c.colorArg(), c.Config.GetUserConfig().Git.DiffContextSize, index)
|
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 {
|
2021-04-10 08:01:46 +02:00
|
|
|
// wrap in 'writing', which uses a mutex
|
2021-12-07 12:59:36 +02:00
|
|
|
if err := c.Run(c.NewCmdObj("git stash --keep-index")); err != nil {
|
2020-09-29 12:03:39 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := c.StashSave(message); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-12-07 12:59:36 +02:00
|
|
|
if err := c.Run(c.NewCmdObj("git stash apply stash@{1}")); 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-07 12:59:36 +02:00
|
|
|
if err := c.Run(c.NewCmdObj("git stash drop stash@{1}")); 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.
|
|
|
|
files := c.GetStatusFiles(GetStatusFileOptions{})
|
|
|
|
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
|
|
|
|
}
|