Run `git -C repo_path git_args` and copy the first output line (newlines * trimmed) into buf. Returns 0 only when git exited 0 with a non-empty line. */
| 47 | /* Run `git -C repo_path git_args` and copy the first output line (newlines |
| 48 | * trimmed) into buf. Returns 0 only when git exited 0 with a non-empty line. */ |
| 49 | static int git_capture_into(const char *repo_path, const char *git_args, char *buf, |
| 50 | size_t buf_size) { |
| 51 | if (!buf || buf_size == 0) { |
| 52 | return CBM_NOT_FOUND; |
| 53 | } |
| 54 | buf[0] = '\0'; |
| 55 | if (!repo_path || !git_args || !git_validate_repo_path(repo_path)) { |
| 56 | return CBM_NOT_FOUND; |
| 57 | } |
| 58 | |
| 59 | char cmd[GIT_CMD_MAX]; |
| 60 | #ifdef _WIN32 |
| 61 | const char *null_dev = "NUL"; |
| 62 | #else |
| 63 | const char *null_dev = "/dev/null"; |
| 64 | #endif |
| 65 | /* Double quotes work for POSIX shells and cmd.exe. cbm_validate_shell_arg() |
| 66 | * rejects quote/backslash/substitution metacharacters before interpolation. */ |
| 67 | int n = snprintf(cmd, sizeof(cmd), "git -C \"%s\" %s 2>%s", repo_path, git_args, null_dev); |
| 68 | if (n < 0 || n >= (int)sizeof(cmd)) { |
| 69 | return CBM_NOT_FOUND; |
| 70 | } |
| 71 | |
| 72 | /* Debug-level stage markers: none of these waits has a deadline, so a |
| 73 | * stuck git child is named by the last marker printed (spawn, read or |
| 74 | * close). Silent at the default level. */ |
| 75 | cbm_log_debug("git.capture", "stage", "spawn", "args", git_args); |
| 76 | FILE *fp = cbm_popen_git(cmd); |
| 77 | if (!fp) { |
| 78 | return CBM_NOT_FOUND; |
| 79 | } |
| 80 | |
| 81 | cbm_log_debug("git.capture", "stage", "read", "args", git_args); |
| 82 | if (!fgets(buf, (int)buf_size, fp)) { |
| 83 | buf[0] = '\0'; |
| 84 | cbm_log_debug("git.capture", "stage", "close", "args", git_args, "output", "none"); |
| 85 | cbm_pclose(fp); |
| 86 | cbm_log_debug("git.capture", "stage", "done", "args", git_args); |
| 87 | return CBM_NOT_FOUND; |
| 88 | } |
| 89 | trim_newlines(buf); |
| 90 | |
| 91 | cbm_log_debug("git.capture", "stage", "close", "args", git_args); |
| 92 | int rc = cbm_pclose(fp); |
| 93 | cbm_log_debug("git.capture", "stage", "done", "args", git_args); |
| 94 | if (rc != 0 || buf[0] == '\0') { |
| 95 | buf[0] = '\0'; |
| 96 | return CBM_NOT_FOUND; |
| 97 | } |
| 98 | return 0; |
| 99 | } |
| 100 | |
| 101 | static int git_capture(const char *repo_path, const char *git_args, char **out) { |
| 102 | if (!out) { |
no test coverage detected