From 118425f99445d359463c95cfabbc0668de00998f Mon Sep 17 00:00:00 2001 From: infeeeee Date: Thu, 30 Mar 2023 01:28:51 +0200 Subject: [PATCH] Add example git hook --- README.md | 30 +++++++++++++++--------------- TODO.md | 2 +- pre-commit | 27 +++++++++++++++++++++++++++ pyproject.toml | 3 +++ 4 files changed, 46 insertions(+), 16 deletions(-) create mode 100644 pre-commit diff --git a/README.md b/README.md index b920321..c167cce 100644 --- a/README.md +++ b/README.md @@ -75,30 +75,30 @@ HEADLESS loglevel only prints modified filenames. *Notes: In Windows cmd use backward slashes as path separators, in any other shells use forward slashes. Powershell accepts both of them. Wrap paths with spaces in double quotes.* -Extract all nodes next to a Dynamo file: - -``` +```shell +# Extract all nodes next to a Dynamo file: dyn2py path/to/dynamofile.dyn -``` -Update a Dynamo file from previously exported and modified python files: - -``` +# Update a Dynamo file from previously exported and modified python files: dyn2py --update path/to/dynamofile.dyn -``` -Extract python nodes to a specific folder, process multiple Dynamo files: - -``` +# Extract python nodes to a specific folder, process multiple Dynamo files: dyn2py --python-folder path/to/pythonfiles path/to/dynamofile1.dyn path/to/dynamofile2.dyn -``` -Update Dynamo files from python files from a folder. Only check python files, create backups: - -``` +# Update Dynamo files from python files from a folder. Only check python files, create backups: dyn2py --filter py --backup path/to/pythonfiles ``` +#### Git hooks + +Git Hooks are a built-in feature of Git that allow developers to automate tasks throughout the Git workflow. Read more here: https://githooks.com/ + +With the `pre-commit` hook it's possible to add more files to the currently initialized commit. + +You can find an example pre-commit hook here: [pre-commit](pre-commit). Copy this file to the `.git/hooks` folder of your repo of Dynamo graph. This folder is hidden by default, but it should exist in all initialized git repo. Do not rename this file. + +This script will go through staged `.dyn` files and export python scripts from them, and add them to the current commit. Now you can check check changed lines in a diff tool! + ### As a python module Full API documentation available here: https://infeeeee.github.io/dyn2py diff --git a/TODO.md b/TODO.md index 0eb4df3..064a968 100644 --- a/TODO.md +++ b/TODO.md @@ -22,7 +22,7 @@ - [x] API docs - [x] Installation in readme - [x] Terminal examples in readme -- [ ] About git hooks in readme +- [x] About git hooks in readme ## Extra features maybe later diff --git a/pre-commit b/pre-commit new file mode 100644 index 0000000..2e938fa --- /dev/null +++ b/pre-commit @@ -0,0 +1,27 @@ +#!/bin/sh + +# Create a list of files from staged files: +mapfile -t NEW_FILES <<<$(git diff --name-only --cached) + +# Go through staged files: +for f in "${NEW_FILES[@]}"; do + + # Export python files, only from Dynamo files. + # On Windows line ending is always CRLF, so remove CR with tr. + mapfile -t PY_FILES <<<$(dyn2py --force --filter dyn --loglevel HEADLESS "$f" | tr -d "\r") + + # Check if something was exported: + if [[ "${PY_FILES[@]}" ]]; then + + # Go through exported files: + for p in "${PY_FILES[@]}"; do + + # Check if file exists: + if [ -f "$p" ]; then + + # Stage file: + git add "$p" + fi + done + fi +done diff --git a/pyproject.toml b/pyproject.toml index a527cf4..f0c7a5a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -34,3 +34,6 @@ dyn2py = "dyn2py:__command_line" [build-system] requires = ["setuptools", "wheel"] + +[tool.setuptools] +packages = ["dyn2py"]