#!/bin/bash
set -x
set -euo pipefail
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'
REPO_URL="https://gitcode.com/openJiuwen/agent-studio.git"
TARGET_DIR="agent-studio"
GIT_BRANCH="${1:-main}"
check_git() {
if ! command -v git &> /dev/null; then
echo -e "${RED}Error: git is not installed, please install git and try again${NC}"
exit 1
fi
}
check_and_handle_dir() {
echo -e "${YELLOW}Checking if target directory [${TARGET_DIR}] exists...${NC}"
if [ -d "${TARGET_DIR}" ]; then
if [ -d "${TARGET_DIR}/.git" ]; then
echo -e "${YELLOW}⚠️ Directory ${TARGET_DIR} already exists and is a git repository!${NC}"
cd "${TARGET_DIR}" || exit 1
CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD 2>/dev/null || echo "unknown")
echo -e "${YELLOW}Current branch: ${CURRENT_BRANCH}, target branch: ${GIT_BRANCH}${NC}"
if [ "$CURRENT_BRANCH" != "$GIT_BRANCH" ]; then
echo -e "${YELLOW}Branches don't match, switching to ${GIT_BRANCH} branch...${NC}"
git fetch origin 2>/dev/null || true
if git checkout -b "${GIT_BRANCH}" "origin/${GIT_BRANCH}" 2>/dev/null || git checkout "${GIT_BRANCH}" 2>/dev/null; then
echo -e "${GREEN}✅ Successfully switched to branch ${GIT_BRANCH}${NC}"
git pull origin "${GIT_BRANCH}" 2>/dev/null || true
else
echo -e "${RED}❌ Failed to switch to branch ${GIT_BRANCH}, please check if the branch exists${NC}"
exit 1
fi
else
echo -e "${GREEN}Already on ${GIT_BRANCH} branch, pulling latest code...${NC}"
git pull origin "${GIT_BRANCH}" 2>/dev/null || true
fi
cd - > /dev/null || exit 1
exit 0
else
echo -e "${YELLOW}⚠️ Directory ${TARGET_DIR} already exists but is not a git repository!${NC}"
read -p "Please choose an option: 1=Delete existing directory and re-clone 2=Keep directory and skip cloning(default 2): " OPTION
case "${OPTION:-2}" in
1)
echo -e "${YELLOW}Deleting existing directory ${TARGET_DIR}...${NC}"
rm -rf "${TARGET_DIR}"
echo -e "${GREEN}Directory deleted, preparing to clone repository...${NC}"
;;
2)
echo -e "${YELLOW}Keeping existing directory, skipping cloning operation${NC}"
exit 0
;;
*)
echo -e "${YELLOW}Invalid input, defaulting to keep directory and skip cloning${NC}"
exit 0
;;
esac
fi
else
echo -e "${GREEN}Target directory doesn't exist, can proceed with normal cloning${NC}"
fi
}
clone_repo() {
echo -e "${YELLOW}Starting to clone repository: ${REPO_URL} (branch: ${GIT_BRANCH})${NC}"
git clone -b "${GIT_BRANCH}" "${REPO_URL}" "${TARGET_DIR}"
if [ $? -eq 0 ]; then
echo -e "${GREEN}✅ Repository cloned successfully! Target directory: $(pwd)/${TARGET_DIR}, branch: ${GIT_BRANCH}${NC}"
else
echo -e "${RED}❌ Repository cloning failed, please check network, repository URL or branch name (${GIT_BRANCH})${NC}"
exit 1
fi
}
echo -e "${YELLOW}=== Starting agent-studio repository cloning process (branch: ${GIT_BRANCH}) ===${NC}"
check_git
check_and_handle_dir
clone_repo
echo -e "\n${GREEN}=== Operation completed ===${NC}"
exit 0