1 Star 0 Fork 1

Mayfool/gitbash

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
mg.sh 4.33 KB
一键复制 编辑 原始数据 按行查看 历史
Mayfool 提交于 2024-08-04 20:50 . 1、批量合并分支非任务模式
#!/usr/bin/env bash
# -*- coding: utf-8 -*-
#
# 一键切换分支
# @author: 徐宙
# @date: 2020-12-08
# 获取脚本的全路径
script_path="$(realpath $0)"
# 提取脚本所在的目录
bash_dir="$(dirname $script_path)"
base_dir=$(pwd)
source "$bash_dir/git_common.sh"
source "$bash_dir/task_common.sh"
task_mode=1
task_id=
flag=0
task_branch=
env=
from_env=
from_branch=
to_branch=
work_dir=
projects=()
function usage() {
cat "$bash_dir/usage/mg.usage"
}
function merge_branch_with_project() {
project_dir=$1
from_br=$2
to_br=$3
# 打开文件夹
cd "$project_dir" || return $FAILED
curr_dir=$(pwd)
success_log "当前目录:$curr_dir"
# 合并分支
if [ $flag == 1 ]; then
git_merge_branch $from_br $to_br -y
else
git_merge_branch $from_br $to_br
fi
success_log "-----------------------"
success_log
}
function batch_merge_branch() {
for i in "${!projects[@]}";
do
project=${projects[$i]}
from_br=$from_branch
to_br=$to_branch
if [[ "$from_br" == '' ]]; then
from_br=`get_branch $from_env $project`
if [ $? == $FAILED ]; then
# 获取分支有异常,跳过
list_task $task_id
error_log $from_br
exit 1
fi
fi
if [[ "$to_br" == '' ]]; then
to_br=`get_branch $env $project`
if [ $? == $FAILED ]; then
# 获取分支有异常,跳过
list_task $task_id
error_log $to_br
exit 1
fi
fi
merge_branch_with_project $work_dir"/"$project $from_br $to_br
done
}
function main() {
# 解析参数
params=`getopt -o hye:f:t:E:p:w: --long from-branch:,to-branch:,from-env:,projects:,work-dir: -n "$0" -- "$@"`
[ $? != 0 ] && exit 1
eval set -- "$params"
while true ; do
case "$1" in
-h) usage; exit 0 ;;
-y) flag=1; shift ;;
-e) env=$2; shift 2 ;;
-E | --from-env) from_env=$2; shift 2 ;;
-f | --from-branch) from_branch=$2; shift 2 ;;
-t | --to-branch) to_branch=$2; shift 2 ;;
-p | --projects) projects=($2); shift 2 ;;
-w | --work-dir) work_dir=$2; shift 2 ;;
--) shift; break ;;
*) usage; exit 1 ;;
esac
done
if [ $# -lt 1 ]; then
# 非任务模式,可交互执行
task_mode=0
if [ "$work_dir" == '' ]; then
success_log "请输入工作空间:"
read work_dir
if [ "$work_dir" == '' ]; then
usage
exit 0
fi
fi
if [ "$from_branch" == '' ]; then
success_log "请输入源分支:"
read from_branch
if [ "$from_branch" == '' ]; then
usage
exit 0
fi
fi
if [ "$to_branch" == '' ]; then
success_log "请输入目标分支:"
read to_branch
if [ "$to_branch" == '' ]; then
usage
exit 0
fi
fi
if [ ${#projects[@]} == 0 ]; then
success_log "请输入项目列表:"
read val
if [ "$val" == '' ]; then
usage
exit 0
fi
projects=(`echo $val | tr "," "\n"`)
fi
batch_merge_branch
exit 0
else
task_id=$1
get_task $1
# 获取任务级别目标分支
if [[ "$to_branch" == '' ]]; then
to_branch=`get_branch $env`
if [ $? == $FAILED ]; then
# 获取分支有异常,推出
list_task $task_id
error_log $to_branch
exit 1
fi
fi
# 获取任务级别源分支
if [[ "$from_branch" == '' ]]; then
from_branch=`get_branch $from_env`
if [ $? == $FAILED ]; then
# 获取分支有异常,推出
list_task $task_id
error_log $from_branch
exit 1
fi
fi
work_dir=${task_info["work_dir"]}
task_branch=${task_info["task_br"]}
projects=(${task_projects[*]})
batch_merge_branch
exit 0
fi
}
main "$@"
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Shell
1
https://gitee.com/mayfool/gitbash.git
git@gitee.com:mayfool/gitbash.git
mayfool
gitbash
gitbash
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385