Count tracked files via git ls-files */
| 657 | |
| 658 | /* Count tracked files via git ls-files */ |
| 659 | static watcher_git_status_t git_file_count(cbm_watcher_t *w, project_state_t *state, |
| 660 | int *count_out) { |
| 661 | if (!count_out) { |
| 662 | return WATCHER_GIT_SUPERVISION_FAILED; |
| 663 | } |
| 664 | *count_out = 0; |
| 665 | const char *argv[] = {"git", "-C", state->root_path, "ls-files", "-z", NULL}; |
| 666 | watcher_git_output_t output; |
| 667 | watcher_git_status_t status = watcher_git_run(w, state, argv, WATCHER_GIT_OUTPUT_MAX, &output); |
| 668 | if (status != WATCHER_GIT_OK) { |
| 669 | return status; |
| 670 | } |
| 671 | FILE *fp = cbm_fopen(output.path, "rb"); |
| 672 | if (!fp) { |
| 673 | watcher_git_output_cleanup(&output); |
| 674 | return WATCHER_GIT_SUPERVISION_FAILED; |
| 675 | } |
| 676 | |
| 677 | /* NUL mode is unambiguous even for tracked filenames containing newlines. */ |
| 678 | int count = 0; |
| 679 | char buf[CBM_SZ_1K]; |
| 680 | size_t n; |
| 681 | while ((n = fread(buf, 1, sizeof(buf), fp)) > 0) { |
| 682 | for (size_t i = 0; i < n; i++) { |
| 683 | if (buf[i] == '\0' && count < INT_MAX) { |
| 684 | count++; |
| 685 | } |
| 686 | } |
| 687 | } |
| 688 | if (ferror(fp) || fclose(fp) != 0) { |
| 689 | watcher_git_output_cleanup(&output); |
| 690 | return WATCHER_GIT_SUPERVISION_FAILED; |
| 691 | } |
| 692 | watcher_git_output_cleanup(&output); |
| 693 | *count_out = count; |
| 694 | return WATCHER_GIT_OK; |
| 695 | } |
| 696 | |
| 697 | /* ── Project state lifecycle ────────────────────────────────────── */ |
| 698 |
no test coverage detected