OSDN Git Service

import nethack-3.6.0
[jnethack/source.git] / DEVEL / nhgitset.pl
1 #!/usr/bin/perl
2 # $NHDT-Date$
3
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)
8
9 use Cwd;
10 use Getopt::Std;
11
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
21         }
22         File::Spec::Functions->import;
23 }
24
25 exit 1 unless(getopts('nvf'));  # TODO: this can probably have better output
26
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.
37 #}
38 if($^O eq "MSWin32"){
39         $DS = quotemeta('\\');
40         $DSP = '\\';
41 }
42
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";
46 }
47
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);
54         }
55 }
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);
61         }
62 }
63
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";
69         }
70 }
71 print STDERR "nethack.gitadddir=$gitadddir\n" if($opt_v);
72
73 # This is (relatively) safe because we know we're at R in R/DEVEL/nhgitset.pl
74 my $srcdir = ($0 =~ m!^(.*)$DS!)[0];
75
76 if(! -f catfile($srcdir, 'nhgitset.pl')){
77         die "I can't find myself in '$srcdir'\n";
78 }
79
80 print STDERR "Copying from: $srcdir\n" if($opt_v);
81
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;
94         }
95 }
96
97
98
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");
105
106 print STDERR "Installing filter/merge\n" if($opt_v);
107
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');
114 #}
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);
122
123         print STDERR "Removing NHtext\n" if($opt_v);
124         unlink catfile(curdir(),'.git','hooks','NHtext') unless($opt_n);
125 }
126
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");
130
131 print STDERR "Running directories\n" if($opt_v);
132
133 foreach my $dir ( glob("$srcdir$DS*") ){
134         next unless(-d $dir);
135
136         my $target = catfile($dir, 'TARGET');
137         next unless(-f $target);
138
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);
145
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);
153                 }
154                 &process_override($enddir, "POST");
155         }
156 }
157
158 &check_prefix;  # for variable substitution
159
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";
166                 }
167         }
168 }
169
170 exit 0;
171
172 sub process_override {
173         my($srcdir, $plname) = @_;
174         return 0 unless(length $gitadddir);
175
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);
179
180         print STDERR "Running $plpath\n" if($opt_v);
181         # current directory is top of target repo
182
183         unless($opt_n){
184                 system("$plpath $opt_v") and die "Callout $plpath failed: $?\n";
185         }
186         return 1;
187 }
188
189 sub add_alias {
190         my($name, $def) = @_;
191         &add_config("alias.$name",$def);
192 }
193
194 sub add_config {
195         my($name, $val) = @_;
196         system('git', 'config', '--local', $name, $val) unless($opt_n);
197 }
198
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);
211                 }
212                 $lcl = $other;  # for display below
213         }
214         print "\n\nUsing prefix '$lcl' - PLEASE MAKE SURE THIS IS CORRECT\n\n";
215 }
216
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);
235         }
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";
242         }
243 }
244 }
245
246 sub do_dir_hooksdir {
247         my($srcdir, $targetdir) = @_;
248
249         for my $path ( glob("$srcdir$DS*") ){
250
251                 next if( $path =~ m!.*${DS}TARGET$! );
252
253                 my $file = $path;
254
255                 $file =~ s!.*$DS!!;
256
257                 $file = catfile($targetdir, $file);
258
259                 next if($opt_n);
260
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;
265                 }
266                 close OUT;
267                 close IN;
268
269                 if(! -x $file){
270                         chmod 0755 ,$file;
271                 }
272         }
273 }
274
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
279
280
281
282 =head1 NAME
283
284 nhgitset.pl - Setup program for NetHack git repositories
285
286 =head1 SYNOPSIS
287
288  cd THE_REPO
289  [git config nethack.gitadddir GITADDDIR]
290  perl SOME_PATH/DEVEL/nhgitset.pl [-v][-n][-f]
291
292 =head1 DESCRIPTION
293
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).
297
298 The follwing options are available:
299
300 B<-f>   Force.  Do not use this unless the program requests it.
301
302 B<-n>   Make no changes.
303
304 B<-v>   Verbose output.
305
306 =head1 CONFIG
307
308 nhgitset.pl uses the following non-standard C<git config> variables:
309
310 nethack.gitadddir
311
312    DOTGIT/INSTEAD
313    DOTGIT/PRE
314    DOTGIT/POST
315    hooksdir/INSTEAD
316    hooksdir/PRE
317    hooksdir/POST
318
319 nethack.setupversion
320
321 nethack.substprefix
322
323
324 =head1 EXIT STATUS
325
326 0       Success.
327
328 1       Fail.
329
330 2       Intervention required.