1、svn集中式版本控制系统
2、svn版本控制系统工作流程如下
3、svn服务常见的运行访问方式有3种
4、SVN档案库数据格式
5、SVN客户端访问方式
6、安装
启动
7、常用操作
post-commit 钩子自动更新 /conf/hooks下、并且给予执行权限
pre-commit钩子限制日志长度 /conf/hooks下、并且给予执行权限
pre-commit钩子限制日志长度和提交的文件类型
8、SVN一键安装脚本
windows下载地址:https://www.visualsvn.com/downloads/
1、svn集中式版本控制系统
svn版本控制系统是集中式的数据管理,存在一个中央版本库,所有开发人员本地开发所使用的代码都是来自于这个版本库,提交代码也都必须提交到这个中央版本库
1、在中央库上创建或从主干复制一个分支
2、从中央库check out 下这个分支的代码
3、增加自己的代码文件,修改现存的代码或删除代码文件
4、commit代码,假设有人在刚刚的分支上提交了代码,你就会被提示代码过期,你得先up你的代码后再提交。up代码的时候如果出现冲突,需要解决好冲突 后再进行提交
(1)独立服务器访问:访问地址如:svn://svn.yunjisuan.org/sadoc;
(2)借助apache等http服务:访问地址如:http://svn.yunjisuan.com/sadoc;
a,单独安装apache+svn(不要用)
b,CSVN(apache+svn)是一个单独的整合的软件,带web界面管理的SVN软件(3)本地直接访问(例如:file:///application/svndata/sadoc)
SVN存储版本数据有2种方式:
BDB(一种事务安全型表类型)
FSFS(一种不需要数据库的存储系统)。因为BDB方式在服务器中断时,有可能锁住数据,所以还是FSFS方式更安全一点。
BDB:伯克利DB(Berkeley DB),版本库可以使用的一种经过充分测试的后台数据库实现,不能在通过网络共享的文件系统上使用,伯克利DB是Subversion 1.2版本以前的缺省版本库格式
FSFS:一个专用于Subversion版本库的文件系统后端,可以使用网络文件系统(例如 NFS 或 SMBFS)。是1.2版本及其后的缺省版本库格式。
访问方式 |
说明 |
file:// |
直接通过本地磁盘或者网络磁盘访问版本库 |
http:// |
通过WebDAV协议访问支持Subversion的Apache服务器 |
https:// |
与http://相似,但是用SSL加密访问 |
svn:// |
通过TCP/IP自定义协议访问svnserve服务器 |
svn+ssh:// |
通过认证并加密的TCP/IP自定义协议访问svnserve服务器、可以先建立VPN或IPSEC隧道,然后跑SVN服务 |
6、安装
yum install subversion
rpm -qa subversion
mkdir -p /data/svn/ #创建svn项目目录
svnadmin create /data/svn/test #创建第一个项目
cd /data/svn/test/conf #进入第一个项目目录
[root@test01 conf]# cat svnserve.conf #编辑配置文件
[general]
anon-access = none
auth-access = write
password-db = passwd
authz-db = authz
[sasl]
#编辑密码文件
[root@test01 conf]# cat passwd
### This file is an example password file for svnserve.
### Its format is similar to that of svnserve.conf. As shown in the
### example below it contains one section labelled [users].
### The name and password for each user follow, one account per line.
[users]
# harry = harryssecret
# sally = sallyssecret
linyaohong = 111111
lin = 111111
yao = 111111
hong = 111111
#编辑认证文件
#------------------------定义组的方式-----------------------------
[root@test01 conf]# cat authz
[groups]
admin = lin,yao,hong
user = hong1,hong2,hong3
# [/foo/bar]
# harry = rw
# &joe = r
# * =
# [repository:/baz/fuz]
# @harry_and_sally = rw
# * = r
[test:/]
@admin = rw
@user = r
#--------------------------或者直接定义用户的方式--------------------------
[root@test01 conf]# cat authz
[t448:/]
linyaohong = rw
lin = r
#------------------------另外配置参考--------------------------------------
#用于主账号管理子账号
[test:/]
linyaohong = rw
@admin = rw
@user = r
[test:/www001]
linyaohong = rw
@admin = rw
hong1 = rw
@user = r
[test:/www002]
linyaohong = rw
@admin = rw
hong2 = rw
@user = r
svnserve --listen-port 3690 -d -r /data/svn/
lsof -i :3690 #检查启动情况
ps -ef|grep svn #检查启动情况
7、常用操作
svn import /data/www/ svn://192.168.1.200/test -m "import" #导入目录到SVN
svn checkout file:///data/svn/test/ /home/test/ #导出项目到目录
svn update --username svn368 --password svn368 --no-auth-cache #更新
svn add * --no-ignore --force #一次添加所有文件
svn revert --depth=infinity . #撤销添加的所有文件or恢复所有文件到SVN版本
svn revert stu003/3.txt #撤销提交的文件
svn ci -m "linux add" --username svn368 --password svn368 #提交代码
svn delete test.php #svn删除文件、删除后需要提交哈
svn up -r 66 #svn恢复到66版本
svn log -l 3 -v #查看更新日志 :3是指需要查看的log条数,可根据需要自行修改
post-commit 钩子自动更新 /conf/hooks下、并且给予执行权限
#!/bin/bash
#2018-8-4
#linyaohong
REPOS="$1"
REV="$2"
USER="svnyanshi"
PASSWD="svnyanshi"
DIR="www.czhan.net"
export LANG=en_US.UTF-8
LOGPATH="/home/svnlog/yanshi.log"
SVN=/usr/bin/svn
echo `date +"%Y-%m-%d %H:%M:%S"`>> $LOGPATH
#svn update --set-depth=exclude backup
$SVN update --username $USER --password $PASSWD --no-auth-cache /www/wwwroot/$DIR >> $LOGPATH
if [ $? -eq 0 ];then
chown -R www:www /www/wwwroot/${DIR}
echo "自动更新更成功" >> $LOGPATH
fi
[root@test01 hooks]# cat pre-commit
#!/bin/bash
REPOS="$1"
TXN="$2"
# Make sure that the log message contains some text.
SVNLOOK=/usr/bin/svnlook
#$SVNLOOK log -t "$TXN" "$REPOS" | \
# grep "[a-zA-Z0-9]" > /dev/null || exit 1
LOGMSG=`$SVNLOOK log -t "$TXN" "$REPOS" | grep "[a-zA-Z0-9]" | wc -c`
if [ "$LOGMSG" -lt 5 ];
then
echo -e "您必须输入注释" 1>&2
exit 1
fi
# Check that the author of this commit has the rights to perform
# the commit on the files and directories being modified.
#commit-access-control.pl "$REPOS" "$TXN" commit-access-control.cfg || exit 1
# All checks passed, so allow the commit.
exit 0
[root@test01 hooks]# cat pre-commit
#!/bin/bash
export LANG="zh_CN.UTF-8" #确保中文日志显示正常,便于统计日志
REPOS="$1"
TXN="$2"
#限制日志长度
LENGTH=10
SVNLOOK="/usr/bin/svnlook"
BLACKLIST="target build classes bin/target .* *.iml *.ipr *.iws *.class "
function error_exit(){
echo -e "1.Not allowed empty log " >&2
echo -e "2.Logs must be greater than ${LENGTH} characters or chinese" >&2
exit 1
}
function blacklist_exit(){
if [ ! -z "${INBLACKLIST}" ] ;then
echo -e "some files in the blacklist , please cheack again !" >&2
echo -e "blacklist:\n ${BLACKLIST}" >&2
exit 1
fi
}
# 获取字符数量(包括换行符)
TEMP_LENGTH=`${SVNLOOK} log -t "${TXN}" "${REPOS}"|sed 's/\s*$//g'|sed 's/^\s*//g'| wc --chars`
# 统计换行符
TEMP_LINE=`${SVNLOOK} log -t "${TXN}" "${REPOS}"| wc --lines`
# 真实字符数量
LOGMSG_LENGTH=`expr ${TEMP_LENGTH} - ${TEMP_LINE}`
# 文件黑名单过滤
# 过滤文件夹
INBLACKLIST="`${SVNLOOK} changed -t "${TXN}" "${REPOS}" | grep "/target/"`" #文件夹前后必须有/,否则匹配会有误伤
blacklist_exit
INBLACKLIST="`${SVNLOOK} changed -t "${TXN}" "${REPOS}" | grep "/build/"`"
blacklist_exit
INBLACKLIST="`${SVNLOOK} changed -t "${TXN}" "${REPOS}" | grep "/classes/"`"
blacklist_exit
INBLACKLIST="`${SVNLOOK} changed -t "${TXN}" "${REPOS}" | grep "/bin/target/"`"
blacklist_exit
# 过滤所有已点开头的文件和文件夹
INBLACKLIST="`${SVNLOOK} changed -t "${TXN}" "${REPOS}" | grep "/\."`"
blacklist_exit
# 过滤文件尾缀
INBLACKLIST="`${SVNLOOK} changed -t "${TXN}" "${REPOS}" | grep "\.iml$"`"
blacklist_exit
INBLACKLIST="`${SVNLOOK} changed -t "${TXN}" "${REPOS}" | grep "\.ipr$"`"
blacklist_exit
INBLACKLIST="`${SVNLOOK} changed -t "${TXN}" "${REPOS}" | grep "\.iws$"`"
blacklist_exit
INBLACKLIST="`${SVNLOOK} changed -t "${TXN}" "${REPOS}" | grep "\.class$"`"
blacklist_exit
# 判断日志长度是否满足要求
if [ "${LOGMSG_LENGTH}" -lt ${LENGTH} ];then
error_exit
fi
exit 0
8、SVN一键安装脚本
#!/bin/bash
# Date:2018/11/29
# Author: linyaohong
# BLOG: ************
# Qq:122123498
# Version:1.0
# User: Centos 7
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
[ -f /etc/init.d/functions ] && . /etc/init.d/functions
# Check if user is root
if [ $(id -u) != "0" ];then
echo "Error: You must be root to run this script"
exit 1
fi
# Check if subversion install
if [ $(rpm -qa|grep subversion|wc -l) -gt "1" ];then
action "check subversion" /bin/true
else
yum -y install subversion
fi
# Set infomation
ip=149.129.84.247
Logdir='/var/log/'
Logname=svncreate.log
# "/ " is must'd
svn_dir=/data/svn/
checkout_dir=/data/wwwroot/svn/
#svn_authz=
#svn_user=
#svn_passwd=
[ ! -d ${Logdir} ]&& mkdir -p ${Logdir}
[ ! -d ${svn_dir} ] && mkdir -p ${svn_dir}
# Check subversion install
svn_check=`netstat -lntup|grep svnserve|wc -l`
if [ "${svn_check}" -ge "1" ];then
action "check svnserve" /bin/true
else
action "check svnserve" /bin/false
svnserve -d -r ${svn_dir} >>/dev/null 2>&1
if [ $? = 0 ];then
action "start svnserve" /bin/true
else
action "start svnserve" /bin/false
exit 1
fi
fi
# Input svnserve infomation
read -p "Please input the svn repository name[svn_authz]:" svn_authz
read -p "Please input the svn repository name[svn_user]:" svn_user
read -p "Please input the svn repository name[svn_passwd]:" svn_passwd
# Check input
[ -z "$svn_authz" ] &&{ echo "svn_authz can't empty "; exit 1;}
[ -z "$svn_user" ] &&{ echo "svn_user can't empty "; exit 1;}
[ -z "$svn_passwd" ] &&{ echo "svn_authz can't empty "; exit 1;}
str1=`echo "${svn_authz}" | tr -d '[A-Za-z0-9]'`
if [ -n "${str1}" ];then
echo "svn_authz you must input[A-Za-z0-9]"
exit 1
fi
str2=`echo "${svn_user}" | tr -d '[A-Za-z0-9]'`
if [ -n "${str2}" ];then
echo "svn_user you must input[A-Za-z0-9]"
exit 1
fi
str3=`echo "${svn_passwd}" | tr -d '[A-Za-z0-9]'`
if [ -n "${str3}" ];then
echo "svn_passwd you must input[A-Za-z0-9]"
exit 1
fi
# Check svn_authz does not exist
if [ ! -d ${svn_dir}${svn_authz} ];then
svnadmin create ${svn_dir}${svn_authz}
else
echo svn_authz already exists
exit 1
fi
echo "------------The detailed information-------------">>${Logdir}${Logname}
cd ${svn_dir}${svn_authz}/conf/
echo "[$svn_authz:/]" >> ./authz
echo "$svn_user = rw" >> ./authz
echo "$svn_user = $svn_passwd" >> ./passwd
cat > ${svn_dir}${svn_authz}/conf/svnserve.conf<<EOF
[general]
anon-access = none
auth-access = write
password-db = passwd
authz-db = authz
[sasl]
EOF
cat >${svn_dir}${svn_authz}/hooks/post-commit<<EOF
#!/bin/bash
#2018-8-4
#linyaohong
REPOS="\$1"
REV="\$2"
USER="${svn_user}"
PASSWD="${svn_passwd}"
dir=$checkout_dir
export LANG=en_US.UTF-8
LOGPATH="${Logdir}${svn_authz}.log"
SVN=/usr/bin/svn
echo \`date +"%Y-%m-%d %H:%M:%S"\`>> \$LOGPATH
\$SVN update --username \${USER} --password \${PASSWD} --no-auth-cache \${dir} >> \${LOGPATH}
\#chown -R www:www \${Dir}
if [ \$? -eq 0 ];then
echo "update successfull" >> \$LOGPATH
else
echo "update failed" >> \${LOGPATH}
fi
EOF
echo "date: `date +"%Y-%m-%d %H:%M:%S"`">> ${Logdir}${Logname}
echo "svn_name(PHP): ${svn_user}" >> ${Logdir}${Logname}
echo "svn_passwd: ${svn_passwd}" >> ${Logdir}${Logname}
echo "svn_add: svn://${ip}/${svn_authz}/" >>${Logdir}${Logname}
chmod +x ${svn_dir}${svn_authz}/hooks/post-commit
# Checkout
if [ -d ${checkout_dir} ];then
work=`find ${checkout_dir} -name ".svn"|wc -l`
if [ ${work} != "0" ];then
echo "The working directory already exists"
exit 1
fi
svn import ${checkout_dir} file:///${svn_dir}${svn_authz} -m "import" >>/dev/null 2>&1
if [ $? -eq 0 ];then
echo "import sucessful" >>${Logdir}${Logname}
action "import " /bin/true
else
echo "import failed" >>${Logdir}${Logname}
action "import " /bin/false
exit 1
fi
svn checkout file:///${svn_dir}${svn_authz} ${checkout_dir} >>/dev/null 2>&1
if [ $? -eq 0 ];then
echo "checkout Successful" >>${Logdir}${Logname}
action "checkout " /bin/true
else
echo "svn create ok , checkout error" >>${Logdir}${Logname}
action "checkout " /bin/false
exit 1
fi
cd ${checkout_dir}
svn revert --depth=infinity . >>/dev/null 2>&1
else
svn checkout file:///${svn_dir}${svn_authz} ${checkout_dir} >>/dev/null 2>&1
if [ $? -eq 0 ];then
echo "checkout Successful" >>${Logdir}${Logname}
action "checkout " /bin/true
else
echo "svn create ok , checkout error" >>${Logdir}${Logname}
action "checkout " /bin/false
exit 1
fi
fi
echo "--------------------Eend-------------------------">>${Logdir}${Logname}