06c183af255fea7d8d86f4e4311add453bf90498
[ghc-hetmet.git] / sync-all
1 #!/usr/bin/perl -w
2
3 use strict;
4 use Cwd;
5
6 # Usage:
7 #
8 # ./sync-all [-q] [-s] [--ignore-failure] [-r repo]
9 #            [--nofib] [--testsuite] [--checked-out] cmd [git flags]
10 #
11 # Applies the command "cmd" to each repository in the tree.
12 # sync-all will try to do the right thing for both git and darcs repositories.
13 #
14 # e.g.
15 #      ./sync-all -r http://darcs.haskell.org/ghc get
16 #          To get any repos which do not exist in the local tree
17 #
18 #      ./sync-all pull
19 #          To pull everything from the default repos
20 #
21 # -------------- Flags -------------------
22 #   -q says to be quite, and -s to be silent.
23 #
24 #   --ignore-failure says to ignore errors and move on to the next repository
25 #
26 #   -r repo says to use repo as the location of package repositories
27 #
28 #   --checked-out says that the remote repo is in checked-out layout, as
29 #   opposed to the layout used for the main repo.  By default a repo on
30 #   the local filesystem is assumed to be checked-out, and repos accessed
31 #   via HTTP or SSH are assumed to be in the main repo layout; use
32 #   --checked-out to override the latter.
33 #
34 #   --nofib, --testsuite also get the nofib and testsuite repos respectively
35 #
36 # ------------ Which repos to use -------------
37 # sync-all uses the following algorithm to decide which remote repos to use
38 #
39 #  It always computes the remote repos from a single base, $repo_base
40 #  How is $repo_base set?  
41 #    If you say "-r repo", then that's $repo_base
42 #    otherwise $repo_base is set by asking git where the ghc repo came
43 #    from, and removing the last component (e.g. /ghc.git/ of /ghc/).
44 #
45 #  Then sync-all iterates over the package found in the file
46 #  ./packages; see that file for a description of the contents.
47
48 #    If $repo_base looks like a local filesystem path, or if you give
49 #    the --checked-out flag, sync-all works on repos of form
50 #          $repo_base/<local-path>
51 #    otherwise sync-all works on repos of form
52 #          $repo_base/<remote-path>
53 #    This logic lets you say
54 #      both    sync-all -r http://darcs.haskell.org/ghc-6.12 pull
55 #      and     sync-all -r ../HEAD pull
56 #    The latter is called a "checked-out tree".
57
58 # NB: sync-all *ignores* the defaultrepo of all repos other than the
59 # root one.  So the remote repos must be laid out in one of the two
60 # formats given by <local-path> and <remote-path> in the file 'packages'.
61
62 $| = 1; # autoflush stdout after each print, to avoid output after die
63
64 my $defaultrepo;
65 my @packages;
66 my $verbose = 2;
67 my $ignore_failure = 0;
68 my $checked_out_flag = 0;
69 my $get_mode;
70
71 my %tags;
72
73 # Figure out where to get the other repositories from.
74 sub getrepo {
75     my $basedir = ".";
76     my $repo;
77
78     if (defined($defaultrepo)) {
79         $repo = $defaultrepo;
80         chomp $repo;
81     } else {
82         # Figure out where to get the other repositories from,
83         # based on where this GHC repo came from.
84         my $branch = `git branch | grep "\* " | sed "s/^\* //"`; chomp $branch;
85         my $remote = `git config branch.$branch.remote`;         chomp $remote;
86         $repo = `git config remote.$remote.url`;       chomp $repo;
87     }
88
89     my $repo_base;
90     my $checked_out_tree;
91
92     if ($repo =~ /^...*:/) {
93         # HTTP or SSH
94         # Above regex says "at least two chars before the :", to avoid
95         # catching Win32 drives ("C:\").
96         $repo_base = $repo;
97
98         # --checked-out is needed if you want to use a checked-out repo
99         # over SSH or HTTP
100         if ($checked_out_flag) {
101             $checked_out_tree = 1;
102         } else {
103             $checked_out_tree = 0;
104         }
105
106         # Don't drop the last part of the path if specified with -r, as
107         # it expects repos of the form:
108         #
109         #   http://darcs.haskell.org
110         #
111         # rather than
112         #   
113         #   http://darcs.haskell.org/ghc
114         #
115         if (!$defaultrepo) {
116             $repo_base =~ s#/[^/]+/?$##;
117         }
118     }
119     elsif ($repo =~ /^\/|\.\.\/|.:(\/|\\)/) {
120         # Local filesystem, either absolute or relative path
121         # (assumes a checked-out tree):
122         $repo_base = $repo;
123         $checked_out_tree = 1;
124     }
125     else {
126         die "Couldn't work out repo";
127     }
128
129     return $repo_base, $checked_out_tree;
130 }
131
132 sub parsePackages {
133     my @repos;
134     my $lineNum;
135
136     open IN, "< packages" or die "Can't open packages file";
137     @repos = <IN>;
138     close IN;
139
140     @packages = ();
141     $lineNum = 0;
142     foreach (@repos) {
143         chomp;
144         $lineNum++;
145         if (/^([^# ]+) +([^ ]+) +([^ ]+) +([^ ]+) +([^ ]+)$/) {
146             my %line;
147             $line{"localpath"}  = $1;
148             $line{"tag"}        = $2;
149             $line{"remotepath"} = $3;
150             $line{"vcs"}        = $4;
151             $line{"upstream"}   = $5;
152             push @packages, \%line;
153         }
154         elsif (! /^(#.*)?$/) {
155             die "Bad content on line $lineNum of packages file: $_";
156         }
157     }
158 }
159
160 sub message {
161     if ($verbose >= 2) {
162         print "@_\n";
163     }
164 }
165
166 sub warning {
167     if ($verbose >= 1) {
168         print "warning: @_\n";
169     }
170 }
171
172 sub scm {
173     my $dir = shift;
174     my $scm = shift;
175     my $pwd;
176
177     if ($dir eq '.') {
178         message "== running $scm @_";
179     } else {
180         message "== $dir: running $scm @_";
181         $pwd = getcwd();
182         chdir($dir);
183     }
184
185     system ($scm, @_) == 0
186         or $ignore_failure
187         or die "$scm failed: $?";
188
189     if ($dir ne '.') {
190         chdir($pwd);
191     }
192 }
193
194 sub scmall {
195     my $command = shift;
196     
197     my $localpath;
198     my $tag;
199     my $remotepath;
200     my $scm;
201     my $upstream;
202     my $line;
203     my $branch_name;
204     my $subcommand;
205
206     my $path;
207     my $wd_before = getcwd;
208
209     my $pwd;
210     my @args;
211
212     my ($repo_base, $checked_out_tree) = getrepo();
213
214     my $is_github_repo = $repo_base =~ m/(git@|git:\/\/|https:\/\/)github.com/;
215
216     parsePackages;
217
218     @args = ();
219
220     if ($command =~ /^remote$/) {
221         while (@_ > 0 && $_[0] =~ /^-/) {
222             push(@args,shift);
223         }
224         if (@_ < 1) { help(); }
225         $subcommand = shift;
226         if ($subcommand ne 'add' && $subcommand ne 'rm' && $subcommand ne 'set-url') {
227             help();
228         }
229         while (@_ > 0 && $_[0] =~ /^-/) {
230             push(@args,shift);
231         }
232         if (($subcommand eq 'add' || $subcommand eq 'rm') && @_ < 1) {
233             help();
234         } elsif (@_ < 1) { # set-url
235             $branch_name = 'origin';
236         } else {
237             $branch_name = shift;
238         }
239     } elsif ($command eq 'new') {
240         if (@_ < 1) {
241             $branch_name = 'origin';
242         } else {
243             $branch_name = shift;
244         }
245     }
246
247     push(@args, @_);
248
249     for $line (@packages) {
250
251         $localpath  = $$line{"localpath"};
252         $tag        = $$line{"tag"};
253         $remotepath = $$line{"remotepath"};
254         $scm        = $$line{"vcs"};
255         $upstream   = $$line{"upstream"};
256
257         # Check the SCM is OK as early as possible
258         die "Unknown SCM: $scm" if (($scm ne "darcs") and ($scm ne "git"));
259
260         # We can't create directories on GitHub, so we translate
261         # "package/foo" into "package-foo".
262         if ($is_github_repo) {
263             $remotepath =~ s/\//-/;
264         }
265
266         # Work out the path for this package in the repo we pulled from
267         if ($checked_out_tree) {
268             $path = "$repo_base/$localpath";
269         }
270         else {
271             $path = "$repo_base/$remotepath";
272         }
273
274         if ($command =~ /^(?:g|ge|get)$/) {
275             # Skip any repositories we have not included the tag for
276             if (not defined($tags{$tag})) {
277                 $tags{$tag} = 0;
278             }
279             if ($tags{$tag} == 0) {
280                 next;
281             }
282             
283             if (-d $localpath) {
284                 warning("$localpath already present; omitting")
285                     if $localpath ne ".";
286                 if ($scm eq "git") {
287                     scm ($localpath, $scm, "config", "core.ignorecase", "true");
288                 }
289                 next;
290             }
291
292             # Note that we use "." as the path, as $localpath
293             # doesn't exist yet.
294             if ($scm eq "darcs") {
295                 # The first time round the loop, default the get-mode
296                 if (not defined($get_mode)) {
297                     warning("adding --partial, to override use --complete");
298                     $get_mode = "--partial";
299                 }
300                 scm (".", $scm, "get", $get_mode, $path, $localpath, @args);
301             }
302             else {
303                 scm (".", $scm, "clone", $path, $localpath, @args);
304                 scm ($localpath, $scm, "config", "core.ignorecase", "true");
305             }
306             next;
307         }
308
309         if (-d "$localpath/_darcs") {
310             if (-d "$localpath/.git") {
311                 die "Found both _darcs and .git in $localpath";
312             }
313             else {
314                 $scm = "darcs";
315             }
316         }
317         else {
318             if (-d "$localpath/.git") {
319                 $scm = "git";
320             }
321             elsif ($tag eq "") {
322                 die "Required repo $localpath is missing";
323             }
324             else {
325                 message "== $localpath repo not present; skipping";
326             }
327         }
328
329         # Work out the arguments we should give to the SCM
330         if ($command =~ /^(?:w|wh|wha|what|whats|whatsn|whatsne|whatsnew|status)$/) {
331             if ($scm eq "darcs") {
332                 $command = "whatsnew";
333             }
334             elsif ($scm eq "git") {
335                 $command = "status";
336             }
337             else {
338                 die "Unknown scm";
339             }
340
341             # Hack around 'darcs whatsnew' failing if there are no changes
342             $ignore_failure = 1;
343             scm ($localpath, $scm, $command, @args);
344         }
345         elsif ($command =~ /^commit$/) {
346             # git fails if there is nothing to commit, so ignore failures
347             $ignore_failure = 1;
348             scm ($localpath, $scm, "commit", @args);
349         }
350         elsif ($command =~ /^(?:pus|push)$/) {
351             scm ($localpath, $scm, "push", @args);
352         }
353         elsif ($command =~ /^(?:pul|pull)$/) {
354             scm ($localpath, $scm, "pull", @args);
355         }
356         elsif ($command =~ /^(?:s|se|sen|send)$/) {
357             if ($scm eq "darcs") {
358                 $command = "send";
359             }
360             elsif ($scm eq "git") {
361                 $command = "send-email";
362             }
363             else {
364                 die "Unknown scm";
365             }
366             scm ($localpath, $scm, $command, @args);
367         }
368         elsif ($command =~ /^fetch$/) {
369             scm ($localpath, $scm, "fetch", @args);
370         }
371         elsif ($command =~ /^new$/) {
372             my @scm_args = ("log", "$branch_name..");
373             scm ($localpath, $scm, @scm_args, @args);
374         }
375         elsif ($command =~ /^remote$/) {
376             my @scm_args;
377             if ($subcommand eq 'add') {
378                 @scm_args = ("remote", "add", $branch_name, $path);
379             } elsif ($subcommand eq 'rm') {
380                 @scm_args = ("remote", "rm", $branch_name);
381             } elsif ($subcommand eq 'set-url') {
382                 @scm_args = ("remote", "set-url", $branch_name, $path);
383             }
384             scm ($localpath, $scm, @scm_args, @args);
385         }
386         elsif ($command =~ /^grep$/) {
387             # Hack around 'git grep' failing if there are no matches
388             $ignore_failure = 1;
389             scm ($localpath, $scm, "grep", @args)
390                 unless $scm eq "darcs";
391         }
392         elsif ($command =~ /^reset$/) {
393             scm ($localpath, $scm, "reset", @args)
394                 unless $scm eq "darcs";
395         }
396         elsif ($command =~ /^config$/) {
397             scm ($localpath, $scm, "config", @args)
398                 unless $scm eq "darcs";
399         }
400         else {
401             die "Unknown command: $command";
402         }
403     }
404 }
405
406
407 sub help()
408 {
409         # Get the built in help
410         my $help = <<END;
411 What do you want to do?
412 Supported commands:
413
414  * whatsnew
415  * commit
416  * push
417  * pull
418  * get, with options:
419   * --<package-tag>
420   * --complete
421   * --partial
422  * fetch
423  * send
424  * new
425  * remote add <branch-name>
426  * remote rm <branch-name>
427  * remote set-url [--push] <branch-name>
428  * grep
429  * reset
430  * config
431
432 Available package-tags are:
433 END
434
435         # Collect all the tags in the packages file
436         my %available_tags;
437         open IN, "< packages" or die "Can't open packages file";
438         while (<IN>) {
439             chomp;
440             if (/^([^# ]+) +(?:([^ ]+) +)?([^ ]+) +([^ ]+)/) {
441                 if (defined($2) && $2 ne "-") {
442                     $available_tags{$2} = 1;
443                 }
444             }
445             elsif (! /^(#.*)?$/) {
446                 die "Bad line: $_";
447             }
448         }
449         close IN;
450         
451         # Show those tags and the help text
452         my @available_tags = keys %available_tags;
453         print "$help@available_tags\n";
454         exit 1;
455 }
456
457 sub main {
458     if (! -d ".git" || ! -d "compiler") {
459         die "error: sync-all must be run from the top level of the ghc tree."
460     }
461
462     $tags{"-"} = 1;
463     $tags{"dph"} = 1;
464
465     while ($#_ ne -1) {
466         my $arg = shift;
467         # We handle -q here as well as lower down as we need to skip over it
468         # if it comes before the source-control command
469         if ($arg eq "-q") {
470             $verbose = 1;
471         }
472         elsif ($arg eq "-s") {
473             $verbose = 0;
474         }
475         elsif ($arg eq "-r") {
476             $defaultrepo = shift;
477         }
478         elsif ($arg eq "--ignore-failure") {
479             $ignore_failure = 1;
480         }
481         elsif ($arg eq "--complete" || $arg eq "--partial") {
482             $get_mode = $arg;
483         }
484         # Use --checked-out if the remote repos are a checked-out tree,
485         # rather than the master trees.
486         elsif ($arg eq "--checked-out") {
487             $checked_out_flag = 1;
488         }
489         # --<tag> says we grab the libs tagged 'tag' with
490         # 'get'. It has no effect on the other commands.
491         elsif ($arg =~ m/^--no-(.*)$/) {
492             $tags{$1} = 0;
493         }
494         elsif ($arg =~ m/^--(.*)$/) {
495             $tags{$1} = 1;
496         }
497         else {
498             unshift @_, $arg;
499             if (grep /^-q$/, @_) {
500                 $verbose = 1;
501             }
502             last;
503         }
504     }
505
506     if ($#_ eq -1) {
507         help();
508     }
509     else {
510         # Give the command and rest of the arguments to the main loop
511         scmall @_;
512     }
513 }
514
515 main(@ARGV);
516