summaryrefslogtreecommitdiff
path: root/localgit-remote-init-push
blob: 1f5717a2152411f30be23d6548f3e74af9314ca7 (plain)
  1. #!/bin/sh
  2. set -eu
  3. PRG=$(basename "$0")
  4. showhelp() {
  5. cat <<EOF
  6. Usage: $PRG PROJECT [GIT_HOST [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_HOST hostname for public git access
  10. default: "source." + current domainname
  11. SSH_CONN hostname for ssh, optionally with user + "@" prepended
  12. default: GIT_HOST
  13. GIT_REMOTE name for local repository set
  14. default: "origin"
  15. Full remote path becomes SSH_USER@SSH_HOST:/srv/git/GIT_HOST/PROJECT.git
  16. When local repository set is "origin" it is marked as default remote.
  17. Examples:
  18. $PRG myproject
  19. $PRG some/subproject git.example.org me@shell.example.org
  20. EOF
  21. }
  22. exit1() {
  23. echo >&2 "${1:+ERROR: }${1:-Internal error!}"
  24. exit 1
  25. }
  26. # parse cmdline options
  27. TEMP="`getopt -s sh -o h -l help -n "$PRG" -- "$@"`" || exit1
  28. eval set -- "$TEMP"
  29. while true; do
  30. case "$1" in
  31. -h|--help) showhelp; exit;;
  32. --) shift; break;;
  33. *) exit1;;
  34. esac
  35. done
  36. PROJECT=${1-$(showhelp; exit1 "project name missing")}
  37. GIT_HOST=${2:-source.$(dnsdomainname --domain)}
  38. SSH_CONN=${3:-$GIT_HOST}
  39. GIT_REMOTE=${4:-origin}
  40. [ origin != "$GIT_ORIGIN" ] || ORIGIN_IS_DEFAULT=yes
  41. ssh "$SSH_CONN" git init --bare --shared /srv/git/"$GIT_HOST"/"$PROJECT".git
  42. git remote add "$GIT_REMOTE" "$SSH_CONN":/srv/git/"$GIT_HOST"/"$PROJECT".git
  43. git push ${ORIGIN_IS_DEFAULT:+--set-upstream "$GIT_REMOTE"} master