165 lines
4.3 KiB
Bash
Executable File
165 lines
4.3 KiB
Bash
Executable File
#!/usr/bin/env sh
|
|
set -eu
|
|
|
|
REPO_URL="${REPO_URL:-http://100.64.0.1:30087/section0/rta-handbook.git}"
|
|
TARGET_DIR="${TARGET_DIR:-$HOME/Developer/Section0/rta-handbook}"
|
|
COMMAND_DIR="${COMMAND_DIR:-$HOME/.local/bin}"
|
|
COMMAND_NAME="${COMMAND_NAME:-section0-docs}"
|
|
COMMAND_PATH="$COMMAND_DIR/$COMMAND_NAME"
|
|
|
|
step() {
|
|
printf "\n==> %s\n" "$*"
|
|
}
|
|
|
|
if ! command -v git >/dev/null 2>&1; then
|
|
echo "git is required but was not found on PATH" >&2
|
|
exit 1
|
|
fi
|
|
|
|
step "Preparing local Markdown workspace"
|
|
if [ -d "$TARGET_DIR/.git" ]; then
|
|
echo "Repo already exists: $TARGET_DIR"
|
|
git -C "$TARGET_DIR" remote set-url origin "$REPO_URL"
|
|
git -C "$TARGET_DIR" fetch origin
|
|
git -C "$TARGET_DIR" checkout main
|
|
git -C "$TARGET_DIR" pull --ff-only
|
|
else
|
|
mkdir -p "$(dirname "$TARGET_DIR")"
|
|
git clone "$REPO_URL" "$TARGET_DIR"
|
|
fi
|
|
|
|
git -C "$TARGET_DIR" config pull.ff only
|
|
|
|
step "Installing helper command"
|
|
mkdir -p "$COMMAND_DIR"
|
|
cat > "$COMMAND_PATH" <<EOF
|
|
#!/usr/bin/env sh
|
|
set -eu
|
|
|
|
REPO_DIR="$TARGET_DIR"
|
|
|
|
usage() {
|
|
cat <<USAGE
|
|
section0-docs - helper for Section 0 shared Markdown docs
|
|
|
|
Commands:
|
|
configure set Git author name/email for this repo
|
|
doctor check clone, author, remote, and read access
|
|
open print the repo path
|
|
pull pull latest Markdown with --ff-only
|
|
status show Git status
|
|
commit commit all current changes with MESSAGE
|
|
push push current branch
|
|
push-test prove write access with a temporary remote branch
|
|
help show this help
|
|
|
|
Examples:
|
|
section0-docs doctor
|
|
section0-docs configure
|
|
section0-docs pull
|
|
section0-docs status
|
|
section0-docs push-test
|
|
MESSAGE="Update concept notes" section0-docs commit
|
|
section0-docs push
|
|
|
|
AFFiNE is read-only for these docs. Make lasting changes in Markdown; the
|
|
AFFiNE copies are refreshed from Git.
|
|
USAGE
|
|
}
|
|
|
|
case "\${1:-help}" in
|
|
configure)
|
|
current_name="\$(git -C "\$REPO_DIR" config user.name || true)"
|
|
current_email="\$(git -C "\$REPO_DIR" config user.email || true)"
|
|
printf "Git author name [%s]: " "\$current_name"
|
|
IFS= read -r author_name
|
|
printf "Git author email [%s]: " "\$current_email"
|
|
IFS= read -r author_email
|
|
if [ -n "\$author_name" ]; then
|
|
git -C "\$REPO_DIR" config user.name "\$author_name"
|
|
elif [ -z "\$current_name" ]; then
|
|
echo "Git author name is required" >&2
|
|
exit 1
|
|
fi
|
|
if [ -n "\$author_email" ]; then
|
|
git -C "\$REPO_DIR" config user.email "\$author_email"
|
|
elif [ -z "\$current_email" ]; then
|
|
echo "Git author email is required" >&2
|
|
exit 1
|
|
fi
|
|
;;
|
|
doctor)
|
|
remote="\$(git -C "\$REPO_DIR" remote get-url origin)"
|
|
branch="\$(git -C "\$REPO_DIR" branch --show-current)"
|
|
author_name="\$(git -C "\$REPO_DIR" config user.name || true)"
|
|
author_email="\$(git -C "\$REPO_DIR" config user.email || true)"
|
|
echo "Repo: \$REPO_DIR"
|
|
echo "Remote: \$remote"
|
|
echo "Branch: \$branch"
|
|
if [ -n "\$author_name" ] && [ -n "\$author_email" ]; then
|
|
echo "Git author: \$author_name <\$author_email>"
|
|
else
|
|
echo "Git author: missing; run section0-docs configure"
|
|
fi
|
|
git -C "\$REPO_DIR" ls-remote --heads origin main >/dev/null
|
|
echo "Read access: ok"
|
|
;;
|
|
open)
|
|
printf "%s\n" "\$REPO_DIR"
|
|
;;
|
|
pull)
|
|
git -C "\$REPO_DIR" pull --ff-only
|
|
;;
|
|
status)
|
|
git -C "\$REPO_DIR" status --short --branch
|
|
;;
|
|
commit)
|
|
: "\${MESSAGE:?Set MESSAGE before running commit}"
|
|
git -C "\$REPO_DIR" add .
|
|
git -C "\$REPO_DIR" commit -m "\$MESSAGE"
|
|
;;
|
|
push)
|
|
git -C "\$REPO_DIR" push
|
|
;;
|
|
push-test)
|
|
branch="section0-smoke-\${USER:-user}-\$(date +%Y%m%d%H%M%S)"
|
|
git -C "\$REPO_DIR" push origin HEAD:refs/heads/"\$branch"
|
|
git -C "\$REPO_DIR" push origin :refs/heads/"\$branch"
|
|
echo "Write access: ok"
|
|
;;
|
|
help|--help|-h)
|
|
usage
|
|
;;
|
|
*)
|
|
usage >&2
|
|
exit 1
|
|
;;
|
|
esac
|
|
EOF
|
|
chmod +x "$COMMAND_PATH"
|
|
|
|
cat <<EOF
|
|
Ready.
|
|
|
|
Repo: $TARGET_DIR
|
|
Remote: $REPO_URL
|
|
Helper: $COMMAND_PATH
|
|
|
|
Daily flow:
|
|
$COMMAND_NAME doctor
|
|
$COMMAND_NAME pull
|
|
# edit Markdown
|
|
$COMMAND_NAME status
|
|
MESSAGE="Update docs" $COMMAND_NAME commit
|
|
$COMMAND_NAME push
|
|
|
|
Contributor setup:
|
|
$COMMAND_NAME configure
|
|
$COMMAND_NAME push-test
|
|
|
|
AFFiNE updates after the Markdown docs are refreshed from Git.
|
|
|
|
If $COMMAND_DIR is not on PATH, add this to your shell profile:
|
|
export PATH="$COMMAND_DIR:\$PATH"
|
|
EOF
|