Skip to content

Conversation

@ajhynes7
Copy link
Contributor

@ajhynes7 ajhynes7 commented Jun 3, 2022

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

Relevant issue: #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

@mark2185
Copy link
Collaborator

mark2185 commented Jun 3, 2022

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

@ajhynes7
Copy link
Contributor Author

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

@mark2185
Copy link
Collaborator

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? :)

@ajhynes7
Copy link
Contributor Author

Sorry haha I haven't looked at this since.

@mark2185
Copy link
Collaborator

mark2185 commented Jun 21, 2022

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

}

func (self *StashCommands) StashUntrackedChanges(message string) error {
if err := self.cmd.New("git stash -u").Run(); err != nil {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Redundant if, you could just return self.cmd.New().

I'd also suggest using the extended form of the flag, i.e. --include-untracked, just so it's easier to see what's going on.

@mark2185
Copy link
Collaborator

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

@ajhynes7
Copy link
Contributor Author

It's passing for me locally now.

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

Copy link
Owner

@jesseduffield jesseduffield left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couple things :)

return nil
}

func (self *StashCommands) StashUntrackedChanges(message string) error {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

StashChangedIncludingUntracked is a little more correct

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok I've changed it now to StashIncludeUntrackedChanges to be consistent with LcStashIncludeUntrackedChanges.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But I can change those names again if you'd prefer.


func (self *FilesController) handleStashSave(stashFunc func(message string) error, action string, errorMsg string) error {
if !self.helpers.WorkingTree.IsWorkingTreeDirty() {
if action != self.c.Tr.Actions.StashIncludeUntrackedChanges && !self.helpers.WorkingTree.IsWorkingTreeDirty() {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's the intention behind this line?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The method IsWorkingTreeDirty returns false if there are only untracked files, so the original expression evaluates to true and an error is raised. But I want to be able to stash these untracked files even if they're the only ones shown by git status.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. Rather than have conditionally logic inside this function for the sake of one use case, I say we pull the check out to the places that actually need it. This will add some duplication but I much prefer duplication to a clunky abstraction. So in the end handleStashSave will only call self.c.Prompt and it'll be up to the calling function to handle the validation

@ajhynes7 ajhynes7 changed the title Add stash option to include untracked files feat: add stash option to include untracked files Jul 3, 2022
Copy link
Owner

@jesseduffield jesseduffield left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just one thing


func (self *FilesController) handleStashSave(stashFunc func(message string) error, action string, errorMsg string) error {
if !self.helpers.WorkingTree.IsWorkingTreeDirty() {
if action != self.c.Tr.Actions.StashIncludeUntrackedChanges && !self.helpers.WorkingTree.IsWorkingTreeDirty() {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. Rather than have conditionally logic inside this function for the sake of one use case, I say we pull the check out to the places that actually need it. This will add some duplication but I much prefer duplication to a clunky abstraction. So in the end handleStashSave will only call self.c.Prompt and it'll be up to the calling function to handle the validation

@ajhynes7
Copy link
Contributor Author

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.

@jesseduffield
Copy link
Owner

@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',
			},
			...

@ajhynes7 ajhynes7 requested a review from jesseduffield July 28, 2022 13:17
@ajhynes7 ajhynes7 requested review from jesseduffield and mark2185 and removed request for jesseduffield and mark2185 September 14, 2022 16:20
@mark2185
Copy link
Collaborator

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.

@ajhynes7 ajhynes7 force-pushed the stash-untracked-changes branch from 9b62a86 to e189546 Compare September 16, 2022 00:19
@ajhynes7 ajhynes7 requested review from jesseduffield and mark2185 and removed request for mark2185 October 7, 2022 01:34
@ajhynes7
Copy link
Contributor Author

ajhynes7 commented Nov 1, 2022

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

Copy link
Owner

@jesseduffield jesseduffield left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couple things :)

assert.InMenu()

input.PressKeys("a")
input.Type("stash name")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should assert that when 'a' is pressed, a prompt appears. Here's an example from another file:

input.PressKeys("a")
assert.InPrompt()
assert.MatchCurrentViewTitle(Equals("Enter a file name"))
input.Type("my file")


input.PressKeys("U")
input.Type("stash name")
input.Confirm()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above

"github.com/jesseduffield/lazygit/pkg/integration/tests/custom_commands"
"github.com/jesseduffield/lazygit/pkg/integration/tests/interactive_rebase"
"github.com/jesseduffield/lazygit/pkg/integration/tests/stash"
"github.com/jesseduffield/lazygit/pkg/utils"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we've got some linting issues that need to be fixed here

@ajhynes7
Copy link
Contributor Author

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

Copy link
Owner

@jesseduffield jesseduffield left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

one more thing

return self.c.PostRefreshUpdate(self.context())
}

func (self *FilesController) handleStashSave(stashFunc func(message string) error, action string, errorMsg string) error {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can remove errorMsg because it's no longer used here

@jesseduffield jesseduffield merged commit b33ec5a into jesseduffield:master Nov 14, 2022
@jesseduffield
Copy link
Owner

Great work @ajhynes7 !

@ajhynes7
Copy link
Contributor Author

Awesome, thanks @jesseduffield and @mark2185!

@ajhynes7 ajhynes7 deleted the stash-untracked-changes branch November 14, 2022 13:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants