Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pre-commit hook description added to documentation. #442

Merged
merged 7 commits into from
Oct 17, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions docs/source/install.rst
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,54 @@ Via git clone (dev install)
cd CredSweeper
# Annotate "numpy", "scikit-learn" and "tensorflow" if you don't want to use the ML validation feature.
pip install -qr requirements.txt

Pre-commit git hook
---------------------------
Install CredSweeper into system and copy ``pre-commit`` file in your ``.git/hooks`` repo.

.. note::
CredSweeper must be available in current python environment.

.. note::
pre-commit file context:
.. code-block:: python

#!/usr/bin/env python
import io
import subprocess
import sys

from credsweeper import CredSweeper
from credsweeper.common.constants import DiffRowType
from credsweeper.file_handler.patch_provider import PatchProvider


def main() -> int:
command = ["git", "diff", "--cached"]
with subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE) as pipe:
_stdout, _stderr = pipe.communicate()
if pipe.returncode:
print(str(_stdout), flush=True)
print(str(_stderr), flush=True)
print(f"{command} EXIT CODE:{pipe.returncode}", flush=True)
return 1

patch = io.BytesIO(_stdout)
added = PatchProvider([patch], change_type=DiffRowType.ADDED)
deleted = PatchProvider([patch], change_type=DiffRowType.DELETED)

credsweeper = CredSweeper()

if credsweeper.run(content_provider=deleted):
print(f"CREDENTIALS FOUND IN DELETED CONTENT", flush=True)
# return 1 # <<< UNCOMMENT THE LINE IF YOU WANT TO MANAGE DELETED CREDENTIALS

if credsweeper.run(content_provider=added):
print(f"CREDENTIALS FOUND IN ADDED CONTENT", flush=True)
return 1

return 0


if __name__ == "__main__":
sys.exit(main())