diff --git a/README.md b/README.md index ad0e2f4..8e0ca5a 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,7 @@ - [aptsync](#aptsync) - [apt-sync](#apt-sync) - [archvsync](#archvsync) + - [crates-io-index](#crates-io-index) - [debian-cd](#debian-cd) - [docker-ce](#docker-ce) - [fedora](#fedora) @@ -130,6 +131,20 @@ A.K.A. [ftpsync](https://anonscm.debian.org/cgit/mirror/archvsync.git/) | ------------- | ---------------------------------------------- | | `IGNORE_LOCK` | Purge lockfiles at first. Defaults to `false`. | +### crates-io-index + +[![crates-io-index](https://img.shields.io/docker/image-size/ustcmirror/crates-io-index/latest)](https://hub.docker.com/r/ustcmirror/crates-io-index "crates-io-index") +[![crates-io-index](https://img.shields.io/docker/pulls/ustcmirror/crates-io-index)](https://hub.docker.com/r/ustcmirror/crates-io-index "crates-io-index") + +A dedicated script to sync . + +| Parameter | Description | +| ---------------- | ---------------------------------------------------------------------------------------------------------- | +| `CRATES_PROXY` | The URL that crates will be redirected to. Defaults to `https://crates-io.proxy.ustclug.org/api/v1/crates` | +| `CRATES_GITMSG` | The commit message of `config.json`. Defaults to `Redirect to USTC Mirrors` | +| `CRATES_GITMAIL` | `user.email` when committing `config.json`. Defaults to `lug AT ustc.edu.cn` | +| `CRATES_GITNAME` | `user.name` when committing `config.json`. Defaults to `mirror` | + ### debian-cd [![debian-cd](https://img.shields.io/docker/image-size/ustcmirror/debian-cd/latest)](https://hub.docker.com/r/ustcmirror/debian-cd "debian-cd") diff --git a/crates-io-index/Dockerfile b/crates-io-index/Dockerfile new file mode 100644 index 0000000..e406794 --- /dev/null +++ b/crates-io-index/Dockerfile @@ -0,0 +1,4 @@ +FROM ustcmirror/base:alpine +LABEL maintainer "Keyu Tao " +RUN apk add --no-cache git +ADD sync.sh / diff --git a/crates-io-index/sync.sh b/crates-io-index/sync.sh new file mode 100755 index 0000000..2a705c2 --- /dev/null +++ b/crates-io-index/sync.sh @@ -0,0 +1,56 @@ +#!/bin/bash + +# Migrated to yuki (docker) by @taoky +# Created by @ksqsf +# Based on the original version written by @knight42 + +# ENVS: +# CRATES_PROXY= +# CRATES_GITMSG= +# CRATES_GITMAIL= +# CRATES_GITNAME= + +is_empty() { + [[ -z $(ls -A "$1" 2>/dev/null) ]] +} + +set -e +[[ -n $DEBUG ]] && set -x + +CRATES_PROXY="${CRATES_PROXY:-https://crates-io.proxy.ustclug.org/api/v1/crates}" +CRATES_GITMSG="${CRATES_GITMSG:-Redirect to USTC Mirrors}" +CRATES_GITMAIL="${CRATES_GITMAIL:-lug AT ustc.edu.cn}" +CRATES_GITNAME="${CRATES_GITNAME:-mirror}" + +ensure_redirect() { + pushd "$TO" + if grep -F -q "$CRATES_PROXY" 'config.json'; then + return + else + cat < 'config.json' +{ + "dl": "$CRATES_PROXY", + "api": "https://crates.io/" +} +EOF + git add config.json + git -c user.name="$CRATES_GITNAME" -c user.email="$CRATES_GITMAIL" commit -qm "$CRATES_GITMSG" + fi + popd +} + +# crates.io-index has a custom ensure_redirect logic +# so now we don't use gitsync here. + +if ! is_empty "$TO"; then + cd "$TO" + git fetch origin + git reset --hard origin/master + ensure_redirect + git repack -adb + git gc --auto + git update-server-info +else + git clone 'https://github.com/rust-lang/crates.io-index.git' "$TO" + ensure_redirect +fi