summary refs log tree commit diff
path: root/src/ci/init_repo.sh
blob: 282da009eac35c3f3e29c88b1e4e70c7c2ae1214 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/bin/bash
# Copyright 2016 The Rust Project Developers. See the COPYRIGHT
# file at the top-level directory of this distribution and at
# http://rust-lang.org/COPYRIGHT.
#
# Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
# http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
# <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
# option. This file may not be copied, modified, or distributed
# except according to those terms.

set -o errexit
set -o pipefail
set -o nounset

ci_dir=$(cd $(dirname $0) && pwd)
. "$ci_dir/shared.sh"

travis_fold start init_repo

REPO_DIR="$1"
CACHE_DIR="$2"

cache_src_dir="$CACHE_DIR/src"
# If the layout of the cache directory changes, bump the number here
# (and anywhere else this file is referenced) so the cache is wiped
cache_valid_file="$CACHE_DIR/cache_valid1"

if [ ! -d "$REPO_DIR" -o ! -d "$REPO_DIR/.git" ]; then
    echo "Error: $REPO_DIR does not exist or is not a git repo"
    exit 1
fi
cd $REPO_DIR
if [ ! -d "$CACHE_DIR" ]; then
    echo "Error: $CACHE_DIR does not exist or is not an absolute path"
    exit 1
fi

# Wipe the cache if it's not valid, or mark it as invalid while we update it
if [ ! -f "$cache_valid_file" ]; then
    echo "Invalid cache, wiping ($cache_valid_file missing)"
    rm -rf "$CACHE_DIR"
    mkdir "$CACHE_DIR"
else
    # Ignore errors while gathering information about the possible brokenness
    # of the git repo since our gathered info will tell us something is wrong
    set +o errexit
    stat_lines=$(cd "$cache_src_dir" && git status --porcelain | wc -l)
    stat_ec=$(cd "$cache_src_dir" && git status >/dev/null 2>&1; echo $?)
    set -o errexit
    if [ ! -d "$cache_src_dir/.git" -o $stat_lines != 0 -o $stat_ec != 0 ]; then
        # Something is badly wrong - the cache valid file is here, but something
        # about the git repo is fishy. Nuke it all, just in case
        echo "WARNING: $cache_valid_file exists but bad repo: l:$stat_lines, ec:$stat_ec"
        rm -rf "$CACHE_DIR"
        mkdir "$CACHE_DIR"
    else
        echo "Valid cache ($cache_valid_file exists)"
        rm "$cache_valid_file"
    fi
fi

travis_fold start update_cache
travis_time_start

# Update the cache (a pristine copy of the rust source master)
if [ ! -d "$cache_src_dir/.git" ]; then
    retry sh -c "rm -rf $cache_src_dir && mkdir -p $cache_src_dir && \
        git clone https://github.com/rust-lang/rust.git $cache_src_dir"
fi
retry sh -c "cd $cache_src_dir && git reset --hard && git pull"
(cd $cache_src_dir && git rm src/llvm)
retry sh -c "cd $cache_src_dir && \
    git submodule deinit -f . && git submodule sync && git submodule update --init"

# Cache was updated without errors, mark it as valid
echo "Refreshed cache (touch $cache_valid_file)"
touch "$cache_valid_file"

travis_fold end update_cache
travis_time_finish

travis_fold start update_submodules
travis_time_start

# Update the submodules of the repo we're in, using the pristine repo as
# a cache for any object files
# No, `git submodule foreach` won't work:
# http://stackoverflow.com/questions/12641469/list-submodules-in-a-git-repository
modules="$(git config --file .gitmodules --get-regexp '\.path$' | cut -d' ' -f2)"
for module in $modules; do
    if [ "$module" = src/llvm ]; then
        commit="$(git ls-tree HEAD src/llvm | awk '{print $3}')"
        git rm src/llvm
        curl -sSL -O "https://github.com/rust-lang/llvm/archive/$commit.tar.gz"
        tar -C src/ -xf "$commit.tar.gz"
        rm "$commit.tar.gz"
        mv "src/llvm-$commit" src/llvm
        continue
    fi
    if [ ! -d "$cache_src_dir/$module" ]; then
        echo "WARNING: $module not found in pristine repo"
        retry sh -c "git submodule deinit -f $module && git submodule update --init $module"
        continue
    fi
    retry sh -c "git submodule deinit -f $module && \
        git submodule update --init --reference $cache_src_dir/$module $module"
done

travis_fold end update_submodules
travis_time_finish

travis_fold end init_repo