#!/bin/bash # This script is used to initialize the environment for the project. mkdir -p DragonOS-workspace || exit 1 cd DragonOS-workspace || exit 1 # 检查参数信息 # -f 全部采用默认值 # -m 使用国内镜像 # -h 查看帮助 # --repo 指定仓库地址 # --branch 指定分支 # --use-ssh-after-clone 克隆后,仓库使用ssh协议 # 默认参数 mirror=true GITHUB_URL=https://github.com/DragonOS-Community/manifest.git GITHUB_SSH_URL=git@github.com:DragonOS-Community/manifest.git MIRROR_URL=https://git.mirrors.dragonos.org.cn/DragonOS-Community/manifest.git USE_SSH_AFTER_CLONE=false BRANCH=master REPO_URL=$MIRROR_URL show_help() { echo "Usage: init.sh [-f] [-m] [--repo ]" echo "Options:" echo " -f: Use the default value for all parameters." echo " -m: Use the mirror in China." echo " -h: Show this help message." echo " --repo : Specify the repository URL." echo " --branch : Specify the branch of manifest." echo " --use-ssh-after-clone: Use ssh protocol after clone." } # 修改manifest中的仓库地址 # $1: 仓库地址 change_manifest_remote() { # 修改manifest中的仓库地址 echo "Changing manifest remote..." local current=$(pwd) echo "Current path: $current" cd .repo/manifests || (echo "Change manifest remote failed." && exit 1) git remote set-url origin "$1" || (echo "Change manifest remote failed." && exit 1) cd "$current" || (echo "Change manifest remote failed." && exit 1) echo "Change manifest remote success." repo sync -c || (echo "repo sync failed." && exit 1) echo "Syncing code success." } # 解析参数 while [[ $# -gt 0 ]]; do case "$1" in -f) mirror=true REPO_URL=$MIRROR_URL ;; -m) mirror=true REPO_URL=$MIRROR_URL ;; -h) show_help exit 0 ;; --repo) REPO_URL="$2" shift ;; --branch) BRANCH="$2" shift ;; --use-ssh-after-clone) USE_SSH_AFTER_CLONE=true ;; *) echo "Unknown option: $1" show_help exit 1 ;; esac shift done # 检查是否安装了repo if ! command -v repo &> /dev/null; then echo "repo is not installed. Please install repo first." exit 1 fi # 检查是否安装了git if ! command -v git &> /dev/null; then echo "git is not installed. Please install git first." exit 1 fi # 检查是否安装了curl if ! command -v curl &> /dev/null; then echo "curl is not installed. Please install curl first." exit 1 fi echo "cwd: $(pwd)" # 初始化repo 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) # 同步代码 echo "Syncing code..." repo sync -c || (echo "repo sync failed." && exit 1) # 替换manifest中的仓库地址 # 如果设置使用ssh协议,则替换为ssh协议 if [[ "$USE_SSH_AFTER_CLONE" == true ]]; then change_manifest_remote "$GITHUB_SSH_URL" else change_manifest_remote "$GITHUB_URL" fi