Skip to content

feat: ability to show diff when failing on changes#1227

Merged
mrexox merged 2 commits intoevilmartians:masterfrom
scop:feat/fail-on-changes-diff
Dec 15, 2025
Merged

feat: ability to show diff when failing on changes#1227
mrexox merged 2 commits intoevilmartians:masterfrom
scop:feat/fail-on-changes-diff

Conversation

@scop
Copy link
Contributor

@scop scop commented Dec 6, 2025

Closes #1171

Draft: untested. Tested now.

Context

See above issue for discussion.

Changes

Add a cmdline flag and config option for controlling the behavior, default to on in CI.

@scop scop force-pushed the feat/fail-on-changes-diff branch 4 times, most recently from 45ab893 to 361f71f Compare December 9, 2025 06:51
@scop scop force-pushed the feat/fail-on-changes-diff branch from 361f71f to d57cdd8 Compare December 9, 2025 06:55
@scop scop marked this pull request as ready for review December 9, 2025 06:55
@scop scop requested a review from mrexox as a code owner December 9, 2025 06:55
return nil
}

func (g *guard) changesetDiff(changesetAfter map[string]string) {
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 don't particularly like the interface of the method here, I think I would have rather had it return the diff and an error, and checked g.failOnChangesDiff at the call site. However exposing more ifs in after() runs afoul with the nestif linter. But nevermind, I can live with this :)

if !g.failOnChangesDiff {
return
}
changed := make([]string, 0, len(g.changesetBefore))
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We don't currently meet the test coverage target. Calculating changed could be extracted to another function and tests added for it if seen better that way to catch up some. That calculation isn't that complex though, fine with me either way, let me know.

if diff, err := g.git.Git.BatchedCmd(diffCmd, changed); err != nil {
log.Warnf("Couldn't diff changed files: %s", err)
} else {
log.Println(diff)
Copy link
Contributor Author

@scop scop Dec 9, 2025

Choose a reason for hiding this comment

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

As long as we are outputting the diff content from here (and not returning as a string as mentioned in an earlier comment above), we could just let the git diff output out directly. Capturing and re-printing does not add value here, just some memory consumption. BatchedCmd and Cmd lack the ability to do that at the moment though -- should we add that and make use of it here, or do you think this is fine for now?

Copy link
Member

@mrexox mrexox left a comment

Choose a reason for hiding this comment

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

I like this feature! What do you think about always showing diff on fail changes? If needed it may be controlled by output setting, but I think in most cases you'd like to see what has changed.

I can work on diff styling later to display it not as the command output, but as lefthook output (with leading | ).

@scop
Copy link
Contributor Author

scop commented Dec 14, 2025

What do you think about always showing diff on fail changes?

As noted in #1171, talking about the default: "i.e. not show the diff if not in CI, because one can easily figure out locally if need be, and the diff output may wreak havoc in the tool doing the commit/showing its output"

For example, if an IDE shows the Lefthook output in a pop-up dialog, or something like https://git-cola.github.io shows it, having the diff in it may become quite a mess. (Even the usual "fancy" Lefthook output we have tends to do that.) And the diff may be huge, etc etc.

I can work on diff styling later to display it not as the command output, but as lefthook output (with leading | ).

FWIW, I would personally prefer it to be shown as possibly colorized but otherwise unstyled plain diff.

@mrexox mrexox merged commit a4223e9 into evilmartians:master Dec 15, 2025
10 checks passed
@scop scop deleted the feat/fail-on-changes-diff branch December 15, 2025 20:27
@scop scop restored the feat/fail-on-changes-diff branch December 15, 2025 20:27
@scop scop deleted the feat/fail-on-changes-diff branch December 15, 2025 20:27
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.

Ability to show diff on fail_on_changes

2 participants