lazygit icon indicating copy to clipboard operation
lazygit copied to clipboard

feat: add stash option to include untracked files

Open ajhynes7 opened this issue 2 years ago • 11 comments

This PR adds an option to stash all changes including untracked files.

Relevant issue: https://github.com/jesseduffield/lazygit/issues/1974

Unfortunately the integration test is failing for me locally with this error:

Diff:
--- Expected
+++ Actual
@@ -19,3 +19,3 @@
git stash list:
-stash@{0}: WIP on master: 592d439 Initial commit
+stash@{0}: WIP on master: 393c632 Initial commit

But I don't know how to fix that, so I figured I'd commit it anyways and get some feedback from the reviewers.

Screenshot

image

ajhynes7 avatar Jun 03 '22 19:06 ajhynes7

The only thing I'd suggest is to move the option below all the other "stash all changes" options, i.e. the third row.

mark2185 avatar Jun 03 '22 19:06 mark2185

Do you know how to fix the integration test failure I posted above?

ajhynes7 avatar Jun 10 '22 23:06 ajhynes7

Ah, I hadn't noticed that part. I'll take a look at it first thing in the morning and get back to you.

Unless you've had some luck in the meantime? :)

mark2185 avatar Jun 13 '22 20:06 mark2185

Sorry haha I haven't looked at this since.

ajhynes7 avatar Jun 14 '22 00:06 ajhynes7

Said morning had a lot of first things

In any case, I noticed that the given stash message is never actually used, it always ends up as WIP on <branch>. ~I presume some if goes the wrong way. Look into this if / this function.~

And let us know if you need any pointers :)

EDIT: looks like fixing that and giving the stash a message actually fixes the integration tests

mark2185 avatar Jun 21 '22 09:06 mark2185

Do integration tests work for you now? Can't seem to pass locally.

If it fails for you too, could you try adding a random message for stashing in the test? That seems to fix it

mark2185 avatar Jun 21 '22 21:06 mark2185

It's passing for me locally now.

❯ go test ./pkg/gui -run /stashIncludeUntrackedChanges
ok  	github.com/jesseduffield/lazygit/pkg/gui	4.589s

ajhynes7 avatar Jun 21 '22 21:06 ajhynes7

So in the end handleStashSave will only call self.c.Prompt and it'll be up to the calling function to handle the validation

Sorry, I can't figure out how to do this. The stashFunc function needs to check if the working tree is dirty, but I don't see how to access the IsWorkingTreeDirty method in the stash functions, which are located in pkg/commands/git_commands/stash.go. I can't import the FilesController type into this file.

I'm not a Go developer so I might just be missing something here.

ajhynes7 avatar Jul 25 '22 21:07 ajhynes7

@ajhynes7 this can all happen within the files controller. We can remove the validation from handleStashSave like so:

func (self *FilesController) handleStashSave(stashFunc func(message string) error, action string) error {
	return 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 self.c.Error(err)
			}
			return self.c.Refresh(types.RefreshOptions{Scope: []types.RefreshableView{types.STASH, types.FILES}})
		},
	})
}

And then we can pull the validation up to the callsite like so:

func (self *FilesController) createStashMenu() error {
	return self.c.Menu(types.CreateMenuOptions{
		Title: self.c.Tr.LcStashOptions,
		Items: []*types.MenuItem{
			{
				Label: self.c.Tr.LcStashAllChanges,
				OnPress: func() error {
					if !self.helpers.WorkingTree.IsWorkingTreeDirty() {
						return self.c.ErrorMsg(self.c.Tr.NoFilesToStash)
					}

					return self.handleStashSave(self.git.Stash.Save, self.c.Tr.Actions.StashAllChanges)
				},
				Key: 'a',
			},
			...

jesseduffield avatar Jul 25 '22 22:07 jesseduffield

Since there's a transition to a new integration testing system, would you kindly create the test in the new framework? Just so we don't have to convert them down the road.

Here are the instructions.

mark2185 avatar Sep 14 '22 17:09 mark2185

There aren't any tests for stashing in the new framework, so I tried to write one for a basic stash. It's failing for me though, because the length of the StashEntries array is still 0. Do you know how to fix that?

ajhynes7 avatar Sep 16 '22 13:09 ajhynes7

Hi, just checking in about this PR. Is this still under review?

ajhynes7 avatar Nov 01 '22 18:11 ajhynes7

Had to deal with some merge conflicts. Should be good for another review now.

ajhynes7 avatar Nov 12 '22 21:11 ajhynes7

Great work @ajhynes7 !

jesseduffield avatar Nov 14 '22 07:11 jesseduffield

Awesome, thanks @jesseduffield and @mark2185!

ajhynes7 avatar Nov 14 '22 13:11 ajhynes7