shallow: fix relative deepen on non-shallow repositories

The commit "3ef68ff40e (shallow: handling fetch relative-deepen, 2026-02-15)" introduced a bug where using --deepen=<n> on a non- shallow repository incorrectly treated the value as an absolute depth, resulting in a shallow fetch and truncated history. This patch prevents any modification when a relative deepen is requested on a non-shallow repository. A test is added to ensure that history is not changed when --deepen is used on a non-shallow repository. Reported-by: Owen Stephens <owen@owenstephens.co.uk> Signed-off-by: Samo Pogačnik <samo_pogacnik@t-2.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Samo Pogačnik committed May 11, 2026 at 21:20 UTC 2431f5e0e5cd415a3ab1bddb435b692536cc95e8
2 files changed +15 -1
shallow.c
+5 -1
@@ -245,7 +245,11 @@ struct commit_list *get_shallow_commits(struct object_array *heads,
245 int depth, int shallow_flag, int not_shallow_flag)
246 {
247 if (shallows && deepen_relative) {
248 - depth += get_shallows_depth(heads, shallows);
248 + int cur_shallow_depth = get_shallows_depth(heads, shallows);
249 + if (cur_shallow_depth)
250 + depth += cur_shallow_depth;
251 + else
252 + return NULL;
253 }
254 return get_shallows_or_depth(heads, NULL, NULL,
255 depth, shallow_flag, not_shallow_flag);
t/t5537-fetch-shallow.sh
+10
@@ -251,6 +251,16 @@ test_expect_success '.git/shallow is edited by repack' '
251 origin "+refs/heads/*:refs/remotes/origin/*"
252 '
253
254 +test_expect_success 'fetch --deepen does not truncate' '
255 + git clone --no-local .git full-clone &&
256 + git -C full-clone rev-parse --is-shallow-repository >expect &&
257 + git -C full-clone log --oneline >>expect &&
258 + git -C full-clone fetch --deepen=1 &&
259 + git -C full-clone rev-parse --is-shallow-repository >actual &&
260 + git -C full-clone log --oneline >>actual &&
261 + test_cmp expect actual
262 +'
263 +
264 . "$TEST_DIRECTORY"/lib-httpd.sh
265 start_httpd
266