gitUpload.sh 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #!/bin/bash
  2. target="${1:-}"
  3. repo="${2:-}"
  4. branch="${3:-main}"
  5. clone="${4:-}"
  6. [ -n "${target}" ] && [ -n "${repo}" ] || exit 1
  7. tmp="$(mktemp -d)"
  8. trap "rm -rf ${tmp}" EXIT
  9. cd "$tmp"
  10. function createRepo(){
  11. rn="${1:-}"
  12. [ -n "$rn" ] || return 0
  13. resp=`curl -X POST -sSL "${rn%github.com*}api.github.com/user/repos" -d "{\"name\":\"${rn##*/}\"}"`
  14. echo "$resp" |grep '"status":' && return 1 || return 0
  15. }
  16. # init
  17. git config --global init.defaultBranch "$branch"
  18. git config --global pull.rebase true
  19. git config --global user.name "remote"
  20. git config --global user.email "remote@user"
  21. git init
  22. git checkout -b "$branch"
  23. git remote rm origin >/dev/null 2>&1
  24. git remote rm clone >/dev/null 2>&1
  25. [ "$repo" != "-" ] && {
  26. git remote add origin "$repo"
  27. git pull origin "$branch"
  28. }
  29. [ -n "$clone" ] && {
  30. git remote add clone "$clone"
  31. createRepo "$clone"
  32. }
  33. [ -n "${target}" ] && [ "${target}" != "-" ] && {
  34. [ -f "${target}" ] && cp -rf "${target}" "${tmp}"
  35. [ -d "${target}" ] && cp -rf "${target%/}/." "${tmp}"
  36. }
  37. [ "${target}" == "-" ] && read -p "Pause <${tmp}> ..."
  38. git add .
  39. git commit -m `date +'%Y%m%d%H%M%S'`
  40. # git config http.postBuffer 524288000
  41. [ -n "$clone" ] && git push clone "$branch" -f || git push origin "$branch" -f