OSDN Git Service

patch for win32/gui
[jnethack/source.git] / DEVEL / nhgitset.pl
1 #!/usr/bin/perl
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.
5
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
8 my $version_new = 3;
9 my $version_old = 0;    # current version, if any (0 is no entry ergo new repo)
10
11 use Cwd;
12 use Getopt::Std;
13
14 # Activestate Perl doesn't include File::Spec.  Grr.
15 BEGIN {
16         eval "require File::Spec::Functions";
17         if($@){
18                 die <<E_O_M;
19 File::Spec not found.  (If you are running ActiveState Perl please run:
20                 cpan File::Spec
21 and re-run this program.
22 E_O_M
23         }
24         File::Spec::Functions->import;
25 }
26
27 exit 1 unless(getopts('nvf'));  # TODO: this can probably have better output
28
29 # OS hackery
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).
34 #if($^O eq "msys"){
35 #       $/ = "\r\n";
36 #       $\ = "\r\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.
39 #}
40 if($^O eq "MSWin32"){
41         $DS = quotemeta('\\');
42         $DSP = '\\';
43 }
44
45 # make sure we're at the top level of a repo
46 if(! -d ".git"){
47         die "This is not the top level of a git repository.\n";
48 }
49
50 my $vtemp = `git config --local --get nethack.setupversion`;
51 chomp($vtemp);
52 if($vtemp > 0){
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);
56         }
57 }
58 # legacy check:
59 if(length $vtemp == 0){
60         if(`git config --get merge.NHsubst.name` =~ m/^Net/){
61                 $version_old = 1;
62                 print STDERR "Migrating to setup version 1\n" if($opt_v);
63         }
64 }
65
66 my $gitadddir = `git config --get nethack.gitadddir`;
67 chomp($gitadddir);
68 if(length $gitadddir){
69         if(! -d $gitadddir){
70                 die "nethack.gitadddir has invalid value '$gitadddir'\n";
71         }
72 }
73 print STDERR "nethack.gitadddir=$gitadddir\n" if($opt_v);
74
75 # This is (relatively) safe because we know we're at R in R/DEVEL/nhgitset.pl
76 my $srcdir = ($0 =~ m!^(.*)$DS!)[0];
77
78 if(! -f catfile($srcdir, 'nhgitset.pl')){
79         die "I can't find myself in '$srcdir'\n";
80 }
81
82 print STDERR "Copying from: $srcdir\n" if($opt_v);
83
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`;
92         if($xx !~ m/true/){
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";
95                 exit 2;
96         }
97 }
98
99
100
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");
107
108 print STDERR "Installing filter/merge\n" if($opt_v);
109
110 # XXXX need it in NHadd to find nhsub???
111 # removed at version 3
112 #if($^O eq "MSWin32"){
113 #       $cmd = '.git\\\\hooks\\\\NHtext';
114 #} else {
115 #       $cmd = catfile(curdir(),'.git','hooks','NHtext');
116 #}
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);
124
125         print STDERR "Removing NHtext\n" if($opt_v);
126         unlink catfile(curdir(),'.git','hooks','NHtext') unless($opt_n);
127 }
128
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");
132
133 print STDERR "Running directories\n" if($opt_v);
134
135 foreach my $dir ( glob("$srcdir$DS*") ){
136         next unless(-d $dir);
137
138         my $target = catfile($dir, 'TARGET');
139         next unless(-f $target);
140
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;
145         close TARGET;
146         print STDERR "Directory $dir -> $targetpath\n" if($opt_v);
147
148         my $enddir = $dir;
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);
155                 }
156                 &process_override($enddir, "POST");
157         }
158 }
159
160 &check_prefix;  # for variable substitution
161
162 if($version_old != $version_new){
163         print STDERR "Setting version to $version_new\n" if($opt_v);
164         if(! $opt_n){
165                 system("git config nethack.setupversion $version_new");
166                 if($?){
167                         die "Can't set nethack.setupversion $version_new: $?,$!\n";
168                 }
169         }
170 }
171
172 exit 0;
173
174 sub process_override {
175         my($srcdir, $plname) = @_;
176         return 0 unless(length $gitadddir);
177
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);
181
182         print STDERR "Running $plpath\n" if($opt_v);
183         # current directory is top of target repo
184
185         unless($opt_n){
186                 system("$plpath $opt_v") and die "Callout $plpath failed: $?\n";
187         }
188         return 1;
189 }
190
191 sub add_alias {
192         my($name, $def) = @_;
193         &add_config("alias.$name",$def);
194 }
195
196 sub add_config {
197         my($name, $val) = @_;
198         system('git', 'config', '--local', $name, $val) unless($opt_n);
199 }
200
201 sub check_prefix {
202         my $lcl = `git config --local --get nethack.substprefix`;
203         chomp($lcl);
204         if(0==length $lcl){
205                 my $other = `git config --get nethack.substprefix`;
206                 chomp($other);
207                 if(0==length $other){
208                         print STDERR "ERROR: nethack.substprefix is not set anywhere.  Set it and re-run.\n";
209                         exit 2;
210                 } else {
211                         &add_config('nethack.substprefix', $other);
212                         print STDERR "Copying prefix '$other' to local repository.\n" if($opt_v);
213                 }
214                 $lcl = $other;  # for display below
215         }
216         print "\n\nUsing prefix '$lcl' - PLEASE MAKE SURE THIS IS CORRECT\n\n";
217 }
218
219 sub do_dir_DOTGIT {
220 if(1){
221         # We are NOT going to mess with config now.
222         return;
223 } else {
224         my($srcdir, $targetdir) = @_;
225 #warn "do_dir_DOTGIT($srcdir, $targetdir)\n";
226         my $cname = "$srcdir/config";
227         if(-e $cname){
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: $!";
231                 my @data = <IN>;
232                 print CONFIG @data;
233                 close IN;
234                 close CONFIG;
235         } else {
236                 print STDERR " Nothing to add to .git/config\n" if($opt_v);
237         }
238 # XXX are there other files in .git that we might want to handle?
239 # So just in case:
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";
244         }
245 }
246 }
247
248 sub do_dir_hooksdir {
249         my($srcdir, $targetdir) = @_;
250
251         for my $path ( glob("$srcdir$DS*") ){
252
253                 next if( $path =~ m!.*${DS}TARGET$! );
254
255                 my $file = $path;
256
257                 $file =~ s!.*$DS!!;
258
259                 $file = catfile($targetdir, $file);
260
261                 next if($opt_n);
262
263                 open IN, "<", $path or die "Can't open $path: $!";
264                 open OUT, ">", "$file" or die "Can't open $file: $!";
265                 while(<IN>){
266                         print OUT;
267                 }
268                 close OUT;
269                 close IN;
270
271                 if(! -x $file){
272                         chmod 0755 ,$file;
273                 }
274         }
275 }
276
277 __END__
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
281
282
283
284 =head1 NAME
285
286 nhgitset.pl - Setup program for NetHack git repositories
287
288 =head1 SYNOPSIS
289
290  cd THE_REPO
291  [git config nethack.gitadddir GITADDDIR]
292  perl SOME_PATH/DEVEL/nhgitset.pl [-v][-n][-f]
293
294 =head1 DESCRIPTION
295
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
298 nhgitset.pl).
299
300 The follwing options are available:
301
302 B<-f>   Force.  Do not use this unless the program requests it.
303
304 B<-n>   Make no changes.
305
306 B<-v>   Verbose output.
307
308 =head1 CONFIG
309
310 nhgitset.pl uses the following non-standard C<git config> variables:
311
312 nethack.gitadddir
313
314    DOTGIT/INSTEAD
315    DOTGIT/PRE
316    DOTGIT/POST
317    hooksdir/INSTEAD
318    hooksdir/PRE
319    hooksdir/POST
320
321 nethack.setupversion
322
323 nethack.substprefix
324
325
326 =head1 EXIT STATUS
327
328 0       Success.
329
330 1       Fail.
331
332 2       Intervention required.