|
1 | 1 | #!/usr/bin/env bash |
2 | 2 | set -euo pipefail |
3 | 3 |
|
4 | | -# Print candidate cndocs files that likely need translation sync |
5 | | -# Strategy: |
6 | | -# 1) Compare upstream/main vs local production for docs/*.md |
7 | | -# 2) Map changed docs/<name>.md -> cndocs/<name>.md when target exists |
8 | | -# 3) Output unique, sorted cndocs paths (one per line) |
| 4 | +# Print candidate cndocs files that need translation sync. |
| 5 | +# Two strategies combined: |
| 6 | +# A) Compare upstream/main vs local production for docs/*.md (new upstream changes) |
| 7 | +# B) For previously-merged translations, check if upstream file was updated |
| 8 | +# more recently than the cndocs translation — if so, re-translate. |
9 | 9 |
|
10 | 10 | REPO_ROOT="$(cd "$(dirname "$0")/.." && pwd)" |
11 | 11 | cd "$REPO_ROOT" |
12 | 12 |
|
| 13 | +PROGRESS_FILE="$REPO_ROOT/scripts/translate-progress.json" |
| 14 | + |
13 | 15 | # Ensure upstream exists |
14 | 16 | if ! git remote get-url upstream >/dev/null 2>&1; then |
15 | 17 | echo "ERROR: missing upstream remote" >&2 |
|
18 | 20 |
|
19 | 21 | git fetch upstream --quiet |
20 | 22 |
|
21 | | -# Changed docs files between production and upstream/main |
22 | | -changed_docs=$(git diff --name-only production..upstream/main -- 'docs/*.md' || true) |
| 23 | +candidates=() |
23 | 24 |
|
24 | | -if [ -z "${changed_docs}" ]; then |
25 | | - exit 0 |
| 25 | +# --- Strategy A: new upstream changes not yet in production --- |
| 26 | +changed_docs=$(git diff --name-only production..upstream/main -- 'docs/*.md' || true) |
| 27 | +if [ -n "${changed_docs}" ]; then |
| 28 | + while IFS= read -r f; do |
| 29 | + [ -z "$f" ] && continue |
| 30 | + name="${f#docs/}" |
| 31 | + target="cndocs/${name}" |
| 32 | + if [ -f "$target" ]; then |
| 33 | + candidates+=("$target") |
| 34 | + fi |
| 35 | + done <<< "$changed_docs" |
26 | 36 | fi |
27 | 37 |
|
28 | | -while IFS= read -r f; do |
29 | | - [ -z "$f" ] && continue |
30 | | - name="${f#docs/}" |
31 | | - target="cndocs/${name}" |
32 | | - if [ -f "$target" ]; then |
33 | | - echo "$target" |
| 38 | +# --- Strategy B: merged translations whose upstream source is now newer --- |
| 39 | +if [ -f "$PROGRESS_FILE" ]; then |
| 40 | + merged_files=$(python3 -c " |
| 41 | +import json, sys |
| 42 | +with open('$PROGRESS_FILE') as f: |
| 43 | + data = json.load(f) |
| 44 | +for item in data.get('merged', []): |
| 45 | + print(item) |
| 46 | +" 2>/dev/null || true) |
| 47 | + |
| 48 | + if [ -n "$merged_files" ]; then |
| 49 | + while IFS= read -r target; do |
| 50 | + [ -z "$target" ] && continue |
| 51 | + # cndocs/foo.md -> docs/foo.md |
| 52 | + name="${target#cndocs/}" |
| 53 | + source="docs/${name}" |
| 54 | + |
| 55 | + # Get upstream last-modified epoch for the source file |
| 56 | + upstream_epoch=$(git log -1 --format="%ct" upstream/main -- "$source" 2>/dev/null || echo 0) |
| 57 | + # Get production last-modified epoch for the translated file |
| 58 | + translation_epoch=$(git log -1 --format="%ct" production -- "$target" 2>/dev/null || echo 0) |
| 59 | + |
| 60 | + if [ "$upstream_epoch" -gt "$translation_epoch" ] 2>/dev/null; then |
| 61 | + candidates+=("$target") |
| 62 | + fi |
| 63 | + done <<< "$merged_files" |
34 | 64 | fi |
35 | | -done <<< "$changed_docs" | sort -u |
| 65 | +fi |
| 66 | + |
| 67 | +# Deduplicate and sort |
| 68 | +if [ ${#candidates[@]} -gt 0 ]; then |
| 69 | + printf '%s\n' "${candidates[@]}" | sort -u |
| 70 | +fi |
0 commit comments