Skip to content

Commit 157b5fc

Browse files
YuxiuBaiYuxiuBai
authored andcommitted
chore: add content branch workflow
1 parent 654d217 commit 157b5fc

4 files changed

Lines changed: 138 additions & 1 deletion

File tree

README.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,14 @@
11
# EmptyDust.github.io
2-
Fengling's free website.
2+
Fengling's free website.
3+
4+
## Content Branch Workflow
5+
6+
This repo can keep written content on a separate `content` branch.
7+
8+
Content paths are defined in `scripts/content-paths.txt`.
9+
10+
- Run `./scripts/export-content-branch.sh` on `master` to refresh the `content` branch from the current branch.
11+
- Run `./scripts/import-content-branch.sh` on `master` to pull content changes back from the `content` branch.
12+
- Push the content branch with `git push origin content` when you want it on GitHub.
13+
14+
This avoids merging whole branches and only syncs the content directories you care about.

scripts/content-paths.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Paths that belong to the content branch.
2+
# Keep this list limited to written content and content-specific data.
3+
source/_posts
4+
source/about
5+
source/index
6+
source/link
7+
source/fcircle
8+
source/_data/link.yml
9+
source/tree.txt

scripts/export-content-branch.sh

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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'."

scripts/import-content-branch.sh

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
10+
cd "$repo_root"
11+
12+
if [[ -n "$(git status --porcelain)" ]]; then
13+
echo "Working tree is not clean. Commit or stash changes first." >&2
14+
exit 1
15+
fi
16+
17+
if [[ ! -f "$paths_file" ]]; then
18+
echo "Missing content paths file: $paths_file" >&2
19+
exit 1
20+
fi
21+
22+
if ! git rev-parse --verify "$content_branch" >/dev/null 2>&1; then
23+
echo "Branch '$content_branch' does not exist." >&2
24+
exit 1
25+
fi
26+
27+
mapfile -t content_paths < <(grep -Ev '^[[:space:]]*(#|$)' "$paths_file")
28+
29+
if [[ ${#content_paths[@]} -eq 0 ]]; then
30+
echo "No content paths configured." >&2
31+
exit 1
32+
fi
33+
34+
git restore --source "$content_branch" --worktree -- "${content_paths[@]}"
35+
36+
echo "Restored content paths from '$content_branch' into the working tree."

0 commit comments

Comments
 (0)