[neomutt-devel] clang-format pre-commit hook

Julian Andres Klode jak at jak-linux.org
Sun Jun 25 11:37:00 CEST 2017


On Sat, Jun 03, 2017 at 06:23:32PM +0200, Julian Andres Klode wrote:
> Hi everyone,
> 
> I created a simple pre-commit hook to automatically run
> clang-format on all changed files and prevent a commit
> if there is a formatting error. This is quite useful.
> 
> ==== START OF SCRIPT
> result=0
> for file in `git diff-index --cached --name-only HEAD` ; do
>   clang-format  ${file} > ${GIT_DIR}/current_file
>   colordiff --label "$file (to be committed)" --label "$file (reformatted)" -u ${file} ${GIT_DIR}/current_file || result=1
> done
> rm ${GIT_DIR}/current_file
> exit $result
> ==== END OF SCRIPT

Updated hook to only format .c and .h files:

result=0
for file in `git diff-index --cached --name-only HEAD | grep '\.[ch]$'` ; do
  clang-format  ${file} > ${GIT_DIR}/current_file
  colordiff --label "$file (to be committed)" --label "$file (reformatted)" -u ${file} ${GIT_DIR}/current_file || result=1
done
rm ${GIT_DIR}/current_file

=> Added grep '\.[ch]$' to file list

Otherwise clang reformats xml too, and does weird things with other
files.

-- 
Debian Developer - deb.li/jak | jak-linux.org - free software dev
                  |  Ubuntu Core Developer |
When replying, only quote what is necessary, and write each reply
directly below the part(s) it pertains to ('inline').  Thank you.


More information about the neomutt-devel mailing list