jesseduffield / lazygit

simple terminal UI for git commands

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

git clang-format

aosterhage opened this issue · comments

Is your feature request related to a problem? Please describe.
Before committing I often run git clang-format on changed files. I do not always do this so I do not use any git hook to run it.

Describe the solution you'd like
Single key-press that runs git clang-format on:

  • Staged changes (against HEAD)
  • A selected commit (against its parent) and automatically amends the commit with the diff output

Describe alternatives you've considered
I either:

  • run this command before running lazygit or
  • drop to the command line to execute this command outside of lazygit after I commit

Additional context
I think that implementing this for selected commits should be the most straightforward:

  • Stash all changes
  • Rebase to edit the commit in question
  • Run git clang-format --diff HEAD~ HEAD to generate a diff (there's an outstanding question on whether to support different parents but I would suggest not to, to simplify things and it seems like a rare use case)
  • Apply said diff, stage changes, amend commit
  • Complete rebase

Implementing this for staged changes would be the pinnacle of my desires:

  • Run git clang-format --diff --staged to generate a diff of staged changes
  • Apply said diff
  • Stage only the additional changes introduced from the diff

But I don't know how to do the last part. I suppose lazygit already has that figured out: if I can stage random hunks in files using space you could use the same mechanism?

I should have also mentioned: git clang-format requires that you have git-clang-format in your path and executable (as well as clang-format). I'm not sure exactly how this intersects with lazygit's implementation: is it okay to rely on external tools? If not then it would require a change to go-git to add clang-format support which is probably out of scope.

Have you tried using custom commands for that? I.e. run git clang-format on the selected file/all files?

Thanks for the suggestion. I had not considered custom commands and it does seem like an appropriate application. I attempted to get it working but was not quite successful. Regardless, I’ll close this request since I think custom commands is the right solution here given git clang-format’s external requirements. My suggestion to anyone who finds this in the future is to utilize custom commands to call a Python script that runs clang-format in the fashion you desire (e.g. if you want to run it on staged files than it should be identical to the common pre-commit hook scripts that are around).

ezoic increase your site revenue