shallow: use commit_stack
Replace a commit array implementation with commit_stack. Signed-off-by: René Scharfe <l.s.r@web.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Rene Scharfe committed
Dec 24, 2025 at 18:03 UTC
506a7b66908eb5c3898a3eadbd402308f5b43cf8
2 files changed
+19
-29
shallow.c
+17
-27
@@ -471,6 +471,7 @@ void prepare_shallow_info(struct shallow_info *info, struct oid_array *sa)
471
{
472
trace_printf_key(&trace_shallow, "shallow: prepare_shallow_info\n");
473
memset(info, 0, sizeof(*info));
474
+ commit_stack_init(&info->commits);
475
info->shallow = sa;
476
if (!sa)
477
return;
@@ -503,6 +504,7 @@ void clear_shallow_info(struct shallow_info *info)
504
free(info->shallow_ref);
505
free(info->ours);
506
free(info->theirs);
507
+ commit_stack_clear(&info->commits);
508
}
509
510
/* Step 4, remove non-existent ones in "theirs" after getting the pack */
@@ -733,19 +735,13 @@ void assign_shallow_commits_to_refs(struct shallow_info *info,
735
free(shallow);
736
}
737
736
-struct commit_array {
737
- struct commit **commits;
738
- size_t nr, alloc;
739
-};
740
-
738
static int add_ref(const struct reference *ref, void *cb_data)
739
{
743
- struct commit_array *ca = cb_data;
744
- ALLOC_GROW(ca->commits, ca->nr + 1, ca->alloc);
745
- ca->commits[ca->nr] = lookup_commit_reference_gently(the_repository,
746
- ref->oid, 1);
747
- if (ca->commits[ca->nr])
748
- ca->nr++;
740
+ struct commit_stack *cs = cb_data;
741
+ struct commit *commit = lookup_commit_reference_gently(the_repository,
742
+ ref->oid, 1);
743
+ if (commit)
744
+ commit_stack_push(cs, commit);
745
return 0;
746
}
747
@@ -770,7 +766,7 @@ static void post_assign_shallow(struct shallow_info *info,
766
uint32_t **bitmap;
767
size_t dst, i, j;
768
size_t bitmap_nr = DIV_ROUND_UP(info->ref->nr, 32);
773
- struct commit_array ca;
769
+ struct commit_stack cs = COMMIT_STACK_INIT;
770
771
trace_printf_key(&trace_shallow, "shallow: post_assign_shallow\n");
772
if (ref_status)
@@ -793,9 +789,8 @@ static void post_assign_shallow(struct shallow_info *info,
789
}
790
info->nr_theirs = dst;
791
796
- memset(&ca, 0, sizeof(ca));
797
- refs_head_ref(get_main_ref_store(the_repository), add_ref, &ca);
798
- refs_for_each_ref(get_main_ref_store(the_repository), add_ref, &ca);
792
+ refs_head_ref(get_main_ref_store(the_repository), add_ref, &cs);
793
+ refs_for_each_ref(get_main_ref_store(the_repository), add_ref, &cs);
794
795
/* Remove unreachable shallow commits from "ours" */
796
for (i = dst = 0; i < info->nr_ours; i++) {
@@ -808,7 +803,7 @@ static void post_assign_shallow(struct shallow_info *info,
803
for (j = 0; j < bitmap_nr; j++)
804
if (bitmap[0][j]) {
805
/* Step 7, reachability test at commit level */
811
- int ret = repo_in_merge_bases_many(the_repository, c, ca.nr, ca.commits, 1);
806
+ int ret = repo_in_merge_bases_many(the_repository, c, cs.nr, cs.items, 1);
807
if (ret < 0)
808
exit(128);
809
if (!ret) {
@@ -820,7 +815,7 @@ static void post_assign_shallow(struct shallow_info *info,
815
}
816
info->nr_ours = dst;
817
823
- free(ca.commits);
818
+ commit_stack_clear(&cs);
819
}
820
821
/* (Delayed) step 7, reachability test at commit level */
@@ -830,22 +825,17 @@ int delayed_reachability_test(struct shallow_info *si, int c)
825
struct commit *commit = lookup_commit(the_repository,
826
&si->shallow->oid[c]);
827
833
- if (!si->commits) {
834
- struct commit_array ca;
835
-
836
- memset(&ca, 0, sizeof(ca));
828
+ if (!si->commits.nr) {
829
refs_head_ref(get_main_ref_store(the_repository),
838
- add_ref, &ca);
830
+ add_ref, &si->commits);
831
refs_for_each_ref(get_main_ref_store(the_repository),
840
- add_ref, &ca);
841
- si->commits = ca.commits;
842
- si->nr_commits = ca.nr;
832
+ add_ref, &si->commits);
833
}
834
835
si->reachable[c] = repo_in_merge_bases_many(the_repository,
836
commit,
847
- si->nr_commits,
848
- si->commits,
837
+ si->commits.nr,
838
+ si->commits.items,
839
1);
840
if (si->reachable[c] < 0)
841
exit(128);
shallow.h
+2
-2
@@ -1,6 +1,7 @@
1
#ifndef SHALLOW_H
2
#define SHALLOW_H
3
4
+#include "commit.h"
5
#include "lockfile.h"
6
#include "object.h"
7
#include "repository.h"
@@ -69,8 +70,7 @@ struct shallow_info {
70
int *need_reachability_test;
71
int *reachable;
72
int *shallow_ref;
72
- struct commit **commits;
73
- size_t nr_commits;
73
+ struct commit_stack commits;
74
};
75
76
void prepare_shallow_info(struct shallow_info *, struct oid_array *);