]> git.lizzy.rs Git - rust.git/blob - src/ci/scripts/checkout-submodules.sh
Rollup merge of #100040 - ChrisDenton:broken-pipe, r=davidtwco
[rust.git] / src / ci / scripts / checkout-submodules.sh
1 #!/bin/bash
2 # Check out all our submodules, but more quickly than using git by using one of
3 # our custom scripts
4
5 set -o errexit
6 set -o pipefail
7 set -o nounset
8
9 if [ ! -d ".git" ]; then
10     echo "Error: This must run in the root of the repository"
11     exit 1
12 fi
13
14 ci_dir=$(cd $(dirname $0) && pwd)/..
15 . "$ci_dir/shared.sh"
16
17 # On the beta channel we'll be automatically calculating the prerelease version
18 # via the git history, so unshallow our shallow clone from CI.
19 if [ "$(releaseChannel)" = "beta" ]; then
20   git fetch origin --unshallow beta master
21 fi
22
23 function fetch_github_commit_archive {
24     local module=$1
25     local cached="download-${module//\//-}.tar.gz"
26     retry sh -c "rm -f $cached && \
27         curl -f -sSL -o $cached $2"
28     mkdir $module
29     touch "$module/.git"
30     # On Windows, the default behavior is to emulate symlinks by copying
31     # files. However, that ends up being order-dependent while extracting,
32     # which can cause a failure if the symlink comes first. This env var
33     # causes tar to use real symlinks instead, which are allowed to dangle.
34     export MSYS=winsymlinks:nativestrict
35     tar -C $module --strip-components=1 -xf $cached
36     rm $cached
37 }
38
39 included="src/llvm-project src/doc/book src/doc/rust-by-example"
40 modules="$(git config --file .gitmodules --get-regexp '\.path$' | cut -d' ' -f2)"
41 modules=($modules)
42 use_git=""
43 urls="$(git config --file .gitmodules --get-regexp '\.url$' | cut -d' ' -f2)"
44 urls=($urls)
45 # shellcheck disable=SC2068
46 for i in ${!modules[@]}; do
47     module=${modules[$i]}
48     if [[ " $included " = *" $module "* ]]; then
49         commit="$(git ls-tree HEAD $module | awk '{print $3}')"
50         git rm $module
51         url=${urls[$i]}
52         url=${url/\.git/}
53         fetch_github_commit_archive $module "$url/archive/$commit.tar.gz" &
54         bg_pids[${i}]=$!
55         continue
56     else
57         use_git="$use_git $module"
58     fi
59 done
60 retry sh -c "git submodule deinit -f $use_git && \
61     git submodule sync && \
62     git submodule update -j 16 --init --recursive --depth 1 $use_git"
63 STATUS=0
64 for pid in ${bg_pids[*]}
65 do
66     wait $pid || STATUS=1
67 done
68 exit ${STATUS}