From 06db6825e314f1e5328d19c5ae08207e08b2a57b Mon Sep 17 00:00:00 2001 From: Oleksiy Chugayenko Date: Tue, 17 Oct 2023 10:29:46 +0300 Subject: [PATCH 1/5] pre-commit description added to install.rst pre-commit file body and description added. --- docs/source/install.rst | 50 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/docs/source/install.rst b/docs/source/install.rst index 1bc35ea33..f1024c751 100644 --- a/docs/source/install.rst +++ b/docs/source/install.rst @@ -53,3 +53,53 @@ 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 + + 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()) From ab37a89c4b007ca4251307df6e7f0d0e5a194d2c Mon Sep 17 00:00:00 2001 From: Roman Babenko Date: Tue, 17 Oct 2023 11:19:21 +0300 Subject: [PATCH 2/5] Update docs/source/install.rst --- docs/source/install.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/source/install.rst b/docs/source/install.rst index f1024c751..093c5ba7a 100644 --- a/docs/source/install.rst +++ b/docs/source/install.rst @@ -57,6 +57,7 @@ Via git clone (dev install) 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. From ef7c59fb4142f754f26c1eeb4f60a0e6607c245f Mon Sep 17 00:00:00 2001 From: Oleksiy Chugayenko Date: Tue, 17 Oct 2023 12:10:47 +0300 Subject: [PATCH 3/5] Update docs/source/install.rst Co-authored-by: ShinHyung Choi --- docs/source/install.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/source/install.rst b/docs/source/install.rst index 093c5ba7a..cc6a831d3 100644 --- a/docs/source/install.rst +++ b/docs/source/install.rst @@ -56,7 +56,7 @@ Via git clone (dev install) Pre-commit git hook --------------------------- - Install credsweeper into system and copy ``pre-commit`` file in your ``.git/hooks`` repo. + Install CredSweeper into system and copy ``pre-commit`` file in your ``.git/hooks`` repo. .. note:: credsweeper must be available in current python environment. From 6bfbe09c35bea6594befbbff63f51de79a90c116 Mon Sep 17 00:00:00 2001 From: Oleksiy Chugayenko Date: Tue, 17 Oct 2023 12:10:59 +0300 Subject: [PATCH 4/5] Update docs/source/install.rst Co-authored-by: ShinHyung Choi --- docs/source/install.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/source/install.rst b/docs/source/install.rst index cc6a831d3..afe027240 100644 --- a/docs/source/install.rst +++ b/docs/source/install.rst @@ -59,7 +59,7 @@ 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. + CredSweeper must be available in current python environment. .. note:: pre-commit file context: From 526cc5c329918ea06dfd274ca92b3ec1de79f92a Mon Sep 17 00:00:00 2001 From: Oleksiy Chugayenko Date: Tue, 17 Oct 2023 12:22:17 +0300 Subject: [PATCH 5/5] Update docs/source/install.rst Agree. See comments upper. Co-authored-by: Roman Babenko --- docs/source/install.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/source/install.rst b/docs/source/install.rst index afe027240..e34e8cb9f 100644 --- a/docs/source/install.rst +++ b/docs/source/install.rst @@ -93,7 +93,7 @@ Pre-commit git hook if credsweeper.run(content_provider=deleted): print(f"CREDENTIALS FOUND IN DELETED CONTENT", flush=True) - return 1 + # 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)