shallow: handling fetch relative-deepen

When a shallowed repository gets deepened beyond the beginning of a merged branch, we may end up with some shallows that are hidden behind the reachable shallow commits. Added test 'fetching deepen beyond merged branch' exposes that behaviour. An example showing the problem based on added test: 0. Whole initial git repo to be cloned from Graph: * 033585d (HEAD -> main) Merge branch 'branch' |\ | * 984f8b1 (branch) five | * ecb578a four |/ * 0cb5d20 three * 2b4e70d two * 61ba98b one 1. Initial shallow clone --depth=3 (all good) Shallows: 2b4e70da2a10e1d3231a0ae2df396024735601f1 ecb578a3cf37198d122ae5df7efed9abaca17144 Graph: * 033585d (HEAD -> main) Merge branch 'branch' |\ | * 984f8b1 five | * ecb578a (grafted) four * 0cb5d20 three * 2b4e70d (grafted) two 2. Deepen shallow clone with fetch --deepen=1 (NOT OK) Shallows: 0cb5d204f4ef96ed241feb0f2088c9f4794ba758 61ba98be443fd51c542eb66585a1f6d7e15fcdae Graph: * 033585d (HEAD -> main) Merge branch 'branch' |\ | * 984f8b1 five | * ecb578a four |/ * 0cb5d20 (grafted) three --- Note that second shallow commit 61ba98be443fd51c542eb66585a1f6d7e15fcdae is not reachable. On the other hand, it seems that equivalent absolute depth driven fetches result in all the correct shallows. That led to this proposal, which unifies absolute and relative deepening in a way that the same get_shallow_commits() call is used in both cases. The difference is only that depth is adapted for relative deepening by measuring equivalent depth of current local shallow commits in the current remote repo. Thus a new function get_shallows_depth() has been added and the function get_reachable_list() became redundant / removed. Same example showing the corrected second step: 2. Deepen shallow clone with fetch --deepen=1 (all good) Shallow: 61ba98be443fd51c542eb66585a1f6d7e15fcdae Graph: * 033585d (HEAD -> main) Merge branch 'branch' |\ | * 984f8b1 five | * ecb578a four |/ * 0cb5d20 three * 2b4e70d two * 61ba98b (grafted) one The get_shallows_depth() function also shares the logic of the get_shallow_commits() function, but it focuses on counting depth of each existing shallow commit. The minimum result is stored as 'data->deepen_relative', which is set not to be zero for relative deepening anyway. That way we can always sum 'data->deepen_relative' and 'depth' values, because 'data->deepen_relative' is always 0 in absolute deepening. To avoid duplicating logic between get_shallows_depth() and get_shallow_commits(), get_shallow_commits() was modified so that it is used by get_shallows_depth(). Signed-off-by: Samo Pogačnik <samo_pogacnik@t-2.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Samo Pogačnik committed Feb 15, 2026 at 20:11 UTC 3ef68ff40ebf75bba9c4b05f50197190ff1abda2
4 files changed +87 -82
shallow.c
+60 -12
@@ -130,11 +130,24 @@ static void free_depth_in_slab(int **ptr)
130 {
131 FREE_AND_NULL(*ptr);
132 }
133 -struct commit_list *get_shallow_commits(struct object_array *heads, int depth,
134 - int shallow_flag, int not_shallow_flag)
133 +/*
134 + * This is a common internal function that can either return a list of
135 + * shallow commits or calculate the current maximum depth of a shallow
136 + * repository, depending on the input parameters.
137 + *
138 + * Depth calculation is triggered by passing the `shallows` parameter.
139 + * In this case, the computed depth is stored in `max_cur_depth` (if it is
140 + * provided), and the function returns NULL.
141 + *
142 + * Otherwise, `max_cur_depth` remains unchanged and the function returns
143 + * a list of shallow commits.
144 + */
145 +static struct commit_list *get_shallows_or_depth(struct object_array *heads,
146 + struct object_array *shallows, int *max_cur_depth,
147 + int depth, int shallow_flag, int not_shallow_flag)
148 {
149 size_t i = 0;
137 - int cur_depth = 0;
150 + int cur_depth = 0, cur_depth_shallow = 0;
151 struct commit_list *result = NULL;
152 struct object_array stack = OBJECT_ARRAY_INIT;
153 struct commit *commit = NULL;
@@ -168,16 +181,30 @@ struct commit_list *get_shallow_commits(struct object_array *heads, int depth,
181 }
182 parse_commit_or_die(commit);
183 cur_depth++;
171 - if ((depth != INFINITE_DEPTH && cur_depth >= depth) ||
172 - (is_repository_shallow(the_repository) && !commit->parents &&
173 - (graft = lookup_commit_graft(the_repository, &commit->object.oid)) != NULL &&
174 - graft->nr_parent < 0)) {
175 - commit_list_insert(commit, &result);
176 - commit->object.flags |= shallow_flag;
177 - commit = NULL;
178 - continue;
184 + if (shallows) {
185 + for (size_t j = 0; j < shallows->nr; j++)
186 + if (oideq(&commit->object.oid, &shallows->objects[j].item->oid))
187 + if (!cur_depth_shallow || cur_depth < cur_depth_shallow)
188 + cur_depth_shallow = cur_depth;
189 +
190 + if ((is_repository_shallow(the_repository) && !commit->parents &&
191 + (graft = lookup_commit_graft(the_repository, &commit->object.oid)) != NULL &&
192 + graft->nr_parent < 0)) {
193 + commit = NULL;
194 + continue;
195 + }
196 + } else {
197 + if ((depth != INFINITE_DEPTH && cur_depth >= depth) ||
198 + (is_repository_shallow(the_repository) && !commit->parents &&
199 + (graft = lookup_commit_graft(the_repository, &commit->object.oid)) != NULL &&
200 + graft->nr_parent < 0)) {
201 + commit_list_insert(commit, &result);
202 + commit->object.flags |= shallow_flag;
203 + commit = NULL;
204 + continue;
205 + }
206 + commit->object.flags |= not_shallow_flag;
207 }
180 - commit->object.flags |= not_shallow_flag;
208 for (p = commit->parents, commit = NULL; p; p = p->next) {
209 int **depth_slot = commit_depth_at(&depths, p->item);
210 if (!*depth_slot) {
@@ -200,9 +227,30 @@ struct commit_list *get_shallow_commits(struct object_array *heads, int depth,
227 deep_clear_commit_depth(&depths, free_depth_in_slab);
228 object_array_clear(&stack);
229
230 + if (shallows && max_cur_depth)
231 + *max_cur_depth = cur_depth_shallow;
232 return result;
233 }
234
235 +int get_shallows_depth(struct object_array *heads, struct object_array *shallows)
236 +{
237 + int max_cur_depth = 0;
238 + get_shallows_or_depth(heads, shallows, &max_cur_depth, 0, 0, 0);
239 + return max_cur_depth;
240 +
241 +}
242 +
243 +struct commit_list *get_shallow_commits(struct object_array *heads,
244 + struct object_array *shallows, int deepen_relative,
245 + int depth, int shallow_flag, int not_shallow_flag)
246 +{
247 + if (shallows && deepen_relative) {
248 + depth += get_shallows_depth(heads, shallows);
249 + }
250 + return get_shallows_or_depth(heads, NULL, NULL,
251 + depth, shallow_flag, not_shallow_flag);
252 +}
253 +
254 static void show_commit(struct commit *commit, void *data)
255 {
256 commit_list_insert(commit, data);
shallow.h
+2
@@ -35,7 +35,9 @@ int commit_shallow_file(struct repository *r, struct shallow_lock *lk);
35 /* rollback $GIT_DIR/shallow and reset stat-validity checks */
36 void rollback_shallow_file(struct repository *r, struct shallow_lock *lk);
37
38 +int get_shallows_depth(struct object_array *heads, struct object_array *shallows);
39 struct commit_list *get_shallow_commits(struct object_array *heads,
40 + struct object_array *shallows, int deepen_relative,
41 int depth, int shallow_flag, int not_shallow_flag);
42 struct commit_list *get_shallow_commits_by_rev_list(struct strvec *argv,
43 int shallow_flag, int not_shallow_flag);
t/t5500-fetch-pack.sh
+23
@@ -955,6 +955,29 @@ test_expect_success 'fetching deepen' '
955 )
956 '
957
958 +test_expect_success 'fetching deepen beyond merged branch' '
959 + test_create_repo shallow-deepen-merged &&
960 + (
961 + cd shallow-deepen-merged &&
962 + git commit --allow-empty -m one &&
963 + git commit --allow-empty -m two &&
964 + git commit --allow-empty -m three &&
965 + git switch -c branch &&
966 + git commit --allow-empty -m four &&
967 + git commit --allow-empty -m five &&
968 + git switch main &&
969 + git merge --no-ff branch &&
970 + cd - &&
971 + git clone --bare --depth 3 "file://$(pwd)/shallow-deepen-merged" deepen.git &&
972 + git -C deepen.git fetch origin --deepen=1 &&
973 + git -C deepen.git rev-list --all >actual &&
974 + for commit in $(sed "/^$/d" deepen.git/shallow)
975 + do
976 + test_grep "$commit" actual || exit 1
977 + done
978 + )
979 +'
980 +
981 test_negotiation_algorithm_default () {
982 test_when_finished rm -rf clientv0 clientv2 &&
983 rm -rf server client &&
upload-pack.c
+2 -70
@@ -704,56 +704,6 @@ error:
704 return -1;
705 }
706
707 -static int get_reachable_list(struct upload_pack_data *data,
708 - struct object_array *reachable)
709 -{
710 - struct child_process cmd = CHILD_PROCESS_INIT;
711 - int i;
712 - struct object *o;
713 - char namebuf[GIT_MAX_HEXSZ + 2]; /* ^ + hash + LF */
714 - const unsigned hexsz = the_hash_algo->hexsz;
715 - int ret;
716 -
717 - if (do_reachable_revlist(&cmd, &data->shallows, reachable,
718 - data->allow_uor) < 0) {
719 - ret = -1;
720 - goto out;
721 - }
722 -
723 - while ((i = read_in_full(cmd.out, namebuf, hexsz + 1)) == hexsz + 1) {
724 - struct object_id oid;
725 - const char *p;
726 -
727 - if (parse_oid_hex(namebuf, &oid, &p) || *p != '\n')
728 - break;
729 -
730 - o = lookup_object(the_repository, &oid);
731 - if (o && o->type == OBJ_COMMIT) {
732 - o->flags &= ~TMP_MARK;
733 - }
734 - }
735 - for (i = get_max_object_index(the_repository); 0 < i; i--) {
736 - o = get_indexed_object(the_repository, i - 1);
737 - if (o && o->type == OBJ_COMMIT &&
738 - (o->flags & TMP_MARK)) {
739 - add_object_array(o, NULL, reachable);
740 - o->flags &= ~TMP_MARK;
741 - }
742 - }
743 - close(cmd.out);
744 -
745 - if (finish_command(&cmd)) {
746 - ret = -1;
747 - goto out;
748 - }
749 -
750 - ret = 0;
751 -
752 -out:
753 - child_process_clear(&cmd);
754 - return ret;
755 -}
756 -
707 static int has_unreachable(struct object_array *src, enum allow_uor allow_uor)
708 {
709 struct child_process cmd = CHILD_PROCESS_INIT;
@@ -881,29 +831,11 @@ static void deepen(struct upload_pack_data *data, int depth)
831 struct object *object = data->shallows.objects[i].item;
832 object->flags |= NOT_SHALLOW;
833 }
884 - } else if (data->deepen_relative) {
885 - struct object_array reachable_shallows = OBJECT_ARRAY_INIT;
886 - struct commit_list *result;
887 -
888 - /*
889 - * Checking for reachable shallows requires that our refs be
890 - * marked with OUR_REF.
891 - */
892 - refs_head_ref_namespaced(get_main_ref_store(the_repository),
893 - check_ref, data);
894 - for_each_namespaced_ref_1(check_ref, data);
895 -
896 - get_reachable_list(data, &reachable_shallows);
897 - result = get_shallow_commits(&reachable_shallows,
898 - depth + 1,
899 - SHALLOW, NOT_SHALLOW);
900 - send_shallow(data, result);
901 - free_commit_list(result);
902 - object_array_clear(&reachable_shallows);
834 } else {
835 struct commit_list *result;
836
906 - result = get_shallow_commits(&data->want_obj, depth,
837 + result = get_shallow_commits(&data->want_obj, &data->shallows,
838 + data->deepen_relative, depth,
839 SHALLOW, NOT_SHALLOW);
840 send_shallow(data, result);
841 free_commit_list(result);