Count tracked files via git ls-files */
| 835 | |
| 836 | /* Count tracked files via git ls-files */ |
| 837 | static watcher_git_status_t git_file_count(cbm_watcher_t *w, project_state_t *state, |
| 838 | int *count_out) { |
| 839 | if (!count_out) { |
| 840 | return WATCHER_GIT_SUPERVISION_FAILED; |
| 841 | } |
| 842 | *count_out = 0; |
| 843 | const char *argv[] = {"git", "-C", state->root_path, "ls-files", "-z", NULL}; |
| 844 | watcher_git_output_t output; |
| 845 | watcher_git_status_t status = watcher_git_run(w, state, argv, WATCHER_GIT_OUTPUT_MAX, &output); |
| 846 | if (status != WATCHER_GIT_OK) { |
| 847 | return status; |
| 848 | } |
| 849 | FILE *fp = cbm_fopen(output.path, "rb"); |
| 850 | if (!fp) { |
| 851 | watcher_git_output_cleanup(&output); |
| 852 | return WATCHER_GIT_SUPERVISION_FAILED; |
| 853 | } |
| 854 | |
| 855 | /* NUL mode is unambiguous even for tracked filenames containing newlines. */ |
| 856 | int count = 0; |
| 857 | char buf[CBM_SZ_1K]; |
| 858 | size_t n; |
| 859 | while ((n = fread(buf, 1, sizeof(buf), fp)) > 0) { |
| 860 | for (size_t i = 0; i < n; i++) { |
| 861 | if (buf[i] == '\0' && count < INT_MAX) { |
| 862 | count++; |
| 863 | } |
| 864 | } |
| 865 | } |
| 866 | if (ferror(fp) || fclose(fp) != 0) { |
| 867 | watcher_git_output_cleanup(&output); |
| 868 | return WATCHER_GIT_SUPERVISION_FAILED; |
| 869 | } |
| 870 | watcher_git_output_cleanup(&output); |
| 871 | *count_out = count; |
| 872 | return WATCHER_GIT_OK; |
| 873 | } |
| 874 | |
| 875 | /* ── Non-git tree signature (#1948) ─────────────────────────────── */ |
| 876 |
no test coverage detected