@@ -786,8 +786,8 @@ static const tool_def_t TOOLS[] = {
786786 "\"module_cursor\":{\"type\":\"string\"},"
787787 "\"max_output_tokens\":{\"type\":\"integer\",\"default\":3200,\"minimum\":128,"
788788 "\"maximum\":1000000,\"description\":\"Sizing hint; hard ceiling is 4 UTF-8 bytes/token.\"},"
789- "\"base_branch\":{\"type\":"
790- "\"string\",\"default\":\" main\"},\"since\":{\"type\":\"string\"},"
789+ "\"base_branch\":{\"type\":\"string\",\"description\":\"Default: origin/HEAD, else "
790+ "main, else master, else the upstream. \"},\"since\":{\"type\":\"string\"},"
791791 "\"format\":{\"type\":\"string\",\"enum\":[\"tree\",\"json\"],\"default\":\"tree\"}},"
792792 "\"required\":"
793793 "[\"project\"]}"},
@@ -16021,6 +16021,92 @@ static bool detect_valid_object_id(const char *value) {
1602116021 return true;
1602216022}
1602316023
16024+ /* Run one contained git query and return its first output line. False when
16025+ * the command fails, is cancelled, prints nothing, or prints a value that
16026+ * cannot be spliced back into the later git commands safely. */
16027+ static bool detect_git_first_line(cbm_mcp_server_t *srv, const char *command, char *out,
16028+ size_t out_size) {
16029+ out[0] = '\0';
16030+ char output_path[CBM_SZ_2K] = {0};
16031+ cbm_proc_result_t result = {0};
16032+ int run = mcp_run_shell_command_cancellable(srv, command, output_path, &result);
16033+ bool ok = run == 0 && result.exit_code == 0 && !result.cancellation_requested &&
16034+ !mcp_request_cancelled(srv);
16035+ FILE *fp = ok ? cbm_fopen(output_path, "rb") : NULL;
16036+ bool read = fp && fgets(out, (int)out_size, fp) != NULL;
16037+ if (fp) {
16038+ (void)fclose(fp);
16039+ }
16040+ if (output_path[0]) {
16041+ (void)cbm_unlink(output_path);
16042+ }
16043+ size_t length = read ? strlen(out) : 0;
16044+ /* A line that filled the buffer without its newline was truncated. */
16045+ bool complete = length > 0 && out[length - 1] == '\n';
16046+ while (length > 0 && (out[length - 1] == '\n' || out[length - 1] == '\r')) {
16047+ out[--length] = '\0';
16048+ }
16049+ ok = ok && complete && length > 0 && out[0] != '-' && cbm_validate_shell_arg(out) &&
16050+ validate_windows_cmd_interpolation_arg(out);
16051+ if (!ok) {
16052+ out[0] = '\0';
16053+ }
16054+ return ok;
16055+ }
16056+
16057+ /* #1357: without base_branch, diff against the repository's default branch
16058+ * rather than a literal "main" that trunk/master/develop repositories do not
16059+ * have. Order: origin/HEAD (what the clone calls the default), a local main,
16060+ * a local master, then the current branch's upstream. main and master come
16061+ * before the upstream on purpose: a pushed feature branch tracks its own
16062+ * remote twin, and diffing against that would silently shrink the impact
16063+ * analysis to unpushed commits. Nothing resolves -> false, out = "main", and
16064+ * the caller reports that ref as the one it tried. */
16065+ static bool detect_default_base(cbm_mcp_server_t *srv, const char *root_path, char *out,
16066+ size_t out_size) {
16067+ #ifdef _WIN32
16068+ #define DETECT_GIT_Q "\""
16069+ #define DETECT_GIT_NULL "2>NUL"
16070+ #else
16071+ #define DETECT_GIT_Q "'"
16072+ #define DETECT_GIT_NULL "2>/dev/null"
16073+ #endif
16074+ char command[CBM_SZ_2K];
16075+ char line[CBM_SZ_1K];
16076+ snprintf(command, sizeof(command),
16077+ "git -C " DETECT_GIT_Q "%s" DETECT_GIT_Q
16078+ " symbolic-ref -q --short refs/remotes/origin/HEAD " DETECT_GIT_NULL,
16079+ root_path);
16080+ if (detect_git_first_line(srv, command, line, sizeof(line))) {
16081+ snprintf(out, out_size, "%s", line);
16082+ return true;
16083+ }
16084+ static const char *const local_defaults[] = {"main", "master"};
16085+ for (size_t i = 0; i < sizeof(local_defaults) / sizeof(local_defaults[0]); i++) {
16086+ snprintf(command, sizeof(command),
16087+ "git -C " DETECT_GIT_Q "%s" DETECT_GIT_Q " rev-parse -q --verify " DETECT_GIT_Q
16088+ "refs/heads/%s^{commit}" DETECT_GIT_Q " " DETECT_GIT_NULL,
16089+ root_path, local_defaults[i]);
16090+ if (detect_git_first_line(srv, command, line, sizeof(line))) {
16091+ snprintf(out, out_size, "%s", local_defaults[i]);
16092+ return true;
16093+ }
16094+ }
16095+ snprintf(command, sizeof(command),
16096+ "git -C " DETECT_GIT_Q "%s" DETECT_GIT_Q
16097+ " rev-parse -q --abbrev-ref --symbolic-full-name " DETECT_GIT_Q
16098+ "@{upstream}" DETECT_GIT_Q " " DETECT_GIT_NULL,
16099+ root_path);
16100+ if (detect_git_first_line(srv, command, line, sizeof(line))) {
16101+ snprintf(out, out_size, "%s", line);
16102+ return true;
16103+ }
16104+ #undef DETECT_GIT_Q
16105+ #undef DETECT_GIT_NULL
16106+ snprintf(out, out_size, "%s", "main");
16107+ return false;
16108+ }
16109+
1602416110typedef struct {
1602516111 char stream; /* c=changed files, i=impacted symbols, m=module rollup */
1602616112 char snapshot[33]; /* first 128 bits of the SHA-256 live-state fingerprint */
@@ -16267,17 +16353,17 @@ static char *handle_detect_changes(cbm_mcp_server_t *srv, const char *args) {
1626716353 }
1626816354 free(since); /* no-op after the swap (since is NULL); frees it otherwise */
1626916355
16270- if (!base_branch) {
16271- base_branch = heap_strdup("main");
16272- }
16356+ /* No base given: resolved from the repository after the project root is
16357+ * known (#1357). An explicit base_branch/since is used as given. */
16358+ bool base_defaulted = base_branch == NULL;
1627316359
1627416360 /* Reject shell metacharacters, and a leading '-', in the user-supplied
1627516361 * branch name. base_branch is spliced into `git diff --name-only
1627616362 * "<base>"...HEAD`; a value starting with '-' would be read by git as an
1627716363 * option rather than a ref (e.g. `--output=<path>` writes the diff to an
1627816364 * arbitrary file). A real git ref never begins with '-'. */
16279- if (!cbm_validate_shell_arg(base_branch) || base_branch[0] == '-' ||
16280- !validate_windows_cmd_interpolation_arg(base_branch)) {
16365+ if (base_branch && ( !cbm_validate_shell_arg(base_branch) || base_branch[0] == '-' ||
16366+ !validate_windows_cmd_interpolation_arg(base_branch) )) {
1628116367 free(project);
1628216368 free(base_branch);
1628316369 free(scope);
@@ -16295,13 +16381,24 @@ static char *handle_detect_changes(cbm_mcp_server_t *srv, const char *args) {
1629516381 return res;
1629616382 }
1629716383
16298- if (!validate_search_path_arg(root_path) ||
16299- !validate_windows_cmd_interpolation_arg(root_path)) {
16384+ /* The default base is looked up only once root_path is known to be safe
16385+ * to splice into the git command line. */
16386+ bool root_ok =
16387+ validate_search_path_arg(root_path) && validate_windows_cmd_interpolation_arg(root_path);
16388+ bool default_base_found = true;
16389+ if (root_ok && base_defaulted) {
16390+ char default_base[CBM_SZ_1K];
16391+ default_base_found =
16392+ detect_default_base(srv, root_path, default_base, sizeof(default_base));
16393+ base_branch = heap_strdup(default_base);
16394+ }
16395+ if (!root_ok || !base_branch) {
1630016396 free(root_path);
1630116397 free(project);
1630216398 free(base_branch);
1630316399 free(scope);
16304- return cbm_mcp_text_result("project path contains invalid characters", true);
16400+ return cbm_mcp_text_result(
16401+ root_ok ? "out of memory" : "project path contains invalid characters", true);
1630516402 }
1630616403
1630716404 /* Every detect snapshot and cursor is generation-bound. Validate the
@@ -16391,6 +16488,25 @@ static char *handle_detect_changes(cbm_mcp_server_t *srv, const char *args) {
1639116488 }
1639216489 if (resolve_cancelled || resolve_run != 0 || resolve_result.exit_code != 0 || resolve_oom ||
1639316490 !head_oid[0] || !base_oid[0] || !git_prefix_valid) {
16491+ /* Name the ref that was tried: a bare "not a commit" left callers of
16492+ * the implicit default guessing which base was meant (#1357). */
16493+ char resolve_error[CBM_SZ_2K];
16494+ if (!base_defaulted) {
16495+ snprintf(resolve_error, sizeof(resolve_error),
16496+ "git revision resolution failed: base_branch \"%s\" or HEAD is not a commit",
16497+ base_branch);
16498+ } else if (!default_base_found) {
16499+ snprintf(resolve_error, sizeof(resolve_error),
16500+ "git revision resolution failed: no default base found (tried origin/HEAD, "
16501+ "main, master and the upstream; fell back to \"%s\"), or HEAD is not a "
16502+ "commit; pass base_branch",
16503+ base_branch);
16504+ } else {
16505+ snprintf(resolve_error, sizeof(resolve_error),
16506+ "git revision resolution failed: default base \"%s\" or HEAD is not a "
16507+ "commit; pass base_branch",
16508+ base_branch);
16509+ }
1639416510 free(direction);
1639516511 free(root_path);
1639616512 free(project);
@@ -16409,8 +16525,7 @@ static char *handle_detect_changes(cbm_mcp_server_t *srv, const char *args) {
1640916525 "worktree could not be determined",
1641016526 true);
1641116527 }
16412- return cbm_mcp_text_result(
16413- "git revision resolution failed: base_branch or HEAD is not a commit", true);
16528+ return cbm_mcp_text_result(resolve_error, true);
1641416529 }
1641516530
1641616531 char merge_base[65] = "";
0 commit comments