commit 76e3a65db41d1d8d6b3bb6d590aca1e8faac1ed3
Author: kst
Date: 2020-07-17 22:37Z

init, take 2

Diffstat:
Agrm | 228+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 228 insertions(+), 0 deletions(-)

diff --git a/grm b/grm @@ -0,0 +1,228 @@ +#!/bin/sh +# grm: git repo manager, tilde edition + +#---------------+----------+-----------------# +# | config | # +# +----------+ # + +# root directory of git repositories +GRM_REPOS_ROOT="$HOME/public_html" + +# default owner +GRM_OWNER="$USER" + +# default url prefix (without ending slash) +GRM_URL_PREFIX="https://tilde.team/~$USER" + +# path of the post-receive hooks for stagit +GRM_POSTRECV_HOOK="$HOME/bin/stagit-post-receive" + +# root directory of stagit web pages +STAGIT_WEB_ROOT="$HOME/public_html/git" + +# # +# # +#--------------------------------------------# + +# for stagit +export LC_CTYPE="en_US.UTF-8" + +prog_name="${0##*/}" +repos_root=${GRM_REPOS_ROOT:-$HOME/public_html} +web_root=${STAGIT_WEB_ROOT:-$HOME/public_html/git} + +recompile_repo() { + echo "[$1] recompiling stagit pages..." + repo_dir="${repos_root}/${1}.git" + repo_web_dir="${web_root}/${repo_name}" + cachefile="${repo_dir}/.htmlcache" + + [ -d "$repo_dir" ] || { echo "[$1] repo not found"; return 1; } + mkdir -p "$repo_web_dir" + + cd "${repo_web_dir:?}" && \ + rm -f "$cachefile" && \ + rm -rf "commit" "file" && \ + stagit -c "$cachefile" "$repo_dir" && \ + ln -sf log.html index.html && \ + echo "[$1] done!" +} + +rebuild_index() { + echo "[index] rebuilding index..." + mkdir -p "${web_root}" || return 1; + # 1. find all directories in $repos_root ending with .git + # 2. exclude any repo marked with stagit-no-index + # 3. sort the result + # 4. hack for posix compatibility + # 5. run stagit-index on the result + # 6. export result to index.html + find "${repos_root}/." ! -name . -prune \ + -type d -name "*.git" \ + -exec test ! -e "{}/stagit-no-index" \; \ + -print \ + | sort -f \ + | sed -e 's/"/"\\""/g' -e 's/.*/"&"/' \ + | xargs stagit-index \ + > "${web_root}/index.html" && \ + echo "[index] done!" +} + +RED="\033[91m" +GREEN="\033[92m" +BLUE="\033[94m" +RESET="\033[0m" + +grm_new() { + set -e + default_owner=${GRM_OWNER:-$USER} + url_prefix=${GRM_URL_PREFIX:-https://tilde.team/~$USER} + postrecv_path=${GRM_POSTRECV_HOOK:-$HOME/bin/stagit-post-receive} + default_desc="a work in progress" + + printf "%b\n> " "${BLUE}repo name${RESET}" + read -r repo_name + [ -z "$repo_name" ] && \ + { echo "no repo name given, exiting..."; exit 1; } + + # now we have the complete path of the repo + repo_path="$repos_root/${repo_name}.git" + [ -e "$repo_path" ] && \ + { echo "repository already exists"; exit 1; } + + printf "%b\n> " "${BLUE}no index? [${GREEN}y/N${BLUE}]${RESET}" + read -r hidden + + printf "%b%s%b\n> " "${BLUE}description [${GREEN}" "$default_desc" "${BLUE}]${RESET}" + read -r repo_desc + repo_desc=${repo_desc:-$default_desc} + + printf "%b%s%b\n> " "${BLUE}owner [${GREEN}" "$default_owner" "${BLUE}]${RESET}" + read -r owner + owner=${owner:-$default_owner} + + printf "%b%s%b\n> " \ + "${BLUE}clone url [${GREEN}" "$url_prefix/${repo_name}.git" "${BLUE}]${RESET}" + read -r clone_url + clone_url=${clone_url:-$url_prefix/$repo_name} + + # start creating repo + git init --bare "$repo_path" + + echo "writing stagit metadata..." + printf "%s\n" "$repo_desc" > "$repo_path/description" + printf "%s\n" "$owner" > "$repo_path/owner" + printf "%s\n" "$clone_url" > "$repo_path/url" + + if echo "$hidden" | grep -iq "^y$"; then + : >> "$repo_path/stagit-no-index" + fi + + echo "installing stagit post-receive hook..." + ln -sf "$postrecv_path" "$repo_path/hooks/post-receive" + echo "installing post-update hook for updating server info..." + mv "$repo_path/hooks/post-update.sample" "$repo_path/hooks/post-update" + + echo "updating server info for the first time..." + git -C "$repo_path" update-server-info + + echo "done!" +} + +grm_remove() { + [ $# -gt 0 ] || { echo "no repo name given, exiting..."; exit 1; } + + for repo in "$@" + do + printf "remove %s? [y/N] " "$repo" + read -r resp + if echo "$resp" | grep -iq "^y$"; then + rm -rf "${repos_root:?}/${repo:?}.git" || continue; + rm -rf "${web_root:?}/${repo:?}" || continue; + fi + done + # only rebuild index if stagit exists + command -v stagit-index >/dev/null && rebuild_index & + wait +} + +grm_list() { + find "${repos_root}/." ! -name . -prune \ + -type d -name "*.git" \ + -exec basename {} '.git' \; +} + +grm_recompile() { + for repo_name in "$@" + do + recompile_repo "$repo_name" & + done + rebuild_index & + wait + echo "recompilation done!" +} + +grm_recompileall() { + grm_list \ + | sed -e 's/"/"\\""/g' -e 's/.*/"&"/' \ + | xargs "$0" rc +} + +grm_info() { + [ -z "$1" ] && { echo "no repo name given, exiting..."; exit 1; } + + repo_name=$1 + repo_dir="${repos_root}/${repo_name}.git" + + [ -d "$repo_dir" ] || { echo "can't find repo named $repo_name"; exit 1; } + echo "name: $repo_name" + printf "indexed: " + + if [ -e "${repo_dir}/stagit-no-index" ]; then + printf "%b\n" "${RED}no${RESET}" + else + printf "%b\n" "${GREEN}yes${RESET}" + fi + + [ -f "${repo_dir}/description" ] && \ + echo "description: $(cat "${repo_dir}/description")" + + [ -f "${repo_dir}/owner" ] && \ + echo "owner: $(cat "${repo_dir}/owner")" + + [ -f "${repo_dir}/url" ] && \ + echo "url: $(cat "${repo_dir}/url")" +} + +show_help() { + cat << EOF +usage: $prog_name <command> [<args>] + +Git repo manager, tilde edition + +commands: + new create a new repo + info repo_name display metadata of the repo + ls list all repos + rm repo1 [repo2..] remove repos + rc recompile stagit index + rc repo1 [repo2..] recompile stagit pages for repos, + and recompile index + rca recompile all repos + help show help +EOF +} + +# parse subcommands +case "$1" in + new) cmd=new;; + ls|list) cmd=list;; + rm|remove) cmd=remove;; + rc|recompile) cmd=recompile;; + rca|recompileall) cmd=recompileall;; + info) cmd=info;; + *) { show_help; exit; };; +esac + +shift +grm_"$cmd" "$@"