1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
编码的问题
# !/usr/bin/env python
# coding:utf-8
import  sys,urllib
default_encoding  =  'utf-8'
if  sys.getdefaultencoding() ! =  default_encoding:
     reload (sys)
     sys.setdefaultencoding(default_encoding)
=  '项目迁移到'
print  sys.stdin.encoding
print  urllib.quote(s.decode(sys.stdin.encoding).encode( 'gbk' ))
print  urllib.quote(s.decode(sys.stdin.encoding).encode( 'utf8' ))
print  urllib.quote(s.decode(sys.stdin.encoding).encode( 'gb2312' ))
print  urllib.quote(s.decode( 'utf-8' ).encode( 'utf8' ))
 
str  =  '项目迁移到'
str  =  str .encode( 'utf8' )
=  { 'name' : str }
=  urllib.urlencode(d)
print  q
unicode_str  =  unicode ( '中文' , encoding = 'utf-8' )
print  unicode_str.encode( 'utf-8' )



参考 https://blog.phpgao.com/svn_pre_commit.html


检查提交日志是否为空

检查提交日志最少需要N个字符

检查提交文件是否是UTF-8格式

检查新文件的换行模式是否为LF

检查提交的文件是否含有TABs换行符




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
#!/bin/bash
 
REPOS = "$1"
TXN = "$2"
 
 
# Make sure that the log message contains some text.
SVNLOOK = / usr / bin / svnlook
ICONV = / usr / bin / iconv
 
SVNLOOKOK = 1
LOGMSG = `$SVNLOOK log  - "$TXN"  "$REPOS"  | wc  - c` 
if  "$LOGMSG"  - lt  2  ];
then
    echo  - "\t That logmessage contains at least 2 alphanumeric characters. Commit aborted!"  1 >& 2
   exit  1
fi
 
 
# Make sure that all files to be committed are encoded in UTF-8.
while  read changeline; 
do
 
   # Get just the file (not the add / update / etc. status).
   file = ${changeline: 4 }
 
   # Only check source files.
   if  [[ $ file  = =  * .java || $ file  = =  * .xhtml || $ file  = =  * .css || $ file  = =  * .xml || $ file  = =  * .js ]] ; then
     $SVNLOOK cat  - "$TXN"  "$REPOS"  "$file"  | $ICONV  - f UTF - 8  - t UTF - 8  - / dev / null
     if  "${PIPESTATUS[1]}"  ! =  0  ] ; then
       echo  "Only UTF-8 files can be committed (" $ file ")"  1 >& 2
       exit  1
     fi
   fi
done < <($SVNLOOK changed  - "$TXN"  "$REPOS" )
 
# Check files for svn:eol-style property
# Exit on all errors.
set  - e
EOL_STYLE = "LF"
echo  "`$SVNLOOK changed -t " $TXN " " $REPOS "`"  while  read REPOS_PATH
do
  if  [[ $REPOS_PATH  = ~ A[[:blank:]]{ 3 }(. * )\.(java|css|properties|xhtml|xml|js) ]]
  then
   if  [ ${ #BASH_REMATCH[*]} -ge 2 ]
     then
   FILENAME = ${BASH_REMATCH[ 1 ]}.${BASH_REMATCH[ 2 ]};
 
   # Make sure every file has the right svn:eol-style property set
    if  [ $EOL_STYLE ! =  "`$SVNLOOK propget -t \"$TXN\" \"$REPOS\" svn:eol-style \"$FILENAME\" 2> /dev/null`"  ]
     then
     ERROR = 1 ;
       echo  "svn ps svn:eol-style $EOL_STYLE \"$FILENAME\""  >& 2
    fi
   fi
  fi
  test  - z $ERROR || (echo  "Please execute above commands to correct svn property settings. EOL Style LF must be used!"  >&  2 ; exit  1 )
done
 
 
 
# Block commits with tabs
# This is coded in python
# Exit on all errors
set  - e
 
$SVNLOOK diff  - "$TXN"  "$REPOS"  | python  / dev / fd / 3  3 << 'EOF'
import  sys
ignore  =  True
SUFFIXES  =  ".java" ".css" ".xhtml" ".js" ".xml" ".properties"  ]
filename  =  None
 
for  ln  in  sys.stdin:
 
     if  ignore  and  ln.startswith( "+++ " ):
         filename  =  ln[ 4 :ln.find( "\t" )].strip()
         ignore  =  not  reduce ( lambda  x, y: x  or  y,  map ( lambda  x: filename.endswith(x), SUFFIXES))
 
     elif  not  ignore:
         if  ln.startswith( "+" ):
 
            if  ln.count( "\t" ) >  0 :
               sys.stderr.write( "\n*** Transaction blocked, %s contains tab character:\n\n%s"  %  (filename, ln))
               sys.exit( 1 )
 
         if  not  (ln.startswith( "@" or  \
            ln.startswith( "-" or  \
            ln.startswith( "+" or  \
            ln.startswith( " " )):
 
            ignore  =  True
 
sys.exit( 0 )
EOF
 
# All checks passed, so allow the commit.
exit  0


使用Python写服务器端的SVN Hook

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
#!/usr/bin/env python
# -*- coding: utf-8 -*- 
 
import  sys
import  os
import  re
 
def  main(argv):
     # 从参数中取出来代码库和事务信息
     (repos, txn)  =  argv
     # svnlook log拿到的是用户提交时填写的log信息,然后随便你想做什么
     message  =  " ".join(os.popen(" svnlook log  '%s'  - '%s' %  (repos, txn)).readlines()).strip()        
 
     # svnlook changed拿到的是用户提交的文件,这个列表中有文件的状态(A,U,D之类的)和文件名
     changelist  =  os.popen( "svnlook changed '%s' -t '%s'"  %  (repos, txn)).readlines()
 
     # svnlook cat能读出用户提交文件的内容
     fileContent  =  " ".join(os.popen(" svnlook cat  - % % % s"  %  (txn, repos, filename)).readlines()).strip()
 
     # 返回
     if  msg  = =  '':
         sys.exit( 0 )
     else :
         # 输出返回信息
         sys.stderr.write(msg)
         sys.exit( 1 )    
 
if  __name__  = =  "__main__" :
     # sys.stderr.write(os.getcwd())
     # 发现默认的工作目录竟然是跟目录“/”,这里要换一下当前工作目录
     os.chdir(sys.path[ 0 ])
     # sys.stderr.write(os.getcwd())
     main(sys.argv[ 1 :])




我自己做的,我只是提供给接口判断

cat pre-commit

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#!/bin/bash
REPOS = "$1"
TXN = "$2"
RES = "OK"
SVNLOOK = '/svn/csvn/bin/svnlook'
USER = `$SVNLOOK author  - t $TXN $REPOS`
LOG = `$SVNLOOK log  - "$TXN"  "$REPOS" `
var = `python  / svn / csvn / data / repositories / xx / hooks / check.py  "${LOG}"  "${REPOS}"  "${USER}"  "${REPOS}" `
if  "$var"  = =  "success"  ];then
exit  0
else
echo  "$result"  1 >& 2
exit  1
fi


shell 

curl -v -L -G --data-urlencode 'wd=手机' "spacer.gifhttp://www.baidu.com/s" 

curl -s -d



cat check.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# !/usr/bin/env python
# coding:utf-8
import  urllib
import  urllib2
import  sys
message  =  sys.argv[ 1 ]
repos  =  sys.argv[ 2 ]
user = sys.argv[ 3 ]
project = sys.argv[ 4 ]
def  postHttp(message = None ,repos = None ,user = None ,project = None ):
   url =
   #上面是java接口,去判断和过滤提交的信息 
   postdata = dict (message = message,repos = repos,user = user,project = project)
   postdata = urllib.urlencode(postdata)
   request  =  urllib2.Request(url,postdata)
   response = urllib2.urlopen(request)
   print  response.read()
#postHttp('a b','/svn/csvn/data/repositories/xx','test','/svn/csvn/data/repositories/xx')
#'a b' 中间空格,意味着提交可以有空格字符
postHttp(message,repos,user,project)


另外说下post几个参数

#REPOS="$1"

#REV="$2"

#AUTHOR=$(svnlook author -r $REV $REPOS)

#MESSAGE=$(svnlook log $REPOS -r $REV)

#MESSAGE=$(svnlook propget --revprop -r $REV $REPOS svn:log)


具体实例

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
cat pre - commit.sh
cd  / svn / csvn / data / repositories /
for  in  `ls  - l|grep ^d|awk  '{print $NF}' `
do
yes|cp  - / home / back / pre - commit . / $i / hooks /
yes|cp  - / home / back / check.py . / $i / hooks /
sed  - 's/sadoc/$i/g'  . / $i / hooks / check.py
done
cd  / svn / csvn / data / repositories / sadoc / hooks
rm  - rf pre - commit
 
cat pre - commit
#!/bin/bash
REPOS = "$1"
TXN = "$2"
RES = "OK"
SVNLOOK = '/svn/csvn/bin/svnlook'
USER = `$SVNLOOK author  - t $TXN $REPOS`
LOG = `$SVNLOOK log  - "$TXN"  "$REPOS" `
var = `python  / svn / csvn / data / repositories / sadoc / hooks / check.py  "${LOG}"  "${REPOS}"  "${USER}"  "${REPOS}" `
if  "$var"  = =  "success"  ];then
exit  0
else
echo  "$var"  1 >& 2
exit  1
fi
 
cat check.py
# !/usr/bin/env python
# coding:utf-8
import  urllib
import  urllib2
import  sys
message  =  sys.argv[ 1 ]
repos  =  sys.argv[ 2 ]
user = sys.argv[ 3 ]
project = sys.argv[ 4 ]
def  postHttp(message = None ,repos = None ,user = None ,project = None ):
   url = "http://xx/hooks/svn/pre-commit/verify"
   postdata = dict (message = message,repos = repos,user = user,project = project)
   postdata = urllib.urlencode(postdata)
   request  =  urllib2.Request(url,postdata)
   response = urllib2.urlopen(request)
   print  response.read()
 
 
#postHttp('xx-1 将git上项目迁移到svn上','/svn/csvn/data/repositories/ued-resource','xx','/svn/csvn/data/repositories/ued-resource')
postHttp(message,repos,user,project)


本文转自 liqius 51CTO博客,原文链接:http://blog.51cto.com/szgb17/1936734,如需转载请自行联系原作者