| 205 | } |
| 206 | |
| 207 | static int mcp_test_git(const char *root, const char *const *arguments) { |
| 208 | char empty_config[CBM_SZ_4K]; |
| 209 | int config_length = |
| 210 | snprintf(empty_config, sizeof(empty_config), "%s/.cbm-empty-gitconfig", root); |
| 211 | if (config_length <= 0 || (size_t)config_length >= sizeof(empty_config)) { |
| 212 | return -1; |
| 213 | } |
| 214 | FILE *config = cbm_fopen(empty_config, "wb"); |
| 215 | if (!config) { |
| 216 | return -1; |
| 217 | } |
| 218 | if (fclose(config) != 0) { |
| 219 | return -1; |
| 220 | } |
| 221 | |
| 222 | mcp_test_env_backup_t backups[] = { |
| 223 | {.name = "GIT_CONFIG_GLOBAL"}, {.name = "GIT_CONFIG_SYSTEM"}, |
| 224 | {.name = "GIT_CONFIG_NOSYSTEM"}, {.name = "GIT_CONFIG_COUNT"}, |
| 225 | {.name = "GIT_CONFIG_PARAMETERS"}, |
| 226 | }; |
| 227 | bool snapshot_ok = true; |
| 228 | for (size_t index = 0; index < sizeof(backups) / sizeof(backups[0]); index++) { |
| 229 | const char *value = getenv(backups[index].name); |
| 230 | backups[index].present = value != NULL; |
| 231 | backups[index].value = value ? strdup(value) : NULL; |
| 232 | snapshot_ok = snapshot_ok && (!value || backups[index].value); |
| 233 | } |
| 234 | if (!snapshot_ok) { |
| 235 | for (size_t index = 0; index < sizeof(backups) / sizeof(backups[0]); index++) { |
| 236 | free(backups[index].value); |
| 237 | } |
| 238 | return -1; |
| 239 | } |
| 240 | bool environment_ok = cbm_setenv("GIT_CONFIG_GLOBAL", empty_config, 1) == 0 && |
| 241 | cbm_setenv("GIT_CONFIG_SYSTEM", empty_config, 1) == 0 && |
| 242 | cbm_setenv("GIT_CONFIG_NOSYSTEM", "1", 1) == 0 && |
| 243 | cbm_setenv("GIT_CONFIG_COUNT", "0", 1) == 0 && |
| 244 | cbm_unsetenv("GIT_CONFIG_PARAMETERS") == 0; |
| 245 | if (!environment_ok) { |
| 246 | mcp_test_restore_env(backups, sizeof(backups) / sizeof(backups[0])); |
| 247 | return -1; |
| 248 | } |
| 249 | |
| 250 | const char *git = "git"; |
| 251 | #ifdef _WIN32 |
| 252 | char git_executable[CBM_SZ_4K]; |
| 253 | const char *resolved = cbm_find_cli("git", cbm_get_home_dir()); |
| 254 | int resolved_length = |
| 255 | resolved ? snprintf(git_executable, sizeof(git_executable), "%s", resolved) : -1; |
| 256 | if (resolved_length <= 0 || (size_t)resolved_length >= sizeof(git_executable)) { |
| 257 | mcp_test_restore_env(backups, sizeof(backups) / sizeof(backups[0])); |
| 258 | return -1; |
| 259 | } |
| 260 | git = git_executable; |
| 261 | #endif |
| 262 | const char *argv[24] = {git, "-C", root}; |
| 263 | size_t index = 3; |
| 264 | while (arguments && *arguments && index + 1 < sizeof(argv) / sizeof(argv[0])) { |
no test coverage detected