]> git.lizzy.rs Git - rust.git/blob - src/ci/init_repo.sh
rustdoc: Hide `self: Box<Self>` in list of deref methods
[rust.git] / src / ci / init_repo.sh
1 #!/bin/bash
2 # Copyright 2016 The Rust Project Developers. See the COPYRIGHT
3 # file at the top-level directory of this distribution and at
4 # http://rust-lang.org/COPYRIGHT.
5 #
6 # Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
7 # http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
8 # <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
9 # option. This file may not be copied, modified, or distributed
10 # except according to those terms.
11
12 set -o errexit
13 set -o pipefail
14 set -o nounset
15
16 set -o xtrace
17
18 ci_dir=$(cd $(dirname $0) && pwd)
19 . "$ci_dir/shared.sh"
20
21 REPO_DIR="$1"
22 CACHE_DIR="$2"
23
24 cache_src_dir="$CACHE_DIR/src"
25 # If the layout of the cache directory changes, bump the number here
26 # (and anywhere else this file is referenced) so the cache is wiped
27 cache_valid_file="$CACHE_DIR/cache_valid1"
28
29 if [ ! -d "$REPO_DIR" -o ! -d "$REPO_DIR/.git" ]; then
30     echo "Error: $REPO_DIR does not exist or is not a git repo"
31     exit 1
32 fi
33 cd $REPO_DIR
34 if [ ! -d "$CACHE_DIR" ]; then
35     echo "Error: $CACHE_DIR does not exist or is not an absolute path"
36     exit 1
37 fi
38
39 # Wipe the cache if it's not valid, or mark it as invalid while we update it
40 if [ ! -f "$cache_valid_file" ]; then
41     rm -rf "$CACHE_DIR"
42     mkdir "$CACHE_DIR"
43 else
44     # Ignore errors while gathering information about the possible brokenness
45     # of the git repo since our gathered info will tell us something is wrong
46     set +o errexit
47     stat_lines=$(cd "$cache_src_dir" && git status --porcelain | wc -l)
48     stat_ec=$(cd "$cache_src_dir" && git status >/dev/null 2>&1; echo $?)
49     set -o errexit
50     if [ ! -d "$cache_src_dir/.git" -o $stat_lines != 0 -o $stat_ec != 0 ]; then
51         # Something is badly wrong - the cache valid file is here, but something
52         # about the git repo is fishy. Nuke it all, just in case
53         echo "WARNING: $cache_valid_file exists but bad repo: l:$stat_lines, ec:$stat_ec"
54         rm -rf "$CACHE_DIR"
55         mkdir "$CACHE_DIR"
56     else
57         rm "$cache_valid_file"
58     fi
59 fi
60
61 # Update the cache (a pristine copy of the rust source master)
62 if [ ! -d "$cache_src_dir/.git" ]; then
63     retry sh -c "rm -rf $cache_src_dir && mkdir -p $cache_src_dir && \
64         git clone https://github.com/rust-lang/rust.git $cache_src_dir"
65 fi
66 retry sh -c "cd $cache_src_dir && git reset --hard && git pull"
67 (cd $cache_src_dir && git rm src/llvm)
68 retry sh -c "cd $cache_src_dir && \
69     git submodule deinit -f . && git submodule sync && git submodule update --init"
70
71 # Cache was updated without errors, mark it as valid
72 touch "$cache_valid_file"
73
74 # Update the submodules of the repo we're in, using the pristine repo as
75 # a cache for any object files
76 # No, `git submodule foreach` won't work:
77 # http://stackoverflow.com/questions/12641469/list-submodules-in-a-git-repository
78 modules="$(git config --file .gitmodules --get-regexp '\.path$' | cut -d' ' -f2)"
79 for module in $modules; do
80     if [ "$module" = src/llvm ]; then
81         commit="$(git ls-tree HEAD src/llvm | awk '{print $3}')"
82         git rm src/llvm
83         curl -sSL -O "https://github.com/rust-lang/llvm/archive/$commit.tar.gz"
84         tar -C src/ -xf "$commit.tar.gz"
85         rm "$commit.tar.gz"
86         mv "src/llvm-$commit" src/llvm
87         continue
88     fi
89     if [ ! -d "$cache_src_dir/$module" ]; then
90         echo "WARNING: $module not found in pristine repo"
91         retry sh -c "git submodule deinit -f $module && git submodule update --init $module"
92         continue
93     fi
94     retry sh -c "git submodule deinit -f $module && \
95         git submodule update --init --reference $cache_src_dir/$module $module"
96 done