init.sh 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #!/bin/bash
  2. # This script is used to initialize the environment for the project.
  3. mkdir -p DragonOS-workspace || exit 1
  4. cd DragonOS-workspace || exit 1
  5. # 检查参数信息
  6. # -f 全部采用默认值
  7. # -m 使用国内镜像
  8. # -h 查看帮助
  9. # --repo 指定仓库地址
  10. # --branch 指定分支
  11. # --use-ssh-after-clone 克隆后,仓库使用ssh协议
  12. # 默认参数
  13. mirror=true
  14. GITHUB_URL=https://github.com/DragonOS-Community/manifest.git
  15. [email protected]:DragonOS-Community/manifest.git
  16. MIRROR_URL=https://git.mirrors.dragonos.org.cn/DragonOS-Community/manifest.git
  17. USE_SSH_AFTER_CLONE=false
  18. BRANCH=master
  19. REPO_URL=$MIRROR_URL
  20. show_help() {
  21. echo "Usage: init.sh [-f] [-m] [--repo <repo_url>]"
  22. echo "Options:"
  23. echo " -f: Use the default value for all parameters."
  24. echo " -m: Use the mirror in China."
  25. echo " -h: Show this help message."
  26. echo " --repo <repo_url>: Specify the repository URL."
  27. echo " --branch <branch>: Specify the branch of manifest."
  28. echo " --use-ssh-after-clone: Use ssh protocol after clone."
  29. }
  30. # 修改manifest中的仓库地址
  31. # $1: 仓库地址
  32. change_manifest_remote() {
  33. # 修改manifest中的仓库地址
  34. echo "Changing manifest remote..."
  35. local current=$(pwd)
  36. echo "Current path: $current"
  37. cd .repo/manifests || (echo "Change manifest remote failed." && exit 1)
  38. git remote set-url origin "$1" || (echo "Change manifest remote failed." && exit 1)
  39. cd "$current" || (echo "Change manifest remote failed." && exit 1)
  40. echo "Change manifest remote success."
  41. repo sync -c || (echo "repo sync failed." && exit 1)
  42. echo "Syncing code success."
  43. }
  44. # 解析参数
  45. while [[ $# -gt 0 ]]; do
  46. case "$1" in
  47. -f)
  48. mirror=true
  49. REPO_URL=$MIRROR_URL
  50. ;;
  51. -m)
  52. mirror=true
  53. REPO_URL=$MIRROR_URL
  54. ;;
  55. -h)
  56. show_help
  57. exit 0
  58. ;;
  59. --repo)
  60. REPO_URL="$2"
  61. shift
  62. ;;
  63. --branch)
  64. BRANCH="$2"
  65. shift
  66. ;;
  67. --use-ssh-after-clone)
  68. USE_SSH_AFTER_CLONE=true
  69. ;;
  70. *)
  71. echo "Unknown option: $1"
  72. show_help
  73. exit 1
  74. ;;
  75. esac
  76. shift
  77. done
  78. # 检查是否安装了repo
  79. if ! command -v repo &> /dev/null; then
  80. echo "repo is not installed. Please install repo first."
  81. exit 1
  82. fi
  83. # 检查是否安装了git
  84. if ! command -v git &> /dev/null; then
  85. echo "git is not installed. Please install git first."
  86. exit 1
  87. fi
  88. # 检查是否安装了curl
  89. if ! command -v curl &> /dev/null; then
  90. echo "curl is not installed. Please install curl first."
  91. exit 1
  92. fi
  93. echo "cwd: $(pwd)"
  94. # 初始化repo
  95. repo init -u "$REPO_URL" -b "$BRANCH" --repo-url=https://mirrors.tuna.tsinghua.edu.cn/git/git-repo/ --no-repo-verify || (echo "repo init failed." && exit 1)
  96. # 同步代码
  97. echo "Syncing code..."
  98. repo sync -c || (echo "repo sync failed." && exit 1)
  99. # 替换manifest中的仓库地址
  100. # 如果设置使用ssh协议,则替换为ssh协议
  101. if [[ "$USE_SSH_AFTER_CLONE" == true ]]; then
  102. change_manifest_remote "$GITHUB_SSH_URL"
  103. else
  104. change_manifest_remote "$GITHUB_URL"
  105. fi