@@ -787,8 +787,8 @@ static const tool_def_t TOOLS[] = {
787787 "\"module_cursor\":{\"type\":\"string\"},"
788788 "\"max_output_tokens\":{\"type\":\"integer\",\"default\":3200,\"minimum\":128,"
789789 "\"maximum\":1000000,\"description\":\"Sizing hint; hard ceiling is 4 UTF-8 bytes/token.\"},"
790- "\"base_branch\":{\"type\":"
791- "\"string\",\"default\":\" main\"},\"since\":{\"type\":\"string\"},"
790+ "\"base_branch\":{\"type\":\"string\",\"description\":\"Default: origin/HEAD, else "
791+ "main, else master, else the upstream. \"},\"since\":{\"type\":\"string\"},"
792792 "\"format\":{\"type\":\"string\",\"enum\":[\"tree\",\"json\"],\"default\":\"tree\"}},"
793793 "\"required\":"
794794 "[\"project\"]}"},
@@ -16179,6 +16179,92 @@ static bool detect_valid_object_id(const char *value) {
1617916179 return true;
1618016180}
1618116181
16182+ /* Run one contained git query and return its first output line. False when
16183+ * the command fails, is cancelled, prints nothing, or prints a value that
16184+ * cannot be spliced back into the later git commands safely. */
16185+ static bool detect_git_first_line(cbm_mcp_server_t *srv, const char *command, char *out,
16186+ size_t out_size) {
16187+ out[0] = '\0';
16188+ char output_path[CBM_SZ_2K] = {0};
16189+ cbm_proc_result_t result = {0};
16190+ int run = mcp_run_shell_command_cancellable(srv, command, output_path, &result);
16191+ bool ok = run == 0 && result.exit_code == 0 && !result.cancellation_requested &&
16192+ !mcp_request_cancelled(srv);
16193+ FILE *fp = ok ? cbm_fopen(output_path, "rb") : NULL;
16194+ bool read = fp && fgets(out, (int)out_size, fp) != NULL;
16195+ if (fp) {
16196+ (void)fclose(fp);
16197+ }
16198+ if (output_path[0]) {
16199+ (void)cbm_unlink(output_path);
16200+ }
16201+ size_t length = read ? strlen(out) : 0;
16202+ /* A line that filled the buffer without its newline was truncated. */
16203+ bool complete = length > 0 && out[length - 1] == '\n';
16204+ while (length > 0 && (out[length - 1] == '\n' || out[length - 1] == '\r')) {
16205+ out[--length] = '\0';
16206+ }
16207+ ok = ok && complete && length > 0 && out[0] != '-' && cbm_validate_shell_arg(out) &&
16208+ validate_windows_cmd_interpolation_arg(out);
16209+ if (!ok) {
16210+ out[0] = '\0';
16211+ }
16212+ return ok;
16213+ }
16214+
16215+ /* #1357: without base_branch, diff against the repository's default branch
16216+ * rather than a literal "main" that trunk/master/develop repositories do not
16217+ * have. Order: origin/HEAD (what the clone calls the default), a local main,
16218+ * a local master, then the current branch's upstream. main and master come
16219+ * before the upstream on purpose: a pushed feature branch tracks its own
16220+ * remote twin, and diffing against that would silently shrink the impact
16221+ * analysis to unpushed commits. Nothing resolves -> false, out = "main", and
16222+ * the caller reports that ref as the one it tried. */
16223+ static bool detect_default_base(cbm_mcp_server_t *srv, const char *root_path, char *out,
16224+ size_t out_size) {
16225+ #ifdef _WIN32
16226+ #define DETECT_GIT_Q "\""
16227+ #define DETECT_GIT_NULL "2>NUL"
16228+ #else
16229+ #define DETECT_GIT_Q "'"
16230+ #define DETECT_GIT_NULL "2>/dev/null"
16231+ #endif
16232+ char command[CBM_SZ_2K];
16233+ char line[CBM_SZ_1K];
16234+ snprintf(command, sizeof(command),
16235+ "git -C " DETECT_GIT_Q "%s" DETECT_GIT_Q
16236+ " symbolic-ref -q --short refs/remotes/origin/HEAD " DETECT_GIT_NULL,
16237+ root_path);
16238+ if (detect_git_first_line(srv, command, line, sizeof(line))) {
16239+ snprintf(out, out_size, "%s", line);
16240+ return true;
16241+ }
16242+ static const char *const local_defaults[] = {"main", "master"};
16243+ for (size_t i = 0; i < sizeof(local_defaults) / sizeof(local_defaults[0]); i++) {
16244+ snprintf(command, sizeof(command),
16245+ "git -C " DETECT_GIT_Q "%s" DETECT_GIT_Q " rev-parse -q --verify " DETECT_GIT_Q
16246+ "refs/heads/%s^{commit}" DETECT_GIT_Q " " DETECT_GIT_NULL,
16247+ root_path, local_defaults[i]);
16248+ if (detect_git_first_line(srv, command, line, sizeof(line))) {
16249+ snprintf(out, out_size, "%s", local_defaults[i]);
16250+ return true;
16251+ }
16252+ }
16253+ snprintf(command, sizeof(command),
16254+ "git -C " DETECT_GIT_Q "%s" DETECT_GIT_Q
16255+ " rev-parse -q --abbrev-ref --symbolic-full-name " DETECT_GIT_Q
16256+ "@{upstream}" DETECT_GIT_Q " " DETECT_GIT_NULL,
16257+ root_path);
16258+ if (detect_git_first_line(srv, command, line, sizeof(line))) {
16259+ snprintf(out, out_size, "%s", line);
16260+ return true;
16261+ }
16262+ #undef DETECT_GIT_Q
16263+ #undef DETECT_GIT_NULL
16264+ snprintf(out, out_size, "%s", "main");
16265+ return false;
16266+ }
16267+
1618216268typedef struct {
1618316269 char stream; /* c=changed files, i=impacted symbols, m=module rollup */
1618416270 char snapshot[33]; /* first 128 bits of the SHA-256 live-state fingerprint */
@@ -16425,17 +16511,17 @@ static char *handle_detect_changes(cbm_mcp_server_t *srv, const char *args) {
1642516511 }
1642616512 free(since); /* no-op after the swap (since is NULL); frees it otherwise */
1642716513
16428- if (!base_branch) {
16429- base_branch = heap_strdup("main");
16430- }
16514+ /* No base given: resolved from the repository after the project root is
16515+ * known (#1357). An explicit base_branch/since is used as given. */
16516+ bool base_defaulted = base_branch == NULL;
1643116517
1643216518 /* Reject shell metacharacters, and a leading '-', in the user-supplied
1643316519 * branch name. base_branch is spliced into `git diff --name-only
1643416520 * "<base>"...HEAD`; a value starting with '-' would be read by git as an
1643516521 * option rather than a ref (e.g. `--output=<path>` writes the diff to an
1643616522 * arbitrary file). A real git ref never begins with '-'. */
16437- if (!cbm_validate_shell_arg(base_branch) || base_branch[0] == '-' ||
16438- !validate_windows_cmd_interpolation_arg(base_branch)) {
16523+ if (base_branch && ( !cbm_validate_shell_arg(base_branch) || base_branch[0] == '-' ||
16524+ !validate_windows_cmd_interpolation_arg(base_branch) )) {
1643916525 free(project);
1644016526 free(base_branch);
1644116527 free(scope);
@@ -16453,13 +16539,24 @@ static char *handle_detect_changes(cbm_mcp_server_t *srv, const char *args) {
1645316539 return res;
1645416540 }
1645516541
16456- if (!validate_search_path_arg(root_path) ||
16457- !validate_windows_cmd_interpolation_arg(root_path)) {
16542+ /* The default base is looked up only once root_path is known to be safe
16543+ * to splice into the git command line. */
16544+ bool root_ok =
16545+ validate_search_path_arg(root_path) && validate_windows_cmd_interpolation_arg(root_path);
16546+ bool default_base_found = true;
16547+ if (root_ok && base_defaulted) {
16548+ char default_base[CBM_SZ_1K];
16549+ default_base_found =
16550+ detect_default_base(srv, root_path, default_base, sizeof(default_base));
16551+ base_branch = heap_strdup(default_base);
16552+ }
16553+ if (!root_ok || !base_branch) {
1645816554 free(root_path);
1645916555 free(project);
1646016556 free(base_branch);
1646116557 free(scope);
16462- return cbm_mcp_text_result("project path contains invalid characters", true);
16558+ return cbm_mcp_text_result(
16559+ root_ok ? "out of memory" : "project path contains invalid characters", true);
1646316560 }
1646416561
1646516562 /* Every detect snapshot and cursor is generation-bound. Validate the
@@ -16549,6 +16646,25 @@ static char *handle_detect_changes(cbm_mcp_server_t *srv, const char *args) {
1654916646 }
1655016647 if (resolve_cancelled || resolve_run != 0 || resolve_result.exit_code != 0 || resolve_oom ||
1655116648 !head_oid[0] || !base_oid[0] || !git_prefix_valid) {
16649+ /* Name the ref that was tried: a bare "not a commit" left callers of
16650+ * the implicit default guessing which base was meant (#1357). */
16651+ char resolve_error[CBM_SZ_2K];
16652+ if (!base_defaulted) {
16653+ snprintf(resolve_error, sizeof(resolve_error),
16654+ "git revision resolution failed: base_branch \"%s\" or HEAD is not a commit",
16655+ base_branch);
16656+ } else if (!default_base_found) {
16657+ snprintf(resolve_error, sizeof(resolve_error),
16658+ "git revision resolution failed: no default base found (tried origin/HEAD, "
16659+ "main, master and the upstream; fell back to \"%s\"), or HEAD is not a "
16660+ "commit; pass base_branch",
16661+ base_branch);
16662+ } else {
16663+ snprintf(resolve_error, sizeof(resolve_error),
16664+ "git revision resolution failed: default base \"%s\" or HEAD is not a "
16665+ "commit; pass base_branch",
16666+ base_branch);
16667+ }
1655216668 free(direction);
1655316669 free(root_path);
1655416670 free(project);
@@ -16567,8 +16683,7 @@ static char *handle_detect_changes(cbm_mcp_server_t *srv, const char *args) {
1656716683 "worktree could not be determined",
1656816684 true);
1656916685 }
16570- return cbm_mcp_text_result(
16571- "git revision resolution failed: base_branch or HEAD is not a commit", true);
16686+ return cbm_mcp_text_result(resolve_error, true);
1657216687 }
1657316688
1657416689 char merge_base[65] = "";
0 commit comments