2 # $NHDT-Date: 1524689669 2018/04/25 20:54:29 $ $NHDT-Branch: NetHack-3.6.0 $:$NHDT-Revision: 1.2 $
3 # Copyright (c) 2015 by Kenneth Lorber, Kensington, Maryland
4 # NetHack may be freely redistributed. See license for details.
6 # value of nethack.setupversion we will end up with when this is done
7 # version 1 is reserved for repos checked out before versioning was added
9 my $version_old = 0; # current version, if any (0 is no entry ergo new repo)
14 # Activestate Perl doesn't include File::Spec. Grr.
16 eval "require File::Spec::Functions";
19 File::Spec not found. (If you are running ActiveState Perl please run:
21 and re-run this program.
24 File::Spec::Functions->import;
27 exit 1 unless(getopts('nvf')); # TODO: this can probably have better output
30 my $DS = quotemeta('/'); # Directory Separator (for regex)
31 my $DSP = '/'; # ... for printing
32 # Temporarily disabled; there's something weird about msys
33 # msys: POSIXish over a Windows filesystem (so / not \ but \r\n not \n).
37 # # NB: We don't need to do anything about File::Spec. It doesn't know
38 # # about msys but it defaults to Unix, so we'll be ok.
41 $DS = quotemeta('\\');
45 # make sure we're at the top level of a repo
47 die "This is not the top level of a git repository.\n";
50 my $vtemp = `git config --local --get nethack.setupversion`;
53 $version_old = 0+$vtemp;
54 if($version_old != $version_new){
55 print STDERR "Migrating from setup version $version_old to $version_new\n" if($opt_v);
59 if(length $vtemp == 0){
60 if(`git config --get merge.NHsubst.name` =~ m/^Net/){
62 print STDERR "Migrating to setup version 1\n" if($opt_v);
66 my $gitadddir = `git config --get nethack.gitadddir`;
68 if(length $gitadddir){
70 die "nethack.gitadddir has invalid value '$gitadddir'\n";
73 print STDERR "nethack.gitadddir=$gitadddir\n" if($opt_v);
75 # This is (relatively) safe because we know we're at R in R/DEVEL/nhgitset.pl
76 my $srcdir = ($0 =~ m!^(.*)$DS!)[0];
78 if(! -f catfile($srcdir, 'nhgitset.pl')){
79 die "I can't find myself in '$srcdir'\n";
82 print STDERR "Copying from: $srcdir\n" if($opt_v);
84 if($opt_f || $version_old==0){
85 print STDERR "Configuring line endings\n" if($opt_v);
86 unlink catfile('.git','index') unless($opt_n);
87 system("git reset") unless($opt_n);
88 system("git config --local core.safecrlf true") unless($opt_n);
89 system("git config --local core.autocrlf false") unless($opt_n);
90 } elsif($version_old <2){
91 my $xx = `git config --get --local core.safecrlf`;
93 print STDERR "\nNeed to 'rm .git${DSP}index;git reset'.\n";
94 print STDERR " When ready to proceed, re-run with -f flag.\n";
101 print STDERR "Installing aliases\n" if($opt_v);
102 $addpath = catfile(curdir(),'.git','hooks','NHadd');
103 &add_alias('nhadd', "!$addpath add");
104 &add_alias('nhcommit', "!$addpath commit");
105 my $nhsub = catfile(curdir(),'.git','hooks','nhsub');
106 &add_alias('nhsub', "!$nhsub");
108 print STDERR "Installing filter/merge\n" if($opt_v);
110 # XXXX need it in NHadd to find nhsub???
111 # removed at version 3
112 #if($^O eq "MSWin32"){
113 # $cmd = '.git\\\\hooks\\\\NHtext';
115 # $cmd = catfile(curdir(),'.git','hooks','NHtext');
117 #&add_config('filter.NHtext.clean', "$cmd --clean %f");
118 #&add_config('filter.NHtext.smudge', "$cmd --smudge %f");
119 if($version_old == 1 or $version_old == 2){
120 print STDERR "Removing filter.NHtext\n" if($opt_v);
121 system('git','config','--unset','filter.NHtext.clean') unless($opt_n);
122 system('git','config','--unset','filter.NHtext.smudge') unless($opt_n);
123 system('git','config','--remove-section','filter.NHtext') unless($opt_n);
125 print STDERR "Removing NHtext\n" if($opt_v);
126 unlink catfile(curdir(),'.git','hooks','NHtext') unless($opt_n);
129 $cmd = catfile(curdir(),'.git','hooks','NHsubst');
130 &add_config('merge.NHsubst.name', 'NetHack Keyword Substitution');
131 &add_config('merge.NHsubst.driver', "$cmd %O %A %B %L");
133 print STDERR "Running directories\n" if($opt_v);
135 foreach my $dir ( glob("$srcdir$DS*") ){
136 next unless(-d $dir);
138 my $target = catfile($dir, 'TARGET');
139 next unless(-f $target);
141 open TARGET, '<', $target or die "$target: $!";
142 my $targetpath = <TARGET>;
143 # still have to eat all these line endings under msys, so instead of chomp use this:
144 $targetpath =~ s![\r\n]!!g;
146 print STDERR "Directory $dir -> $targetpath\n" if($opt_v);
149 $enddir =~ s!.*$DS!!;
150 if(! &process_override($enddir, "INSTEAD")){
151 &process_override($enddir, "PRE");
152 my $fnname = "do_dir_$enddir";
153 if(defined &$fnname){
154 &$fnname($dir, $targetpath);
156 &process_override($enddir, "POST");
160 &check_prefix; # for variable substitution
162 if($version_old != $version_new){
163 print STDERR "Setting version to $version_new\n" if($opt_v);
165 system("git config nethack.setupversion $version_new");
167 die "Can't set nethack.setupversion $version_new: $?,$!\n";
174 sub process_override {
175 my($srcdir, $plname) = @_;
176 return 0 unless(length $gitadddir);
178 my $plpath = catfile($gitadddir, $srcdir, $plname);
179 #print STDERR " ",catfile($srcdir, $plname),"\n"; # save this for updating docs - list of overrides
180 return 0 unless(-x $plpath);
182 print STDERR "Running $plpath\n" if($opt_v);
183 # current directory is top of target repo
186 system("$plpath $opt_v") and die "Callout $plpath failed: $?\n";
192 my($name, $def) = @_;
193 &add_config("alias.$name",$def);
197 my($name, $val) = @_;
198 system('git', 'config', '--local', $name, $val) unless($opt_n);
202 my $lcl = `git config --local --get nethack.substprefix`;
205 my $other = `git config --get nethack.substprefix`;
207 if(0==length $other){
208 print STDERR "ERROR: nethack.substprefix is not set anywhere. Set it and re-run.\n";
211 &add_config('nethack.substprefix', $other);
212 print STDERR "Copying prefix '$other' to local repository.\n" if($opt_v);
214 $lcl = $other; # for display below
216 print "\n\nUsing prefix '$lcl' - PLEASE MAKE SURE THIS IS CORRECT\n\n";
221 # We are NOT going to mess with config now.
224 my($srcdir, $targetdir) = @_;
225 #warn "do_dir_DOTGIT($srcdir, $targetdir)\n";
226 my $cname = "$srcdir/config";
228 print STDERR "Appending to .git/config\n" if($opt_v);
229 open CONFIG, ">>.git/config" or die "open .git/config: $!";
230 open IN, "<", $cname or die "open $cname: $!";
236 print STDERR " Nothing to add to .git/config\n" if($opt_v);
238 # XXX are there other files in .git that we might want to handle?
240 for my $file ( glob("$srcdir/*") ){
241 next if( $file =~ m!.*/TARGET$! );
242 next if( $file =~ m!.*/config$! );
243 die "ERROR: no handler for $file\n";
248 sub do_dir_hooksdir {
249 my($srcdir, $targetdir) = @_;
251 for my $path ( glob("$srcdir$DS*") ){
253 next if( $path =~ m!.*${DS}TARGET$! );
259 $file = catfile($targetdir, $file);
263 open IN, "<", $path or die "Can't open $path: $!";
264 open OUT, ">", "$file" or die "Can't open $file: $!";
278 (can we change the .gitattributes syntax to include a comment character?)
279 maybe [comment] attr.c:parse_attr_line
280 grr - looks like # is the comment character
286 nhgitset.pl - Setup program for NetHack git repositories
291 [git config nethack.gitadddir GITADDDIR]
292 perl SOME_PATH/DEVEL/nhgitset.pl [-v][-n][-f]
296 nhgitset.pl installs NetHack-specific setup after a C<git clone> (or after
297 changes to the desired configuration, which are installed by re-running
300 The follwing options are available:
302 B<-f> Force. Do not use this unless the program requests it.
304 B<-n> Make no changes.
306 B<-v> Verbose output.
310 nhgitset.pl uses the following non-standard C<git config> variables:
332 2 Intervention required.