sync-git-mirrors.sh 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #!/bin/sh
  2. set -e
  3. set -x
  4. # chdir to repo root
  5. cd "$(dirname "$0")"/..
  6. opts="--force"
  7. main_branch="main"
  8. extra_branches=""
  9. branches="$main_branch $extra_branches"
  10. ignore_remotes=""
  11. # force english language so we can parse git output
  12. export LANG=C
  13. export LC_ALL=C
  14. # no. dont pull changes from github
  15. if false; then
  16. # done: remove. better: socks5h proxy:
  17. # git -c remote.origin.proxy=socks5h://127.0.0.1:9050 clone
  18. # git config --add remote.darktea.proxy socks5h://127.0.0.1:9050
  19. # curl --proxy socks5h://127.0.0.1:9050
  20. #
  21. # build git with patched curl to fix:
  22. # Not resolving .onion address (RFC 7686)
  23. # https://github.com/curl/curl/pull/11236
  24. #export CURL_ALLOW_DOT_ONION=1
  25. remote=github.com
  26. git fetch $remote $branches --tags
  27. # rebase the main branches with changes from the main repo
  28. # usually, these changes come from the Issues2Markdown github action
  29. date=$(date +%F-%H-%M-%S)
  30. # stash changes to the main branch
  31. stash_output="$(git stash -m "git pull-push $(date)")"
  32. echo "$stash_output"
  33. if [[ "$stash_output" == "No local changes to save" ]]; then
  34. did_stash_main=false
  35. else
  36. did_stash_main=true
  37. fi
  38. for branch in $branches; do
  39. git branch --copy $branch $branch-bak-$date
  40. if [[ "$branch" == "$main_branch" ]]; then
  41. git rebase remotes/$remote/$branch
  42. if $did_stash_main; then
  43. git stash pop
  44. fi
  45. else
  46. branch_path="$(git worktree list | grep " \[$branch\]$" || true)"
  47. is_temp_branch_path=false
  48. if [ -n "$branch_path" ]; then
  49. branch_path="$(echo "$branch_path" | sed -E 's| +[0-9a-f]+ \[[^]]+\]$||')"
  50. else
  51. branch_path="worktree-branch-$branch-$date"
  52. git worktree add "$branch_path" $branch
  53. is_temp_branch_path=true
  54. fi
  55. stash_output="$(git -C "$branch_path" stash -m "git pull-push $(date)")"
  56. echo "$stash_output"
  57. if [[ "$stash_output" == "No local changes to save" ]]; then
  58. did_stash=false
  59. else
  60. did_stash=true
  61. fi
  62. git -C "$branch_path" rebase remotes/$remote/$branch
  63. if $is_temp_branch_path; then
  64. git worktree remove "$branch_path"
  65. fi
  66. if $did_stash; then
  67. git -C "$branch_path" stash pop
  68. fi
  69. fi
  70. done
  71. fi
  72. # push changes to all repos
  73. for remote in $(git remote show); do
  74. if [[ " $ignore_remotes " =~ " $remote " ]]; then
  75. continue
  76. fi
  77. if echo "$remote" | grep -q readonly; then
  78. continue
  79. fi
  80. # TODO push branches and tags in one command
  81. git push $remote $branches $opts || true
  82. git push $remote $branches $opts --tags || true
  83. done
  84. # codeberg.org has no automatic update like github pages
  85. echo updating https://milahu.codeberg.page/alchi/
  86. git push codeberg.org $main_branch:pages
  87. exit
  88. # TODO
  89. # sourceforge.net has no automatic update like github pages
  90. echo updating https://milahu-alchi.sourceforge.io/
  91. repo_root="$(readlink -f "$(dirname "$0")"/../..)"
  92. "$repo_root/src/scripts/sync-sourceforge-pages.sh"