Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 86 additions & 0 deletions pkg/gui/controllers/staging_controller.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package controllers

import (
"errors"
"fmt"
"strings"

Expand Down Expand Up @@ -108,6 +109,13 @@ func (self *StagingController) GetKeybindings(opts types.KeybindingsOpts) []*typ
Description: self.c.Tr.FindBaseCommitForFixup,
Tooltip: self.c.Tr.FindBaseCommitForFixupTooltip,
},
{
Keys: opts.GetKeys(opts.Config.Files.ViewStashOptions),
Handler: self.stashOptions,
Description: self.c.Tr.Stash,
Tooltip: self.c.Tr.Stash,
DisplayOnScreen: true,
},
}
}

Expand Down Expand Up @@ -351,3 +359,81 @@ func (self *StagingController) editHunk() error {
func (self *StagingController) FilePath() string {
return self.c.Contexts().Files.GetSelectedPath()
}

func (self *StagingController) handleStash(stashFunc func(message string) error, action string) error {
self.c.Prompt(types.PromptOpts{
Title: self.c.Tr.StashChanges,
HandleConfirm: func(stashComment string) error {
self.c.LogAction(action)
if err := stashFunc(stashComment); err != nil {
return err
}

self.c.Refresh(types.RefreshOptions{Scope: []types.RefreshableView{types.STASH, types.STAGING}})
return nil
},
AllowEmptyInput: true,
})
return nil
}

func (self *StagingController) stashOptions() error {
return self.c.Menu(types.CreateMenuOptions{
Title: self.c.Tr.StashOptions,
Items: []*types.MenuItem{
{
Label: self.c.Tr.StashAllChanges,
OnPress: func() error {
if !self.c.Helpers().WorkingTree.IsWorkingTreeDirtyExceptSubmodules() {
return errors.New(self.c.Tr.NoFilesToStash)
}
return self.handleStash(self.c.Git().Stash.Push, self.c.Tr.Actions.StashAllChanges)
},
Keys: menuKey('a'),
},
{
Label: self.c.Tr.StashAllChangesKeepIndex,
OnPress: func() error {
if !self.c.Helpers().WorkingTree.IsWorkingTreeDirtyExceptSubmodules() {
return errors.New(self.c.Tr.NoFilesToStash)
}
return self.handleStash(self.c.Git().Stash.StashAndKeepIndex, self.c.Tr.Actions.StashAllChangesKeepIndex)
},
Keys: menuKey('i'),
},

{
Label: self.c.Tr.StashIncludeUntrackedChanges,
OnPress: func() error {
return self.handleStash(self.c.Git().Stash.StashIncludeUntrackedChanges, self.c.Tr.Actions.StashIncludeUntrackedChanges)
},
Keys: menuKey('U'),
},
{
Label: self.c.Tr.StashStagedChanges,
OnPress: func() error {
// there must be something in staging otherwise the current implementation mucks the stash up
if !self.c.Helpers().WorkingTree.AnyStagedFilesExceptSubmodules() {
return errors.New(self.c.Tr.NoTrackedStagedFilesStash)
}
return self.handleStash(self.c.Git().Stash.SaveStagedChanges, self.c.Tr.Actions.StashStagedChanges)
},
Keys: menuKey('s'),
},
{
Label: self.c.Tr.StashUnstagedChanges,
OnPress: func() error {
if !self.c.Helpers().WorkingTree.IsWorkingTreeDirtyExceptSubmodules() {
return errors.New(self.c.Tr.NoFilesToStash)
}
if self.c.Helpers().WorkingTree.AnyStagedFilesExceptSubmodules() {
return self.handleStash(self.c.Git().Stash.StashUnstagedChanges, self.c.Tr.Actions.StashUnstagedChanges)
}
// ordinary stash
return self.handleStash(self.c.Git().Stash.Push, self.c.Tr.Actions.StashUnstagedChanges)
},
Keys: menuKey('u'),
},
},
})
}