git-remote-add-all.sh 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #!/usr/bin/env bash
  2. main_branches="master journal"
  3. username=milahu
  4. force=true
  5. function git_remote_add() {
  6. local name="$1"
  7. local url="$2"
  8. exists=false
  9. if git remote get-url "$name" >/dev/null 2>&1; then
  10. exists=true
  11. fi
  12. if ! $force && $exists; then
  13. # remote with this name already exists
  14. echo "remote exists: $name"
  15. return
  16. fi
  17. # add username
  18. url="$(echo "$url" | sed -E "s|^(https?://)|\1${username}@|")"
  19. if ! $exists; then
  20. echo "adding remote: $name"
  21. git remote add "$name" "$url"
  22. else
  23. echo "updating remote: $name"
  24. git remote set-url "$name" "$url"
  25. fi
  26. if echo "$name" | grep -q '.onion$'; then
  27. echo "torifying remote: $name"
  28. git config --add "remote.$name.proxy" socks5h://127.0.0.1:9050
  29. fi
  30. }
  31. git_remote_add github.com https://github.com/milahu/alchi
  32. git_remote_add gitlab.com https://gitlab.com/milahu/alchi
  33. git_remote_add codeberg.org https://codeberg.org/milahu/alchi
  34. git_remote_add sourceforge.net https://git.code.sourceforge.net/p/milahu-alchi/code
  35. git_remote_add notabug.org https://notabug.org/milahu/alchi
  36. git_remote_add disroot.org https://git.disroot.org/milahu/alchi
  37. git_remote_add sr.ht git@git.sr.ht:~milahu/alchi
  38. git_remote_add darktea.onion http://it7otdanqu7ktntxzm427cba6i53w6wlanlh23v5i3siqmos47pzhvyd.onion/milahu/alchi
  39. git_remote_add righttoprivacy.onion http://gg6zxtreajiijztyy5g6bt5o6l3qu32nrg7eulyemlhxwwl6enk6ghad.onion/milahu/alchi
  40. # done
  41. exit 0
  42. # not needed?
  43. # what problem should this solve?
  44. if false; then
  45. # fix error: There are multiple remotes whose fetch refspecs map to the remote
  46. # caused by: git branch --set-upstream-to=...
  47. if origin_url=$(git remote get-url origin); then
  48. new_origin_name=""
  49. git remote show | grep -v -x origin | while read remote; do
  50. remote_url=$(git remote get-url $remote)
  51. if [[ "$origin_url" == "$remote_url" ]]; then
  52. new_origin_name="$remote"
  53. break
  54. fi
  55. done
  56. if [[ -n "$new_origin_name" ]]; then
  57. git remote remove origin
  58. # use the previous "origin" as default remote, so we can just "git pull" and "git push"
  59. for branch in $main_branches; do
  60. git branch --set-upstream-to=$new_origin_name/$branch $branch
  61. fi
  62. fi
  63. fi
  64. fi