5 # clean/smudge filter for handling substitutions
8 #my $debug = 0; # save trace to file
9 #my $debug2 = 0; # annotate output when running from command line
11 #my $sink = ($^O eq "MSWin32")? "NUL" :"/dev/null";
12 #my $dbgfile = ($^O eq "MSWin32") ? "$ENV{TEMP}.$$" : "/tmp/trace.$$";
13 #open TRACE, ">>", ($debug==0)? $sink : $dbgfile;
16 my($section, $var) = @_;
18 # Sigh. Without GIT_DIR we have to do it the slow way, and sometimes we don't
20 if(0 == length($ENV{GIT_DIR
})){
21 my $raw = `git config --local --get $section.$var`;
25 open(CONFIG
, "<", "$ENV{GIT_DIR}/config") or die "Missing .git/config: $!";
27 m/^\[$section]/ && do {
29 m/^\s+$var\s+=\s+(.*)/ && do {
35 die "Missing config var: [$section] $var\n";
37 # pick up the prefix for substitutions in this repo
38 my $PREFIX = &git_config
('nethack','substprefix');
40 my $submode = 0; # ok to make non-cleaning changes to file
43 if($ARGV[0] eq "--clean"){
45 if(0 == 0+$ENV{NHMODE
}){
46 $submode = 1; # do NOT add extra changes to the file
47 # print TRACE "SKIPPING\n";
49 } elsif($ARGV[0] eq "--smudge"){
52 warn "Unknown mode '$ARGV[0]'\n";
56 # XXX for now, there isn't any - if we get called, we subst. No options for now.
57 # get relevent config info
59 #git check-attr -a $ARGV[1]
61 # Process stdin to stdout.
62 # For speed we read in the entire file then do the substitutions.
67 # On at least some systems we only get 64K.
68 my $len = sysread(STDIN
, $_, 999999, length($_));
70 die "read failed: $!" unless defined($len);
73 # $1 - var and value (including trailing space but not $)
76 # s/\$$PREFIX-(([A-Za-z][A-Za-z0-9_]*)(: ([^\N{DOLLAR SIGN}]+))?)\$/&handlevar($2,$4)/eg;
77 s/\$$PREFIX-(([A-Za-z][A-Za-z0-9_]*)(: ([^\x24]+))?)\$/&handlevar($2,$4)/ego;
79 die "write failed: $!" unless defined syswrite(STDOUT
, $_);
84 # print "HIT '$var' '$val'\n" if($debug2);
86 my $subname = "PREFIX::$var";
87 if(defined &$subname){
90 $val = &$subname($val,$mode,$submode);
92 warn "No handler for \$$PREFIX-$var\n";
96 return "\$$PREFIX-$var: $val \$";
98 return "\$$PREFIX-$var\$";
103 use POSIX
qw(strftime);
105 # On push, put in the current date because we changed the file.
106 # On pull, keep the current value so we can see the last change date.
108 my($val, $mode, $submode) = @_;
111 # we add this to make merge easier for now XXX
112 my $now = time; # not %s below - may not be portable
113 # YYYY/MM/DD HH:MM:SS
114 $val = "$now " . strftime
("%Y/%m/%d %H:%M:%S", gmtime($now));
127 # NB: the standard-ish Revision line isn't enough - you need Branch:Revision -
128 # but we split it into 2 so we can use the standard processing code on Revision
129 # and just slip Branch in.
131 my($val, $mode, $submode) = @_;
134 $val = `git symbolic-ref -q --short HEAD`;
135 $val =~ s/[\n\r]*$//;
137 $val = "(unknown)" unless($val =~ m/^[[:print:]]+$/);
146 my($val, $mode, $submode) = @_;
150 my @val = `git log --follow --oneline $file`;
152 $ver = 0 if($ver < 0);