git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v2] upload-pack: fix slow fetches from a shallow repository

From
Harald Nordgren via GitGitGadget <gitgitgadget@gmail.com>
Date
Oct 20, 2026, 13:40 UTC
Message-ID
<pull.2455.v2.git.git.1792503631364.gitgitgadget@gmail.com>
In-Reply-To
<pull.2455.git.git.1792275106640.gitgitgadget@gmail.com>
From: Harald Nordgren <haraldnordgren@gmail.com>

Fetching from a shallow repository with a long history can hang. In a homebrew-core clone with 900k commits, fetching one new commit into another clone took 7 minutes. The same fetch over protocol v0 takes about a second.

Over protocol v2, the server goes through all of its history to work out which of its shallow commits to tell the client about, and a history with many merges makes it go over the same commits again and again. Protocol v0 lists all shallow commits and lets the client sort them out.

Do the same in v2 when the client does not ask for a depth. This also makes `git clone --reject-shallow` refuse a shallow repository over v2 in every case, as it already does over v0. For `git fetch --unshallow`, which still needs to go through the history, visit each commit only once.

Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>
---
    upload-pack: fix slow fetches from a shallow repository
    
    Fix git fetch hanging for minutes when the remote is a shallow
    repository with a long history.
    
    Changes in v2:
    
     * Fix fetches with sideband-all by writing the shallow lines through
       the packet writer. Add a test that clones from a shallow repository
       with uploadpack.allowSidebandAll set.
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2455%2FHaraldNordgren%2Fupload-pack-shallow-walk-v2
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2455/HaraldNordgren/upload-pack-shallow-walk-v2
Pull-Request: https://github.com/git/git/pull/2455
Range-diff vs v1:
 1:  4f4192120c ! 1:  8b0cb4c0e0 upload-pack: fix slow fetches from a shallow repository
     @@ shallow.c: static struct commit_list *get_shallows_or_depth(struct object_array
       				**depth_slot = cur_depth;
       			}
      
     + ## t/t5537-fetch-shallow.sh ##
     +@@ t/t5537-fetch-shallow.sh: test_expect_success 'clone from shallow clone' '
     + 	)
     + '
     + 
     ++test_expect_success 'clone from shallow clone with sideband-all' '
     ++	test_config -C shallow uploadpack.allowSidebandAll true &&
     ++	test_when_finished "rm -rf shallow-sideband-all" &&
     ++	git clone --no-local shallow shallow-sideband-all &&
     ++	(
     ++		cd shallow-sideband-all &&
     ++		git log --format=%s >actual &&
     ++		test_write_lines 4 3 >expect &&
     ++		test_cmp expect actual
     ++	)
     ++'
     ++
     + test_expect_success 'fetch from shallow clone' '
     + 	(
     + 	cd shallow &&
     +
       ## upload-pack.c ##
     -@@ upload-pack.c: static void send_shallow_info(struct upload_pack_data *data)
     +@@ upload-pack.c: static void send_wanted_ref_info(struct upload_pack_data *data)
     + 	packet_writer_delim(&data->writer);
     + }
       
     - 	packet_writer_write(&data->writer, "shallow-info\n");
     ++static int send_shallow_graft(const struct commit_graft *graft, void *cb_data)
     ++{
     ++	struct packet_writer *writer = cb_data;
     ++
     ++	if (graft->nr_parent == -1)
     ++		packet_writer_write(writer, "shallow %s\n",
     ++				    oid_to_hex(&graft->oid));
     ++	return 0;
     ++}
     ++
     + static void send_shallow_info(struct upload_pack_data *data)
     + {
     + 	/* No shallow info needs to be sent */
     +@@ upload-pack.c: static void send_shallow_info(struct upload_pack_data *data)
       
     --	if (!send_shallow_list(data) &&
     --	    is_repository_shallow(the_repository))
     + 	if (!send_shallow_list(data) &&
     + 	    is_repository_shallow(the_repository))
      -		deepen(data, INFINITE_DEPTH);
     -+	if (!send_shallow_list(data))
     -+		advertise_shallow_grafts(1);
     ++		for_each_commit_graft(send_shallow_graft, &data->writer);
       
       	packet_delim(1);
       }
 shallow.c                |  3 ++-
 t/t5537-fetch-shallow.sh | 12 ++++++++++++
 upload-pack.c            | 12 +++++++++++-
 3 files changed, 25 insertions(+), 2 deletions(-)
diff --git a/shallow.c b/shallow.c
index 8e244a5669..921db5e17a 100644
--- a/shallow.c
+++ b/shallow.c
@@ -209,7 +209,8 @@ static struct commit_list *get_shallows_or_depth(struct object_array *heads,
 				*depth_slot = xmalloc(sizeof(int));
 				**depth_slot = cur_depth;
 			} else {
-				if (cur_depth >= **depth_slot)
+				if (depth == INFINITE_DEPTH ||
+				    cur_depth >= **depth_slot)
 					continue;
 				**depth_slot = cur_depth;
 			}
diff --git a/t/t5537-fetch-shallow.sh b/t/t5537-fetch-shallow.sh
index f323ceebd2..140d5c2dba 100755
--- a/t/t5537-fetch-shallow.sh
+++ b/t/t5537-fetch-shallow.sh
@@ -43,6 +43,18 @@ test_expect_success 'clone from shallow clone' '
 	)
 '
 
+test_expect_success 'clone from shallow clone with sideband-all' '
+	test_config -C shallow uploadpack.allowSidebandAll true &&
+	test_when_finished "rm -rf shallow-sideband-all" &&
+	git clone --no-local shallow shallow-sideband-all &&
+	(
+		cd shallow-sideband-all &&
+		git log --format=%s >actual &&
+		test_write_lines 4 3 >expect &&
+		test_cmp expect actual
+	)
+'
+
 test_expect_success 'fetch from shallow clone' '
 	(
 	cd shallow &&
diff --git a/upload-pack.c b/upload-pack.c
index 197bcfa279..e6dc19526c 100644
--- a/upload-pack.c
+++ b/upload-pack.c
@@ -1709,6 +1709,16 @@ static void send_wanted_ref_info(struct upload_pack_data *data)
 	packet_writer_delim(&data->writer);
 }
 
+static int send_shallow_graft(const struct commit_graft *graft, void *cb_data)
+{
+	struct packet_writer *writer = cb_data;
+
+	if (graft->nr_parent == -1)
+		packet_writer_write(writer, "shallow %s\n",
+				    oid_to_hex(&graft->oid));
+	return 0;
+}
+
 static void send_shallow_info(struct upload_pack_data *data)
 {
 	/* No shallow info needs to be sent */
@@ -1720,7 +1730,7 @@ static void send_shallow_info(struct upload_pack_data *data)
 
 	if (!send_shallow_list(data) &&
 	    is_repository_shallow(the_repository))
-		deepen(data, INFINITE_DEPTH);
+		for_each_commit_graft(send_shallow_graft, &data->writer);
 
 	packet_delim(1);
 }

base-commit: c51408889c3b372814bc20b9431c3b74d0d268eb
-- 
gitgitgadget
Previous: Harald NordgrenNext: Harald Nordgren
Message 4 of 5 in “upload-pack: fix slow fetches from a shallow repository”
  1. upload-pack: fix slow fetches from a shallow repositoryHarald Nordgren via GitGitGadget, Oct 17, 2026
  2. Junio C HamanoOct 19, 2026
  3. Harald NordgrenOct 19, 2026
  4. upload-pack: fix slow fetches from a shallow repositoryHarald Nordgren via GitGitGadget, Oct 20, 2026
  5. Harald NordgrenNov 11, 2026

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.