nextsimhub / nextsimdg

neXtSIM_DG : next generation sea-ice model with DG
https://nextsim-dg.readthedocs.io/en/latest/?badge=latest
Apache License 2.0
10 stars 13 forks source link

Add pre-commit for automatically updating date stamps #660

Closed jwallwork23 closed 2 months ago

jwallwork23 commented 3 months ago

Add pre-commit for automatically updating date stamps

Closes #658.

Works for me but worth please check this works for you too.

einola commented 2 months ago

It kind of works:

  1. You need GNU sed. BSD sed doesn't work.
  2. It modifies your files but just commits the parts I've already git add-ed.

I don't know if we want to make a fuss about 1. Can this be done with plain BSD sed?

The behaviour in 2 can be considered a feature. What I added is committed, nothing more. But the hook kindly suggests how I should modify my files so I can review them and then add them. I guess this is normal behaviour, but it was unexpected to me, not having used pre-commit hooks before.

jwallwork23 commented 2 months ago

Can you check if using /bin/bash instead of /usr/bin/bash works on linux? Other than that, it's good.

Ah yes, I always forget about this. Works fine on Linux yeah. Thanks!

jwallwork23 commented 2 months ago

@einola did the spaces come out okay for you? I had to add a few extra spaces to get things to align nicely. I'm currently using

#!/bin/bash

# An example pre-commit file for nextSIM-DG development

for FILE in $(git diff --cached --name-only | grep -iE '\.(cpp|cc|h|hpp)$'); do
  # Apply clang-format for linting
  clang-format -i ${FILE} --verbose
  # Update date stamp to today's date
  sed -i "s/$(grep '\* @date' ${FILE})/ @date    $(date '+%d %b %Y')/" ${FILE}
done
einola commented 2 months ago

I didn't check the alignment :) ... but feel free to change it before merging.

jwallwork23 commented 2 months ago

Okay I think the latest commit should preserve spaces. Do the additional commands work on Mac, too?