8 # ./sync-all [-q] [-s] [--ignore-failure] [-r repo]
9 # [--nofib] [--testsuite] [--checked-out] cmd [git flags]
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.
15 # ./sync-all -r http://darcs.haskell.org/ghc get
16 # To get any repos which do not exist in the local tree
19 # To pull everything from the default repos
21 # -------------- Flags -------------------
22 # -q says to be quite, and -s to be silent.
24 # --ignore-failure says to ignore errors and move on to the next repository
26 # -r repo says to use repo as the location of package repositories
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.
34 # --nofib, --testsuite also get the nofib and testsuite repos respectively
36 # ------------ Which repos to use -------------
37 # sync-all uses the following algorithm to decide which remote repos to use
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/).
45 # Then sync-all iterates over the package found in the file
46 # ./packages; see that file for a description of the contents.
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".
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'.
62 $| = 1; # autoflush stdout after each print, to avoid output after die
67 my $ignore_failure = 0;
68 my $checked_out_flag = 0;
71 # Flags specific to a particular command
72 my $local_repo_unnecessary = 0;
76 # Figure out where to get the other repositories from.
81 if (defined($defaultrepo)) {
85 # Figure out where to get the other repositories from,
86 # based on where this GHC repo came from.
87 my $branch = `git branch | grep "\* " | sed "s/^\* //"`; chomp $branch;
88 my $remote = `git config branch.$branch.remote`; chomp $remote;
89 $repo = `git config remote.$remote.url`; chomp $repo;
95 if ($repo =~ /^...*:/) {
97 # Above regex says "at least two chars before the :", to avoid
98 # catching Win32 drives ("C:\").
101 # --checked-out is needed if you want to use a checked-out repo
103 if ($checked_out_flag) {
104 $checked_out_tree = 1;
106 $checked_out_tree = 0;
109 # Don't drop the last part of the path if specified with -r, as
110 # it expects repos of the form:
112 # http://darcs.haskell.org
116 # http://darcs.haskell.org/ghc
119 $repo_base =~ s#/[^/]+/?$##;
122 elsif ($repo =~ /^\/|\.\.\/|.:(\/|\\)/) {
123 # Local filesystem, either absolute or relative path
124 # (assumes a checked-out tree):
126 $checked_out_tree = 1;
129 die "Couldn't work out repo";
132 return $repo_base, $checked_out_tree;
139 open IN, "< packages" or die "Can't open packages file";
148 if (/^([^# ]+) +([^ ]+) +([^ ]+) +([^ ]+) +([^ ]+)$/) {
150 $line{"localpath"} = $1;
152 $line{"remotepath"} = $3;
154 $line{"upstream"} = $5;
155 push @packages, \%line;
157 elsif (! /^(#.*)?$/) {
158 die "Bad content on line $lineNum of packages file: $_";
171 print "warning: @_\n";
181 message "== running $scm @_";
183 message "== $dir: running $scm @_";
188 system ($scm, @_) == 0
190 or die "$scm failed: $?";
210 my $wd_before = getcwd;
217 my ($repo_base, $checked_out_tree) = getrepo();
219 my $is_github_repo = $repo_base =~ m/(git@|git:\/\/|https:\/\/)github.com/;
225 if ($command =~ /^remote$/) {
226 while (@_ > 0 && $_[0] =~ /^-/) {
229 if (@_ < 1) { help(); }
231 if ($subcommand ne 'add' && $subcommand ne 'rm' && $subcommand ne 'set-url') {
234 while (@_ > 0 && $_[0] =~ /^-/) {
237 if (($subcommand eq 'add' || $subcommand eq 'rm') && @_ < 1) {
239 } elsif (@_ < 1) { # set-url
240 $branch_name = 'origin';
242 $branch_name = shift;
244 } elsif ($command eq 'new' || $command eq 'fetch') {
246 $branch_name = 'origin';
248 $branch_name = shift;
254 for $line (@packages) {
256 $localpath = $$line{"localpath"};
257 $tag = $$line{"tag"};
258 $remotepath = $$line{"remotepath"};
259 $scm = $$line{"vcs"};
260 $upstream = $$line{"upstream"};
262 # Check the SCM is OK as early as possible
263 die "Unknown SCM: $scm" if (($scm ne "darcs") and ($scm ne "git"));
265 # We can't create directories on GitHub, so we translate
266 # "package/foo" into "package-foo".
267 if ($is_github_repo) {
268 $remotepath =~ s/\//-/;
271 # Work out the path for this package in the repo we pulled from
272 if ($checked_out_tree) {
273 $path = "$repo_base/$localpath";
276 $path = "$repo_base/$remotepath";
279 if ($command =~ /^(?:g|ge|get)$/) {
280 # Skip any repositories we have not included the tag for
281 if (not defined($tags{$tag})) {
284 if ($tags{$tag} == 0) {
289 warning("$localpath already present; omitting")
290 if $localpath ne ".";
294 # The first time round the loop, default the get-mode
295 if ($scm eq "darcs" && not defined($get_mode)) {
296 warning("adding --partial, to override use --complete");
297 $get_mode = "--partial";
300 # The only command that doesn't need a repo
301 $local_repo_unnecessary = 1;
303 if ($scm eq "darcs") {
304 # Note: we can only use the get-mode with darcs for now
305 @scm_args = ("get", $get_mode, $path, $localpath);
308 @scm_args = ("clone", $path, $localpath);
311 # Note that we use "." as the path, as $localpath
313 scm (".", $scm, @scm_args, @args);
317 if (-d "$localpath/_darcs") {
318 if (-d "$localpath/.git") {
319 die "Found both _darcs and .git in $localpath";
326 if (-d "$localpath/.git") {
330 die "Required repo $localpath is missing";
333 message "== $localpath repo not present; skipping";
337 # Work out the arguments we should give to the SCM
338 if ($command =~ /^(?:w|wh|wha|what|whats|whatsn|whatsne|whatsnew|status)$/) {
339 @scm_args = (($scm eq "darcs" and "whatsnew")
340 or ($scm eq "git" and "status"));
342 # Hack around 'darcs whatsnew' failing if there are no changes
345 elsif ($command =~ /^commit$/) {
346 @scm_args = ("commit");
347 # git fails if there is nothing to commit, so ignore failures
350 elsif ($command =~ /^(?:pus|push)$/) {
353 elsif ($command =~ /^(?:pul|pull)$/) {
356 elsif ($command =~ /^(?:s|se|sen|send)$/) {
357 @scm_args = (($scm eq "darcs" and "send")
358 or ($scm eq "git" and "send-email"));
360 elsif ($command =~ /^fetch$/) {
361 @scm_args = ("fetch", "$branch_name");
363 elsif ($command =~ /^new$/) {
364 @scm_args = ("log", "$branch_name..");
366 elsif ($command =~ /^remote$/) {
367 if ($subcommand eq 'add') {
368 @scm_args = ("remote", "add", $branch_name, $path);
369 } elsif ($subcommand eq 'rm') {
370 @scm_args = ("remote", "rm", $branch_name);
371 } elsif ($subcommand eq 'set-url') {
372 @scm_args = ("remote", "set-url", $branch_name, $path);
375 elsif ($command =~ /^grep$/) {
376 @scm_args = ("grep");
377 # Hack around 'git grep' failing if there are no matches
380 elsif ($command =~ /^reset$/) {
383 elsif ($command =~ /^config$/) {
384 @scm_args = "config";
387 die "Unknown command: $command";
390 # Actually execute the command
391 scm ($localpath, $scm, @scm_args, @args);
398 # Get the built in help
400 What do you want to do?
414 * remote add <branch-name>
415 * remote rm <branch-name>
416 * remote set-url [--push] <branch-name>
421 Available package-tags are:
424 # Collect all the tags in the packages file
426 open IN, "< packages" or die "Can't open packages file";
429 if (/^([^# ]+) +(?:([^ ]+) +)?([^ ]+) +([^ ]+)/) {
430 if (defined($2) && $2 ne "-") {
431 $available_tags{$2} = 1;
434 elsif (! /^(#.*)?$/) {
440 # Show those tags and the help text
441 my @available_tags = keys %available_tags;
442 print "$help@available_tags\n";
447 if (! -d ".git" || ! -d "compiler") {
448 die "error: sync-all must be run from the top level of the ghc tree."
456 # We handle -q here as well as lower down as we need to skip over it
457 # if it comes before the source-control command
461 elsif ($arg eq "-s") {
464 elsif ($arg eq "-r") {
465 $defaultrepo = shift;
467 elsif ($arg eq "--ignore-failure") {
470 elsif ($arg eq "--complete" || $arg eq "--partial") {
473 # Use --checked-out if the remote repos are a checked-out tree,
474 # rather than the master trees.
475 elsif ($arg eq "--checked-out") {
476 $checked_out_flag = 1;
478 # --<tag> says we grab the libs tagged 'tag' with
479 # 'get'. It has no effect on the other commands.
480 elsif ($arg =~ m/^--no-(.*)$/) {
483 elsif ($arg =~ m/^--(.*)$/) {
488 if (grep /^-q$/, @_) {
499 # Give the command and rest of the arguments to the main loop