Skip to content

Commit 442d46c

Browse files
committed
vendor: capture crate provenance in inhouse metadata
1 parent 93e44d6 commit 442d46c

1 file changed

Lines changed: 104 additions & 0 deletions

File tree

scripts/vendor/inhouse-crate.sh

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,18 @@ cd "$repo_root"
3737
vendor_root="lib/vendor"
3838
history_root="lib/vendor-history"
3939
manifest_root="lib/vendor-manifest"
40+
registry_index="https://github.com/rust-lang/crates.io-index"
4041

4142
find_cached_src_dir() {
4243
find "$HOME/.cargo/registry/src" -maxdepth 2 -type d -name "${crate}-${version}" 2>/dev/null \
4344
| head -n 1
4445
}
4546

47+
find_cached_crate_file() {
48+
find "$HOME/.cargo/registry/cache" -maxdepth 2 -type f -name "${crate}-${version}.crate" 2>/dev/null \
49+
| head -n 1
50+
}
51+
4652
fetch_into_cache() {
4753
local fetch_tmp
4854
fetch_tmp="$(mktemp -d)"
@@ -107,6 +113,69 @@ resolve_version_from_lock() {
107113
' Cargo.lock | sort -V | tail -n 1
108114
}
109115

116+
resolve_checksum_from_lock() {
117+
awk -v crate="$crate" -v version="$version" '
118+
BEGIN { name = ""; ver = ""; checksum = ""; found = 0 }
119+
$0 == "[[package]]" {
120+
if (name == crate && ver == version && checksum != "") {
121+
print checksum
122+
found = 1
123+
exit 0
124+
}
125+
name = ""
126+
ver = ""
127+
checksum = ""
128+
next
129+
}
130+
/^name = "/ {
131+
name = $3
132+
gsub(/"/, "", name)
133+
next
134+
}
135+
/^version = "/ {
136+
ver = $3
137+
gsub(/"/, "", ver)
138+
next
139+
}
140+
/^checksum = "/ {
141+
checksum = $3
142+
gsub(/"/, "", checksum)
143+
next
144+
}
145+
END {
146+
if (found == 0 && name == crate && ver == version && checksum != "") {
147+
print checksum
148+
}
149+
}
150+
' Cargo.lock
151+
}
152+
153+
resolve_checksum_from_crates_io() {
154+
if ! command -v curl >/dev/null 2>&1 || ! command -v jq >/dev/null 2>&1; then
155+
return 0
156+
fi
157+
curl -fsSL "https://crates.io/api/v1/crates/${crate}/${version}" 2>/dev/null \
158+
| jq -r '.version.checksum // empty' 2>/dev/null \
159+
|| true
160+
}
161+
162+
extract_toml_string() {
163+
local file="$1"
164+
local key="$2"
165+
awk -F'"' -v key="$key" '$1 ~ "^" key " = " { print $2; exit }' "$file"
166+
}
167+
168+
sha256_file() {
169+
local file="$1"
170+
if command -v shasum >/dev/null 2>&1; then
171+
shasum -a 256 "$file" | awk '{print $1}'
172+
elif command -v sha256sum >/dev/null 2>&1; then
173+
sha256sum "$file" | awk '{print $1}'
174+
else
175+
echo ""
176+
fi
177+
}
178+
110179
if [[ -z "$version" ]]; then
111180
version="$(resolve_version_from_lock)"
112181
fi
@@ -129,6 +198,8 @@ if [[ -z "$src_dir" ]]; then
129198
fi
130199
fi
131200

201+
crate_archive_file="$(find_cached_crate_file || true)"
202+
132203
mkdir -p "$history_root" "$vendor_root" "$manifest_root"
133204

134205
history_repo_rel="${history_root}/${crate}.git"
@@ -189,6 +260,27 @@ git -C "$checkout_dir" push -q -f origin "refs/tags/v${version}"
189260

190261
history_head="$(git -C "$checkout_dir" rev-parse HEAD)"
191262

263+
upstream_repository="$(extract_toml_string "$src_dir/Cargo.toml" "repository")"
264+
upstream_homepage="$(extract_toml_string "$src_dir/Cargo.toml" "homepage")"
265+
registry_checksum="$(resolve_checksum_from_lock)"
266+
if [[ -z "$registry_checksum" ]]; then
267+
registry_checksum="$(resolve_checksum_from_crates_io)"
268+
fi
269+
registry_checksum="$(printf '%s' "$registry_checksum" | head -n 1 | tr -d '\r\n')"
270+
archive_sha256=""
271+
if [[ -n "$crate_archive_file" ]]; then
272+
archive_sha256="$(sha256_file "$crate_archive_file")"
273+
fi
274+
archive_sha256="$(printf '%s' "$archive_sha256" | tr -d '\r\n')"
275+
checksum_match="unknown"
276+
if [[ -n "$registry_checksum" && -n "$archive_sha256" ]]; then
277+
if [[ "$registry_checksum" == "$archive_sha256" ]]; then
278+
checksum_match="yes"
279+
else
280+
checksum_match="no"
281+
fi
282+
fi
283+
192284
dest_dir_rel="${vendor_root}/${crate}"
193285
dest_dir_abs="${repo_root}/${dest_dir_rel}"
194286
rm -rf "$dest_dir_abs"
@@ -203,6 +295,12 @@ cat > "$manifest_file" <<MANIFEST
203295
crate = "${crate}"
204296
version = "${version}"
205297
source = "crates.io"
298+
registry_index = "${registry_index}"
299+
cargo_registry_checksum = "${registry_checksum}"
300+
crate_archive_sha256 = "${archive_sha256}"
301+
checksum_match = "${checksum_match}"
302+
upstream_repository = "${upstream_repository}"
303+
upstream_homepage = "${upstream_homepage}"
206304
synced_at_utc = "${synced_at_utc}"
207305
history_repo = "${history_repo_rel}"
208306
history_head = "${history_head}"
@@ -215,6 +313,12 @@ cat > "${dest_dir_abs}/UPSTREAM.toml" <<UPSTREAM
215313
crate = "${crate}"
216314
version = "${version}"
217315
source = "crates.io"
316+
registry_index = "${registry_index}"
317+
cargo_registry_checksum = "${registry_checksum}"
318+
crate_archive_sha256 = "${archive_sha256}"
319+
checksum_match = "${checksum_match}"
320+
upstream_repository = "${upstream_repository}"
321+
upstream_homepage = "${upstream_homepage}"
218322
synced_at_utc = "${synced_at_utc}"
219323
history_repo = "${history_repo_rel}"
220324
history_head = "${history_head}"

0 commit comments

Comments
 (0)