Git/gitosis: How to check validity of user name and email?

后端 未结 5 1663
傲寒
傲寒 2020-12-03 05:57

I have installed git and gitosis and I need to add a check that the user name is valid when something is pushed into the repository.

I reckon the pre-receive hook i

5条回答
  •  情歌与酒
    2020-12-03 06:21

    OK, we managed to get the job done, Bombe's reply above helped a lot. This is how it was done:

    • I added os.environ['GITOSIS_USER'] = user to Gitosis/Serve.py, function Main() and reinstalled it.
    • I then created the following pre-receive script:

    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;
    

    This means the user name has to be the same as the one used to create the ssh key for gitosis key ring.

提交回复
热议问题