使用svn命令合并版本, 一个没有实际使用过的脚本, 备份下

svn_merge.shview raw
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/bin/bash - 
#===============================================================================
#
# FILE: svn_merge.sh
#
# USAGE: ./svn_merge.sh
#
# DESCRIPTION:
# 合并脚本
# 1. 根据版本依次合并
# 2. 遇到冲突使用Ctrl-C中断
# 3. 解决冲突后再次执行,继续合并
#
# OPTIONS: ---
# REQUIREMENTS: ---
# BUGS: ---
# NOTES: ---
# AUTHOR: kingwen0302, kingwen0302@msn.com
# ORGANIZATION:
# CREATED: 2017/5/12 16:15
# REVISION: ---
#===============================================================================

set -o nounset # Treat unset variables as an error

## 配置
SRC_SVN_URL=http://svn_url/server/trunk
SVN_USER="svn_user"
SVN_PASS="svn_pass"

SRC_SVN_URL_BASE=$(basename $SRC_SVN_URL)

CUR_DIR=$(cd $(dirname $0); pwd)
## 已经合并成功的版本号
MERGE_SUCCESS_VERSION=${CUR_DIR}/versions
## 已经合并成功的注释
MERGE_SUCCESS_COMMENT=${CUR_DIR}/comment

export SVN_EDITOR="vim"

function svn_merge_one(){
version=$1
## 如果svn是中文的话,要过滤中文
merge_files=$(svn log -r ${version} -qv ${SRC_SVN_URL} --username ${SVN_USER} --password ${SVN_PASS} | grep -v "^r" | grep -v "^Changed paths:" | grep -v "^-------------" | awk "{print \$1\$2}")

base_merge_files=""
for f in $merge_files; do
if [[ $base_merge_files == "" ]]; then
base_merge_files=$(basename $f)
else
base_merge_files="${base_merge_files} $(basename $f)"
fi
done

if [ "$base_merge_files" != "" ]; then
echo "==>START MERGE ${version} [${base_merge_files}]"
for f in ${merge_files}; do
svn_cmd=$(expr substr $f 1 1)
f=$(echo $f | sed -e "s/^.//" | sed -e "s/${SRC_SVN_URL_BASE}//" | sed -e "s/\/\//\//g" | sed -e "s/\/\//\//g")
local_f=".$f"
case $svn_cmd in
A)
tmp="svn copy --username ${SVN_USER} --password ${SVN_PASS} -r $version ${SRC_SVN_URL}$f $local_f"
;;
M)
## tmp="svn merge --username ${SVN_USER} --password ${SVN_PASS} -r $((version-1)):$version --ignore-ancestry ${SRC_SVN_URL}$f $local_f"
tmp="svn merge --username ${SVN_USER} --password ${SVN_PASS} -c $version --ignore-ancestry ${SRC_SVN_URL}$f $local_f"
;;
D)
tmp="svn del $local_f"
;;
*)
echo "暂不支持的命令"
exit
;;
esac
ttmp=$($tmp)
if [ $? -ne 0 ]; then
echo " [FAIL]$ttmp"
exit
else
echo " [SUCCESS]$f"
fi
done

## 写入合并成功版本号
echo "$version" >> ${MERGE_SUCCESS_VERSION}
## 写入合并成功日志
svn log -r ${version} -v ${SRC_SVN_URL} --username ${SVN_USER} --password ${SVN_PASS} | grep -v "^r" | grep -v "^Changed paths:" | grep -v "^-------------" | grep -v "^$" | grep -v "^ " >> ${MERGE_SUCCESS_COMMENT}
fi
}

if [ $# -eq 0 ]; then
echo "---------------------"
echo "$0 version1:version2 version3 "
echo "---------------------"
exit
fi

while true; do
if [ $# -eq 0 ]; then
break
else

## 确保文件存在
touch ${MERGE_SUCCESS_VERSION}
touch ${MERGE_SUCCESS_COMMENT}
## 获取版本
f_version=$(echo $1 | cut -d : -f 1)
t_version=$(echo $1 | cut -d : -f 2)
for version in $(seq $f_version $t_version); do
## 是否已经合并成功
is_merge_success=0
for v in $(cat ${MERGE_SUCCESS_VERSION}); do
if [ "${v}" == "$version" ]; then
is_merge_success=1
fi
done
if [ $is_merge_success -eq 0 ]; then
svn_merge_one $version
fi
done
fi
shift
done

mv ${MERGE_SUCCESS_VERSION} ${MERGE_SUCCESS_VERSION}_bak
cat ${MERGE_SUCCESS_COMMENT}
mv ${MERGE_SUCCESS_COMMENT} ${MERGE_SUCCESS_COMMENT}_bak

乱码是因为编码问题, 在windows下使用cp936

tor_merge.shview raw
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
#!/bin/bash

##
## kingwen0302
##
## ��һ�����ϲ����뵽������/��֧
## ��windows����, ���˱�����gbk������utf8
## ��Ҫ��װgit for windows
## sh tor_merge.sh ��֧�汾 1,3-4
##

CUR_DIR=$(cd $(dirname $0); pwd)
## �ȶ����ĵ�ַ
## svn�˺�����
SVN_BASE_URL="svn://url/xxx"
SVN_PARAM="--username kingwen0302 --password xxxxx"
## �ϲ���Դ��ַ
SRC_SVN_URL="${SVN_BASE_URL}/trunk_cn_s"
## ��ǰĿ¼���� �ȶ�/��֧ Ŀ¼
RELATIVE_DIR="$CUR_DIR/../"

## ## �����ȶ���/��֧��Ŀ¼
## DEST_DIR_LIST=(
## "$CUR_DIR/../trunk_kr_s"
## "$CUR_DIR/../trunk_tw_s"
## ## "$CUR_DIR/../trunk_cn_s"
## "$CUR_DIR/../trunk_vng_s"
## "$CUR_DIR/../branch_cn/jysy_cn_2102700"
## "$CUR_DIR/../branch_cn/jysy_cn_2102701"
## "$CUR_DIR/../branch_kr/jysy_kr_2102700"
## "$CUR_DIR/../branch_vng/jysy_vn_2102700"
## )

DEST_DIR_LIST=""
function find_branch_list(){
MIN_VER=$1
echo "��Ѱ >=$MIN_VER �汾�ķ�֧ ..."
stable_dir=$(svn list $SVN_BASE_URL $SVN_PARAM | grep trunk_.*_s)
for i in ${stable_dir[@]}; do
echo "�����ȶ���: $RELATIVE_DIR/$i"
DEST_DIR_LIST="$DEST_DIR_LIST $RELATIVE_DIR/$i"
done

branch_dir=$(svn list $SVN_BASE_URL $SVN_PARAM | grep branch_.*)
for i in ${branch_dir[@]}; do
for j in $(svn list $SVN_BASE_URL/$i $SVN_PARAM); do
ver=$(echo $j | grep -Eo '[0-9]+')
if [[ "$ver" == "" ]]; then
ver=0
fi
if [ "$ver" -ge "$MIN_VER" ]; then
echo "���ַ�֧: $RELATIVE_DIR/$i/$j"
DEST_DIR_LIST="$DEST_DIR_LIST $RELATIVE_DIR/$i/$j"
fi
done
done
}

## �ϲ�һ����֧
function svn_merge_one(){
dest_dir=$1
version=$2
## �������ɵ�ַ
dest_dir="$(dirname $dest_dir)/$(basename $dest_dir)"
if [ ! -d "${dest_dir}" ]; then
svn_dir=$(basename $(dirname ${dest_dir}))/$(basename ${dest_dir})
svn co $SVN_PARAM ${SVN_BASE_URL}/$svn_dir $dest_dir
fi
echo "����Ŀ¼: ${dest_dir}" && cd $dest_dir && svn up
TortoiseProc /command:merge /fromurl:$SRC_SVN_URL /revrange:${version/:/-} /path:"*" /closeonend:1
TortoiseProc /command:commit /path:"*" /closeonend:1
}

function svn_merge(){
echo "ԴUrl: ${SRC_SVN_URL}"
echo "�汾: $1"
for dest_dir in ${DEST_DIR_LIST[@]}; do
echo "Ŀ����֧: ${dest_dir}"
read -p "�Ƿ��ϲ�(Y|N):"
case $REPLY in
Y|y)
svn_merge_one $dest_dir $1
;;
*)
echo "��ȡ���˱��κϲ�"
;;
esac
done
}

function help(){
echo "ʹ��1: $0 BRANCH_VERSION ver,ver1:ver2"
echo "ʹ��2: $0 BRANCH_VERSION ver,ver1-ver2"
echo "example: $0 2102700 1000,10002-10005"
}

BRANCH_VERSION=$1
SVN_VERSION=$2
if [[ "$BRANCH_VERSION" == "" || "$SVN_VERSION" == "" ]]; then
help
else
find_branch_list $BRANCH_VERSION
svn_merge ${SVN_VERSION}
fi