Skip to content

Clear preserved commit message when entering CommitEditorPanel #4558

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

ChrisMcD1
Copy link
Contributor

@ChrisMcD1 ChrisMcD1 commented May 11, 2025

  • PR Description

Fixes the problem described in #4557:

I often find myself initiating a commit message with c, but then halfway through typing the message, realize that is is likely to be more substantial than I thought. I press <esc> and then C to launch my editor where I can edit a larger commit body more effectively. I finish my commit message, close my editor, return to LazyGit and all is well.

Unfortunately, the next time I press c, creating a totally new commit, my partially completed message is still present. It no longer is relevant, so I have to delete the few words I typed before.

  • Please check if the PR fulfills these requirements
  • Cheatsheets are up-to-date (run go generate ./...)
  • Code has been formatted (see here)
  • Tests have been added/updated (see here for the integration test guide)
  • Text is internationalised (see here)
  • If a new UserConfig entry was added, make sure it can be hot-reloaded (see here)
  • Docs have been updated if necessary
  • You've read through your own file changes for silly mistakes etc

@jesseduffield jesseduffield added the bug Something isn't working label May 13, 2025
@stefanhaller
Copy link
Collaborator

Pretty basic implementation of this fix.

Looks good to me.

The one thing I considered doing that I didn't was to rename OnCommitSuccess,

I'd be in favor of doing that. Maybe ClearPreservedCommitMessage or something like that.

and one does it here to invoke a tag, which might be a separate bug)

I don't think that's a bug, but this call could be removed, because it does nothing (since we are opening the tags panel with preserveMessage=false, see below). Could be another, separate cleanup commit.

The implementation of OnCommitSuccess also leaves me a bit confused. I'm not sure why it doesn't clear the message every time. By checking with the CommitMessageViewModel.preserveMessage, we only open up the option that we don't clear an existing message should that be false. Is there some world where this is desired?

Yes. I think the idea is that creating tags doesn't take part in the remember-my-partial-commit-message business. I'm not sure how relevant this really is in practice, but something like:

  • start to make a commit, begin typing a message, cancel halfway through
  • create a tag; neither uses nor clears the preserved message
  • press c again to continue your commit. Message is still there.

@ChrisMcD1
Copy link
Contributor Author

I'd be in favor of doing that. Maybe ClearPreservedCommitMessage or something like that.

Stole your name because naming is hard 😆

I don't think that's a bug, but this call could be removed

Yeah, I misused the word "bug" there, I just meant "This is weird code". I have removed that call

I'm not sure how relevant this really is in practice

I have done some research and I believe this is now never relevant in practice. So I added a commit removing the check. With previous commits like 41a7afb we have been whittling down the usages of OnCommitSuccess, and at this point there are 3 call sites:

  1. working_tree_helper.handleCommit https://github.com/ChrisMcD1/lazygit/blob/2988df76aaa5fd1e0d31b57c7749984bc7ae328a/pkg/gui/controllers/helpers/working_tree_helper.go#L115
  2. working_tree_helper.HandleCommitEditorPress https://github.com/ChrisMcD1/lazygit/blob/2988df76aaa5fd1e0d31b57c7749984bc7ae328a/pkg/gui/controllers/helpers/working_tree_helper.go#L141
  3. working_tree.switchFromCommitMessagePanelToEditor https://github.com/ChrisMcD1/lazygit/blob/2988df76aaa5fd1e0d31b57c7749984bc7ae328a/pkg/gui/controllers/helpers/working_tree_helper.go#L127

which tracking their upstream leads to:

  1. handleCommit and switchFromCommitMessgePanelToEditor share the same singular callsite has preserveMessage: true https://github.com/ChrisMcD1/lazygit/blob/2988df76aaa5fd1e0d31b57c7749984bc7ae328a/pkg/gui/controllers/helpers/working_tree_helper.go#L94-L97
  2. HandleCommitEditorPress (this is the upstream)

^ This is the method we are adding this clearing to in this PR, and I'm thinking it should clear the preserved message regardless of the previous state of preserveMessage. If you begin a partial commit message with c, switch and create a tag with T, you have now made preserveMessage = false, so when you go back to committing, create a full commit message with C, you don't end up clearing the partial commit message. This feels less intuitive to me than "When I create a commit with C, I clear my partial message"

(But now that I've put the whole paragraph into words, I'm actually less convinced than when it was an idea floating around in my head. I'm open to reverting this last commit if people want)

@stefanhaller
Copy link
Collaborator

I'm not sure how relevant this really is in practice

I have done some research and I believe this is now never relevant in practice.

Misunderstanding (I think). I was talking about the relevance of the described behavior (i.e. that tag creation doesn't interfere with aborting commits and preserving their message), not about the relevance of some code. This is just a UX decision, you can't answer it by looking at code or at the commit history.

Personally I think the behavior is good the way it is. You might argue that starting to create an annotated tag, typing a message, canceling out, and starting over should also preserve the message for that, but I have never heard requests for this, and if we wanted it, we would probably have to preserve two different messages, one for commits and one for tags. But I'm not proposing we do that.

If you begin a partial commit message with c, switch and create a tag with T, you have now made preserveMessage = false, so when you go back to committing, create a full commit message with C, you don't end up clearing the partial commit message.

Ah, I wasn't aware of this scenario. I totally agree that this doesn't make sense, and that unconditionally clearing the message is better.

So the end state of the branch looks good to me now, but I would personally have chosen a different order of the commits to make things a bit clearer:

  1. Remove the call to OnCommitSuccess from tag creation
    • with a commit message explaining that not only is this a refactoring that doesn't change behavior, but also that this behavior is in fact the desired one because we don't want tag creation to interfere with commit message preservation
  2. Remove the "if" statement from OnCommitSuccess
  3. Rename OnCommitSuccess to ClearPreservedCommitMessage, and remove the comment from inside the function
  4. Call ClearPreservedCommitMessage from HandleCommitEditorPress

I'll leave it up to you if you want to change it to this.

@ChrisMcD1
Copy link
Contributor Author

I'll leave it up to you if you want to change it to this.

I like the sound of that history, but the renames make it marginally too annoying for me to want to move the commits around.

Chris McDonnell added 4 commits May 15, 2025 18:33
This is no change in behavior because OnCommitSuccess only clears the message
when the commit message panel was opened with preserveMessage=true, which it
isn't in the case of creating a tag.

And this is in fact the desired behavior, because we don't want creating a tag
to interfere with preserving commit messages in any way.
Previously it would only clear the message if the panel had been opened with
preserveMessage=true; we don't need this check, because callers know how they
opened the panel, and whether they want to clear the message.
In one case it was actually called *before* making a commit (when switching from
the commit message panel to committing in the editor). And clearing the
preserved message is the only thing it does, so name it after what it does
rather than when it's called.
@stefanhaller
Copy link
Collaborator

Hm, that literally took 5 minutes. I pushed 4557-preserved-commit-message-sh in case you want to hard-reset to it. (The tip is identical to yours, but I changed some of the commit messages; feel free to reword them further if you want, I didn't spend much time on it.)

@ChrisMcD1
Copy link
Contributor Author

Hm, I'm curious what your flow is that working through rebase conflicts isn't that annoying. On my first thought, I was going to move the commits around with a set of individual <c-j> and <c-k>. When I just did that, somehow my conflict resolution resulted in an invalid state where my final commit introduced OnCommitSuccess into HandleCommitEditorPress (at the point no such name existed...

I then tried it again using e on the base commit and moving around that way, and it went smoother. I was able to reproduce what you created. Should I be defaulting to that 2nd rebasing mode when dealing with conflict-likely areas?

Reset to your branch, and pushed

@ChrisMcD1 ChrisMcD1 force-pushed the 4557-preserved-commit-message branch from 2988df7 to 3bd8a92 Compare May 15, 2025 19:34
@stefanhaller
Copy link
Collaborator

Hm, I'm curious what your flow is that working through rebase conflicts isn't that annoying.

I didn't rebase at all, I just recreated the commits from scratch. The changes were so small that this was way faster than resolving conflicts. 😄

I then tried it again using e on the base commit and moving around that way, and it went smoother. I was able to reproduce what you created. Should I be defaulting to that 2nd rebasing mode when dealing with conflict-likely areas?

Yes, I recommend that in general. I use <c-j> and <c-k> outside of a rebase only when I want to move by only one step; for any distances larger than one, I always enter a rebase first. I do this both because it's faster, and because I have to deal with conflicts only once and not for each step.

@stefanhaller stefanhaller merged commit a0ec22c into jesseduffield:master May 15, 2025
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants