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

Function upsert_hooks_json

src/cli/cli.c:4034–4139  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

4032}
4033
4034static int upsert_hooks_json(hooks_upsert_args_t args) {
4035 const char *settings_path = args.settings_path;
4036 const char *hook_event = args.hook_event;
4037 const char *matcher_str = args.matcher_str;
4038 const char *command_str = args.command_str;
4039 const char *const *old_matchers = args.old_matchers;
4040 if (!settings_path || !hook_event || !command_str) {
4041 return CLI_ERR;
4042 }
4043
4044 char *expected_content = NULL;
4045 size_t expected_length = 0U;
4046 int read_result =
4047 cbm_json_like_read_document(settings_path, &expected_content, &expected_length);
4048 if (read_result < 0) {
4049 return CLI_ERR;
4050 }
4051
4052 int rc = CLI_ERR;
4053 yyjson_mut_doc *mdoc = yyjson_mut_doc_new(NULL);
4054 yyjson_doc *doc = NULL;
4055 yyjson_mut_val *root = NULL;
4056 if (!mdoc) {
4057 free(expected_content);
4058 return CLI_ERR;
4059 }
4060 if (read_result == 0) {
4061 yyjson_read_flag flags = YYJSON_READ_ALLOW_COMMENTS | YYJSON_READ_ALLOW_TRAILING_COMMAS;
4062 doc = yyjson_read(expected_content, expected_length, flags);
4063 if (!doc || !yyjson_is_obj(yyjson_doc_get_root(doc))) {
4064 goto cleanup;
4065 }
4066 root = yyjson_val_mut_copy(mdoc, yyjson_doc_get_root(doc));
4067 } else {
4068 root = yyjson_mut_obj(mdoc);
4069 }
4070 if (!root) {
4071 goto cleanup;
4072 }
4073 yyjson_mut_doc_set_root(mdoc, root);
4074
4075 /* Get or create hooks object in the temporary semantic copy. The actual
4076 * write below splices only this event array into the original JSON-like
4077 * file, retaining comments and unrelated formatting. */
4078 yyjson_mut_val *hooks = yyjson_mut_obj_get(root, "hooks");
4079 if (hooks && !yyjson_mut_is_obj(hooks)) {
4080 goto cleanup;
4081 }
4082 if (!hooks) {
4083 hooks = yyjson_mut_obj(mdoc);
4084 if (!hooks || !yyjson_mut_obj_add_val(mdoc, root, "hooks", hooks)) {
4085 goto cleanup;
4086 }
4087 }
4088
4089 /* Get or create the hook event array (e.g. PreToolUse / BeforeTool) */
4090 yyjson_mut_val *event_arr = yyjson_mut_obj_get(hooks, hook_event);
4091 if (event_arr && !yyjson_mut_is_arr(event_arr)) {

Calls 3

write_hook_event_arrayFunction · 0.85

Tested by

no test coverage detected