|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +set -euo pipefail |
| 4 | + |
| 5 | +repo_root=$(git rev-parse --show-toplevel) |
| 6 | +script_dir=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) |
| 7 | +paths_file="$script_dir/content-paths.txt" |
| 8 | +content_branch="${1:-content}" |
| 9 | +source_ref="${2:-HEAD}" |
| 10 | + |
| 11 | +cd "$repo_root" |
| 12 | + |
| 13 | +if [[ -n "$(git status --porcelain)" ]]; then |
| 14 | + echo "Working tree is not clean. Commit or stash changes first." >&2 |
| 15 | + exit 1 |
| 16 | +fi |
| 17 | + |
| 18 | +if [[ ! -f "$paths_file" ]]; then |
| 19 | + echo "Missing content paths file: $paths_file" >&2 |
| 20 | + exit 1 |
| 21 | +fi |
| 22 | + |
| 23 | +mapfile -t content_paths < <(grep -Ev '^[[:space:]]*(#|$)' "$paths_file") |
| 24 | + |
| 25 | +if [[ ${#content_paths[@]} -eq 0 ]]; then |
| 26 | + echo "No content paths configured." >&2 |
| 27 | + exit 1 |
| 28 | +fi |
| 29 | + |
| 30 | +mapfile -t content_files < <(git ls-tree -r --name-only "$source_ref" -- "${content_paths[@]}") |
| 31 | + |
| 32 | +tmp_dir=$(mktemp -d "${TMPDIR:-/tmp}/content-branch.XXXXXX") |
| 33 | + |
| 34 | +cleanup() { |
| 35 | + git worktree remove --force "$tmp_dir" >/dev/null 2>&1 || true |
| 36 | + rm -rf "$tmp_dir" |
| 37 | +} |
| 38 | + |
| 39 | +trap cleanup EXIT |
| 40 | + |
| 41 | +if git show-ref --verify --quiet "refs/heads/$content_branch"; then |
| 42 | + git worktree add "$tmp_dir" "$content_branch" >/dev/null |
| 43 | +else |
| 44 | + git worktree add -b "$content_branch" "$tmp_dir" "$source_ref" >/dev/null |
| 45 | +fi |
| 46 | + |
| 47 | +find "$tmp_dir" -mindepth 1 -maxdepth 1 ! -name .git -exec rm -rf {} + |
| 48 | + |
| 49 | +if [[ ${#content_files[@]} -gt 0 ]]; then |
| 50 | + git archive "$source_ref" "${content_files[@]}" | tar -x -C "$tmp_dir" |
| 51 | +fi |
| 52 | + |
| 53 | +git show "$source_ref:.gitignore" > "$tmp_dir/.gitignore" |
| 54 | + |
| 55 | +cat > "$tmp_dir/README.md" <<EOF |
| 56 | +# content branch |
| 57 | +
|
| 58 | +This branch stores authored content extracted from \`$source_ref\`. |
| 59 | +
|
| 60 | +Edit content here if you want to keep Markdown separate from theme and build files. |
| 61 | +Bring the changes back to the main branch with: |
| 62 | +
|
| 63 | +\`\`\`bash |
| 64 | +./scripts/import-content-branch.sh $content_branch |
| 65 | +\`\`\` |
| 66 | +EOF |
| 67 | + |
| 68 | +( |
| 69 | + cd "$tmp_dir" |
| 70 | + git add --all |
| 71 | + |
| 72 | + if git diff --cached --quiet; then |
| 73 | + echo "No content changes to commit on '$content_branch'." |
| 74 | + exit 0 |
| 75 | + fi |
| 76 | + |
| 77 | + git commit -m "sync content from $source_ref" >/dev/null |
| 78 | +) |
| 79 | + |
| 80 | +echo "Updated local branch '$content_branch' from '$source_ref'." |
0 commit comments