吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 661|回复: 9
上一主题 下一主题
收起左侧

[其他原创] 同步切换多个仓库分支的git脚本

  [复制链接]
跳转到指定楼层
楼主
水木杉 发表于 2024-8-22 09:19 回帖奖励
本帖最后由 水木杉 于 2024-8-22 09:26 编辑

  背景:一个软件有三个仓库,每个仓库都是编译出一个dll,现在这个软件有两个版本,需要经常切换这些仓库的版本,于是就让GPT帮忙写了这个脚本,功能就是配置好两个版本对应的分支名称,然后实现一键同步切换,代码放出来供大家参考,还有什么其他方案也欢迎讨论。
  使用:脚本可以传参1、2,1表示切换到第一个版本,2表示切换到第二个版本;不传参的话会切换到当前版本相对的另一个版本;如果有未提交更改会有10s提示,不操作就把修改还原后再切换;我是创建了两个快捷方式,分别传递1和2的参数,点快捷方式切换到相应的版本。
[Shell] 纯文本查看 复制代码
#!/bin/bash

# 定义仓库目录和仓库名称
REPO_DIR="xxxxxx"
REPOS=("仓库1" "仓库2" "仓库3") # 修改为你的仓库名称

# 定义版本分支
VERSION_1_BRANCHES=("仓库1_版本1" "仓库2_版本1" "仓库3_版本1")
VERSION_2_BRANCHES=("仓库1_版本2" "仓库2_版本2" "仓库3_版本2")

# 检查是否有未提交的更改
function check_uncommitted_changes {
    if git diff-index --quiet HEAD -- || git diff-files --quiet; then  
        return 1
    else
        return 0
    fi
}

# 等待用户输入决定是否提交或丢弃更改
function prompt_uncommitted_changes {
    echo "There are uncommitted changes in $1."
    echo "Please commit your changes or they will be discarded in 10 seconds..."
    read -t 10 -p "Press Enter to continue or Ctrl+C to cancel..."

    if [ $? -gt 128 ]; then
        echo "No input received. Discarding changes..."
        git reset --hard
    fi
}

# 切换所有仓库到指定的版本分支
function switch_to_version_branches {
    local version=$1

    # 根据传入的版本参数选择目标分支
    if [ "$version" == "1" ]; then
        target_branches=("${VERSION_1_BRANCHES[@]}")
    elif [ "$version" == "2" ]; then
        target_branches=("${VERSION_2_BRANCHES[@]}")
    else
        # 获取第一个仓库的当前分支
        cd "$REPO_DIR/${REPOS[0]}"
        current_branch=$(git rev-parse --abbrev-ref HEAD)

        # 检查当前分支是否在 VERSION_1_BRANCHES 或 VERSION_2_BRANCHES 中
        is_version_1=false
        for branch in "${VERSION_1_BRANCHES[@]}"; do
            if [ "$current_branch" == "$branch" ]; then
                is_version_1=true
                break
            fi
        done

        # 如果第一个仓库的分支在 VERSION_1_BRANCHES 中,则切换到 VERSION_2_BRANCHES
        if $is_version_1; then
            target_branches=("${VERSION_2_BRANCHES[@]}")
        else
            # 检查是否在 VERSION_2_BRANCHES 中
            is_version_2=false
            for branch in "${VERSION_2_BRANCHES[@]}"; do
                if [ "$current_branch" == "$branch" ]; then
                    is_version_2=true
                    break
                fi
            done

            # 如果在 VERSION_2_BRANCHES 中,则切换到 VERSION_1_BRANCHES
            if $is_version_2; then
                target_branches=("${VERSION_1_BRANCHES[@]}")
            else
                # 默认使用 VERSION_1_BRANCHES
                target_branches=("${VERSION_1_BRANCHES[@]}")
            fi
        fi
    fi

    # 遍历所有仓库并切换到目标分支
    for (( i=0; i<${#REPOS[@]}; i++ )); do
        repo="${REPOS[$i]}"
        cd "$REPO_DIR/$repo"

        # 检查是否有未提交的更改
        if check_uncommitted_changes; then
            prompt_uncommitted_changes "$repo"
        fi

        target_branch="${target_branches[$i]}"

        echo "Switching $repo from $(git rev-parse --abbrev-ref HEAD) to $target_branch"
        git checkout "$target_branch" || { 
            echo "Failed to switch $repo to $target_branch, rolling back..."
            git checkout -
            exit 1
        }

        # 如果需要,可以添加 git pull 来确保分支是最新的
        git pull origin "$target_branch"

        cd ..
    done

    echo "All repositories have been switched to the specified version branches."
}

# 调用函数并传递参数
if [ $# -eq 0 ]; then
    switch_to_version_branches
else
    switch_to_version_branches $1
fi

免费评分

参与人数 1吾爱币 +7 热心值 +1 收起 理由
苏紫方璇 + 7 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

沙发
Andy1024 发表于 2024-8-22 09:24
linux交叉编译dll文件?
3#
 楼主| 水木杉 发表于 2024-8-22 09:27 |楼主
Andy1024 发表于 2024-8-22 09:24
linux交叉编译dll文件?

用的windows,也没啥交叉编译,都是独立的dll
4#
Andy1024 发表于 2024-8-22 09:33
5#
anorith 发表于 2024-8-22 09:46
正好要学习git
6#
Gijia 发表于 2024-8-22 09:49
最近的学习的要点来了
7#
xiaolai1995 发表于 2024-8-22 09:57
正好要学习git
8#
不负韶华 发表于 2024-8-22 10:01
用idea来管理挺好的呀
9#
wpdzdx 发表于 2024-8-22 13:05
请问有什么好用的仓库管理软件吗
10#
zhanghaixin110 发表于 2024-8-23 21:41
学习学习
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-9-21 09:50

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表