以前写脚本有问题。现在已经解决了。
( }5 ~* G" h2 m) |#!/bin/sh7 K- R: J0 i8 j8 ~6 t, s
2 ~% E! |3 k5 A0 p3 |" s) g- M/ o5 L# PRE-COMMIT HOOK
& ` k% m; c& W1 X#
5 B! j# ?& ]; y- a1 k# The pre-commit hook is invoked before a Subversion txn is
+ u% D( q5 U# w' j; y# committed. Subversion runs this hook by invoking a program* g$ U# B! a4 \7 o R
# (script, executable, binary, etc.) named 'pre-commit' (for which7 y+ o: V z5 I8 X2 n2 E7 z
# this file is a template), with the following ordered arguments:% f, d) N4 G' d" f `$ ~
#
. z2 S3 m B+ G# [1] REPOS-PATH (the path to this repository)8 e' Q$ k9 j# @9 ^4 U8 W
# [2] TXN-NAME (the name of the txn about to be committed). G* b; G" K% E8 H- {
#' b7 r5 B" x) e8 o/ M3 T/ J
# The default working directory for the invocation is undefined, so& G5 d; Q4 N+ A2 n
# the program should set one explicitly if it cares.6 D" K/ L0 l0 Q
#
5 d, T7 G: Y: M* E- F# If the hook program exits with success, the txn is committed; but
; A2 Y9 G" V) q: @# if it exits with failure (non-zero), the txn is aborted, no commit
9 ~& X% S6 x. K r( R$ O# takes place, and STDERR is returned to the client. The hook
/ C9 H' p7 p6 ~# program can use the 'svnlook' utility to help it examine the txn.
$ u2 k) w; J _, i% Q0 o3 x" k#
: [5 L& Y S9 |& c- ~' k# On a Unix system, the normal procedure is to have 'pre-commit'
, ?+ R; k! C4 c4 z" l( p8 J- l' q# invoke other programs to do the real work, though it may do the
: {( S7 O0 k; z: g1 p" R$ f# work itself too.; J n5 c/ n0 }
#9 X% p' z& u! j0 z8 p( S
# *** NOTE: THE HOOK PROGRAM MUST NOT MODIFY THE TXN, EXCEPT ***
0 a0 n6 g5 S% T% @: R9 L0 p# *** FOR REVISION PROPERTIES (like svn:log or svn:author). ***
/ j& @3 Z0 `+ I9 ?#
@8 x, [9 G5 e+ p' P+ G* z# \# This is why we recommend using the read-only 'svnlook' utility.' R m) j* x* v
# In the future, Subversion may enforce the rule that pre-commit7 L9 V9 A6 S7 s! a# a: h3 d
# hooks should not modify the versioned data in txns, or else come
6 A, [% J; `. h: g* F7 J# up with a mechanism to make it safe to do so (by informing the
* m; {4 w A9 z! |# committing client of the changes). However, right now neither
+ D, v0 U/ A! B# x- R# mechanism is implemented, so hook writers just have to be careful.0 V5 L# m9 ?7 u; O# v3 W% E
#
x4 f' q( N9 k2 F* y! [# Note that 'pre-commit' must be executable by the user(s) who will" m/ y9 C/ O" w- A$ ?* a2 B. {4 V! r
# invoke it (typically the user httpd runs as), and that user must
4 Z1 K5 m' U u# have filesystem-level permission to access the repository.4 V* N j/ a8 d: ^- a" d
#5 k3 L1 q" N; k! y j/ I7 i3 Q5 J
# On a Windows system, you should name the hook program8 ~; N( h7 s3 ~0 ^& M
# 'pre-commit.bat' or 'pre-commit.exe',
. h# e K6 \# U1 i% |5 Y% @0 h# but the basic idea is the same.* G1 t# E* i* d$ Q9 R/ h4 z" p7 O8 i, m; z2 @
#: y4 K+ f( ^! M* ^$ w9 Z
# The hook program typically does not inherit the environment of
6 }* m7 H1 m4 k0 H1 q) N+ _# its parent process. For example, a common problem is for the/ {8 `1 u' i" V' \% s# l/ z A
# PATH environment variable to not be set to its usual value, so
2 R) B8 ?# T' q8 J f# that subprograms fail to launch unless invoked via absolute path.
/ S# ?5 T" i0 y# W# If you're having unexpected problems with a hook program, the
5 |* p W& K) j! b. r# culprit may be unusual (or missing) environment variables.
0 Q" ? U/ Q; @! x P: M#
- s' [; X U" |, X0 o1 p* B% G# Here is an example hook script, for a Unix /bin/sh interpreter.0 y) s/ M, K0 m4 z9 M: q, s
# For more examples and pre-written hooks, see those in
* t9 s( c1 u$ j# the Subversion repository at" s" N3 g' a# O: z+ Y6 D b! X
# http://svn.collab.net/repos/svn/trunk/tools/hook-scripts/ and
$ N9 S" t% T% M; @3 j# http://svn.collab.net/repos/svn/trunk/contrib/hook-scripts/+ {$ l0 N- n3 `* [, D& ]. ~, }
( U, m8 p' o" W# q$ e. v* z6 d/ Z4 }) \
#REPOS="$1"* s& B2 B' k* f6 H0 `
#TXN="$2"
' P) _+ D# h+ y; f#RES="OK"* b. F' o% P6 c, O" [! b# ?
* G* T$ K# y3 A& D% J& N9 Z' `
# Make sure that the log message contains some text.: u/ u0 w/ U/ E# P$ F
#SVNLOOK=/usr/bin/svnlook2 |2 _5 R" r# s/ s
#$SVNLOOK log -t "$TXN" "$REPOS" | \" d, _( w V2 p8 F2 w+ N& V
# grep "[a-zA-Z0-9]" > /dev/null || unset RES
5 P: r* F1 @! L6 t9 M( S8 n
$ K0 N, q( o, [9 ^* `9 e1 u S#if ["$RES" !="OK"]
' D6 {4 q4 Q8 d9 J- b8 D/ d#then
* x2 b7 l- i# G7 F4 x4 d# echo "You must input some commits for you commit" >&2
8 b4 i4 n2 f) L) q! h' V# exit 1
, y( N. g8 I6 y, D6 }. B#fi
& r; f) {& _3 P( p8 P# P
, l/ S" E4 q8 n/ A6 i& S; z2 M' o/ F1 q, o% w! j4 I
# Check that the author of this commit has the rights to perform! Y; c. S/ b# J; Y5 m
# the commit on the files and directories being modified.
: w1 Y5 k# b/ z# Z2 p& b b#commit-access-control.pl "$REPOS" "$TXN" commit-access-control.cfg || exit 1; }% A' r, I, I* p c. l( H( o
+ T3 Y1 @1 ]) U5 Q
# All checks passed, so allow the commit.* U: ]/ U# B/ s5 s
#exit 0
3 r' \; w& H+ Y% L7 D! z- S7 P* L `) z5 T/ v: K! T3 O2 h3 @ v
#!/bin/sh( n9 P; G3 ~6 q/ O+ H
REPOS="$1"/ }! H' \; m# Y% A5 d! S0 F
TXN="$2"
6 ~3 K! ~ N1 `* I, R0 sRES="OK"
8 {! Z; V* e" N) \MAX_SIZE=102400009 v8 z% K; G1 O7 V5 e; e
FILTER='\.(zip|rar|o|obj|tar|gz)$'
& r1 F$ h7 b- B: Y% M J( E
% O" B( J) J9 t. N# Make sure that the log message contains some text.7 a7 K; {/ h- }, z6 q! R
#SVNLOOK=/usr/bin/svnlook
( S( d/ L' w7 F: w2 H& m8 ^ QSVNLOOK=/opt/CollabNet_Subversion/bin/svnlook
& h; g- D0 y3 H$ r" X+ z7 g& h; u& C, o7 t
$SVNLOOK log -t "$TXN" "$REPOS" | egrep "[^[:space:]]+" >/dev/null || unset RES * d9 J" R( P) [0 ?4 w# L. }
if [ "$RES" != "OK" ]
0 `8 W: }; O, Y" l6 e2 o; p+ k; Sthen
. V$ U% M3 e3 ~% l- |! H8 m1 [echo "You must input some comments for you commit" >&2, f6 E3 J# S- T% N' p
exit 1
2 T5 ~# P' O9 M& K- F" R4 gfi
, I- o/ Q, ?' B x0 z& R, \2 |% J: ?" Z& _; ~
files=$($SVNLOOK changed -t $TXN $REPOS |awk '{print $2}')6 A* {- Y f* [% M( y
$ D5 n x8 f8 f ^# ?5 [7 E% qfor f in $files, f' P: l0 E' C# N+ b0 o3 t
do3 y$ m# M3 s! G$ v; [
#check file type
7 J3 D% A1 I1 O% y" d; oif echo $f|tr A-Z a-z|grep -Eq $FILTER
1 q( _' q3 s7 a/ A+ k6 }then7 s: H5 J3 Q0 K) `8 j& y- ?6 {
echo "File $f is not allow ($FILTER) file" >&2' S+ P1 J9 J; D" G
exit 1
6 d) v% T9 y' _) C' @( T( w9 R' vfi# g/ I, w9 Q1 f3 i1 R1 T9 \5 h2 ]0 u
q' s1 R9 y/ j; d& i( Q# j7 s9 T8 k2 ^#check file size$ ^* e+ i) I( q) _5 _
filesize=$($SVNLOOK cat -t $TXN $REPOS $f|wc -c)* k, Y* [' y( c" z% d$ ^
if [ "$filesize" -gt "$MAX_SIZE" ]2 M) P0 b3 A9 @2 `
then4 o+ e1 @) V2 f' V8 Q0 D
echo "File $f is too large(must <=$MAX_SIZE)" >&2$ s9 A3 i+ k* w
exit 1/ A) I! ?) M. b" H
fi: [* h; I3 j$ K% g7 `
done
" t" W! M: t, U
* T* q( t, z) y& x# |# All checks passed, so allow the commit.( A, \4 x; G, a2 C+ Y: u
exit 0