repos/kloeckner.com.ar

Backup of part of my webpage
Commits Files Refs README LICENSE
git/sync-repos (96 lines)
   1 #!/bin/sh
   2 
   3 
   4 BIN_DIR="/root/git"
   5 GIT_DIR="/var/www/git"
   6 GIT_SSH_COMMAND='ssh -i ~/.ssh/key-mjkloeckner'
   7 
   8 MAX_PARALLEL_JOBS=1
   9 
  10 mkdir -p "$GIT_DIR"
  11 
  12 count_repos_in_list()
  13 {
  14     set -- $1
  15     echo "$#"
  16 }
  17 
  18 parallel_jobs_cleanup()
  19 {
  20     trap - INT TERM
  21 
  22     for pid in $pids; do
  23         kill "$pid" 2>/dev/null
  24     done
  25 
  26     wait
  27 
  28     exit 130
  29 }
  30 
  31 update_repo()
  32 {
  33     repo="$1"
  34     repo_dir="$2"
  35     bare_repo_path="$3"
  36 
  37     if [ -d "$repo_dir" ] && [ -d "$bare_repo_path" ]; then
  38         git -C "$bare_repo_path" remote update --prune
  39     else
  40         mkdir -p "$repo_dir"
  41         git clone --mirror "https://github.com/$repo.git" "$bare_repo_path"
  42         ln -sf "$bare_repo_path" /home/git
  43     fi
  44 
  45     # always update description
  46     gh repo view "$repo" --json description --jq '.description' > \
  47         "$bare_repo_path/description"
  48 
  49     (
  50         cd "$GIT_DIR" || exit
  51         "$BIN_DIR/update-git-repo" "$repo_dir"
  52     )
  53 }
  54 
  55 repos_list=$(gh repo list --limit 1000 --json nameWithOwner,isPrivate,isFork \
  56     --jq '.[] | select(.isPrivate == false and .isFork == false) |
  57     .nameWithOwner' | sort)
  58 repos_count=$(count_repos_in_list "$repos_list")
  59 
  60 jobs=0
  61 pids=""
  62 
  63 trap parallel_jobs_cleanup INT TERM
  64 
  65 for repo in $repos_list; do
  66     name=$(basename "$repo")
  67     repo_dir="$GIT_DIR/$name"
  68     bare_repo_path="$repo_dir/$name.git"
  69 
  70     update_repo "$repo" "$repo_dir" "$bare_repo_path" &
  71     pid=$!
  72     pids="$pids $pid"
  73     jobs=$((jobs + 1))
  74 
  75     if [ "$jobs" -ge "$MAX_PARALLEL_JOBS" ]; then
  76         wait
  77         pids=""
  78         jobs=0
  79     fi
  80 
  81     # always update description
  82     # gh repo view $repo --json description --jq '.description' > \
  83     #     $bare_repo_path/description
  84 
  85     # if [ -d "$repo_dir" ] && [ -d "$bare_repo_path" ]; then
  86     #     git -C "$bare_repo_path" remote update --prune
  87     # else
  88     #     mkdir -p "$repo_dir"
  89     #     git clone --mirror "https://github.com/$repo.git" "$bare_repo_path"
  90     #     ln -sf "$bare_repo_path" /home/git
  91     # fi
  92 
  93     # (cd "$GIT_DIR"; "$BIN_DIR"/update-git-repo "$repo_dir")
  94 done
  95 
  96 # (cd "$GIT_DIR"; "$BIN_DIR"/generate-git-main-index .)