import.sh 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #!/bin/bash
  2. INTERACTIVE=1
  3. REPO_DIR="/gitlab/repositories"
  4. GOGS_URL=""
  5. GOGS_TOKEN=""
  6. GOGS_UID="1"
  7. GOGS_MIRROR="false"
  8. GOGS_PRIVATE="false"
  9. # Ignore gitlab wiki repositories
  10. IGNORE_WIKI=1
  11. #Hack to add directory the repository is in to name
  12. LONG_NAME=1
  13. # Remove trailing .git from directory
  14. REMOVE_TRAILING_GIT=1
  15. import() {
  16. if [[ $IGNORE_WIKI == 1 && $1 == *".wiki.git" ]]; then
  17. return
  18. fi
  19. description=$(<description)
  20. if [[ $LONG_NAME == 1 ]]; then
  21. name=$(basename $(dirname `pwd`))
  22. name+="_$1"
  23. else
  24. name=$1
  25. fi
  26. if [[ $REMOVE_TRAILING_GIT == 1 ]]; then
  27. name="${name%????}"
  28. fi
  29. if [[ $INTERACTIVE == 1 ]]; then
  30. read -n 1 -r -p "Import from "`pwd`" with name: $name (y/n): "
  31. echo ''
  32. if [[ ! $REPLY =~ ^[Yy]$ ]]; then
  33. echo $'Skipping repository\n'
  34. return
  35. fi
  36. fi
  37. curl -k -H "Content-Type: application/json" \
  38. -H "Authorization: token $GOGS_TOKEN" \
  39. -X POST -d '{
  40. "clone_addr": "'`pwd`'",
  41. "uid": '"$GOGS_UID"',
  42. "repo_name": "'"$name"'",
  43. "mirror": '"$GOGS_MIRROR"',
  44. "private": '"$GOGS_PRIVATE"',
  45. "description": "'"$description"'" }' "$GOGS_URL/api/v1/repos/migrate"
  46. echo $'\n'
  47. }
  48. searchdir() {
  49. for d in "$@"; do
  50. test -d "$d" -a \! -L "$d" || continue
  51. cd "$d"
  52. if [ -f "HEAD" ]; then
  53. import $d
  54. else
  55. searchdir *
  56. fi
  57. cd ..
  58. done
  59. }
  60. if [[ -d "$REPO_DIR" ]]; then
  61. cd "$REPO_DIR"
  62. else
  63. exit "Repo directory not found"
  64. fi
  65. searchdir *