MCPcopy Create free account
hub / github.com/DeusData/codebase-memory-mcp / upsert_hooks_json

Function upsert_hooks_json

src/cli/cli.c:4096–4201  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

4094}
4095
4096static int upsert_hooks_json(hooks_upsert_args_t args) {
4097 const char *settings_path = args.settings_path;
4098 const char *hook_event = args.hook_event;
4099 const char *matcher_str = args.matcher_str;
4100 const char *command_str = args.command_str;
4101 const char *const *old_matchers = args.old_matchers;
4102 if (!settings_path || !hook_event || !command_str) {
4103 return CLI_ERR;
4104 }
4105
4106 char *expected_content = NULL;
4107 size_t expected_length = 0U;
4108 int read_result =
4109 cbm_json_like_read_document(settings_path, &expected_content, &expected_length);
4110 if (read_result < 0) {
4111 return CLI_ERR;
4112 }
4113
4114 int rc = CLI_ERR;
4115 yyjson_mut_doc *mdoc = yyjson_mut_doc_new(NULL);
4116 yyjson_doc *doc = NULL;
4117 yyjson_mut_val *root = NULL;
4118 if (!mdoc) {
4119 free(expected_content);
4120 return CLI_ERR;
4121 }
4122 if (read_result == 0) {
4123 yyjson_read_flag flags = YYJSON_READ_ALLOW_COMMENTS | YYJSON_READ_ALLOW_TRAILING_COMMAS;
4124 doc = yyjson_read(expected_content, expected_length, flags);
4125 if (!doc || !yyjson_is_obj(yyjson_doc_get_root(doc))) {
4126 goto cleanup;
4127 }
4128 root = yyjson_val_mut_copy(mdoc, yyjson_doc_get_root(doc));
4129 } else {
4130 root = yyjson_mut_obj(mdoc);
4131 }
4132 if (!root) {
4133 goto cleanup;
4134 }
4135 yyjson_mut_doc_set_root(mdoc, root);
4136
4137 /* Get or create hooks object in the temporary semantic copy. The actual
4138 * write below splices only this event array into the original JSON-like
4139 * file, retaining comments and unrelated formatting. */
4140 yyjson_mut_val *hooks = yyjson_mut_obj_get(root, "hooks");
4141 if (hooks && !yyjson_mut_is_obj(hooks)) {
4142 goto cleanup;
4143 }
4144 if (!hooks) {
4145 hooks = yyjson_mut_obj(mdoc);
4146 if (!hooks || !yyjson_mut_obj_add_val(mdoc, root, "hooks", hooks)) {
4147 goto cleanup;
4148 }
4149 }
4150
4151 /* Get or create the hook event array (e.g. PreToolUse / BeforeTool) */
4152 yyjson_mut_val *event_arr = yyjson_mut_obj_get(hooks, hook_event);
4153 if (event_arr && !yyjson_mut_is_arr(event_arr)) {

Calls 3

write_hook_event_arrayFunction · 0.85

Tested by

no test coverage detected