-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpull
More file actions
executable file
·190 lines (166 loc) · 4.01 KB
/
Copy pathpull
File metadata and controls
executable file
·190 lines (166 loc) · 4.01 KB
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#!/bin/sh
set -o errexit
set -o nounset
ROOT="$(git rev-parse --show-toplevel)"
DEPENDENCIES="$ROOT/DEPENDENCIES"
VENDOR="$ROOT/vendor"
PATCHES="$ROOT/patches"
fail() {
echo "$1" 1>&2
exit 1
}
log() {
echo "-- $1" 1>&2
}
# $1 = version
is_file_dependency() {
echo "$1" | grep -Eq '^[a-f0-9]{64}$'
}
# $1 = path
checksum() {
if command -v sha256sum > /dev/null
then
sha256sum "$1" | cut -d ' ' -f 1
elif command -v shasum > /dev/null
then
shasum --algorithm 256 "$1" | cut -d ' ' -f 1
elif command -v openssl > /dev/null
then
openssl dgst -sha256 "$1" | awk '{ print $NF }'
else
fail "Cannot find sha256sum, shasum, or openssl to compute checksums"
fi
}
# $1 = name
# $2 = url
# $3 = version
# $4 = tmp
vendor() {
# Cloning
log "Fetching $2@$3 into $4/$1"
git clone --recurse-submodules --jobs 8 "$2" "$4/$1"
git -C "$4/$1" reset --hard "$3"
# Patching
if [ -d "$PATCHES/$1" ]
then
for patch in "$PATCHES/$1"/*.patch
do
log "Patching $1: $patch"
git -C "$4/$1" apply --3way "$patch"
done
fi
# Pruning
log "Pruning $4/$1"
git -C "$4/$1" submodule foreach "rm -rf .git"
git -C "$4/$1" submodule foreach "rm -rf .gitignore"
git -C "$4/$1" submodule foreach "rm -rf .github"
git -C "$4/$1" submodule foreach "rm -rf .gitmodules"
git -C "$4/$1" submodule foreach "rm -rf .gitattributes"
rm -rf "$4/$1/.git"
rm -rf "$4/$1/.gitignore"
rm -rf "$4/$1/.github"
rm -rf "$4/$1/.gitmodules"
rm -rf "$4/$1/.gitattributes"
OUTPUT="$VENDOR/$1"
# Masking
if [ -f "$OUTPUT.mask" ]
then
while read -r path
do
log "Masking $1: $path"
rm -rf "$4/$1/${path:?}"
done < "$OUTPUT.mask"
elif [ -f "$4/$1/vendorpull.mask" ]
then
while read -r path
do
log "Masking $1: $path"
rm -rf "$4/$1/${path:?}"
done < "$4/$1/vendorpull.mask"
rm -f "$4/$1/vendorpull.mask"
fi
# Swap
log "Moving $4/$1 to $OUTPUT"
rm -rf "$OUTPUT"
mkdir -p "$(dirname "$OUTPUT")"
mv "$4/$1" "$OUTPUT"
}
# $1 = name
# $2 = url
# $3 = checksum
# $4 = tmp
vendor_file() {
# Downloading
log "Downloading $2 into $4/$1"
mkdir -p "$(dirname "$4/$1")"
curl --fail --silent --show-error --location --output "$4/$1" "$2"
# Verification
ACTUAL_CHECKSUM="$(checksum "$4/$1")"
if [ "$ACTUAL_CHECKSUM" != "$3" ]
then
fail "Checksum mismatch for $1: expected $3 but got $ACTUAL_CHECKSUM"
fi
OUTPUT="$VENDOR/$1"
# Swap
log "Moving $4/$1 to $OUTPUT"
rm -rf "$OUTPUT"
mkdir -p "$(dirname "$OUTPUT")"
mv "$4/$1" "$OUTPUT"
}
if [ ! -f "$DEPENDENCIES" ]
then
fail "File not found: $DEPENDENCIES"
fi
DEPENDENCY="${1-}"
if [ -n "$DEPENDENCY" ]
then
LINE="$(grep "^$DEPENDENCY " < "$DEPENDENCIES" || true)"
if [ -z "$LINE" ]
then
fail "Unknown dependency: $DEPENDENCY"
fi
NAME="$(echo "$LINE" | cut -d ' ' -f 1)"
PULL_LOCATION="${2-}"
if [ -n "$PULL_LOCATION" ]
then
URL="$PULL_LOCATION"
else
URL="$(echo "$LINE" | cut -d ' ' -f 2)"
fi
VERSION="$(echo "$LINE" | cut -d ' ' -f 3)"
if [ -z "$NAME" ] || [ -z "$URL" ] || [ -z "$VERSION" ]
then
fail "Invalid dependency definition: $DEPENDENCY"
fi
TMP="$(mktemp -d -t vendorpull-clone-XXXXXX)"
log "Setting up temporary directory at $TMP..."
clean() { rm -rf "$TMP"; }
trap clean EXIT
if is_file_dependency "$VERSION"
then
vendor_file "$NAME" "$URL" "$VERSION" "$TMP"
else
vendor "$NAME" "$URL" "$VERSION" "$TMP"
fi
else
TMP="$(mktemp -d -t vendorpull-clone-XXXXXX)"
log "Setting up temporary directory at $TMP..."
clean() { rm -rf "$TMP"; }
trap clean EXIT
while read -r line
do
NAME="$(echo "$line" | cut -d ' ' -f 1)"
URL="$(echo "$line" | cut -d ' ' -f 2)"
VERSION="$(echo "$line" | cut -d ' ' -f 3)"
if [ -z "$NAME" ] || [ -z "$URL" ] || [ -z "$VERSION" ]
then
fail "Invalid dependency definition"
fi
if is_file_dependency "$VERSION"
then
vendor_file "$NAME" "$URL" "$VERSION" "$TMP"
else
vendor "$NAME" "$URL" "$VERSION" "$TMP"
fi
done < "$DEPENDENCIES"
fi