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"
|
2022-10-14 15:19:53 +02:00
|
|
|
"strings"
|
2021-12-30 08:19:01 +02:00
|
|
|
|
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
|
|
|
|
2022-01-02 01:34:33 +02:00
|
|
|
type StashCommands struct {
|
2022-01-18 12:26:21 +02:00
|
|
|
*GitCommon
|
2022-11-11 04:19:29 +02:00
|
|
|
fileLoader *FileLoader
|
2022-01-02 01:34:33 +02:00
|
|
|
workingTree *WorkingTreeCommands
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewStashCommands(
|
2022-01-18 12:26:21 +02:00
|
|
|
gitCommon *GitCommon,
|
2022-11-11 04:19:29 +02:00
|
|
|
fileLoader *FileLoader,
|
2022-01-02 01:34:33 +02:00
|
|
|
workingTree *WorkingTreeCommands,
|
|
|
|
) *StashCommands {
|
|
|
|
return &StashCommands{
|
2022-01-18 12:26:21 +02:00
|
|
|
GitCommon: gitCommon,
|
2022-01-02 01:34:33 +02:00
|
|
|
fileLoader: fileLoader,
|
|
|
|
workingTree: workingTree,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-08 13:30:43 +02:00
|
|
|
func (self *StashCommands) DropNewest() error {
|
2023-05-21 09:00:29 +02:00
|
|
|
cmdArgs := NewGitCmd("stash").Arg("drop").ToArgv()
|
2023-05-19 12:18:02 +02:00
|
|
|
|
2023-05-21 09:00:29 +02:00
|
|
|
return self.cmd.New(cmdArgs).Run()
|
2022-05-08 13:30:43 +02:00
|
|
|
}
|
|
|
|
|
2022-10-16 02:11:54 +02:00
|
|
|
func (self *StashCommands) Drop(index int) error {
|
2023-05-21 09:00:29 +02:00
|
|
|
cmdArgs := NewGitCmd("stash").Arg("drop", fmt.Sprintf("stash@{%d}", index)).
|
|
|
|
ToArgv()
|
2023-05-19 12:18:02 +02:00
|
|
|
|
2023-05-21 09:00:29 +02:00
|
|
|
return self.cmd.New(cmdArgs).Run()
|
2022-01-02 01:34:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (self *StashCommands) Pop(index int) error {
|
2023-05-21 09:00:29 +02:00
|
|
|
cmdArgs := NewGitCmd("stash").Arg("pop", fmt.Sprintf("stash@{%d}", index)).
|
|
|
|
ToArgv()
|
2023-05-19 12:18:02 +02:00
|
|
|
|
2023-05-21 09:00:29 +02:00
|
|
|
return self.cmd.New(cmdArgs).Run()
|
2022-01-02 01:34:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (self *StashCommands) Apply(index int) error {
|
2023-05-21 09:00:29 +02:00
|
|
|
cmdArgs := NewGitCmd("stash").Arg("apply", fmt.Sprintf("stash@{%d}", index)).
|
|
|
|
ToArgv()
|
2023-05-19 12:18:02 +02:00
|
|
|
|
2023-05-21 09:00:29 +02:00
|
|
|
return self.cmd.New(cmdArgs).Run()
|
2020-09-29 12:03:39 +02:00
|
|
|
}
|
|
|
|
|
2023-07-01 16:22:32 +02:00
|
|
|
// Push push stash
|
|
|
|
func (self *StashCommands) Push(message string) error {
|
|
|
|
cmdArgs := NewGitCmd("stash").Arg("push", "-m", message).
|
2023-05-21 09:00:29 +02:00
|
|
|
ToArgv()
|
2023-05-19 12:18:02 +02:00
|
|
|
|
2023-05-21 09:00:29 +02:00
|
|
|
return self.cmd.New(cmdArgs).Run()
|
2022-10-14 15:19:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (self *StashCommands) Store(sha string, message string) error {
|
|
|
|
trimmedMessage := strings.Trim(message, " \t")
|
2023-05-19 12:18:02 +02:00
|
|
|
|
2023-07-01 16:18:38 +02:00
|
|
|
cmdArgs := NewGitCmd("stash").Arg("store").
|
2023-05-21 09:00:29 +02:00
|
|
|
ArgIf(trimmedMessage != "", "-m", trimmedMessage).
|
2023-07-01 16:18:38 +02:00
|
|
|
Arg(sha).
|
2023-05-21 09:00:29 +02:00
|
|
|
ToArgv()
|
2023-05-19 12:18:02 +02:00
|
|
|
|
2023-05-21 09:00:29 +02:00
|
|
|
return self.cmd.New(cmdArgs).Run()
|
2020-09-29 12:03:39 +02:00
|
|
|
}
|
|
|
|
|
2022-10-16 02:11:54 +02:00
|
|
|
func (self *StashCommands) Sha(index int) (string, error) {
|
2023-05-21 09:00:29 +02:00
|
|
|
cmdArgs := NewGitCmd("rev-parse").
|
2023-05-19 12:18:02 +02:00
|
|
|
Arg(fmt.Sprintf("refs/stash@{%d}", index)).
|
2023-05-21 09:00:29 +02:00
|
|
|
ToArgv()
|
2023-05-19 12:18:02 +02:00
|
|
|
|
2023-05-21 09:00:29 +02:00
|
|
|
sha, _, err := self.cmd.New(cmdArgs).DontLog().RunWithOutputs()
|
2022-10-16 02:11:54 +02:00
|
|
|
return strings.Trim(sha, "\r\n"), err
|
|
|
|
}
|
|
|
|
|
2023-08-28 12:39:52 +02:00
|
|
|
func (self *StashCommands) ShowStashEntryCmdObj(index int) oscommands.ICmdObj {
|
2023-05-21 09:00:29 +02:00
|
|
|
cmdArgs := NewGitCmd("stash").Arg("show").
|
2023-05-19 12:18:02 +02:00
|
|
|
Arg("-p").
|
|
|
|
Arg("--stat").
|
|
|
|
Arg(fmt.Sprintf("--color=%s", self.UserConfig.Git.Paging.ColorArg)).
|
2023-08-28 13:09:55 +02:00
|
|
|
Arg(fmt.Sprintf("--unified=%d", self.AppState.DiffContextSize)).
|
2023-08-28 12:39:52 +02:00
|
|
|
ArgIf(self.AppState.IgnoreWhitespaceInDiffView, "--ignore-all-space").
|
2023-05-19 12:18:02 +02:00
|
|
|
Arg(fmt.Sprintf("stash@{%d}", index)).
|
2023-05-21 09:00:29 +02:00
|
|
|
ToArgv()
|
2022-01-05 02:57:32 +02:00
|
|
|
|
2023-05-21 09:00:29 +02:00
|
|
|
return self.cmd.New(cmdArgs).DontLog()
|
2020-09-29 12:03:39 +02:00
|
|
|
}
|
|
|
|
|
2022-04-08 11:32:23 +02:00
|
|
|
func (self *StashCommands) StashAndKeepIndex(message string) error {
|
2023-07-01 16:22:32 +02:00
|
|
|
cmdArgs := NewGitCmd("stash").Arg("push", "--keep-index", "-m", message).
|
2023-05-21 09:00:29 +02:00
|
|
|
ToArgv()
|
2023-05-19 12:18:02 +02:00
|
|
|
|
2023-05-21 09:00:29 +02:00
|
|
|
return self.cmd.New(cmdArgs).Run()
|
2022-04-08 11:32:23 +02:00
|
|
|
}
|
|
|
|
|
2022-04-14 21:45:55 +02:00
|
|
|
func (self *StashCommands) StashUnstagedChanges(message string) error {
|
2023-05-19 12:18:02 +02:00
|
|
|
if err := self.cmd.New(
|
|
|
|
NewGitCmd("commit").
|
2023-05-21 09:00:29 +02:00
|
|
|
Arg("--no-verify", "-m", "[lazygit] stashing unstaged changes").
|
|
|
|
ToArgv(),
|
2023-05-19 12:18:02 +02:00
|
|
|
).Run(); err != nil {
|
2022-04-14 21:45:55 +02:00
|
|
|
return err
|
|
|
|
}
|
2023-07-01 16:22:32 +02:00
|
|
|
if err := self.Push(message); err != nil {
|
2022-04-14 21:45:55 +02:00
|
|
|
return err
|
|
|
|
}
|
2023-05-19 12:18:02 +02:00
|
|
|
|
|
|
|
if err := self.cmd.New(
|
2023-05-21 09:00:29 +02:00
|
|
|
NewGitCmd("reset").Arg("--soft", "HEAD^").ToArgv(),
|
2023-05-19 12:18:02 +02:00
|
|
|
).Run(); err != nil {
|
2022-04-14 21:45:55 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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
|
2023-05-19 12:18:02 +02:00
|
|
|
if err := self.cmd.New(
|
2023-05-21 09:00:29 +02:00
|
|
|
NewGitCmd("stash").Arg("--keep-index").ToArgv(),
|
2023-05-19 12:18:02 +02:00
|
|
|
).Run(); err != nil {
|
2020-09-29 12:03:39 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-07-01 16:22:32 +02:00
|
|
|
if err := self.Push(message); err != nil {
|
2020-09-29 12:03:39 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-05-19 12:18:02 +02:00
|
|
|
if err := self.cmd.New(
|
2023-05-21 09:00:29 +02:00
|
|
|
NewGitCmd("stash").Arg("apply", "stash@{1}").ToArgv(),
|
2023-05-19 12:18:02 +02:00
|
|
|
).Run(); err != nil {
|
2020-09-29 12:03:39 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-05-19 12:18:02 +02:00
|
|
|
if err := self.os.PipeCommands(
|
2023-05-21 09:00:29 +02:00
|
|
|
self.cmd.New(NewGitCmd("stash").Arg("show", "-p").ToArgv()),
|
|
|
|
self.cmd.New(NewGitCmd("apply").Arg("-R").ToArgv()),
|
2023-05-19 12:18:02 +02:00
|
|
|
); err != nil {
|
2020-09-29 12:03:39 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-05-19 12:18:02 +02:00
|
|
|
if err := self.cmd.New(
|
2023-05-21 09:00:29 +02:00
|
|
|
NewGitCmd("stash").Arg("drop", "stash@{1}").ToArgv(),
|
2023-05-19 12:18:02 +02:00
|
|
|
).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.
|
2022-11-11 04:19:29 +02:00
|
|
|
GetStatusFiles(GetStatusFileOptions{})
|
2021-12-30 08:19:01 +02:00
|
|
|
|
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
|
|
|
|
}
|
2022-10-15 04:15:31 +02:00
|
|
|
|
2022-07-04 00:14:07 +02:00
|
|
|
func (self *StashCommands) StashIncludeUntrackedChanges(message string) error {
|
2023-05-19 12:18:02 +02:00
|
|
|
return self.cmd.New(
|
2023-07-01 16:22:32 +02:00
|
|
|
NewGitCmd("stash").Arg("push", "--include-untracked", "-m", message).
|
2023-05-21 09:00:29 +02:00
|
|
|
ToArgv(),
|
2023-05-19 12:18:02 +02:00
|
|
|
).Run()
|
2022-06-03 20:11:08 +02:00
|
|
|
}
|
|
|
|
|
2022-10-15 04:15:31 +02:00
|
|
|
func (self *StashCommands) Rename(index int, message string) error {
|
2022-10-16 02:11:54 +02:00
|
|
|
sha, err := self.Sha(index)
|
2022-10-15 04:15:31 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-10-16 02:11:54 +02:00
|
|
|
if err := self.Drop(index); err != nil {
|
|
|
|
return err
|
2022-10-15 04:15:31 +02:00
|
|
|
}
|
|
|
|
|
2022-10-16 02:11:54 +02:00
|
|
|
err = self.Store(sha, message)
|
2022-10-15 04:15:31 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|