Skip to content

Commit without pre-commit hooks action is independent on prefix #4374

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
merged 2 commits into from
Mar 22, 2025

Conversation

kschweiger
Copy link
Contributor

As discussed in #1984, this PR modifies the code such that the w command always commits without pre-commit hooks. The skipHookPrefix is still considered when starting the commit messaged with the prefix in the regular commit command using c.

Skipping pre-commits also works when switching to the editor and adding a co author from the the w command

I think this PR could also remove the SkipHookPrefixNotConfigured text in english and all internationalisations but I was not sure how this is handled.

  • 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

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.

This is a great start, but it does still need quite a bit of work; see comments below.

@@ -85,13 +85,11 @@ func (self *CommitCommands) ResetToCommit(hash string, strength string, envVars
Run()
}

func (self *CommitCommands) CommitCmdObj(summary string, description string) oscommands.ICmdObj {
func (self *CommitCommands) CommitCmdObj(summary string, description string, verify bool) oscommands.ICmdObj {
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is a super confusing name for this variable; it's backwards. It should be renamed to noVerify, or probably better to skipHooks (because variables with no in them are an invitation for double negations, which I have a strong aversion against).

But see further discussion below.

cmdArgs := NewGitCmd("commit").
ArgIf(skipHookPrefix != "" && strings.HasPrefix(summary, skipHookPrefix), "--no-verify").
ArgIf(verify, "--no-verify").
Copy link
Collaborator

Choose a reason for hiding this comment

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

I would propose to keep the previous logic here, and just "or" in the flag. The rationale is that there are several call sites that still want the old logic without having to pass in the flag themselves (I'll annotate them below).

Which means that the flag should probably be called forceSkipHooks, and be used like this:

ArgIf(forceSkipHooks || (skipHookPrefix != "" && strings.HasPrefix(summary, skipHookPrefix)), "--no-verify").

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I can get behind this. I did not particularly like having to put the logic into the handleCommit function anyways.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I adapted the code as you suggested with 97e05e5

configSignoff: false,
configSkipHookPrefix: "",
expectedArgs: []string{"commit", "-m", "test"},
},
{
testName: "Commit with --no-verify flag",
summary: "WIP: test",
verify: true,
Copy link
Collaborator

Choose a reason for hiding this comment

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

If we do what I suggest above (keeping the prefix logic in CommitCmdObj), then it would be good to add two more test cases here for the different combinations of prefix vs flag.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

See 97e05e5

@@ -303,7 +303,7 @@ func (self *PatchCommands) PullPatchIntoNewCommit(
return err
}

if err := self.commit.CommitCmdObj(commitSummary, commitDescription).Run(); err != nil {
if err := self.commit.CommitCmdObj(commitSummary, commitDescription, false).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.

This is a regression; previously it was possible to move a custom patch into a new commit with a WIP prefix and have this skip the hooks; now this is no longer possible. Keeping the prefix logic in CommitCmdObj would fix this.

Note that there are other problems while working with custom patches currently doesn't work well with WIP prefixes; see #3532. But those are unrelated to your PR and can be fixed separately.

@@ -481,7 +481,7 @@ func (self *MergeAndRebaseHelper) SquashMergeCommitted(refName, checkedOutBranch
"selectedRef": refName,
"currentBranch": checkedOutBranchName,
})
err = self.c.Git().Commit.CommitCmdObj(message, "").Run()
err = self.c.Git().Commit.CommitCmdObj(message, "", false).Run()
Copy link
Collaborator

Choose a reason for hiding this comment

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

Another regression: when setting the squashMergeMessage config to start with the skipHooksPrefix, then we would previously skip the hooks when committing the result of a squash merge. You might argue whether this was intentional, but I do think it makes sense. Again, keeping the prefix logic in CommitCmdObj fixes this.

Comment on lines 22 to 24
getCommitSummary func() string
setCommitSummary func(string)
getCommitDescription func() string
// getVerifyFlag func() bool
getCommitSummary func() string
setCommitSummary func(string)
getCommitDescription func() string
Copy link
Collaborator

Choose a reason for hiding this comment

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

This should go into the previous commit, I suppose. Also below.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

True. I kind of expected you would squash on merge 😄

Copy link
Collaborator

Choose a reason for hiding this comment

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

No, we don't do that here. On the contrary, I value clean and well-structured commit histories very much. (Not just for the end result of a branch when it is merged, but also during review, so I prefer a fixup-based workflow over just piling commits onto one another, which is what we are now doing here. Something to keep in mind for next time, it's too late for this one. 😄)


t.Views().Commits().Focus()
t.Views().Main().Content(Contains("foo bar"))
t.Views().Extras().Content(Contains("--no-verify"))
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is an interesting way of checking whether we passed the no-verify flag, but I'm not sure I like it much. If you had a test that committed twice, once with and once without the flag, you couldn't use this check for the second commit.

I'd prefer it if we installed an actual hook (using the technique in fail_hooks_then_commit_no_hooks.go), and have it write something to a file; we can then check whether the file exists (or what it contains) using t.FileSystem().PathPresent() or t.FileSystem().FileContent().

Copy link
Contributor Author

@kschweiger kschweiger left a comment

Choose a reason for hiding this comment

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

About 2a4911c:

I adapted this test to use an actual hook but I'm not quite certain this is what you meant. So this is more a draft and I'm happy to reiterate and update the other integration (e.g. CommitSwitchToEditorSkipHooks) tests if necessary.

Like this, the hook would update the file content and if you replace keys.Files.CommitChangesWithoutHook with keys.Files.CommitChanges the test fails.

Overall I'm fine with it and it is certainly more explicit but I'm not sure it is that much clearer than just looking at the --no-verify in the command log for this particular test.

@stefanhaller stefanhaller added the enhancement New feature or request label Mar 15, 2025
@stefanhaller
Copy link
Collaborator

I adapted this test to use an actual hook but I'm not quite certain this is what you meant.

Yes, this is close to what I meant, but I was wrong. 😄 We can simplify the tests quite a bit by installing a hook that always fails. This way we don't need to check for the existence of files, or for text appearing in the Extras view, in order to tell whether the hook ran; we can tell whether it ran or not simply by whether the commit succeeded. I pushed 98ac340 to do that for some of the tests; this needs further adaptions for the others, and I think we now have some redundancy between the tests. I was too lazy to look at that in more detail, can you please clean this up?

Another thing I noticed now (and missed in the first round of review) is that there's unnecessary work happening with storing the forceSkipHooks flag in the OpenCommitMessagePanelOpts, and then passing it back to the OnConfirm handler. This is unnecessary and confusing because it is only used by the OnConfirm of the HandleCommitPressWithMessage call; all other callsites ignored the flag, and the concept of skipping hooks doesn't even make sense e.g. for creating a tag. It's much cleaner to simply capture the flag in the OnConfirm lambda in the only place where we need it; I pushed 2a86979 to do that.

Feel free to squash everything into one commit now, and then to push further fixups for the remaining test adaptations.

@kschweiger kschweiger force-pushed the no_verify_wo_prefix branch from 98ac340 to 11842f7 Compare March 17, 2025 21:11
@kschweiger
Copy link
Contributor Author

I squashed all the previous commit and refactored the tests.

Only fail_hooks_then_commit_no_hooks.go explicitly tests that the hook fail with the regular commit. I'm usually a fan of making integration tests rather explicit even if not really necessary. So I added a shared function for the other tests that basically tries to commit w/ hooks, fails and resets the working directory. In this case my rational is that they all share the same "hook" and if somebody down the line would decide to change it, the tests might still succeed even though the hook would not fail.

@stefanhaller
Copy link
Collaborator

@kschweiger Great work, looks good to me. I agree that it's good to add the sanity check that the hook works at the beginning of each test.

I had to add a fixup (47bec2d) because one of the tests failed after rebasing onto #4346. Will merge when green, thanks for the contribution!

Add verify flag

Add and update integration tests

Rename verify to forceSkipHooks

Adapt CommitSkipHooks integration test to actually use a hook

Remove forceSkipHooks param from OnConfirm et al

Simplify tests
@stefanhaller
Copy link
Collaborator

Oh, and I also added a commit to remove the obsolete text.

@stefanhaller stefanhaller enabled auto-merge March 22, 2025 10:07
@stefanhaller stefanhaller merged commit a51bf12 into jesseduffield:master Mar 22, 2025
15 checks passed
mikavilpas added a commit to mikavilpas/dotfiles that referenced this pull request Mar 30, 2025
This was added in jesseduffield/lazygit#4374
which also changed the previous behaviour of the `w` keybinding to
not include a default message.

and discussed here
jesseduffield/lazygit#1984

^I found the solution from there too, thanks 🙏🏻
stefanhaller added a commit that referenced this pull request Apr 10, 2025
…#4456)

We removed prefilling the skipHook prefix in b102646 (#4374) with
the intention of making it clearer that using the prefix in normal
commits and typing `w` to skip hooks are now two independent features.

It turns out that some people liked it with prefilling the prefix and
perceive it as a regression, so put it back in.

But only if we don't have a preserved message; this is an important use
case, when you try to make a normal commit, the hook fails, and then you
want to make the same commit with skipping the hook, but with the same
message that you already typed.
tmeijn pushed a commit to tmeijn/dotfiles that referenced this pull request May 10, 2025
This MR contains the following updates:

| Package | Update | Change |
|---|---|---|
| [jesseduffield/lazygit](https://github.com/jesseduffield/lazygit) | minor | `v0.48.0` -> `v0.50.0` |

MR created with the help of [el-capitano/tools/renovate-bot](https://gitlab.com/el-capitano/tools/renovate-bot).

**Proposed changes to behavior should be submitted there as MRs.**

---

### Release Notes

<details>
<summary>jesseduffield/lazygit (jesseduffield/lazygit)</summary>

### [`v0.50.0`](https://github.com/jesseduffield/lazygit/releases/tag/v0.50.0)

[Compare Source](jesseduffield/lazygit@v0.49.0...v0.50.0)

<!-- Release notes generated using configuration in .github/release.yml at v0.50.0 -->

#### What's Changed

##### Enhancements 🔥

-   Continue/abort a conflicted cherry-pick or revert by [@&#8203;stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4441
-   Show todo items for pending cherry-picks and reverts by [@&#8203;stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4442
-   Use "git cherry-pick" for implementing copy/paste of commits by [@&#8203;stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4443
-   Allow reverting a range of commits by [@&#8203;stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4444
-   Section headers for rebase todos and actual commits by [@&#8203;stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4463
-   Focus the main view by [@&#8203;stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4429
-   Auto-forward main branches after fetching by [@&#8203;stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4493
-   Add new command "Move commits to new branch" by [@&#8203;stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#3876
-   Strip the '+' and '-' characters when copying parts of a diff to the clipboard by [@&#8203;stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4519
-   Reduce memory consumption of graph (pipe sets) by [@&#8203;stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4498

##### Fixes 🔧

-   Fix truncation of branches when scrolling branches panel to the left by [@&#8203;stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4483
-   Fix nvim-remote commands for fish shell by [@&#8203;SavingFrame](https://github.com/SavingFrame) in jesseduffield/lazygit#4506
-   Disallow creating custom patches when the diff context size is 0 by [@&#8203;stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4522
-   Remove double space between rebase todo and author columns by [@&#8203;stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4520

##### Maintenance ⚙️

-   Allow closing issues via github actions by [@&#8203;jesseduffield](https://github.com/jesseduffield) in jesseduffield/lazygit#4515

##### Docs 📖

-   Add Debian installation instructions alongside Ubuntu by [@&#8203;jmkim](https://github.com/jmkim) in jesseduffield/lazygit#4501
-   fix wording of random tip by [@&#8203;dawedawe](https://github.com/dawedawe) in jesseduffield/lazygit#4488

#### New Contributors

-   [@&#8203;jmkim](https://github.com/jmkim) made their first contribution in jesseduffield/lazygit#4501
-   [@&#8203;SavingFrame](https://github.com/SavingFrame) made their first contribution in jesseduffield/lazygit#4506
-   [@&#8203;dawedawe](https://github.com/dawedawe) made their first contribution in jesseduffield/lazygit#4488

**Full Changelog**: jesseduffield/lazygit@v0.49.0...v0.50.0

### [`v0.49.0`](https://github.com/jesseduffield/lazygit/releases/tag/v0.49.0)

[Compare Source](jesseduffield/lazygit@v0.48.0...v0.49.0)

<!-- Release notes generated using configuration in .github/release.yml at v0.49.0 -->

#### What's Changed

##### Enhancements 🔥

-   Support fish when running shell command by [@&#8203;stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4350
-   Add acme editor preset by [@&#8203;rakoo](https://github.com/rakoo) in jesseduffield/lazygit#4360
-   Support home and end as alternatives to '<' and '>' by [@&#8203;stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4396
-   Drop the git config cache when getting focus by [@&#8203;stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4376
-   Add a "Content of selected file" entry to the copy menu for commit files by [@&#8203;stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4341
-   Add root node in file tree by [@&#8203;stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4346
-   \[FEAT] Add Recursive Bulk Initialize and Update for Submodules by [@&#8203;cesarandr](https://github.com/cesarandr) in jesseduffield/lazygit#4259
-   Commit without pre-commit hooks action is independent on prefix by [@&#8203;kschweiger](https://github.com/kschweiger) in jesseduffield/lazygit#4374
-   Let users define custom icons and color for files on the config file by [@&#8203;hasecilu](https://github.com/hasecilu) in jesseduffield/lazygit#4395
-   Add "Absolute path" item to the file view's copy menu by [@&#8203;stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4410
-   Allow range drop stashes by [@&#8203;gaogao-qwq](https://github.com/gaogao-qwq) in jesseduffield/lazygit#4451
-   More navigation keybindings for confirmation panel by [@&#8203;stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4404
-   Resolve non-inline merge conflicts by [@&#8203;stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4431
-   Add `runCommand` function to Go template syntax + add support for templates in git `branchPrefix` setting by [@&#8203;ruudk](https://github.com/ruudk) in jesseduffield/lazygit#4438
-   Show "(hooks disabled)" in title bar of commit message editor by [@&#8203;stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4467
-   Add a command to select all commits of the current branch by [@&#8203;stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4448

##### Fixes 🔧

-   Use a waiting status for rewording a non-head commit by [@&#8203;stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4343
-   Fix layout of options view for non-english languages by [@&#8203;stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4359
-   Fix changing language while lazygit is running by [@&#8203;stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4361
-   URL encode gitlab brackets to make consistent with branch names by [@&#8203;ChrisMcD1](https://github.com/ChrisMcD1) in jesseduffield/lazygit#4336
-   Fix commitPrefix setting with empty pattern by [@&#8203;stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4381
-   Commit only tracked files in tracked filter view by [@&#8203;parthokunda](https://github.com/parthokunda) in jesseduffield/lazygit#4386
-   Revert [#&#8203;4313](jesseduffield/lazygit#4313) (Skip post-checkout hook when discarding changes) by [@&#8203;stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4407
-   Enhance support for GPG signed tags by [@&#8203;ChrisMcD1](https://github.com/ChrisMcD1) in jesseduffield/lazygit#4394
-   Fix checking out a file from a range selection of commits by [@&#8203;stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4423
-   Fix discarding submodule changes in nested folders by [@&#8203;brandondong](https://github.com/brandondong) in jesseduffield/lazygit#4317
-   Better support for shell aliases by [@&#8203;stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4385
-   Fix hyperlinks in last line of confirmation popups by [@&#8203;stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4454
-   Fix wrong main view content after pressing `e` in a stack of branches by [@&#8203;stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4450
-   fix: update vscode color to logo color by [@&#8203;PeterCardenas](https://github.com/PeterCardenas) in jesseduffield/lazygit#4459
-   Fix crash with drag selecting and staging by [@&#8203;stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4480
-   Escape special characters in filenames when git-ignoring files by [@&#8203;stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4475

##### Maintenance ⚙️

-   Fix linter warnings by [@&#8203;stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4352
-   Fix release schedule again by [@&#8203;stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4364
-   Update to go 1.24 by [@&#8203;radsaq](https://github.com/radsaq) in jesseduffield/lazygit#4377
-   Add an integration test for a config with a negative refspec by [@&#8203;radsaq](https://github.com/radsaq) in jesseduffield/lazygit#4382
-   Specify a go release minor version by [@&#8203;radsaq](https://github.com/radsaq) in jesseduffield/lazygit#4393
-   Fix flaky integration test by [@&#8203;stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4432
-   Some code cleanup by [@&#8203;stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4449
-   Bump the minimum required git version to 2.22 by [@&#8203;stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4439
-   Bump go-git, and get rid of github.com/imdario/mergo by [@&#8203;stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4460
-   Skip date check when release worfklow is manually invoked by [@&#8203;jesseduffield](https://github.com/jesseduffield) in jesseduffield/lazygit#4484

##### Docs 📖

-   Include empty arrays and maps in config docs by [@&#8203;stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4413
-   Filter out deprecated user config fields from generated Config.md by [@&#8203;karimkhaleel](https://github.com/karimkhaleel) in jesseduffield/lazygit#4416
-   Corrected interactive rebase keybind example in README.md by [@&#8203;NewtonChutney](https://github.com/NewtonChutney) in jesseduffield/lazygit#4426
-   Update kidpix link in README to active url by [@&#8203;ChrisMcD1](https://github.com/ChrisMcD1) in jesseduffield/lazygit#4425

##### I18n 🌎

-   Update translation files from Crowdin by [@&#8203;stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4473

#### New Contributors

-   [@&#8203;rakoo](https://github.com/rakoo) made their first contribution in jesseduffield/lazygit#4360
-   [@&#8203;radsaq](https://github.com/radsaq) made their first contribution in jesseduffield/lazygit#4377
-   [@&#8203;cesarandr](https://github.com/cesarandr) made their first contribution in jesseduffield/lazygit#4259
-   [@&#8203;kschweiger](https://github.com/kschweiger) made their first contribution in jesseduffield/lazygit#4374
-   [@&#8203;NewtonChutney](https://github.com/NewtonChutney) made their first contribution in jesseduffield/lazygit#4426
-   [@&#8203;gaogao-qwq](https://github.com/gaogao-qwq) made their first contribution in jesseduffield/lazygit#4451
-   [@&#8203;ruudk](https://github.com/ruudk) made their first contribution in jesseduffield/lazygit#4438

**Full Changelog**: jesseduffield/lazygit@v0.48.0...v0.49.0

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.

♻ **Rebasing**: Whenever MR becomes conflicted, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this MR and you won't be reminded about this update again.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this MR, check this box

---

This MR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate).
<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiI0MC4xMS4yIiwidXBkYXRlZEluVmVyIjoiNDAuMTEuMiIsInRhcmdldEJyYW5jaCI6Im1haW4iLCJsYWJlbHMiOlsiUmVub3ZhdGUgQm90Il19-->
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.

2 participants