Skip to content

Commit 72f206d

Browse files
committed
fix(mcp): resolve repository default for detect_changes
Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent 96c3f41 commit 72f206d

2 files changed

Lines changed: 280 additions & 12 deletions

File tree

‎src/mcp/mcp.c‎

Lines changed: 127 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
1602416110
typedef 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] = "";

‎tests/test_mcp.c‎

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14317,6 +14317,155 @@ TEST(tool_detect_changes_invalid_base_is_an_error) {
1431714317
PASS();
1431814318
}
1431914319

14320+
/* #1357: detect_changes without base_branch must diff against the
14321+
* repository's default branch, not a literal "main". Each fixture is a real
14322+
* git repository: a base commit on the default branch, then a feature branch
14323+
* with one committed file, so the correct base yields exactly that file. */
14324+
typedef struct {
14325+
char repo[CBM_SZ_4K];
14326+
char cache[CBM_SZ_4K];
14327+
char base[CBM_SZ_256];
14328+
bool is_error;
14329+
bool feature_listed;
14330+
char error_text[CBM_SZ_1K];
14331+
} detect_default_base_probe_t;
14332+
14333+
static bool detect_default_base_repo(const char *repo, const char *default_branch,
14334+
bool set_origin_head) {
14335+
char head_ref[CBM_SZ_256];
14336+
snprintf(head_ref, sizeof(head_ref), "refs/heads/%s", default_branch);
14337+
char remote_ref[CBM_SZ_256];
14338+
snprintf(remote_ref, sizeof(remote_ref), "refs/remotes/origin/%s", default_branch);
14339+
char base_file[CBM_SZ_4K];
14340+
char feature_file[CBM_SZ_4K];
14341+
snprintf(base_file, sizeof(base_file), "%s/base.c", repo);
14342+
snprintf(feature_file, sizeof(feature_file), "%s/feature.c", repo);
14343+
const char *const init_args[] = {"init", "-q", NULL};
14344+
const char *const head_args[] = {"symbolic-ref", "HEAD", head_ref, NULL};
14345+
const char *const add_base_args[] = {"add", "base.c", NULL};
14346+
const char *const add_feature_args[] = {"add", "feature.c", NULL};
14347+
const char *const commit_args[] = {
14348+
"-c", "user.name=cbm-test",
14349+
"-c", "user.email=cbm-test@example.invalid",
14350+
"-c", "commit.gpgsign=false",
14351+
"commit", "-q",
14352+
"-m", "fixture",
14353+
NULL,
14354+
};
14355+
const char *const remote_args[] = {"update-ref", remote_ref, "HEAD", NULL};
14356+
const char *const origin_head_args[] = {"symbolic-ref", "refs/remotes/origin/HEAD", remote_ref,
14357+
NULL};
14358+
const char *const feature_args[] = {"checkout", "-q", "-b", "feature", NULL};
14359+
bool ok = mcp_test_git(repo, init_args) == 0 && mcp_test_git(repo, head_args) == 0 &&
14360+
th_write_file(base_file, "int base_value = 1;\n") == 0 &&
14361+
mcp_test_git(repo, add_base_args) == 0 && mcp_test_git(repo, commit_args) == 0;
14362+
if (ok && set_origin_head) {
14363+
ok = mcp_test_git(repo, remote_args) == 0 && mcp_test_git(repo, origin_head_args) == 0;
14364+
}
14365+
return ok && mcp_test_git(repo, feature_args) == 0 &&
14366+
th_write_file(feature_file, "int feature_value = 2;\n") == 0 &&
14367+
mcp_test_git(repo, add_feature_args) == 0 && mcp_test_git(repo, commit_args) == 0;
14368+
}
14369+
14370+
static bool detect_default_base_run(const char *default_branch, bool set_origin_head,
14371+
const char *base_arg, detect_default_base_probe_t *probe) {
14372+
memset(probe, 0, sizeof(*probe));
14373+
snprintf(probe->repo, sizeof(probe->repo), "%s/cbm-detect-default-XXXXXX", cbm_tmpdir());
14374+
snprintf(probe->cache, sizeof(probe->cache), "%s/cbm-detect-default-cache-XXXXXX",
14375+
cbm_tmpdir());
14376+
if (!cbm_mkdtemp(probe->repo) || !cbm_mkdtemp(probe->cache) ||
14377+
!detect_default_base_repo(probe->repo, default_branch, set_origin_head)) {
14378+
return false;
14379+
}
14380+
const char *saved_cache = getenv("CBM_CACHE_DIR");
14381+
char *saved_cache_copy = saved_cache ? strdup(saved_cache) : NULL;
14382+
bool ok = cbm_setenv("CBM_CACHE_DIR", probe->cache, 1) == 0;
14383+
cbm_mcp_server_t *srv = ok ? cbm_mcp_server_new(NULL) : NULL;
14384+
cbm_store_t *store = srv ? cbm_mcp_server_store(srv) : NULL;
14385+
const char *project = "detect-default-base-project";
14386+
ok = store && cbm_store_upsert_project(store, project, probe->repo) == CBM_STORE_OK;
14387+
char args[CBM_SZ_1K];
14388+
if (base_arg) {
14389+
snprintf(args, sizeof(args),
14390+
"{\"project\":\"%s\",\"base_branch\":\"%s\",\"scope\":\"files\","
14391+
"\"format\":\"json\"}",
14392+
project, base_arg);
14393+
} else {
14394+
snprintf(args, sizeof(args), "{\"project\":\"%s\",\"scope\":\"files\",\"format\":\"json\"}",
14395+
project);
14396+
}
14397+
if (ok) {
14398+
cbm_mcp_server_set_project(srv, project);
14399+
}
14400+
char *response = ok ? cbm_mcp_handle_tool(srv, "detect_changes", args) : NULL;
14401+
char *inner = response ? extract_text_content(response) : NULL;
14402+
probe->is_error = response && response_contains_json_fragment(response, "\"isError\":true");
14403+
if (inner && probe->is_error) {
14404+
snprintf(probe->error_text, sizeof(probe->error_text), "%s", inner);
14405+
}
14406+
yyjson_doc *doc = inner && !probe->is_error ? yyjson_read(inner, strlen(inner), 0) : NULL;
14407+
yyjson_val *root = doc ? yyjson_doc_get_root(doc) : NULL;
14408+
const char *base = root ? yyjson_get_str(yyjson_obj_get(root, "base")) : NULL;
14409+
if (base) {
14410+
snprintf(probe->base, sizeof(probe->base), "%s", base);
14411+
}
14412+
yyjson_val *changed = root ? yyjson_obj_get(root, "changed_files") : NULL;
14413+
size_t index;
14414+
size_t count;
14415+
yyjson_val *entry;
14416+
yyjson_arr_foreach(changed, index, count, entry) {
14417+
if (yyjson_is_str(entry) && strcmp(yyjson_get_str(entry), "feature.c") == 0) {
14418+
probe->feature_listed = true;
14419+
}
14420+
}
14421+
yyjson_doc_free(doc);
14422+
free(inner);
14423+
free(response);
14424+
if (srv) {
14425+
cbm_mcp_server_free(srv);
14426+
}
14427+
restore_cache_dir(saved_cache_copy);
14428+
free(saved_cache_copy);
14429+
bool cleaned = th_rmtree(probe->cache) == 0 && th_rmtree(probe->repo) == 0;
14430+
return ok && cleaned;
14431+
}
14432+
14433+
TEST(detect_changes_default_base_follows_origin_head_issue1357) {
14434+
detect_default_base_probe_t probe;
14435+
ASSERT_TRUE(detect_default_base_run("trunk", true, NULL, &probe));
14436+
ASSERT_FALSE(probe.is_error);
14437+
ASSERT_STR_EQ(probe.base, "origin/trunk");
14438+
ASSERT_TRUE(probe.feature_listed);
14439+
PASS();
14440+
}
14441+
14442+
TEST(detect_changes_default_base_falls_back_to_master_issue1357) {
14443+
detect_default_base_probe_t probe;
14444+
ASSERT_TRUE(detect_default_base_run("master", false, NULL, &probe));
14445+
ASSERT_FALSE(probe.is_error);
14446+
ASSERT_STR_EQ(probe.base, "master");
14447+
ASSERT_TRUE(probe.feature_listed);
14448+
PASS();
14449+
}
14450+
14451+
TEST(detect_changes_explicit_base_is_used_as_given_issue1357) {
14452+
detect_default_base_probe_t probe;
14453+
ASSERT_TRUE(detect_default_base_run("trunk", true, "trunk", &probe));
14454+
ASSERT_FALSE(probe.is_error);
14455+
ASSERT_STR_EQ(probe.base, "trunk");
14456+
ASSERT_TRUE(probe.feature_listed);
14457+
PASS();
14458+
}
14459+
14460+
TEST(detect_changes_unresolvable_default_names_tried_ref_issue1357) {
14461+
detect_default_base_probe_t probe;
14462+
ASSERT_TRUE(detect_default_base_run("develop", false, NULL, &probe));
14463+
ASSERT_TRUE(probe.is_error);
14464+
ASSERT_NOT_NULL(strstr(probe.error_text, "\"main\""));
14465+
ASSERT_NOT_NULL(strstr(probe.error_text, "origin/HEAD"));
14466+
PASS();
14467+
}
14468+
1432014469
TEST(tool_detect_changes_preserves_utf8_git_path_and_impact_seed) {
1432114470
char repo[CBM_SZ_4K];
1432214471
snprintf(repo, sizeof(repo), "%s/cbm-detect-utf8-path-XXXXXX", cbm_tmpdir());
@@ -21289,6 +21438,10 @@ SUITE(mcp) {
2128921438
RUN_TEST(tool_manage_adr_get_accepts_symlink_path);
2129021439
RUN_TEST(tool_detect_changes_not_found_rich_error);
2129121440
RUN_TEST(tool_detect_changes_invalid_base_is_an_error);
21441+
RUN_TEST(detect_changes_default_base_follows_origin_head_issue1357);
21442+
RUN_TEST(detect_changes_default_base_falls_back_to_master_issue1357);
21443+
RUN_TEST(detect_changes_explicit_base_is_used_as_given_issue1357);
21444+
RUN_TEST(detect_changes_unresolvable_default_names_tried_ref_issue1357);
2129221445
RUN_TEST(tool_detect_changes_preserves_utf8_git_path_and_impact_seed);
2129321446
RUN_TEST(tool_detect_changes_finds_nested_untracked_file_and_impact_seed);
2129421447
RUN_TEST(tool_detect_changes_subdirectory_project_translates_git_root_paths_issue1951);

0 commit comments

Comments
 (0)