Skip to content
This repository was archived by the owner on Sep 27, 2019. It is now read-only.

Extend formatter script to run on all staged files, merge add/strip action #941

Merged
merged 5 commits into from
Dec 18, 2017

Conversation

tcm-marcel
Copy link
Contributor

@tcm-marcel tcm-marcel commented Dec 6, 2017

This is especially useful when using the new pre-commit hook that prevents me from committing.

  • add -f option to formatter that applies the selected action to all staged files (git)
  • adapt pre-commit hook to only check stages files

(since -a, -s and -c were already taken, I just picked any other letter that somehow looks like "staged files" when looking from far away)

Testing: I run Ubuntu, can someone test this on a Mac?

@tcm-marcel tcm-marcel changed the title Extend formatter script to run on all unstaged files Extend formatter script to run on all staged files Dec 6, 2017
@tcm-marcel
Copy link
Contributor Author

tcm-marcel commented Dec 6, 2017

There was a typo in the commit message and in the title - of course it is meant to run on all staged files, not the other way round.

@coveralls
Copy link

Coverage Status

Coverage increased (+0.01%) to 75.392% when pulling a6c6384 on tcm-marcel:pr/formatter into bbcf739 on cmu-db:master.

@coveralls
Copy link

Coverage Status

Coverage decreased (-0.004%) to 75.377% when pulling a6c6384 on tcm-marcel:pr/formatter into bbcf739 on cmu-db:master.

@tcm-marcel tcm-marcel requested review from jarulraj and apavlo December 6, 2017 20:07
* add -f option that applies the selected action to all staged files (git)
* adapt pre-commit hook to only check stages files
@coveralls
Copy link

Coverage Status

Coverage remained the same at 75.427% when pulling 770c42d on tcm-marcel:pr/formatter into 03dc205 on cmu-db:master.

@schedutron
Copy link
Contributor

Perhaps a lame question, but how exactly do I test this? I'm on a Mac.

@tcm-marcel
Copy link
Contributor Author

@schedutron

  1. Install the pre-commit hook
  2. Edit some source or header files in peloton to obviously violate the code style (e.g. lines too long)
  3. Add some (not all) of them to the staging area (git add) and try to commit. The pre-commit hook should prevent this.
  4. run ./script/formatting/formatter.py -c -f - the staged files should be formatted now and ready to commit, the unstaged ones should remain unchanged

Thanks!

@schedutron
Copy link
Contributor

schedutron commented Dec 13, 2017

I installed the hook and edited few source files (made some of the lines longer that 80 chars), staged them and committed some of them, but the hook didn't stop me. Rather, I got this commit output:

[pr/formatter 8c9d66ad4] Check formatter
 1 file changed, 1 insertion(+), 3 deletions(-)

EDIT: Turns out there was a problem with my local version of clang-format (I had 3.7, while the validator and formatter scripts use 3.6). I checked with having 3.7 in the scripts and the hook works great as expected!

And, is there a way to only run the hook if there are staged files? I tried to commit without staging and the hook still ran.

@tcm-marcel
Copy link
Contributor Author

tcm-marcel commented Dec 13, 2017

Tanks for testing!

And, is there a way to only run the hook if there are staged files? I tried to commit without staging and the hook still ran.

The commit hook only checks staged files. But there is no way to temporary disable it, if that's what you mean.

@schedutron
Copy link
Contributor

@tcm-marcel Maybe the formatter can be edited to only run when there are nonzero files passed to the --files parameter?

@tcm-marcel
Copy link
Contributor Author

@schedutron was right, I changed the hook to only run if actually .cpp/.h files are staged. If committing a non-source file, the validator would run with empty parameters otherwise.

Referring to last team meeting I also merged the add/strip actions to a new update action, that updates the header or adds a new one if necessary.

@tcm-marcel tcm-marcel changed the title Extend formatter script to run on all staged files Extend formatter script to run on all staged files, merge add/strip action Dec 14, 2017
@coveralls
Copy link

Coverage Status

Coverage increased (+0.5%) to 75.947% when pulling 812988e on tcm-marcel:pr/formatter into 03dc205 on cmu-db:master.

@cmu-db cmu-db deleted a comment from coveralls Dec 15, 2017
Copy link
Member

@pmenon pmenon left a comment

Choose a reason for hiding this comment

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

I took a quick look at this. Looks good!

Also, since you're in this part anyways, can you add a pre-check in source_validator.py that looks for clang-format. It doesn't have to be 3.6, but it should print a nice error message, rather than a stack trace it currently does.

python script/validators/source_validator.py --files $files
exit $?
files=$(git diff --name-only HEAD --cached --diff-filter=d | grep '\.\(cpp\|h\)$')
echo $files
Copy link
Member

Choose a reason for hiding this comment

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

You can remove this echo since all scanned files are printed in the source_validator.py script.

Copy link
Contributor Author

@tcm-marcel tcm-marcel Dec 15, 2017

Choose a reason for hiding this comment

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

oops, the echo is left from debugging and the exit $? should of course stay there

@schedutron
Copy link
Contributor

@pmenon So you're suggesting that @tcm-marcel should add a try-except block to check for the clang-format command, right?

  * also add useful message if pre-commit hook fails
@coveralls
Copy link

Coverage Status

Coverage increased (+0.01%) to 75.93% when pulling b3011b0 on tcm-marcel:pr/formatter into b9f1e43 on cmu-db:master.

@coveralls
Copy link

Coverage Status

Coverage decreased (-0.02%) to 75.841% when pulling 0dad892 on tcm-marcel:pr/formatter into c4971f3 on cmu-db:master.

@apavlo apavlo merged commit d817cfc into cmu-db:master Dec 18, 2017
tcm-marcel added a commit to tcm-marcel/peloton that referenced this pull request Dec 22, 2017
  * use any clang-format version
  * catch error if clang-format is not installed
    (similar to source validator changed in cmu-db#941)
  * fix wrong path in pre-commit hook fail message
apavlo pushed a commit that referenced this pull request Jan 12, 2018
  * use any clang-format version
  * catch error if clang-format is not installed
    (similar to source validator changed in #941)
  * fix wrong path in pre-commit hook fail message
mtunique pushed a commit to mtunique/peloton that referenced this pull request Apr 16, 2019
  * use any clang-format version
  * catch error if clang-format is not installed
    (similar to source validator changed in cmu-db#941)
  * fix wrong path in pre-commit hook fail message
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants