+ # GNULIB_REFDIR is not set or not usable. Ignore it.
+ shallow='--depth 2'
+ if test -z "$GNULIB_REVISION"; then
+ git clone $shallow "$gnulib_url" "$gnulib_path" \
+ || cleanup_gnulib
+ else
+ # 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 cannot be performed because we do not know what the
+ # depth of the commit is without fetching all commits. So fall back
+ # to fetching all commits.
+ # $GNULIB_REVISION can be a commit id, a tag name, or a branch name.
+ mkdir -p "$gnulib_path"
+ # Use a -c option to silence an annoying message
+ # "hint: Using 'master' as the name for the initial branch."
+ # (cf. <https://stackoverflow.com/questions/65524512/>).
+ git -C "$gnulib_path" -c init.defaultBranch=master init
+ git -C "$gnulib_path" remote add origin "$gnulib_url"
+ if git -C "$gnulib_path" fetch $shallow origin "$GNULIB_REVISION"
+ then
+ # "git fetch" of the specific commit succeeded.
+ git -C "$gnulib_path" reset --hard FETCH_HEAD \
+ || cleanup_gnulib
+ # "git fetch" does not fetch tags (at least in git version 2.43).
+ # If $GNULIB_REVISION is a tag (not a commit id or branch name),
+ # add the tag explicitly.
+ revision=`git -C "$gnulib_path" log -1 --pretty=format:%H`
+ branch=`LC_ALL=C git -C "$gnulib_path" remote show origin \
+ | sed -n -e 's/^ \([^ ]*\) * tracked$/\1/p'`
+ test "$revision" = "$GNULIB_REVISION" \
+ || test "$branch" = "$GNULIB_REVISION" \
+ || git -C "$gnulib_path" tag "$GNULIB_REVISION"
+ else
+ # Fetch the entire repository.
+ git -C "$gnulib_path" fetch origin \
+ || cleanup_gnulib
+ git -C "$gnulib_path" checkout "$GNULIB_REVISION" \
+ || cleanup_gnulib
+ fi