From 035ce56a3353b00d34dbbb338580ff25fb7ef767 Mon Sep 17 00:00:00 2001
From: Glenn Washburn <development@efficientek.com>
Date: Wed, 5 Jan 2022 23:57:25 +0100
Subject: [PATCH] bootstrap: When a commit is specified, do a shallow fetch if
 possible.

Rationale and explanation:
<https://lists.gnu.org/archive/html/bug-gnulib/2021-10/msg00073.html>.

* bootstrap: When a commit hash is specified, ask for this specific
commit on fetch, and fallback to fetching the entire repository if
fetching by commit hash fails.
---
 ChangeLog           |  9 +++++++++
 build-aux/bootstrap | 20 ++++++++++++++++++--
 2 files changed, 27 insertions(+), 2 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 4829d58e83..1208d4c6d5 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+2022-01-05  Glenn Washburn  <development@efficientek.com>
+
+	bootstrap: When a commit is specified, do a shallow fetch if possible.
+	Rationale and explanation:
+	<https://lists.gnu.org/archive/html/bug-gnulib/2021-10/msg00073.html>.
+	* bootstrap: When a commit hash is specified, ask for this specific
+	commit on fetch, and fallback to fetching the entire repository if
+	fetching by commit hash fails.
+
 2022-01-05  Paul Eggert  <eggert@cs.ucla.edu>
 
 	stack: pacify gcc -Wsign-compare
diff --git a/build-aux/bootstrap b/build-aux/bootstrap
index 96588618b7..9535aecce2 100755
--- a/build-aux/bootstrap
+++ b/build-aux/bootstrap
@@ -763,9 +763,25 @@ if $use_gnulib; then
       shallow=
       if test -z "$GNULIB_REVISION"; then
         git clone -h 2>&1 | grep -- --depth > /dev/null && shallow='--depth 2'
+        git clone $shallow ${GNULIB_URL:-$default_gnulib_url} "$gnulib_path" \
+          || cleanup_gnulib
+      else
+        git fetch -h 2>&1 | grep -- --depth > /dev/null && shallow='--depth 2'
+        mkdir -p "$gnulib_path"
+        # Only want a shallow checkout of $GNULIB_REVISION, but git does not
+        # support cloning by commit hash. So attempt a shallow fetch by commit
+        # hash to minimize the amount of data downloaded and changes needed to
+        # be processed, which can drastically reduce download and processing
+        # time for checkout. If the fetch by commit fails, a shallow fetch can
+        # not be performed because we do not know what the depth of the commit
+        # is without fetching all commits. So fallback to fetching all commits.
+        git -C "$gnulib_path" init
+        git -C "$gnulib_path" remote add origin ${GNULIB_URL:-$default_gnulib_url}
+        git -C "$gnulib_path" fetch $shallow origin "$GNULIB_REVISION" \
+          || git -C "$gnulib_path" fetch origin \
+          || cleanup_gnulib
+        git -C "$gnulib_path" reset --hard FETCH_HEAD
       fi
-      git clone $shallow ${GNULIB_URL:-$default_gnulib_url} "$gnulib_path" \
-        || cleanup_gnulib
 
       trap - 1 2 13 15
     fi
-- 
2.39.5