Add end-of-game dumplogs
[aNetHack.git] / DEVEL / nhgitset.pl
blob08068484a9b3af8248853d402583bc33004ed9d8
1 #!/usr/bin/perl
2 # $NHDT-Date$
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
6 my $version_new = 3;
7 my $version_old = 0; # current version, if any (0 is no entry ergo new repo)
9 use Cwd;
10 use Getopt::Std;
12 # Activestate Perl doesn't include File::Spec. Grr.
13 BEGIN {
14 eval "require File::Spec::Functions";
15 if($@){
16 die <<E_O_M;
17 File::Spec not found. (If you are running ActiveState Perl please run:
18 cpan File::Spec
19 and re-run this program.
20 E_O_M
22 File::Spec::Functions->import;
25 exit 1 unless(getopts('nvf')); # TODO: this can probably have better output
27 # OS hackery
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).
32 #if($^O eq "msys"){
33 # $/ = "\r\n";
34 # $\ = "\r\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.
38 if($^O eq "MSWin32"){
39 $DS = quotemeta('\\');
40 $DSP = '\\';
43 # make sure we're at the top level of a repo
44 if(! -d ".git"){
45 die "This is not the top level of a git repository.\n";
48 my $vtemp = `git config --local --get nethack.setupversion`;
49 chomp($vtemp);
50 if($vtemp > 0){
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);
56 # legacy check:
57 if(length $vtemp == 0){
58 if(`git config --get merge.NHsubst.name` =~ m/^Net/){
59 $version_old = 1;
60 print STDERR "Migrating to setup version 1\n" if($opt_v);
64 my $gitadddir = `git config --get nethack.gitadddir`;
65 chomp($gitadddir);
66 if(length $gitadddir){
67 if(! -d $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`;
90 if($xx !~ m/true/){
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";
93 exit 2;
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';
112 #} else {
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;
143 close TARGET;
144 print STDERR "Directory $dir -> $targetpath\n" if($opt_v);
146 my $enddir = $dir;
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);
162 if(! $opt_n){
163 system("git config nethack.setupversion $version_new");
164 if($?){
165 die "Can't set nethack.setupversion $version_new: $?,$!\n";
170 exit 0;
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
183 unless($opt_n){
184 system("$plpath $opt_v") and die "Callout $plpath failed: $?\n";
186 return 1;
189 sub add_alias {
190 my($name, $def) = @_;
191 &add_config("alias.$name",$def);
194 sub add_config {
195 my($name, $val) = @_;
196 system('git', 'config', '--local', $name, $val) unless($opt_n);
199 sub check_prefix {
200 my $lcl = `git config --local --get nethack.substprefix`;
201 chomp($lcl);
202 if(0==length $lcl){
203 my $other = `git config --get nethack.substprefix`;
204 chomp($other);
205 if(0==length $other){
206 print STDERR "ERROR: nethack.substprefix is not set anywhere. Set it and re-run.\n";
207 exit 2;
208 } else {
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";
217 sub do_dir_DOTGIT {
218 if(1){
219 # We are NOT going to mess with config now.
220 return;
221 } else {
222 my($srcdir, $targetdir) = @_;
223 #warn "do_dir_DOTGIT($srcdir, $targetdir)\n";
224 my $cname = "$srcdir/config";
225 if(-e $cname){
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: $!";
229 my @data = <IN>;
230 print CONFIG @data;
231 close IN;
232 close CONFIG;
233 } else {
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?
237 # So just in case:
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$! );
253 my $file = $path;
255 $file =~ s!.*$DS!!;
257 $file = catfile($targetdir, $file);
259 next if($opt_n);
261 open IN, "<", $path or die "Can't open $path: $!";
262 open OUT, ">", "$file" or die "Can't open $file: $!";
263 while(<IN>){
264 print OUT;
266 close OUT;
267 close IN;
269 if(! -x $file){
270 chmod 0755 ,$file;
275 __END__
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
282 =head1 NAME
284 nhgitset.pl - Setup program for NetHack git repositories
286 =head1 SYNOPSIS
288 cd THE_REPO
289 [git config nethack.gitadddir GITADDDIR]
290 perl SOME_PATH/DEVEL/nhgitset.pl [-v][-n][-f]
292 =head1 DESCRIPTION
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
296 nhgitset.pl).
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.
306 =head1 CONFIG
308 nhgitset.pl uses the following non-standard C<git config> variables:
310 nethack.gitadddir
312 DOTGIT/INSTEAD
313 DOTGIT/PRE
314 DOTGIT/POST
315 hooksdir/INSTEAD
316 hooksdir/PRE
317 hooksdir/POST
319 nethack.setupversion
321 nethack.substprefix
324 =head1 EXIT STATUS
326 0 Success.
328 1 Fail.
330 2 Intervention required.