我已经安装了git和gitosis,我需要添加一个检查,当某些东西被推入存储库时,用户名是有效的.
我认为预接收挂钩是放置它的正确钩子,但是我无法找到gitosis进入存储库的真实用户名和电子邮件地址(由git config user.name和git config user设置的那些).电子邮件)来自环境变量.LOGNAME和USER都是'git'.gitosis如何检测此信息,我是否也可以在预接收挂钩中找到它?
嗯,从我从钩子里收集到githooks(5)
的东西pre-receive
得到更新的参考stdin
.
#!/bin/sh while read old new name; do email=$(git log -1 --pretty=format:%ae $new) # check email done
您需要检查电子邮件地址(可能有多行数据)并相应地退出脚本,即exit 0
成功和例如exit 1
失败.
好的,我们设法完成了工作,Bombe上面的回复帮了很多忙.这就是它的完成方式:
我将os.environ ['GITOSIS_USER'] = user添加到Gitosis/Serve.py,函数Main()并重新安装它.
然后我创建了以下预接收脚本:
hooks/pre-receive
#!/usr/bin/perl my $user = $ENV{'GITOSIS_USER'}; if ($user !~ m/^[^@]+@[^@]+$/ ) { print STDERR "Unknown user. Not running under Gitosis?\n"; exit 1; } my $fail = 0; while() { if (m/^([0-9a-f]+)\s+([0-9a-f]+)\s+(\S+)$/) { my $oldver = $1; my $curver = $2; my $ref = $3; my $ret = open (FH, "-|", "git", "rev-list", '--pretty=format:%H:%ae:%ce',$ if ($ret) { # great and less brakets hidden in HTML: >FH< while ( ) { chomp; my $line = $_; if ($_ !~ m/commit /) { my ($rev, $author, $committer) = split(":", $line); if ( $author ne $user && $committer ne $user ) { print STDERR "Unauthorized commit: $rev\n"; $fail++; } } } } } } if ($fail) { exit 1; } exit 0;
hooks/pre-receive
#!/usr/bin/perl
my $user = $ENV{'GITOSIS_USER'};
if ($user !~ m/^[^@]+@[^@]+$/ ) {
print STDERR "Unknown user. Not running under Gitosis?\n";
exit 1;
}
my $fail = 0;
while() {
if (m/^([0-9a-f]+)\s+([0-9a-f]+)\s+(\S+)$/) {
my $oldver = $1;
my $curver = $2;
my $ref = $3;
my $ret = open (FH, "-|", "git", "rev-list", '--pretty=format:%H:%ae:%ce',$
if ($ret) {
# great and less brakets hidden in HTML: >FH<
while () {
chomp;
my $line = $_;
if ($_ !~ m/commit /) {
my ($rev, $author, $committer) = split(":", $line);
if ( $author ne $user && $committer ne $user ) {
print STDERR "Unauthorized commit: $rev\n";
$fail++;
}
}
}
}
}
}
if ($fail) {
exit 1;
}
exit 0;
这意味着用户名必须与用于为gitosis密钥环创建ssh密钥的用户名相同.