# 定义一个函数来处理分支切换和合并
git_merge_to_staging() {
    # 存储当前分支名
    current_branch=$(git symbolic-ref --short HEAD)
    
    # 检查是否在 git 仓库中
    if ! git rev-parse --is-inside-work-tree > /dev/null 2>&1; then
        echo "错误:当前目录不是 git 仓库"
        return 1
    fi
    
    # 检查当前是否在 staging 分支
    if [ "$current_branch" = "staging" ]; then
        echo "当前已经在 staging 分支,无需操作"
        return 1
    fi
    
    # 检查工作区是否干净
    if ! git diff-index --quiet HEAD --; then
        echo "错误:有未提交的更改,请先提交或暂存更改"
        return 1
    fi
    
    # 保存当前分支名到临时文件
    echo $current_branch > /tmp/git_previous_branch
    
    # 切换到 staging 分支
    echo "切换到 staging 分支..."
    if ! git checkout staging; then
        echo "错误:切换到 staging 分支失败"
        return 1
    fi
    
    # 更新 staging 分支
    echo "更新 staging 分支..."
    git pull origin staging
    
    # 合并之前的分支
    echo "合并 $current_branch 分支..."
    if ! git merge $current_branch; then
        echo "警告:合并发生冲突,请手动解决"
        return 1
    fi
    
    echo "操作完成!"
}

# 定义一个函数来返回之前的分支
git_return_to_previous() {
    if [ -f /tmp/git_previous_branch ]; then
        previous_branch=$(cat /tmp/git_previous_branch)
        echo "返回到 $previous_branch 分支..."
        git checkout $previous_branch
        rm /tmp/git_previous_branch
    else
        echo "错误:找不到之前的分支记录"
        return 1
    fi
}

# 创建别名
alias gms='git_merge_to_staging'
alias grp='git_return_to_previous'