-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsyncaction
More file actions
93 lines (80 loc) · 3.38 KB
/
Copy pathsyncaction
File metadata and controls
93 lines (80 loc) · 3.38 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
# Modernized syncaction for x-cmd-action (post-2022)
#
# Strategy:
# - Clone the catalog (x-cmd-sourcecode/sourcecode) once
# - For each lib/* entry: shallow bare clone upstream, push refs to GitHub
# and Gitee. The backup shell snippet is written to a per-job .sh file
# (avoids the sh -c quoting nightmare)
# - xargs fans out across CONCURRENCY slots (default 10)
# - Capture real git push exit codes (not sed's) via "rc=..." output
set -u
git clone git@github.com:x-cmd-sourcecode/sourcecode.git /tmp/sourcecode
cd /tmp/sourcecode || exit 1
JOBDIR=$(mktemp -d)
# Write a per-job script template. The template has placeholders
# that get substituted with name/url.
cat > "$JOBDIR/job.sh" <<'TEMPLATE'
#!/bin/sh
# xargs -I {} passes the whole line as a single arg, so split it here.
arg="$1"
name="${arg%%|*}"
url="${arg#*|}"
workdir=$(mktemp -d)
cd "$workdir" || exit 1
echo "==> backup: $name (from $url)"
# Shallow bare clone first (fast initial pull). We then --unshallow below
# before push so we have full history — required for the
# 'shallow update not allowed' rejection on Gitee pre-existing mirrors
# (curl, openssl) and for the 'non-fast-forward' rejection when upstream
# has moved past our shallow tip (jj on GitHub).
if ! git clone --depth 1 --bare --no-tags "$url" "$name" 2>/dev/null; then
git clone --bare "$url" "$name" 2>/dev/null || {
echo "FAIL clone: $name (from $url)"
cd /; rm -rf "$workdir"; exit 0
}
fi
cd "$name" || { cd /; rm -rf "$workdir"; exit 0; }
# Unshallow so push doesn't get rejected for shallow / non-FF reasons.
# Skip the fetch refspec — --unshallow already pulls the rest of the
# history. Fast on incremental runs because the local already has the
# tip; slower (~5 min for openssl/curl) on a fresh first-time clone.
git fetch --unshallow origin 2>/dev/null || git remote add origin "$url" && git fetch origin 2>/dev/null || true
# Push to GitHub — the contract. Two separate pushes because git forbids
# combining --all and --tags. --all (not --mirror) avoids the
# "refusing to delete default branch" rejection on GitHub.
gh_output=$(git push --all "git@github.com:x-cmd-sourcecode/$name" 2>&1)
gh_rc=$?
gh_tags_output=$(git push --tags "git@github.com:x-cmd-sourcecode/$name" 2>&1)
gh_tags_rc=$?
echo "$gh_output" | sed "s/^/ [github] /"
echo "$gh_tags_output" | sed "s/^/ [github-tags] /"
if [ "$gh_rc" = "0" ] && [ "$gh_tags_rc" = "0" ]; then
echo " [github] OK: $name"
else
echo "FAIL github: $name (rc all=$gh_rc tags=$gh_tags_rc)"
fi
# Push to Gitee — best-effort. Repos may not exist yet (404 from ssh).
gt_output=$(git push --all "git@gitee.com:x-cmd-sourcecode/$name" 2>&1)
gt_rc=$?
gt_tags_output=$(git push --tags "git@gitee.com:x-cmd-sourcecode/$name" 2>&1)
gt_tags_rc=$?
echo "$gt_output" | sed "s/^/ [gitee] /"
echo "$gt_tags_output" | sed "s/^/ [gitee-tags] /"
if [ "$gt_rc" = "0" ] && [ "$gt_tags_rc" = "0" ]; then
echo " [gitee] OK: $name"
else
echo " [gitee] skip-or-fail: $name (rc all=$gt_rc tags=$gt_tags_rc)"
fi
cd /
rm -rf "$workdir"
TEMPLATE
chmod +x "$JOBDIR/job.sh"
# Emit name|url per catalog entry; skip non GIT-HTTP (e.g. lib/README.removed).
for entry in lib/*; do
[ -f "$entry" ] || continue
. "$entry"
[ "${SOURCE_TYPE:-}" = "GIT-HTTP" ] || continue
printf '%s\n' "${SOURCE_TGTNAME}|${SOURCE_URL}"
done | xargs -d '\n' -P "${CONCURRENCY:-10}" -I {} "$JOBDIR/job.sh" {}
# Cleanup
rm -rf "$JOBDIR"