4 # value of nethack.setupversion we will end up with when this is done
5 # version 1 is reserved for repos checked out before versioning was added
7 my $version_old = 0; # current version, if any (0 is no entry ergo new repo)
12 # Activestate Perl doesn't include File::Spec. Grr.
14 eval "require File::Spec::Functions";
17 File::Spec not found. (If you are running ActiveState Perl please run:
19 and re-run this program.
22 File::Spec::Functions->import;
25 exit 1 unless(getopts('nvf')); # TODO: this can probably have better output
28 my $DS = quotemeta('/'); # Directory Separator (for regex)
29 my $DSP = '/'; # ... for printing
30 # Temporarily disabled; there's something weird about msys
31 # msys: POSIXish over a Windows filesystem (so / not \ but \r\n not \n).
35 # # NB: We don't need to do anything about File::Spec. It doesn't know
36 # # about msys but it defaults to Unix, so we'll be ok.
39 $DS = quotemeta('\\');
43 # make sure we're at the top level of a repo
45 die "This is not the top level of a git repository.\n";
48 my $vtemp = `git config --local --get nethack.setupversion`;
51 $version_old = 0+$vtemp;
52 if($version_old != $version_new){
53 print STDERR "Migrating from setup version $version_old to $version_new\n" if($opt_v);
57 if(length $vtemp == 0){
58 if(`git config --get merge.NHsubst.name` =~ m/^Net/){
60 print STDERR "Migrating to setup version 1\n" if($opt_v);
64 my $gitadddir = `git config --get nethack.gitadddir`;
66 if(length $gitadddir){
68 die "nethack.gitadddir has invalid value '$gitadddir'\n";
71 print STDERR "nethack.gitadddir=$gitadddir\n" if($opt_v);
73 # This is (relatively) safe because we know we're at R in R/DEVEL/nhgitset.pl
74 my $srcdir = ($0 =~ m!^(.*)$DS!)[0];
76 if(! -f catfile($srcdir, 'nhgitset.pl')){
77 die "I can't find myself in '$srcdir'\n";
80 print STDERR "Copying from: $srcdir\n" if($opt_v);
82 if($opt_f || $version_old==0){
83 print STDERR "Configuring line endings\n" if($opt_v);
84 unlink catfile('.git','index') unless($opt_n);
85 system("git reset") unless($opt_n);
86 system("git config --local core.safecrlf true") unless($opt_n);
87 system("git config --local core.autocrlf false") unless($opt_n);
88 } elsif($version_old <2){
89 my $xx = `git config --get --local core.safecrlf`;
91 print STDERR "\nNeed to 'rm .git${DSP}index;git reset'.\n";
92 print STDERR " When ready to proceed, re-run with -f flag.\n";
99 print STDERR "Installing aliases\n" if($opt_v);
100 $addpath = catfile(curdir(),'.git','hooks','NHadd');
101 &add_alias('nhadd', "!$addpath add");
102 &add_alias('nhcommit', "!$addpath commit");
103 my $nhsub = catfile(curdir(),'.git','hooks','nhsub');
104 &add_alias('nhsub', "!$nhsub");
106 print STDERR "Installing filter/merge\n" if($opt_v);
108 # XXXX need it in NHadd to find nhsub???
109 # removed at version 3
110 #if($^O eq "MSWin32"){
111 # $cmd = '.git\\\\hooks\\\\NHtext';
113 # $cmd = catfile(curdir(),'.git','hooks','NHtext');
115 #&add_config('filter.NHtext.clean', "$cmd --clean %f");
116 #&add_config('filter.NHtext.smudge', "$cmd --smudge %f");
117 if($version_old == 1 or $version_old == 2){
118 print STDERR "Removing filter.NHtext\n" if($opt_v);
119 system('git','config','--unset','filter.NHtext.clean') unless($opt_n);
120 system('git','config','--unset','filter.NHtext.smudge') unless($opt_n);
121 system('git','config','--remove-section','filter.NHtext') unless($opt_n);
123 print STDERR "Removing NHtext\n" if($opt_v);
124 unlink catfile(curdir(),'.git','hooks','NHtext') unless($opt_n);
127 $cmd = catfile(curdir(),'.git','hooks','NHsubst');
128 &add_config('merge.NHsubst.name', 'NetHack Keyword Substitution');
129 &add_config('merge.NHsubst.driver', "$cmd %O %A %B %L");
131 print STDERR "Running directories\n" if($opt_v);
133 foreach my $dir ( glob("$srcdir$DS*") ){
134 next unless(-d $dir);
136 my $target = catfile($dir, 'TARGET');
137 next unless(-f $target);
139 open TARGET, '<', $target or die "$target: $!";
140 my $targetpath = <TARGET>;
141 # still have to eat all these line endings under msys, so instead of chomp use this:
142 $targetpath =~ s![\r\n]!!g;
144 print STDERR "Directory $dir -> $targetpath\n" if($opt_v);
147 $enddir =~ s!.*$DS!!;
148 if(! &process_override($enddir, "INSTEAD")){
149 &process_override($enddir, "PRE");
150 my $fnname = "do_dir_$enddir";
151 if(defined &$fnname){
152 &$fnname($dir, $targetpath);
154 &process_override($enddir, "POST");
158 &check_prefix; # for variable substitution
160 if($version_old != $version_new){
161 print STDERR "Setting version to $version_new\n" if($opt_v);
163 system("git config nethack.setupversion $version_new");
165 die "Can't set nethack.setupversion $version_new: $?,$!\n";
172 sub process_override {
173 my($srcdir, $plname) = @_;
174 return 0 unless(length $gitadddir);
176 my $plpath = catfile($gitadddir, $srcdir, $plname);
177 #print STDERR " ",catfile($srcdir, $plname),"\n"; # save this for updating docs - list of overrides
178 return 0 unless(-x $plpath);
180 print STDERR "Running $plpath\n" if($opt_v);
181 # current directory is top of target repo
184 system("$plpath $opt_v") and die "Callout $plpath failed: $?\n";
190 my($name, $def) = @_;
191 &add_config("alias.$name",$def);
195 my($name, $val) = @_;
196 system('git', 'config', '--local', $name, $val) unless($opt_n);
200 my $lcl = `git config --local --get nethack.substprefix`;
203 my $other = `git config --get nethack.substprefix`;
205 if(0==length $other){
206 print STDERR "ERROR: nethack.substprefix is not set anywhere. Set it and re-run.\n";
209 &add_config('nethack.substprefix', $other);
210 print STDERR "Copying prefix '$other' to local repository.\n" if($opt_v);
212 $lcl = $other; # for display below
214 print "\n\nUsing prefix '$lcl' - PLEASE MAKE SURE THIS IS CORRECT\n\n";
219 # We are NOT going to mess with config now.
222 my($srcdir, $targetdir) = @_;
223 #warn "do_dir_DOTGIT($srcdir, $targetdir)\n";
224 my $cname = "$srcdir/config";
226 print STDERR "Appending to .git/config\n" if($opt_v);
227 open CONFIG, ">>.git/config" or die "open .git/config: $!";
228 open IN, "<", $cname or die "open $cname: $!";
234 print STDERR " Nothing to add to .git/config\n" if($opt_v);
236 # XXX are there other files in .git that we might want to handle?
238 for my $file ( glob("$srcdir/*") ){
239 next if( $file =~ m!.*/TARGET$! );
240 next if( $file =~ m!.*/config$! );
241 die "ERROR: no handler for $file\n";
246 sub do_dir_hooksdir {
247 my($srcdir, $targetdir) = @_;
249 for my $path ( glob("$srcdir$DS*") ){
251 next if( $path =~ m!.*${DS}TARGET$! );
257 $file = catfile($targetdir, $file);
261 open IN, "<", $path or die "Can't open $path: $!";
262 open OUT, ">", "$file" or die "Can't open $file: $!";
276 (can we change the .gitattributes syntax to include a comment character?)
277 maybe [comment] attr.c:parse_attr_line
278 grr - looks like # is the comment character
284 nhgitset.pl - Setup program for NetHack git repositories
289 [git config nethack.gitadddir GITADDDIR]
290 perl SOME_PATH/DEVEL/nhgitset.pl [-v][-n][-f]
294 nhgitset.pl installs NetHack-specific setup after a C<git clone> (or after
295 changes to the desired configuration, which are installed by re-running
298 The follwing options are available:
300 B<-f> Force. Do not use this unless the program requests it.
302 B<-n> Make no changes.
304 B<-v> Verbose output.
308 nhgitset.pl uses the following non-standard C<git config> variables:
330 2 Intervention required.