revision: make helper for pathspec to bloom keyvec

When preparing to use bloom filters in a revision walk, Git populates a boom_keyvec with an array of bloom keys for the components of a path. Before we create the ability to map multiple pathspecs to multiple bloom_keyvecs, extract the conversion from a pathspec to a bloom_keyvec into its own helper method. This simplifies the state that persists in prepare_to_use_bloom_filter() as well as makes the future change much simpler. Signed-off-by: Derrick Stolee <stolee@gmail.com> Signed-off-by: Lidong Yan <502024330056@smail.nju.edu.cn> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Lidong Yan committed Jul 12, 2025 at 17:35 UTC 937153dece3c2b1e04b0e071298745abd57cd347
1 file changed +29 -16
revision.c
+29 -16
@@ -687,13 +687,37 @@ static int forbid_bloom_filters(struct pathspec *spec)
687
688 static void release_revisions_bloom_keyvecs(struct rev_info *revs);
689
690 -static void prepare_to_use_bloom_filter(struct rev_info *revs)
690 +static int convert_pathspec_to_bloom_keyvec(struct bloom_keyvec **out,
691 + const struct pathspec_item *pi,
692 + const struct bloom_filter_settings *settings)
693 {
692 - struct pathspec_item *pi;
694 char *path_alloc = NULL;
695 const char *path;
696 size_t len;
697 + int res = 0;
698 +
699 + /* remove single trailing slash from path, if needed */
700 + if (pi->len > 0 && pi->match[pi->len - 1] == '/') {
701 + path_alloc = xmemdupz(pi->match, pi->len - 1);
702 + path = path_alloc;
703 + } else
704 + path = pi->match;
705 +
706 + len = strlen(path);
707 + if (!len) {
708 + res = -1;
709 + goto cleanup;
710 + }
711
712 + *out = bloom_keyvec_new(path, len, settings);
713 +
714 +cleanup:
715 + free(path_alloc);
716 + return res;
717 +}
718 +
719 +static void prepare_to_use_bloom_filter(struct rev_info *revs)
720 +{
721 if (!revs->commits)
722 return;
723
@@ -711,22 +735,12 @@ static void prepare_to_use_bloom_filter(struct rev_info *revs)
735
736 revs->bloom_keyvecs_nr = 1;
737 CALLOC_ARRAY(revs->bloom_keyvecs, 1);
714 - pi = &revs->pruning.pathspec.items[0];
738
716 - /* remove single trailing slash from path, if needed */
717 - if (pi->len > 0 && pi->match[pi->len - 1] == '/') {
718 - path_alloc = xmemdupz(pi->match, pi->len - 1);
719 - path = path_alloc;
720 - } else
721 - path = pi->match;
722 -
723 - len = strlen(path);
724 - if (!len)
739 + if (convert_pathspec_to_bloom_keyvec(&revs->bloom_keyvecs[0],
740 + &revs->pruning.pathspec.items[0],
741 + revs->bloom_filter_settings))
742 goto fail;
743
727 - revs->bloom_keyvecs[0] =
728 - bloom_keyvec_new(path, len, revs->bloom_filter_settings);
729 -
744 if (trace2_is_enabled() && !bloom_filter_atexit_registered) {
745 atexit(trace2_bloom_filter_statistics_atexit);
746 bloom_filter_atexit_registered = 1;
@@ -736,7 +750,6 @@ static void prepare_to_use_bloom_filter(struct rev_info *revs)
750
751 fail:
752 revs->bloom_filter_settings = NULL;
739 - free(path_alloc);
753 release_revisions_bloom_keyvecs(revs);
754 }
755