| 290 | } |
| 291 | |
| 292 | static int mcp_test_git(const char *root, const char *const *arguments) { |
| 293 | char empty_config[CBM_SZ_4K]; |
| 294 | int config_length = |
| 295 | snprintf(empty_config, sizeof(empty_config), "%s/.cbm-empty-gitconfig", root); |
| 296 | if (config_length <= 0 || (size_t)config_length >= sizeof(empty_config)) { |
| 297 | return -1; |
| 298 | } |
| 299 | FILE *config = cbm_fopen(empty_config, "wb"); |
| 300 | if (!config) { |
| 301 | return -1; |
| 302 | } |
| 303 | if (fclose(config) != 0) { |
| 304 | return -1; |
| 305 | } |
| 306 | |
| 307 | mcp_test_env_backup_t backups[] = { |
| 308 | {.name = "GIT_CONFIG_GLOBAL"}, {.name = "GIT_CONFIG_SYSTEM"}, |
| 309 | {.name = "GIT_CONFIG_NOSYSTEM"}, {.name = "GIT_CONFIG_COUNT"}, |
| 310 | {.name = "GIT_CONFIG_PARAMETERS"}, |
| 311 | }; |
| 312 | bool snapshot_ok = true; |
| 313 | for (size_t index = 0; index < sizeof(backups) / sizeof(backups[0]); index++) { |
| 314 | const char *value = getenv(backups[index].name); |
| 315 | backups[index].present = value != NULL; |
| 316 | backups[index].value = value ? strdup(value) : NULL; |
| 317 | snapshot_ok = snapshot_ok && (!value || backups[index].value); |
| 318 | } |
| 319 | if (!snapshot_ok) { |
| 320 | for (size_t index = 0; index < sizeof(backups) / sizeof(backups[0]); index++) { |
| 321 | free(backups[index].value); |
| 322 | } |
| 323 | return -1; |
| 324 | } |
| 325 | bool environment_ok = cbm_setenv("GIT_CONFIG_GLOBAL", empty_config, 1) == 0 && |
| 326 | cbm_setenv("GIT_CONFIG_SYSTEM", empty_config, 1) == 0 && |
| 327 | cbm_setenv("GIT_CONFIG_NOSYSTEM", "1", 1) == 0 && |
| 328 | cbm_setenv("GIT_CONFIG_COUNT", "0", 1) == 0 && |
| 329 | cbm_unsetenv("GIT_CONFIG_PARAMETERS") == 0; |
| 330 | if (!environment_ok) { |
| 331 | mcp_test_restore_env(backups, sizeof(backups) / sizeof(backups[0])); |
| 332 | return -1; |
| 333 | } |
| 334 | |
| 335 | const char *git = "git"; |
| 336 | #ifdef _WIN32 |
| 337 | char git_executable[CBM_SZ_4K]; |
| 338 | const char *resolved = cbm_find_cli("git", cbm_get_home_dir()); |
| 339 | int resolved_length = |
| 340 | resolved ? snprintf(git_executable, sizeof(git_executable), "%s", resolved) : -1; |
| 341 | if (resolved_length <= 0 || (size_t)resolved_length >= sizeof(git_executable)) { |
| 342 | mcp_test_restore_env(backups, sizeof(backups) / sizeof(backups[0])); |
| 343 | return -1; |
| 344 | } |
| 345 | git = git_executable; |
| 346 | #endif |
| 347 | const char *argv[24] = {git, "-C", root}; |
| 348 | size_t index = 3; |
| 349 | while (arguments && *arguments && index + 1 < sizeof(argv) / sizeof(argv[0])) { |
no test coverage detected