Files
nethack/DEVEL/hooksdir/NHgithook.pm
nhmall 65655d2cee Incorporate some git information into NetHack
Incorporate some git information into NetHack so that it
is potentially visible to a player. That's useful when
collecting details about the version that they are
running and, if the gitinfo is present, it can tie the
code to a specific git commit in the repository.

This modifies 'makedefs -v' to check for the presence of a data file
called dat/gitinfo.txt and if it is there, parse out its
contents, then write additional lines to include/date.h beyond
what 'makedefs -v' was previously putting in there, similar to
this sample:

      #define NETHACK_GIT_SHA "0c84e564c78e2024e562d39539376ce2e21eec8e"
      #define NETHACK_GIT_BRANCH "NetHack-3.6.0"

The contents of an appropriate dat/gitinfo.txt are as follows,
and trailing/leading whitespace is not significant:

      githash = 0c84e564c78e2024e562d39539376ce2e21eec8e
      gitbranch = NetHack-3.6.0

It also adjusts the contents of the 'v' version information to
include the additional git info when available.

Also adds some hooks DEVEL/hooksdir and a perl file to DEVEL
for simplifying and automating the deposit of dat/gitinfo.txt
so that it generally reflects the most current git commit.

DEVEL/gitinfo.pl can be used to build dat/gitinfo.txt at any
time without doing a commit, merge, or checkout.
    	perl DEVEL/gitinfo.pl

command line --version and -version support

To complement the extra information being provided in the
version by the 'v' command, this also adds support for the
following new command line arguments:
    --version
     -version            Output the NetHack version string then exit.

    --version:paste      Output the NetHack version string and also copy it to
     -version:paste      the platform's paste buffer for insertion somewhere,
                         then exit.

If the paste variation of -version is requested on a platform that
hasn't incorporated any support for the capability, it will deliver
the version info then an error message, prior to exiting.

To support the extended -version:paste variation, a port needs to:
    - provide a port-specific routine to perform
      the paste buffer copy in a port code file.
    - #define RUNTIME_PASTEBUF_SUPPORT in the include/portconf.h header file.

    --skeleton--
    void port_insert_pastebuf(buf)
    char *buf;
    {
    	/* insert code to copy the version info from buf into
    	   platform's paste buffer in a supported way */
    }

macosx and Windows have both added support for RUNTIME_PASTEBUF_SUPPORT
2018-02-23 19:34:44 -05:00

236 lines
5.1 KiB
Perl

#
# NHgithook.pm
# NetHack Git Hook Module
# $NHDT-Date$
package NHgithook;
use Cwd;
###
### CONFIG
###
my $trace = 0;
my $tracefile = "/tmp/nhgitt.$$";
# OS hackery
my $DS = quotemeta('/');
if ($^O eq "MSWin32")
{
$DS = quotemeta('\\');
}
our %saved_env;
our @saved_argv;
our $saved_input;
sub saveSTDIN {
@saved_input = <STDIN>;
if($trace){
print TRACE "STDIN:\n";
print TRACE $saved_input;
print TRACE "ENDSTDIN\n";
}
tie *STDIN, 'NHIO::STDIN', @saved_input;
}
# XXX this needs a re-write (don't tie and untie, just set NEXT=0)
# (the sensitive thing is @foo = <STDIN> )
sub resetSTDIN{
my $x = tied(*STDIN);
my %x = %$x;
my $data = @$x{DATA};
untie *STDIN;
tie *STDIN, 'NHIO::STDIN', $data;
}
# don't need this now
#sub restore {
# open STDIN, "<", \$saved_input or die "reopen STDIN: $!";
# @ARGV = @saved_argv;
# %ENV = %saved_env;
#}
sub PRE {
&do_hook("PRE");
}
sub POST {
&do_hook("POST");
}
###
### store githash and gitbranch in dat/gitinfo.txt
###
sub nhversioning {
use strict;
use warnings;
my $git_sha = `git rev-parse HEAD`;
$git_sha =~ s/\s+//g;
my $git_branch = `git rev-parse --abbrev-ref HEAD`;
$git_branch =~ s/\s+//g;
if (open my $fh, '<', 'dat/gitinfo.txt') {
while(my $line = <$fh>) {
if ((index $line, $git_sha) >= 0) {
close $fh;
print "No update made to dat/gitinfo.txt, existing githash=".$git_sha."\n";
return;
}
}
close $fh;
}
if (open my $fh, '>', 'dat/gitinfo.txt') {
print $fh 'githash='.$git_sha."\n";
print $fh 'gitbranch='.$git_branch."\n";
print "An updated dat/gitinfo.txt was written, githash=".$git_sha."\n";
}
}
# PRIVATE
sub do_hook {
my($p) = @_;
my $hname = $0;
$hname =~ s!^((.*$DS)|())(.*)!$1$p-$4!;
if(-x $hname){
print TRACE "START $p: $hname\n" if($trace);
open TOHOOK, "|-", $hname or die "open $hname: $!";
print TOHOOK <STDIN>;
close TOHOOK or die "close $hname: $! $?";
print TRACE "END $p\n" if($trace);
}
}
sub trace_start {
return unless($trace);
my $self = shift;
open TRACE, ">>", $tracefile;
print TRACE "START CLIENT PID:$$ ARGV:\n";
print TRACE "CWD: " . cwd() . "\n";
print TRACE "[0] $0\n";
my $x1;
for(my $x=0;$x<scalar @ARGV;$x++){
$x1 = $x+1;
print TRACE "[$x1] $ARGV[$x]\n";
}
print TRACE "ENV:\n";
foreach my $k (sort keys %ENV){
next unless ($k =~ m/(^GIT_)|(^NH)/);
print TRACE " $k => $ENV{$k}\n";
}
}
BEGIN {
%saved_env = %ENV;
@saved_argv = @ARGV;
&trace_start;
}
###
### ugly mess so we can re-read STDIN
###
package NHIO::STDIN;
sub TIEHANDLE {
my $class = shift;
my %fh;
# XXX yuck
if(ref @_[0]){
$fh{DATA} = @_[0];
} else {
$fh{DATA} = \@_;
}
$fh{NEXT} = 0;
return bless \%fh, $class;
}
sub READLINE {
my $self = shift;
return undef if($self->{EOF});
if(wantarray){
my $lim = $#{$self->{DATA}};
my @ary = @{$self->{DATA}}[$self->{NEXT}..$lim];
my @rv = @ary[$self->{NEXT}..$#ary];
$self->{EOF} = 1;
return @rv;
} else{
my $rv = $self->{DATA}[$self->{NEXT}];
if(length $rv){
$self->{NEXT}++;
return $rv;
} else {
$self->{EOF} = 1;
return undef;
}
}
}
sub EOF {
$self = shift;
return $self->{EOF};
}
1;
__END__
=head1 NAME
NHgithook - common code for NetHack git hooks (and other git bits)
=head1 SYNOPSIS
BEGIN {
my $DS = quotemeta('/');
my $PDS = '/';
if ($^O eq "MSWin32")
{
$DS = quotemeta('\\');
$PDS = '\\';
}
push(@INC, $ENV{GIT_DIR}.$PDS."hooks"); # for most hooks
push(@INC, ($0 =~ m!^(.*)$DS!)[0]); # when the above doesn't work
$gitdir = `git rev-parse --git-dir`; # and when the above really doesn't work
$gitdir =~ s/[\r\n]*$/;
push(@INC, $gitdir.$PDS."hooks");
}
use NHgithook;
&NHgithook::saveSTDIN;
&NHgithook::PRE;
(core hook code)
&NHgithook::POST;
=head1 DESCRIPTION
Buffers call information so multiple independent actions may be coded for
Git hooks and similar Git callouts.
=head1 SETUP
Changing the C<$trace> and C<$tracefile> variables requires editing the
module source. Setting C<$trace> enables tracing, logs basic information,
and leaves the C<TRACE> filehandle open for additional output; output to this
filehandle must be guarded by C<$NHgithook::trace>. Setting
C<$tracefile> specifies the file used for trace output. Note that C<$$>
may be useful since multiple processes may be live at the same time.
=head1 FUNCTIONS
NHgithook::saveSTDIN reads STDIN until EOF and saves it
NHgithook::PRE runs the PRE hook, if it exists
NHgithook::POST runs the POST hook, if it exists
=head1 BUGS
Some features not well tested, especially under Windows.
=head1 AUTHOR
Kenneth Lorber (keni@his.com)