summaryrefslogtreecommitdiff
path: root/localgit-remote-init-push
blob: c581bde3ee3d05891a87e54e2253050765d44ab0 (plain)
  1. #!/bin/sh
  2. set -eu
  3. PRG=$(basename "$0")
  4. showhelp() {
  5. cat <<EOF
  6. Usage: $PRG PROJECT [GIT_REPO [SSH_CONN [GIT_REMOTE]]]
  7. Initialize remote git, push local git, and set as default remote.
  8. PROJECT basename or relative path for remote git project
  9. GIT_REPO hostname for public git access, or absolute basepath
  10. default: "source." + current domainname
  11. SSH_CONN hostname for ssh, optionally with user + "@" prepended
  12. default: GIT_REPO unless it starts with "/"
  13. GIT_REMOTE name for local repository set
  14. default: "origin"
  15. Full remote path becomes SSH_USER@SSH_HOST:BASEPATH/PROJECT.git
  16. where BASEPATH is resolved as "/srv/git/" + GIT_REPO
  17. or simply GIT_REPO when it begins with "/".
  18. When local repository set is "origin" it is marked as default remote.
  19. Examples:
  20. $PRG myproject
  21. $PRG some/subproject git.example.org me@shell.example.org
  22. $PRG some/subproject /private/path/on/ssh/host me.example.org backup
  23. $PRG some/subproject /private/path/on/local/host
  24. EOF
  25. }
  26. exit1() {
  27. echo >&2 "${1:+ERROR: }${1:-Internal error!}"
  28. exit 1
  29. }
  30. # parse cmdline options
  31. TEMP="`getopt -s sh -o h -l help -n "$PRG" -- "$@"`" || exit1
  32. eval set -- "$TEMP"
  33. while true; do
  34. case "$1" in
  35. -h|--help) showhelp; exit;;
  36. --) shift; break;;
  37. *) exit1;;
  38. esac
  39. done
  40. PROJECT=${1-$(showhelp; exit1 "project name missing")}
  41. GIT_REPO=${2:-source.$(dnsdomainname --domain)}
  42. case "$GIT_REPO" in
  43. /*)
  44. BASEPATH=$GIT_REPO
  45. ;;
  46. *)
  47. GIT_HOST=$GIT_REPO
  48. BASEPATH=/srv/git/"$GIT_REPO"
  49. ;;
  50. esac
  51. SSH_CONN=${3:-$GIT_HOST}
  52. GIT_REMOTE=${4:-origin}
  53. [ origin != "$GIT_REMOTE" ] || ORIGIN_IS_DEFAULT=yes
  54. # TODO: handle /~ BASEPATH specially and stop blindly ignore any error
  55. [ -z "$SSH_CONN" ] || ssh "$SSH_CONN" git init --bare --shared "$BASEPATH"/"$PROJECT".git || true
  56. git remote add "$GIT_REMOTE" "$SSH_CONN":"$BASEPATH"/"$PROJECT".git
  57. git push ${ORIGIN_IS_DEFAULT:+--set-upstream} "$GIT_REMOTE" master