当前位置:  开发笔记 > 编程语言 > 正文

Git/gitosis:如何检查用户名和电子邮件的有效性?

如何解决《Git/gitosis:如何检查用户名和电子邮件的有效性?》经验,为你挑选了2个好方法。

我已经安装了git和gitosis,我需要添加一个检查,当某些东西被推入存储库时,用户名是有效的.

我认为预接收挂钩是放置它的正确钩子,但是我无法找到gitosis进入存储库的真实用户名和电子邮件地址(由git config user.name和git config user设置的那些).电子邮件)来自环境变量.LOGNAME和USER都是'git'.gitosis如何检测此信息,我是否也可以在预接收挂钩中找到它?



1> Bombe..:

嗯,从我从钩子里收集到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失败.



2> Makis..:

好的,我们设法完成了工作,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密钥的用户名相同.

推荐阅读
女女的家_747
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有