七月 302008
 

很多人寫完程式要 commit 的時候會偷懶不寫 log,導致有時候要追問題時,
很難得知到底別人改了什麼,這時候裝個 pre-commit hook 還是蠻有用的

下面這個 pre-commit hook 只是很簡單的不允許空白或不含字母的 commit log,
如果需要的話可以很容易擴充加上更多的判斷.. 記得要 chmod 755 喔

[Subversion pre-commit hook to reject commit with empty log]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#!/usr/bin/perl
 
# pre-commit hook to reject commit with empty log
# remember to chmod 755 on this file
 
die "Usage: $0 [REPOS] [TXN]\n" unless @ARGV > 1;
 
$REPOS=$ARGV[0];
$TXN=$ARGV[1];
 
$svnlook = '/usr/local/bin/svnlook';
 
chomp($author=`$svnlook author -t $TXN $REPOS`);
chomp($log=`$svnlook log -t $TXN $REPOS`);
 
if( $log eq '' || $log =~ /^\W+$/ ) {
    die "\nHello, $author. Empty commit log is not permitted!\n";
}
 
exit(0);

除此之外,對於 pre-commit hook,我還加上了檢查不允許跨 branch 的 commit,
因為一旦有了跨 branch 的 commit,會使得未來需要作 merge 的時候遇到許多麻煩

例如常用的 repository 結構如下:

/trunk/
/branches/helloworld-1.0/
/branches/helloworld-1.1/

要防止使用者同時對兩個以上的 branch (包含 main trunk) 作 commit,
可以把下面這一段程式再加進去 pre-commit 的 hook 中

[Subversion pre-commit hook to reject cross-branch commit]

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
#!/usr/bin/perl
 
# pre-commit hook to reject empty commit log and cross-branch commit
# remember to chmod 755 on this file
 
die "Usage: $0 [REPOS] [TXN]\n" unless @ARGV > 1;
 
$REPOS=$ARGV[0];
$TXN=$ARGV[1];
 
$svnlook = '/usr/local/bin/svnlook';
 
chomp($author=`$svnlook author -t $TXN $REPOS`);
chomp($log=`$svnlook log -t $TXN $REPOS`);
 
if( $log eq '' || $log =~ /^\W+$/ ) {
    die "\nHello, $author. Empty commit log is not permitted!\n";
}
 
open(DIRS,"$svnlook dirs-changed -t $TXN $REPOS|");
while(<DIRS>) {
    if( /^(trunk)\// ) {
        $branches{$1}++;
    }
    elsif( /^branches\/([^\/]+)/ ) {
         $branches{$1}++;
    }
}
close(DIRS);
 
$c = %branches;
 
if( $c > 1 ) {
    die "\nHello, $author. You can't commit to $c branches at the same time!\n";
}
 
exit(0);
 Posted by at 00:54

  2 Responses to “Subversion pre-commit hook”

  1. […] Subversion pre-commit hook […]

  2. varadero hotels

    Subversion pre-commit hook | Dada’s Blog

歡迎留下您的意見