-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgitSh.sh
115 lines (88 loc) · 3.26 KB
/
gitSh.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/bin/bash
# 修改当前路径下项目的,各个分支最后提交账户为“shuaishuai.zhu”,防止部分分支无法push到新仓的情况
function change_last_commit_account ()
{
# 遍历所有分支
# for branch in $(git branch --list | grep -v "HEAD detached" | grep "[^* ]+" -oE); do
# # git log --oneline "$branch" ^origin/master
# echo "分支:$branch"
# done
for branch in $(git rev-parse --symbolic --branches); do
# git log --oneline "$branch" ^origin/master
echo "-->分支:$branch"
git checkout $branch
get_cur_branch=`git symbolic-ref --short -q HEAD`
echo "切到分支:$get_cur_branch"
git_last_commit_id=$(git rev-parse HEAD)
git_last_commit_account=$(git log --pretty=format:“%an” ${git_last_commit_id} -1)
echo "最后提交:$git_last_commit_account"
me_git_account='“shuaishuai.zhu”'
echo $me_git_account
if [[ "$git_last_commit_account" != "$me_git_account" ]];then
echo "最近提交者不是我:$git_last_commit_account"
git pull origin $get_cur_branch
#新建一个文件,刷新下提交记录账户
touch "git_push_test_remove_me.txt"
git add .
git commit -m '新建一个文件,刷新下提交记录账户,用于迁移'
git push origin $get_cur_branch
git pull origin $get_cur_branch
fi
done
#切回master
git checkout master
}
function gitlab_old_to_new ()
{
# 读取shell窗口输入的内容并赋值给变量project_name
# read project_name
project_name=$1
old_gitla=$2
new_gitla=$3
echo "~~~~~~~~gitExt~>${project_name} ${old_gitla} ${new_gitla}"
[ ! $project_name ] && return
[ ! $old_gitla ] && return
[ ! $new_gitla ] && return
project_name_path="./gitData/${project_name}"
mkdir ${project_name_path}
git clone $old_gitla ${project_name_path}
# 进入项目目录
cd ${project_name_path}
pwd
# 将项目的所有分支check到本地
git branch -r | grep -v '\->' | while read remote; do git branch --track "${remote#origin/}" "$remote"; done
# 下面的fetch 和 pull应该执行一条即可,自行测试
# git fetch --all
git pull --all
pwd
#修改各个分支最近为我的
change_last_commit_account
git remote -v
git remote remove origin
git remote add origin ${new_gitla}
git remote -v
# gitlab地址替换成为新gitlab地址
# git remote set-url origin ${new_gitla}:
# git add .
# git commit -m 'git新库迁移' --amend --reset-author --no-edit
# 将所有分支push到新的远程服务器
git push --all
# 推送标签
git push --tags
cd "../.."
pwd
}
# #### 遍历json转为数组
# 目标数据格式: projects = ('url1 newurl1' url2 newurl2')
# configs=`cat gitConfig.json`
count=$( jq '. | length' gitConfig.json)
for ((i = 0; i < $count; i++))
do
# 使用jq工具从json中逐项提取url,jq测试https://jqplay.org/ -r取消双引号保留原始数据
project_name=$(cat gitConfig.json | jq -r ".[${i}].project_name")
old_gitla=$(cat gitConfig.json | jq -r ".[${i}].old_gitla")
new_gitla=$(cat gitConfig.json | jq -r ".[${i}].new_gitla")
# echo "--${project_name} --- ${old_gitla} ---- ${new_gitla}"
# 执行clone&push 略
gitlab_old_to_new ${project_name} ${old_gitla} ${new_gitla}
done