织梦CMS - 轻松建站从此开始!

罗索

linux环境中建svn repo的简单的自动化脚本

落鹤生 发布于 2013-09-13 09:39 点击:次 
在openSUSE-12.2, CentOS-6.3, 以及RHEL6.0(均为64bit环境)下简单测试通过:),ubuntu-12.10默认的shell为dash(当然可以指定使用bash),且对 awk的gensub函数不支持,所以该脚本无法在ubuntu下使用(可以用sed实现同样的功能,但懒得弄了,平时也用不到ubuntu的环境
TAG:

年前说给scienceluo写一个这样的脚本,很简单的逻辑,实现的功能为在linux下建一个svn的版本库,考虑的条件也很简单,只能用在平时写代码的时候为了方便临时建个svn的版本库用用~~
在openSUSE-12.2, CentOS-6.3, 以及RHEL6.0(均为64bit环境)下简单测试通过:),ubuntu-12.10默认的shell为dash(当然可以指定使用bash),且对 awk的gensub函数不支持,所以该脚本无法在ubuntu下使用(可以用sed实现同样的功能,但懒得弄了,平时也用不到ubuntu的环境)~
脚本支持建立多个版本库,但如果想要同时访问,则多个版本库要在相同的父目录下,否则的话就只能访问最新建的那个版本库了,因为启动svnserve服务器时指定的服务器根目录参数填的是用户最新建的那个svn版本库路径的父目录;
还一个需要注意的地方,建立的版本库必须用svn://127.0.0.1/test_repo类似这样的路径来访问,注意红色的svn://前缀,因为服务器用的是subversion自带的独立服务器~

  1. #! /bin/sh 
  2.  
  3. # author runsisi@hust.edu.cn 
  4. # date 2013/02/05 
  5. # modified 2013/02/12 
  6.  
  7. # variables forward declaration, it is not neccessary for shell script though 
  8. svn_repo_root_dir="" 
  9.  
  10. function is_subversion_exist() 
  11.     which svnadmin > /dev/null 2>&1 
  12.     [ $? -eq 0 ] && return 1 
  13.     return 0 
  14.  
  15. function is_sys_root_dir() 
  16.     echo $1 | awk '{if ($0 ~ /^\/$/) exit 1; else exit 0;}' 
  17.  
  18. function is_relative_path() 
  19.     echo $1 | awk '{if ($0 ~ /^\//) exit 0; else exit 1;}' 
  20.  
  21. function get_svn_root_dir() 
  22.     read -e -p "输入要创建的svn根目录:" svn_repo_root_dir 
  23.     [ -z "$svn_repo_root_dir" ] && echo "非法路径,abort!" && return 1 
  24.     # whitespaces at each end of the path are trimed 
  25.     svn_repo_root_dir=$(echo "$svn_repo_root_dir" | sed 's/^[[:blank:]]*\(\)[[:blank:]]*$/\1/g'
  26.     is_sys_root_dir "$svn_repo_root_dir" 
  27.     [ $? -eq 1 ] && echo "非法路径,不能是根目录,abort!" && return 1 
  28.     # get rid of last / character(s) in path 
  29.     svn_repo_root_dir=$(echo "$svn_repo_root_dir" | sed 's/\(\)\/*$/\1/g'
  30.     # escape whitespaces inside path 
  31.     svn_repo_root_dir=$(echo "$svn_repo_root_dir" | sed 's/ /\\ /g'
  32.     # expand ~ character in path 
  33.     eval svn_repo_root_dir="$svn_repo_root_dir" 
  34.     # test if this path already exist 
  35.     [ -e "$svn_repo_root_dir" ] && echo "该svn路径已存在,请删除后再创建,abort!" && return 1 
  36.     # test if this path is a relative path or not 
  37.     is_relative_path "$svn_repo_root_dir" 
  38.     # convert to absolute path 
  39.     [ $? -eq 1 ] && svn_repo_root_dir=$(pwd)/$svn_repo_root_dir 
  40.     return 0 
  41.  
  42. function create_svn_repo() 
  43.     mkdir -p $(dirname "$1"
  44.     svnadmin create "$1" 
  45.     return 0 
  46.  
  47. function edit_svn_conf_file() 
  48.     svn_repo_conf_dir="$1/conf" 
  49.     awk '{ 
  50.         if ($0 ~ /^#[[:blank:]]*anon-access/) 
  51.             print gensub(/^#[[:blank:]]*(anon-access)/, "\\1", "g"); 
  52.         else if ($0 ~ /^#[[:blank:]]*auth-access/) 
  53.             print gensub(/^#[[:blank:]]*(auth-access)/, "\\1", "g"); 
  54.         else if ($0 ~ /^#[[:blank:]]*password-db/) 
  55.             print gensub(/^#[[:blank:]]*(password-db)/, "\\1", "g"); 
  56.         else print;}' "$svn_repo_conf_dir/svnserve.conf" > "$svn_repo_conf_dir/tmp_svnserve.conf" 
  57.     rm -f "$svn_repo_conf_dir/svnserve.conf" 
  58.     mv "$svn_repo_conf_dir/tmp_svnserve.conf" "$svn_repo_conf_dir/svnserve.conf" 
  59.     vi "$svn_repo_conf_dir/passwd" 
  60.     return 0 
  61.  
  62. function start_svn_server() 
  63.     killall -9 svnserve 2&> /dev/null 
  64.     svnserve -d -r $(dirname "$1"
  65.     return 0 
  66.  
  67. function main() 
  68.     is_subversion_exist 
  69.     [ $? -eq 0 ] && echo "subversion不存在,需要先安装subversion" && return 1 
  70.     get_svn_root_dir 
  71.     [ ! $? -eq 0 ] && return 1 
  72.     create_svn_repo "$svn_repo_root_dir" 
  73.     [ ! $? -eq 0 ] && return 1 
  74.     edit_svn_conf_file "$svn_repo_root_dir" 
  75.     [ ! $? -eq 0 ] && return 1 
  76.     start_svn_server "$svn_repo_root_dir" 
  77.     [ ! $? -eq 0 ] && return 1 
  78.     return 0 
  79.  
  80. main 

 

(runsisi)
本站文章除注明转载外,均为本站原创或编译欢迎任何形式的转载,但请务必注明出处,尊重他人劳动,同学习共成长。转载请注明:文章转载自:罗索实验室 [http://www.rosoo.net/a/201309/16741.html]
本文出处:网络 作者:runsisi
顶一下
(0)
0%
踩一下
(0)
0%
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 验证码:点击我更换图片
栏目列表
将本文分享到微信
织梦二维码生成器
推荐内容