Skip to content
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

Preserve pending commit message when closing/re-opening #4191

Conversation

AzraelSec
Copy link
Contributor

@AzraelSec AzraelSec commented Jan 21, 2025

  • PR Description
    This PR allows lazygit to preserve the commit messages when the commit popup gets closed.

While discussing the feature as part of its issue, two approaches were taken into consideration:

  • to store the commit content as part of the global state file
  • to store the commit content in a special file to place inside the .git folder

I opted for the second approach to avoid worrying about associating each preserved message to the worktree it belongs to. I am happy to reconsider this and opt for the alternative approach, but I wanted to discuss this with the maintainers before deciding.

Note: The preserving file (.git/LAZYGIT_PENDING_COMMIT) is deleted when the commit is finalized or when the commit content becomes empty.

  • 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

@AzraelSec AzraelSec force-pushed the save-commit-message-for-current-branch branch from b643d5e to c3b8e04 Compare January 21, 2025 00:54
@AzraelSec
Copy link
Contributor Author

@jesseduffield @stefanhaller - I gave a shot to the approach of storing the pending commit message in a custom file placed in the .git folder instead of using the global state. This should guarantee that it gets preserved between different worktrees, but let me know if you prefer the other approach, and I'll jump on it.

Also, I created two commits so that we could ensure that the original commit persistence test ran correctly before updating it.

@jesseduffield
Copy link
Owner

Nice, I'll give this a test today

@stefanhaller
Copy link
Collaborator

This looks mostly good, and I tested it and it works great. I also like the decision to store the message in a file in .git/.

I only have a concern about error handling; I don't like it that so many functions now return an error that previously didn't. For example, CloseCommitMessagePanel now returns an error, and this feels wrong because closing a window is one of the operations that ought to be possible to do without having to handle errors.

The problem with this is that we now have two different classes of errors, really important ones and then these less important ones when storing the commit message fails (which, let's be honest, will never fail in practice). And this is a problem because it makes error handling more complex; for a function that first runs a git operation and then closes the panel, it now has to check if the git operation failed, and if so return that error (even if the closePanel function also returned one). This is cumbersome and error-prone, so I'd prefer it if these minor, unimportant failures were not reported through error return values, but maybe only logged (like we do for SaveAppStateAndLogError, for example).

@AzraelSec AzraelSec force-pushed the save-commit-message-for-current-branch branch 3 times, most recently from bec599c to 5789d9c Compare February 4, 2025 21:46
@AzraelSec
Copy link
Contributor Author

@stefanhaller - I refactored the branch to log errors instead of handling them when setting or getting the persisted commit message, as you suggested (it honestly makes sense - I wasn't aware that pattern was already part of the codebase). Let me know if this version works better.

@stefanhaller stefanhaller added the enhancement New feature or request label Feb 6, 2025
Copy link
Collaborator

@stefanhaller stefanhaller left a comment

Choose a reason for hiding this comment

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

Much better, looks great to me now. Just two small things below.

One word about this convention of prefixing commit messages with feat: or test: (again): I really don't like it, and I wish we wouldn't use it here. It looks ugly and provides no value to me; on the contrary, it constantly makes me go and verify whether the prefixes are really correct. I find this distracting. If the goal is that this helps you make sure you separate refactorings from adding features, then I do this anyway all the time, and I don't need those prefixes for that.

}
if pendingCommitExists && len(message) == 0 {
return self.c.Fs.Remove(preservedFilePath)
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

I find the logic of this method a bit unnecessarily complicated, and pushed a fixup to simplify it (6f07e2c). Let me know what you think.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@stefanhaller - happy to accept your suggestion, we don't really need to distinguish between presence vs deletion errors. I'll rebase-squash, thanks.

@@ -86,7 +86,7 @@ func (self *CommitMessageContext) GetPreserveMessage() bool {
return self.viewModel.preserveMessage
}

func (self *CommitMessageContext) GetPreservedMessage() (string, error) {
func (self *CommitMessageContext) getPreservedMessage() (string, error) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't really see a reason why this shouldn't be squashed into the first commit of the branch.

@AzraelSec AzraelSec force-pushed the save-commit-message-for-current-branch branch from 6f07e2c to 471ba9d Compare February 6, 2025 21:45
@AzraelSec
Copy link
Contributor Author

One word about this convention of prefixing commit messages with feat: or test: (again): I really don't like it, and I wish we wouldn't use it here. It looks ugly and provides no value to me; on the contrary, it constantly makes me go and verify whether the prefixes are really correct. I find this distracting. If the goal is that this helps you make sure you separate refactorings from adding features, then I do this anyway all the time, and I don't need those prefixes for that.

I'm used to conventional commits since they allow us to automate the creation of release messages, but since we don't use it that way, I'm happy to drop it. In this specific case, I created a test-specific commit so that we could ensure that the test was working with the new code before updating it. In other circumstances, I would have used a single commit to keep the feature + its test(s) in the same place.

@stefanhaller - I rebased the branch on the current master, squashed your fixup and modified the commits to strip the conventional commits prefixes.

@stefanhaller stefanhaller force-pushed the save-commit-message-for-current-branch branch from 471ba9d to 6065908 Compare February 7, 2025 08:28
@stefanhaller stefanhaller merged commit a0aa7a1 into jesseduffield:master Feb 7, 2025
15 checks passed
@stefanhaller
Copy link
Collaborator

In this specific case, I created a test-specific commit so that we could ensure that the test was working with the new code before updating it. In other circumstances, I would have used a single commit to keep the feature + its test(s) in the same place.

Yes, I noticed that and I support the decision. Still, I think the conventional commits prefixes don't help with understanding that. (Personally, I would have mentioned this rationale in the commit message body of the second commit.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants