Skip to content

Conversation

Ana06
Copy link
Member

@Ana06 Ana06 commented Jun 22, 2020

Add the scripts/setup-hooks.sh script which sets the following hooks up:

  • The post-commit hook runs the linter after every git commit, letting you know if there are code style or rule linter offenses you need to fix.
  • The pre-push hook runs the linter and the tests and block the git push if they do not succeed. This way you realise if everything is alright without the need of sending a PR.

git commit when the linter fails

Screenshot 2020-06-22 at 11 08 58

git push in process with uncommited changes

Screenshot 2020-06-22 at 11 15 35

Be careful if you are using git 2.24 as there is a bug affecting git stash pop: https://www.spinics.net/lists/git/msg369520.html It is fixed in the last git version.

@Ana06 Ana06 added the enhancement New feature or request label Jun 22, 2020
@Ana06 Ana06 requested review from williballenthin and mr-tz June 22, 2020 09:17
@@ -42,3 +42,12 @@ Next, use `pip` to install the source code in "editable" mode. This means that P
`$ pip install -e ./local/path/to/src`

You'll find that the `capa.exe` (Windows) or `capa` (Linux) executables in your path now invoke the capa binary from this directory.

### 4. Setup hooks [optional]
Copy link
Member Author

Choose a reason for hiding this comment

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

It may be a good idea to create a CONTRIBUTING file which includes this (and maybe the whole 3 section). 🤔 Although not necessarily something for this PR.

Copy link
Collaborator

@mr-tz mr-tz left a comment

Choose a reason for hiding this comment

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

looks good, thanks! this will hold us over before we go public.

Add the `scripts/setup-hooks.sh` script which sets the following hooks
up:
- The `post-commit` hook runs the linter after every `git commit`,
  letting you know if there are code style or rule linter offenses you
  need to fix.
- The `pre-push` hook runs the linter and the tests and block the `git
  push` if they do not succeed.
  This way you realise if everything is alright without the need of
  sending a PR.
@williballenthin williballenthin merged commit fa9bb94 into master Jun 22, 2020
@williballenthin williballenthin deleted the ana-hooks branch June 22, 2020 15:42
# After that append `scripts/hooks/$arg` and ensure they can be run
create_hook() {
if [[ ! -e .git/hooks/$1 ]]; then
echo '#!/bin/sh' > .git/hooks/$1
Copy link
Collaborator

Choose a reason for hiding this comment

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

even better: #!/usr/bin/env sh

Copy link
Collaborator

Choose a reason for hiding this comment

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

recommend also adding set -euo pipefail

Copy link
Collaborator

Choose a reason for hiding this comment

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

Copy link
Member Author

Choose a reason for hiding this comment

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

@williballenthin

even better: #!/usr/bin/env sh

I think that #!/usr/bin/env bash is prefered vs #!/usr/bin/bash, as #!/usr/bin/bash doesn't always exist. However, #!/bin/sh should exist.

I am anyway using some bash specific features (Bashism), so we could use bash instead

if [[ ! -e .git/hooks/$1 ]]; then
echo '#!/bin/sh' > .git/hooks/$1
fi
cat scripts/hooks/$1 >> .git/hooks/$1
Copy link
Collaborator

Choose a reason for hiding this comment

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

careful to surround variables with double quotes to support filenames with whitespace:

".git/hooks/$1"

Copy link
Member Author

Choose a reason for hiding this comment

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

but hook names never have whitespaces

pip install pycodestyle
pytest-sugar
pip install https://github.com/williballenthin/vivisect/zipball/master
python setup.py develop
Copy link
Collaborator

Choose a reason for hiding this comment

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

this makes assumptions about where the script is run.

i'd recommend that this script be thoroughly documented with some examples and leave it be, rather than making this script bulletproof.

Copy link
Member Author

@Ana06 Ana06 Jun 24, 2020

Choose a reason for hiding this comment

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

No, it changes to the root git directory, so you can run it everywhere inside the git repo:

GIT_DIR=`git rev-parse --show-toplevel`
cd $GIT_DIR

}

# Run style checker and print state
pycodestyle --config=./ci/tox.ini ./capa/ > style-checker-output.log 2>&1
Copy link
Collaborator

Choose a reason for hiding this comment

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

does this make assumptions about where the script is being invoked from?

Copy link
Member Author

Choose a reason for hiding this comment

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

as long as it is run from inside the git directory, it is always run from the root of the git directory

williballenthin pushed a commit that referenced this pull request Mar 24, 2023
colton-gabertan added a commit that referenced this pull request Jun 3, 2023
chf0x pushed a commit to chf0x/capa that referenced this pull request Jun 20, 2025
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 this pull request may close these issues.

3 participants