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

Add option to show git diff before commit #4235

Closed
qianlongzt opened this issue Jan 27, 2025 · 8 comments · Fixed by #4236
Closed

Add option to show git diff before commit #4235

qianlongzt opened this issue Jan 27, 2025 · 8 comments · Fixed by #4236
Labels
enhancement New feature or request

Comments

@qianlongzt
Copy link
Contributor

qianlongzt commented Jan 27, 2025

Is your feature request related to a problem? Please describe.

I use git.autoPush to avoid forgetting to push configuration changes.
I want to know what changes were made before writing the commit message.

vim ~/.config/xxx
chezmoi add ~/.config/xxx

Image

Describe the solution you'd like

add new option git.showDiff(or else) to show git diff --cached before commit

Describe alternatives you've considered

  1. chezmoi diff shows changes in the opposite direction.
  2. chezmoi cd requires temporarily disabling git.autoCommit and git.autoPush.
  3. commitMessageTemplate = "{{output \"git\" \"diff\" \"--color=always\" \"--cached\" | promptString }}"
    1. Sometimes I get the error on windows git no option --cached
    2. chezmoi: template: git.commitMessageTemplate:1:2: executing "git.commitMessageTemplate" at <output "git" "diff" "--color=always" "--cached">: error calling output: 'D:\\scoop\\local\\shims\\git.exe' diff --color=always --cached: exit status 129

Image

Additional context

@twpayne
Copy link
Owner

twpayne commented Jan 27, 2025

#4236 adds git-auto-commit and git-auto-push hooks. With this, you can add a hooks.git-auto-commit.pre hook that runs git diff -C $CHEZMOI_SOURCE_DIR before invoking your commit message template to prompt for the commit message. Would you be able to test this?

@qianlongzt
Copy link
Contributor Author

Thanks for your work.
I tested it, but it runs the command in the home directory. Can we add an option to specify the working directory for the hook?

# ~/.config/chezmoi/chezmoi.toml.tmpl
[hooks.git-auto-commit.pre]
command = "git"
args = ["diff", "--cached", "--color=always"]

directory = {{.chezmoi.workingTree}}

[git]
autoCommit=true
autoPush=true

return c.run(c.homeDirAbsPath, name, args)

@twpayne
Copy link
Owner

twpayne commented Jan 28, 2025

You can use the -C option to git to change the directory that git runs in.

@qianlongzt
Copy link
Contributor Author

qianlongzt commented Jan 28, 2025

Thanks for the reply again. I missed the -C option in the first reply.

Now, it's asking for a commit message before running the git-auto-commit.pre hook. Can we prompt for the commit message after the pre-hook?

chezmoi/internal/cmd/config.go

Lines 1582 to 1593 in 3fdaa59

commitMessage, err := c.gitCommitMessage(cmd, status)
if err != nil {
return err
}
if err := c.runHookPre("git-auto-commit"); err != nil {
return err
}
if err := c.run(c.WorkingTreeAbsPath, c.Git.Command, []string{"commit", "--message", string(commitMessage)}); err != nil {
return err
}
if err := c.runHookPost("git-auto-commit"); err != nil {
return err

@halostatue
Copy link
Collaborator

I agree with this change, but this is better done by doing the following (well, in your dot_config/git/config.tmpl):

git config --global commit.verbose true

@qianlongzt
Copy link
Contributor Author

qianlongzt commented Jan 28, 2025

Thank you @halostatue . commit.verbose is better choice. No, it not work with chezmoi, chezmoi ignore the commit.verbose

@halostatue
Copy link
Collaborator

I thought I had not posted this, because I realized that it won't work because this is auto-commit (something I don't use).

@qianlongzt
Copy link
Contributor Author

Never mind, I learned git usage tips

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 a pull request may close this issue.

3 participants