Factor out the packages file parsing in darcs-all
[ghc-hetmet.git] / darcs-all
1 #!/usr/bin/perl -w
2
3 use strict;
4
5 # Usage:
6 #
7 # ./darcs-all [-q] [-s] [-i] [-r repo]
8 #             [--nofib] [--testsuite] [--checked-out] cmd [darcs flags]
9 #
10 # Applies the darcs command "cmd" to each repository in the tree.
11 #
12 # e.g.
13 #      ./darcs-all -r http://darcs.haskell.org/ghc get
14 #          To get any repos which do not exist in the local tree
15 #
16 #      ./darcs-all -r ~/ghc-validate push
17 #          To push all your repos to the ~/ghc-validate tree
18 #
19 #      ./darcs-all pull -a
20 #          To pull everything from the default repos
21 #
22 #      ./darc-all push --dry-run
23 #          To see what local patches you have relative to the main repos
24 #
25 # -------------- Flags -------------------
26 #   -q says to be quite, and -s to be silent.
27 #
28 #   -i says to ignore darcs errors and move on to the next repository
29 #
30 #   -r repo says to use repo as the location of package repositories
31 #
32 #   --checked-out says that the remote repo is in checked-out layout, as
33 #   opposed to the layout used for the main repo.  By default a repo on
34 #   the local filesystem is assumed to be checked-out, and repos accessed
35 #   via HTTP or SSH are assumed to be in the main repo layout; use
36 #   --checked-out to override the latter.
37 #
38 #   --nofib, --testsuite also get the nofib and testsuite repos respectively
39 #
40 #   The darcs get flag you are most likely to want is --complete. By
41 #   default we pass darcs the --partial flag.
42 #
43 # ------------ Which repos to use -------------
44 # darcs-all uses the following algorithm to decide which remote repos to use
45 #
46 #  It always computes the remote repos from a single base, $repo_base
47 #  How is $repo_base set?  
48 #    If you say "-r repo", then that's $repo_base
49 #    othewise $repo_base is set thus:
50 #       look in _darcs/prefs/defaultrepo, 
51 #       and remove the trailing 'ghc'
52 #
53 #  Then darcs-all iterates over the package found in the file
54 #  ./packages, which has entries like:
55 #         libraries/array  packages/array  darcs
56 #    or, in general
57 #         <local-path>  <remote-path> <vcs>
58
59 #    If $repo_base looks like a local filesystem path, or if you give
60 #    the --checked-out flag, darcs-all works on repos of form
61 #          $repo_base/<local-path>
62 #    otherwise darcs-all works on repos of form
63 #          $repo_base/<remote-path>
64 #    This logic lets you say
65 #      both    darcs-all -r http://darcs.haskell.org/ghc-6.12 pull
66 #      and     darcs-all -r ../HEAD pull
67 #    The latter is called a "checked-out tree".
68
69 # NB: darcs-all *ignores* the defaultrepo of all repos other than the
70 # root one.  So the remote repos must be laid out in one of the two
71 # formats given by <local-path> and <remote-path> in the file 'packages'.
72
73
74 $| = 1; # autoflush stdout after each print, to avoid output after die
75
76 my $defaultrepo;
77
78 my $verbose = 2;
79 my $ignore_failure = 0;
80 my $want_remote_repo = 0;
81 my $checked_out_flag = 0;
82
83 my %tags;
84
85 my @packages;
86
87 # Figure out where to get the other repositories from.
88 sub getrepo {
89     my $basedir = ".";
90     my $repo = $defaultrepo || `cat $basedir/_darcs/prefs/defaultrepo`;
91     chomp $repo;
92
93     my $repo_base;
94     my $checked_out_tree;
95
96     if ($repo =~ /^...*:/) {
97         # HTTP or SSH
98         # Above regex says "at least two chars before the :", to avoid
99         # catching Win32 drives ("C:\").
100         $repo_base = $repo;
101
102         # --checked-out is needed if you want to use a checked-out repo
103         # over SSH or HTTP
104         if ($checked_out_flag) {
105             $checked_out_tree = 1;
106         } else {
107             $checked_out_tree = 0;
108         }
109
110         # Don't drop the last part of the path if specified with -r, as
111         # it expects repos of the form:
112         #
113         #   http://darcs.haskell.org
114         #
115         # rather than
116         #   
117         #   http://darcs.haskell.org/ghc
118         #
119         if (!$defaultrepo) {
120             $repo_base =~ s#/[^/]+/?$##;
121         }
122     }
123     elsif ($repo =~ /^\/|\.\.\/|.:(\/|\\)/) {
124         # Local filesystem, either absolute or relative path
125         # (assumes a checked-out tree):
126         $repo_base = $repo;
127         $checked_out_tree = 1;
128     }
129     else {
130         die "Couldn't work out repo";
131     }
132
133     return $repo_base, $checked_out_tree;
134 }
135
136 sub message {
137     if ($verbose >= 2) {
138         print "@_\n";
139     }
140 }
141
142 sub warning {
143     if ($verbose >= 1) {
144         print "warning: @_\n";
145     }
146 }
147
148 sub darcs {
149     message "== running darcs @_";
150     system ("darcs", @_) == 0
151        or $ignore_failure
152        or die "darcs failed: $?";
153 }
154
155 sub parsePackages {
156     my @repos;
157     my $lineNum;
158
159     my ($repo_base, $checked_out_tree) = getrepo();
160
161     open IN, "< packages" or die "Can't open packages file";
162     @repos = <IN>;
163     close IN;
164
165     @packages = ();
166     $lineNum = 0;
167     foreach (@repos) {
168         chomp;
169         $lineNum++;
170         if (/^([^# ]+) +([^ ]+) +([^ ]+) +([^ ]+) +([^ ]+)$/) {
171             my %line;
172             $line{"localpath"}  = $1;
173             $line{"tag"}        = $2;
174             $line{"remotepath"} = $3;
175             $line{"vcs"}        = $4;
176             $line{"upstream"}   = $5;
177             push @packages, \%line;
178         }
179         elsif (! /^(#.*)?$/) {
180             die "Bad content on line $lineNum of packages file: $_";
181         }
182     }
183 }
184
185 sub darcsall {
186     my $localpath;
187     my $remotepath;
188     my $path;
189     my $tag;
190     my @repos;
191     my $command = $_[0];
192     my $line;
193
194     my ($repo_base, $checked_out_tree) = getrepo();
195
196     for $line (@packages) {
197         $localpath  = $$line{"localpath"};
198         $tag        = $$line{"tag"};
199         $remotepath = $$line{"remotepath"};
200
201         if ($checked_out_tree) {
202             $path = "$repo_base/$localpath";
203         }
204         else {
205             $path = "$repo_base/$remotepath";
206         }
207
208         if (-d "$localpath/_darcs") {
209             if ($want_remote_repo) {
210                 if ($command =~ /^opt/) {
211                     # Allows ./darcs-all optimize --relink
212                     darcs (@_, "--repodir", $localpath, "--sibling=$path");
213                 } else {
214                     darcs (@_, "--repodir", $localpath, $path);
215                 }
216             } else {
217                 darcs (@_, "--repodir", $localpath);
218             }
219         }
220         elsif ($tag eq "-") {
221             message "== Required repo $localpath is missing! Skipping";
222         }
223         else {
224             message "== $localpath repo not present; skipping";
225         }
226     }
227 }
228
229 sub darcsget {
230     my $r_flags;
231     my $localpath;
232     my $remotepath;
233     my $path;
234     my $tag;
235     my @repos;
236     my $line;
237
238     my ($repo_base, $checked_out_tree) = getrepo();
239
240     if (! grep /(?:--complete|--partial|--lazy)/, @_) {
241         warning("adding --partial, to override use --complete");
242         $r_flags = [@_, "--partial"];
243     }
244     else {
245         $r_flags = \@_;
246     }
247
248     for $line (@packages) {
249         $localpath  = $$line{"localpath"};
250         $tag        = $$line{"tag"};
251         $remotepath = $$line{"remotepath"};
252
253         if ($checked_out_tree) {
254             $path = "$repo_base/$localpath";
255         }
256         else {
257             $path = "$repo_base/$remotepath";
258         }
259
260         if (($tag eq "-") || defined($tags{$tag})) {
261             if (-d $localpath) {
262                 warning("$localpath already present; omitting");
263             }
264             else {
265                 darcs (@$r_flags, $path, $localpath);
266             }
267         }
268     }
269 }
270
271 sub main {
272     if (! -d "compiler") {
273         die "error: darcs-all must be run from the top level of the ghc tree."
274     }
275
276     while ($#_ ne -1) {
277         my $arg = shift;
278         # We handle -q here as well as lower down as we need to skip over it
279         # if it comes before the darcs command
280         if ($arg eq "-q") {
281             $verbose = 1;
282         }
283         elsif ($arg eq "-s") {
284             $verbose = 0;
285         }
286         elsif ($arg eq "-r") {
287             $defaultrepo = shift;
288         }
289         elsif ($arg eq "-i") {
290             $ignore_failure = 1;
291         }
292         # --nofib tells get to also grab the nofib repo.
293         # It has no effect on the other commands.
294         elsif ($arg eq "--nofib") {
295             $tags{"nofib"} = 1;
296         }
297         # --testsuite tells get to also grab the testsuite repo.
298         # It has no effect on the other commands.
299         elsif ($arg eq "--testsuite") {
300             $tags{"testsuite"} = 1;
301         }
302         # --extralibs tells get to also grab the extra repos.
303         # It has no effect on the other commands.
304         elsif ($arg eq "--extra") {
305             $tags{"extra"} = 1;
306         }
307         elsif ($arg eq "--checked-out") {
308             $checked_out_flag = 1;
309         }
310         else {
311             unshift @_, $arg;
312             if (grep /^-q$/, @_) {
313                 $verbose = 1;
314             }
315             last;
316         }
317     }
318
319     if ($#_ eq -1) {
320         die "What do you want to do?";
321     }
322     my $command = $_[0];
323     parsePackages;
324     if ($command eq "get") {
325         darcsget @_;
326     }
327     else {
328         if ($command =~ /^(?:w|wh|wha|what|whats|whatsn|whatsne|whatsnew)$/) {
329             # Hack around whatsnew failing if there are no changes
330             $ignore_failure = 1;
331         }
332         if ($command =~ /^(pul|pus|sen|put|opt)/) {
333             $want_remote_repo = 1;
334         }
335         darcsall @_;
336     }
337 }
338
339 END {
340     my $ec = $?;
341
342     message "== Checking for old bytestring repo";
343     if (-d "libraries/bytestring/_darcs") {
344         if ((system "darcs annotate --repodir libraries/bytestring --match 'hash 20080118173113-3fd76-d5b74c04372a297b585ebea4e16d524551ce5035' > /dev/null 2> /dev/null") == 0) {
345             print <<EOF;
346 ============================
347 ATTENTION!
348
349 You have an old bytestring repository in your GHC tree!
350
351 Please remove it (e.g. "rm -r libraries/bytestring"), and the new
352 version of bytestring will be used from a tarball instead.
353 ============================
354 EOF
355         }
356     }
357
358     message "== Checking for bytestring tarball";
359     if (-d "libraries/bytestring" && not -d "libraries/bytestring/_darcs") {
360         print <<EOF;
361 ============================
362 ATTENTION!
363
364 You have an old bytestring in your GHC tree!
365
366 Please remove it (e.g. "rm -r libraries/bytestring"), and then run
367 "./darcs-all get" to get the darcs repository.
368 ============================
369 EOF
370     }
371
372     message "== Checking for unpulled tarball patches";
373     if ((system "darcs annotate --match 'hash 20090930200358-3fd76-cab3bf4a0a9e3902eb6dd41f71712ad3a6a9bcd1' > /dev/null 2> /dev/null") == 0) {
374         print <<EOF;
375 ============================
376 ATTENTION!
377
378 You have the unpulled tarball patches in your GHC tree!
379
380 Please remove them:
381     darcs unpull -p "Use mingw tarballs to get mingw on Windows"
382 and say yes to each patch.
383 ============================
384 EOF
385     }
386
387     $? = $ec;
388 }
389
390 main(@ARGV);
391