| 18444 | } |
| 18445 | |
| 18446 | static char *handle_detect_changes(cbm_mcp_server_t *srv, const char *args) { |
| 18447 | char *project = get_project_arg(args); |
| 18448 | char *base_branch = cbm_mcp_get_string_arg(args, "base_branch"); |
| 18449 | char *since = cbm_mcp_get_string_arg(args, "since"); |
| 18450 | char *scope = cbm_mcp_get_string_arg(args, "scope"); |
| 18451 | int depth = cbm_mcp_get_int_arg(args, "depth", MCP_DEFAULT_BFS_DEPTH); |
| 18452 | depth = clamp_mcp_depth(depth, "detect_changes"); |
| 18453 | |
| 18454 | /* scope: "files" = changed files only; "impact" = files + symbols (default). |
| 18455 | * Keep accepting the legacy "symbols" spelling for compatibility. */ |
| 18456 | bool want_symbols = !scope || strcmp(scope, "symbols") == 0 || strcmp(scope, "impact") == 0; |
| 18457 | |
| 18458 | /* `since` (e.g. "HEAD~10", "v0.5.0") is the documented diff base but was |
| 18459 | * previously parsed and never used: it takes precedence over base_branch. |
| 18460 | * Route it through base_branch so the shared shell-arg validation and the |
| 18461 | * existing `<base>...HEAD` (three-dot) diff apply unchanged — `since` thus |
| 18462 | * adopts the same merge-base semantics base_branch already uses. */ |
| 18463 | if (since && since[0]) { |
| 18464 | free(base_branch); |
| 18465 | base_branch = since; /* transfer ownership */ |
| 18466 | since = NULL; |
| 18467 | } |
| 18468 | free(since); /* no-op after the swap (since is NULL); frees it otherwise */ |
| 18469 | |
| 18470 | /* No base given: resolved from the repository after the project root is |
| 18471 | * known (#1357). An explicit base_branch/since is used as given. */ |
| 18472 | bool base_defaulted = base_branch == NULL; |
| 18473 | |
| 18474 | /* Reject shell metacharacters, and a leading '-', in the user-supplied |
| 18475 | * branch name. base_branch is spliced into `git diff --name-only |
| 18476 | * "<base>"...HEAD`; a value starting with '-' would be read by git as an |
| 18477 | * option rather than a ref (e.g. `--output=<path>` writes the diff to an |
| 18478 | * arbitrary file). A real git ref never begins with '-'. */ |
| 18479 | if (base_branch && (!cbm_validate_shell_arg(base_branch) || base_branch[0] == '-' || |
| 18480 | !validate_windows_cmd_interpolation_arg(base_branch))) { |
| 18481 | free(project); |
| 18482 | free(base_branch); |
| 18483 | free(scope); |
| 18484 | return cbm_mcp_text_result("base_branch contains invalid characters", true); |
| 18485 | } |
| 18486 | |
| 18487 | char *root_path = get_project_root(srv, project); |
| 18488 | if (!root_path) { |
| 18489 | char *err = build_no_store_error_checked(srv, project); |
| 18490 | char *res = cbm_mcp_text_result(err, true); |
| 18491 | free(err); |
| 18492 | free(project); |
| 18493 | free(base_branch); |
| 18494 | free(scope); |
| 18495 | return res; |
| 18496 | } |
| 18497 | |
| 18498 | /* The default base is looked up only once root_path is known to be safe |
| 18499 | * to splice into the git command line. */ |
| 18500 | bool root_ok = |
| 18501 | validate_search_path_arg(root_path) && validate_windows_cmd_interpolation_arg(root_path); |
| 18502 | bool default_base_found = true; |
| 18503 | if (root_ok && base_defaulted) { |
no test coverage detected