Install graphify's post-commit, post-checkout, post-merge, and post-rewrite hooks in the nearest git repo.
(path: Path = Path("."))
| 1114 | |
| 1115 | |
| 1116 | def install(path: Path = Path(".")) -> str: |
| 1117 | """Install graphify's post-commit, post-checkout, post-merge, and post-rewrite hooks in the nearest git repo.""" |
| 1118 | root = _git_root(path) |
| 1119 | if root is None: |
| 1120 | raise RuntimeError(f"No git repository found at or above {path.resolve()}") |
| 1121 | |
| 1122 | hooks_dir = _user_hooks_dir(_hooks_dir(root)) |
| 1123 | |
| 1124 | cfg = _load_graphifyrc(root) |
| 1125 | viz_limit = cfg.get("viz_node_limit") |
| 1126 | if viz_limit is not None: |
| 1127 | # Use the `:-` default form (like GRAPHIFY_MAX_WORKERS below) so an |
| 1128 | # explicit `GRAPHIFY_VIZ_NODE_LIMIT=... git commit` still wins over the |
| 1129 | # baked project default — persisting config must not clobber a per-run |
| 1130 | # override. |
| 1131 | viz_export = f'export GRAPHIFY_VIZ_NODE_LIMIT="${{GRAPHIFY_VIZ_NODE_LIMIT:-{viz_limit}}}"\n' |
| 1132 | else: |
| 1133 | viz_export = "" |
| 1134 | |
| 1135 | pinned = _pinned_python() |
| 1136 | hook = _HOOK_SCRIPT.replace("__PINNED_PYTHON__", pinned).replace("__VIZ_LIMIT_EXPORT__", viz_export) |
| 1137 | checkout = _CHECKOUT_SCRIPT.replace("__PINNED_PYTHON__", pinned).replace("__VIZ_LIMIT_EXPORT__", viz_export) |
| 1138 | merge_hook = _MERGE_SCRIPT.replace("__PINNED_PYTHON__", pinned).replace("__VIZ_LIMIT_EXPORT__", viz_export) |
| 1139 | rewrite_hook = _REWRITE_SCRIPT.replace("__PINNED_PYTHON__", pinned).replace("__VIZ_LIMIT_EXPORT__", viz_export) |
| 1140 | |
| 1141 | commit_msg = _install_hook(hooks_dir, "post-commit", hook, _HOOK_MARKER, _HOOK_MARKER_END) |
| 1142 | checkout_msg = _install_hook(hooks_dir, "post-checkout", checkout, _CHECKOUT_MARKER, _CHECKOUT_MARKER_END) |
| 1143 | post_merge_msg = _install_hook( |
| 1144 | hooks_dir, "post-merge", merge_hook, _MERGE_HOOK_MARKER, _MERGE_HOOK_MARKER_END |
| 1145 | ) |
| 1146 | post_rewrite_msg = _install_hook( |
| 1147 | hooks_dir, "post-rewrite", rewrite_hook, _REWRITE_HOOK_MARKER, _REWRITE_HOOK_MARKER_END |
| 1148 | ) |
| 1149 | merge_msg = _register_merge_driver(root) |
| 1150 | |
| 1151 | return ( |
| 1152 | f"post-commit: {commit_msg}\npost-checkout: {checkout_msg}\n" |
| 1153 | f"post-merge: {post_merge_msg}\npost-rewrite: {post_rewrite_msg}\nmerge driver: {merge_msg}" |
| 1154 | ) |
| 1155 | |
| 1156 | |
| 1157 | def uninstall(path: Path = Path(".")) -> str: |