gitUpload.sh 1.3 KB

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