Simple Directmedia Layer
at main 78 lines 2.4 kB view raw
1#!/usr/bin/perl -w 2 3# To use this script: symlink it to .git/hooks/pre-push, then "git push" 4# 5# This script is called by "git push" after it has checked the remote status, 6# but before anything has been pushed. If this script exits with a non-zero 7# status nothing will be pushed. 8# 9# This hook is called with the following parameters: 10# 11# $1 -- Name of the remote to which the push is being done 12# $2 -- URL to which the push is being done 13# 14# If pushing without using a named remote those arguments will be equal. 15# 16# Information about the commits which are being pushed is supplied as lines to 17# the standard input in the form: 18# 19# <local ref> <local sha1> <remote ref> <remote sha1> 20 21use warnings; 22use strict; 23 24my $remote = $ARGV[0]; 25my $url = $ARGV[1]; 26 27#print("remote: $remote\n"); 28#print("url: $url\n"); 29 30$url =~ s/\.git$//; # change myorg/myproject.git to myorg/myproject 31$url =~ s#^git\@github\.com\:#https://github.com/#i; 32my $commiturl = $url =~ /\Ahttps?:\/\/github.com\// ? "$url/commit/" : ''; 33 34my $z40 = '0000000000000000000000000000000000000000'; 35my $reported = 0; 36 37while (<STDIN>) { 38 chomp; 39 my ($local_ref, $local_sha, $remote_ref, $remote_sha) = split / /; 40 #print("local_ref: $local_ref\n"); 41 #print("local_sha: $local_sha\n"); 42 #print("remote_ref: $remote_ref\n"); 43 #print("remote_sha: $remote_sha\n"); 44 45 my $range = ''; 46 if ($remote_sha eq $z40) { # New branch, examine all commits 47 $range = $local_sha; 48 } else { # Update to existing branch, examine new commits 49 $range = "$remote_sha..$local_sha"; 50 } 51 52 my $gitcmd = "git log --reverse --oneline --no-abbrev-commit '$range'"; 53 open(GITPIPE, '-|', $gitcmd) or die("\n\n$0: Failed to run '$gitcmd': $!\n\nAbort push!\n\n"); 54 while (<GITPIPE>) { 55 chomp; 56 if (/\A([a-fA-F0-9]+)\s+(.*?)\Z/) { 57 my $hash = $1; 58 my $msg = $2; 59 60 if (!$reported) { 61 print("\nCommits expected to be pushed:\n"); 62 $reported = 1; 63 } 64 65 #print("hash: $hash\n"); 66 #print("msg: $msg\n"); 67 68 print("$commiturl$hash -- $msg\n"); 69 } else { 70 die("$0: Unexpected output from '$gitcmd'!\n\nAbort push!\n\n"); 71 } 72 } 73 die("\n\n$0: Failing exit code from running '$gitcmd'!\n\nAbort push!\n\n") if !close(GITPIPE); 74} 75 76print("\n") if $reported; 77 78exit(0); # Let the push go forward.