Skip to content

Commit 3a26acf

Browse files
authored
Merge pull request DeusData#2496 from DeusData/fix/issue-1357-default-branch-v2
fix(mcp): detect changes from the repository default branch
2 parents 79363ad + 72f206d commit 3a26acf

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
@@ -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+
1618216268
typedef 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] = "";

‎tests/test_mcp.c‎

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14500,6 +14500,155 @@ TEST(tool_detect_changes_invalid_base_is_an_error) {
1450014500
PASS();
1450114501
}
1450214502

14503+
/* #1357: detect_changes without base_branch must diff against the
14504+
* repository's default branch, not a literal "main". Each fixture is a real
14505+
* git repository: a base commit on the default branch, then a feature branch
14506+
* with one committed file, so the correct base yields exactly that file. */
14507+
typedef struct {
14508+
char repo[CBM_SZ_4K];
14509+
char cache[CBM_SZ_4K];
14510+
char base[CBM_SZ_256];
14511+
bool is_error;
14512+
bool feature_listed;
14513+
char error_text[CBM_SZ_1K];
14514+
} detect_default_base_probe_t;
14515+
14516+
static bool detect_default_base_repo(const char *repo, const char *default_branch,
14517+
bool set_origin_head) {
14518+
char head_ref[CBM_SZ_256];
14519+
snprintf(head_ref, sizeof(head_ref), "refs/heads/%s", default_branch);
14520+
char remote_ref[CBM_SZ_256];
14521+
snprintf(remote_ref, sizeof(remote_ref), "refs/remotes/origin/%s", default_branch);
14522+
char base_file[CBM_SZ_4K];
14523+
char feature_file[CBM_SZ_4K];
14524+
snprintf(base_file, sizeof(base_file), "%s/base.c", repo);
14525+
snprintf(feature_file, sizeof(feature_file), "%s/feature.c", repo);
14526+
const char *const init_args[] = {"init", "-q", NULL};
14527+
const char *const head_args[] = {"symbolic-ref", "HEAD", head_ref, NULL};
14528+
const char *const add_base_args[] = {"add", "base.c", NULL};
14529+
const char *const add_feature_args[] = {"add", "feature.c", NULL};
14530+
const char *const commit_args[] = {
14531+
"-c", "user.name=cbm-test",
14532+
"-c", "user.email=cbm-test@example.invalid",
14533+
"-c", "commit.gpgsign=false",
14534+
"commit", "-q",
14535+
"-m", "fixture",
14536+
NULL,
14537+
};
14538+
const char *const remote_args[] = {"update-ref", remote_ref, "HEAD", NULL};
14539+
const char *const origin_head_args[] = {"symbolic-ref", "refs/remotes/origin/HEAD", remote_ref,
14540+
NULL};
14541+
const char *const feature_args[] = {"checkout", "-q", "-b", "feature", NULL};
14542+
bool ok = mcp_test_git(repo, init_args) == 0 && mcp_test_git(repo, head_args) == 0 &&
14543+
th_write_file(base_file, "int base_value = 1;\n") == 0 &&
14544+
mcp_test_git(repo, add_base_args) == 0 && mcp_test_git(repo, commit_args) == 0;
14545+
if (ok && set_origin_head) {
14546+
ok = mcp_test_git(repo, remote_args) == 0 && mcp_test_git(repo, origin_head_args) == 0;
14547+
}
14548+
return ok && mcp_test_git(repo, feature_args) == 0 &&
14549+
th_write_file(feature_file, "int feature_value = 2;\n") == 0 &&
14550+
mcp_test_git(repo, add_feature_args) == 0 && mcp_test_git(repo, commit_args) == 0;
14551+
}
14552+
14553+
static bool detect_default_base_run(const char *default_branch, bool set_origin_head,
14554+
const char *base_arg, detect_default_base_probe_t *probe) {
14555+
memset(probe, 0, sizeof(*probe));
14556+
snprintf(probe->repo, sizeof(probe->repo), "%s/cbm-detect-default-XXXXXX", cbm_tmpdir());
14557+
snprintf(probe->cache, sizeof(probe->cache), "%s/cbm-detect-default-cache-XXXXXX",
14558+
cbm_tmpdir());
14559+
if (!cbm_mkdtemp(probe->repo) || !cbm_mkdtemp(probe->cache) ||
14560+
!detect_default_base_repo(probe->repo, default_branch, set_origin_head)) {
14561+
return false;
14562+
}
14563+
const char *saved_cache = getenv("CBM_CACHE_DIR");
14564+
char *saved_cache_copy = saved_cache ? strdup(saved_cache) : NULL;
14565+
bool ok = cbm_setenv("CBM_CACHE_DIR", probe->cache, 1) == 0;
14566+
cbm_mcp_server_t *srv = ok ? cbm_mcp_server_new(NULL) : NULL;
14567+
cbm_store_t *store = srv ? cbm_mcp_server_store(srv) : NULL;
14568+
const char *project = "detect-default-base-project";
14569+
ok = store && cbm_store_upsert_project(store, project, probe->repo) == CBM_STORE_OK;
14570+
char args[CBM_SZ_1K];
14571+
if (base_arg) {
14572+
snprintf(args, sizeof(args),
14573+
"{\"project\":\"%s\",\"base_branch\":\"%s\",\"scope\":\"files\","
14574+
"\"format\":\"json\"}",
14575+
project, base_arg);
14576+
} else {
14577+
snprintf(args, sizeof(args), "{\"project\":\"%s\",\"scope\":\"files\",\"format\":\"json\"}",
14578+
project);
14579+
}
14580+
if (ok) {
14581+
cbm_mcp_server_set_project(srv, project);
14582+
}
14583+
char *response = ok ? cbm_mcp_handle_tool(srv, "detect_changes", args) : NULL;
14584+
char *inner = response ? extract_text_content(response) : NULL;
14585+
probe->is_error = response && response_contains_json_fragment(response, "\"isError\":true");
14586+
if (inner && probe->is_error) {
14587+
snprintf(probe->error_text, sizeof(probe->error_text), "%s", inner);
14588+
}
14589+
yyjson_doc *doc = inner && !probe->is_error ? yyjson_read(inner, strlen(inner), 0) : NULL;
14590+
yyjson_val *root = doc ? yyjson_doc_get_root(doc) : NULL;
14591+
const char *base = root ? yyjson_get_str(yyjson_obj_get(root, "base")) : NULL;
14592+
if (base) {
14593+
snprintf(probe->base, sizeof(probe->base), "%s", base);
14594+
}
14595+
yyjson_val *changed = root ? yyjson_obj_get(root, "changed_files") : NULL;
14596+
size_t index;
14597+
size_t count;
14598+
yyjson_val *entry;
14599+
yyjson_arr_foreach(changed, index, count, entry) {
14600+
if (yyjson_is_str(entry) && strcmp(yyjson_get_str(entry), "feature.c") == 0) {
14601+
probe->feature_listed = true;
14602+
}
14603+
}
14604+
yyjson_doc_free(doc);
14605+
free(inner);
14606+
free(response);
14607+
if (srv) {
14608+
cbm_mcp_server_free(srv);
14609+
}
14610+
restore_cache_dir(saved_cache_copy);
14611+
free(saved_cache_copy);
14612+
bool cleaned = th_rmtree(probe->cache) == 0 && th_rmtree(probe->repo) == 0;
14613+
return ok && cleaned;
14614+
}
14615+
14616+
TEST(detect_changes_default_base_follows_origin_head_issue1357) {
14617+
detect_default_base_probe_t probe;
14618+
ASSERT_TRUE(detect_default_base_run("trunk", true, NULL, &probe));
14619+
ASSERT_FALSE(probe.is_error);
14620+
ASSERT_STR_EQ(probe.base, "origin/trunk");
14621+
ASSERT_TRUE(probe.feature_listed);
14622+
PASS();
14623+
}
14624+
14625+
TEST(detect_changes_default_base_falls_back_to_master_issue1357) {
14626+
detect_default_base_probe_t probe;
14627+
ASSERT_TRUE(detect_default_base_run("master", false, NULL, &probe));
14628+
ASSERT_FALSE(probe.is_error);
14629+
ASSERT_STR_EQ(probe.base, "master");
14630+
ASSERT_TRUE(probe.feature_listed);
14631+
PASS();
14632+
}
14633+
14634+
TEST(detect_changes_explicit_base_is_used_as_given_issue1357) {
14635+
detect_default_base_probe_t probe;
14636+
ASSERT_TRUE(detect_default_base_run("trunk", true, "trunk", &probe));
14637+
ASSERT_FALSE(probe.is_error);
14638+
ASSERT_STR_EQ(probe.base, "trunk");
14639+
ASSERT_TRUE(probe.feature_listed);
14640+
PASS();
14641+
}
14642+
14643+
TEST(detect_changes_unresolvable_default_names_tried_ref_issue1357) {
14644+
detect_default_base_probe_t probe;
14645+
ASSERT_TRUE(detect_default_base_run("develop", false, NULL, &probe));
14646+
ASSERT_TRUE(probe.is_error);
14647+
ASSERT_NOT_NULL(strstr(probe.error_text, "\"main\""));
14648+
ASSERT_NOT_NULL(strstr(probe.error_text, "origin/HEAD"));
14649+
PASS();
14650+
}
14651+
1450314652
TEST(tool_detect_changes_preserves_utf8_git_path_and_impact_seed) {
1450414653
char repo[CBM_SZ_4K];
1450514654
snprintf(repo, sizeof(repo), "%s/cbm-detect-utf8-path-XXXXXX", cbm_tmpdir());
@@ -21513,6 +21662,10 @@ SUITE(mcp) {
2151321662
RUN_TEST(tool_manage_adr_get_accepts_symlink_path);
2151421663
RUN_TEST(tool_detect_changes_not_found_rich_error);
2151521664
RUN_TEST(tool_detect_changes_invalid_base_is_an_error);
21665+
RUN_TEST(detect_changes_default_base_follows_origin_head_issue1357);
21666+
RUN_TEST(detect_changes_default_base_falls_back_to_master_issue1357);
21667+
RUN_TEST(detect_changes_explicit_base_is_used_as_given_issue1357);
21668+
RUN_TEST(detect_changes_unresolvable_default_names_tried_ref_issue1357);
2151621669
RUN_TEST(tool_detect_changes_preserves_utf8_git_path_and_impact_seed);
2151721670
RUN_TEST(tool_detect_changes_finds_nested_untracked_file_and_impact_seed);
2151821671
RUN_TEST(tool_detect_changes_subdirectory_project_translates_git_root_paths_issue1951);

0 commit comments

Comments
 (0)