MCPcopy Create free account
hub / github.com/DeusData/codebase-memory-mcp / detect_default_base

Function detect_default_base

src/mcp/mcp.c:17924–17967  ·  view source on GitHub ↗

#1357: without base_branch, diff against the repository's default branch * rather than a literal "main" that trunk/master/develop repositories do not * have. Order: origin/HEAD (what the clone calls the default), a local main, * a local master, then the current branch's upstream. main and master come * before the upstream on purpose: a pushed feature branch tracks its own * remote twin, and d

Source from the content-addressed store, hash-verified

17922 * analysis to unpushed commits. Nothing resolves -> false, out = "main", and
17923 * the caller reports that ref as the one it tried. */
17924static bool detect_default_base(cbm_mcp_server_t *srv, const char *root_path, char *out,
17925 size_t out_size) {
17926#ifdef _WIN32
17927#define DETECT_GIT_Q "\""
17928#define DETECT_GIT_NULL "2>NUL"
17929#else
17930#define DETECT_GIT_Q "'"
17931#define DETECT_GIT_NULL "2>/dev/null"
17932#endif
17933 char command[CBM_SZ_2K];
17934 char line[CBM_SZ_1K];
17935 snprintf(command, sizeof(command),
17936 "git -C " DETECT_GIT_Q "%s" DETECT_GIT_Q
17937 " symbolic-ref -q --short refs/remotes/origin/HEAD " DETECT_GIT_NULL,
17938 root_path);
17939 if (detect_git_first_line(srv, command, line, sizeof(line))) {
17940 snprintf(out, out_size, "%s", line);
17941 return true;
17942 }
17943 static const char *const local_defaults[] = {"main", "master"};
17944 for (size_t i = 0; i < sizeof(local_defaults) / sizeof(local_defaults[0]); i++) {
17945 snprintf(command, sizeof(command),
17946 "git -C " DETECT_GIT_Q "%s" DETECT_GIT_Q " rev-parse -q --verify " DETECT_GIT_Q
17947 "refs/heads/%s^{commit}" DETECT_GIT_Q " " DETECT_GIT_NULL,
17948 root_path, local_defaults[i]);
17949 if (detect_git_first_line(srv, command, line, sizeof(line))) {
17950 snprintf(out, out_size, "%s", local_defaults[i]);
17951 return true;
17952 }
17953 }
17954 snprintf(command, sizeof(command),
17955 "git -C " DETECT_GIT_Q "%s" DETECT_GIT_Q
17956 " rev-parse -q --abbrev-ref --symbolic-full-name " DETECT_GIT_Q
17957 "@{upstream}" DETECT_GIT_Q " " DETECT_GIT_NULL,
17958 root_path);
17959 if (detect_git_first_line(srv, command, line, sizeof(line))) {
17960 snprintf(out, out_size, "%s", line);
17961 return true;
17962 }
17963#undef DETECT_GIT_Q
17964#undef DETECT_GIT_NULL
17965 snprintf(out, out_size, "%s", "main");
17966 return false;
17967}
17968
17969typedef struct {
17970 char stream; /* c=changed files, i=impacted symbols, m=module rollup */

Callers 1

handle_detect_changesFunction · 0.85

Calls 1

detect_git_first_lineFunction · 0.85

Tested by

no test coverage detected