Fix darcs-all get
[ghc-hetmet.git] / darcs-all
1 #!/usr/bin/perl -w
2
3 use strict;
4
5 # Figure out where to get the other repositories from,
6 # based on where this GHC repo came from.
7 my $defaultrepo = `cat _darcs/prefs/defaultrepo`;
8 chomp $defaultrepo;
9 my $defaultrepo_base;
10 my $checked_out_tree;
11
12 if ($defaultrepo =~ /^...*:/) {
13     # HTTP or SSH
14     # Above regex says "at least two chars before the :", to avoid
15     # catching Win32 drives ("C:\").
16     $defaultrepo_base = $defaultrepo;
17     $defaultrepo_base =~ s#/[^/]+/?$##;
18     $checked_out_tree = 0;
19 }
20 elsif ($defaultrepo =~ /^\/|\.\.\/|.:(\/|\\)/) {
21     # Local filesystem, either absolute or relative path
22     # (assumes a checked-out tree):
23     $defaultrepo_base = $defaultrepo;
24     $checked_out_tree = 1;
25 }
26 else {
27     die "Couldn't work out defaultrepo";
28 }
29
30 my $verbose = 2;
31 my $ignore_failure = 0;
32
33 my %tags;
34
35 sub message {
36     if ($verbose >= 2) {
37         print "@_\n";
38     }
39 }
40
41 sub warning {
42     if ($verbose >= 1) {
43         print "warning: @_\n";
44     }
45 }
46
47 sub darcs {
48     message "== running darcs @_";
49     system ("darcs", @_) == 0
50         or $ignore_failure
51         or die "darcs failed: $?";
52 }
53
54 sub darcsall {
55     my $localpath;
56     my $path;
57     my $tag;
58
59     open IN, "< packages" or die "Can't open packages file";
60     while (<IN>) {
61         chomp;
62         if (/^([^# ]+) +(?:([^ ]+) +)?([^ ]+) +([^ ]+)$/) {
63             $localpath = $1;
64             $tag = defined($2) ? $2 : "";
65
66             if (-d "$localpath/_darcs") {
67                 darcs (@_, "--repodir", $localpath);
68             }
69             elsif ($tag eq "") {
70                 message "== Required repo $localpath is missing! Skipping";
71             }
72             else {
73                 message "== $localpath repo not present; skipping";
74             }
75         }
76         elsif (! /^(#.*)?$/) {
77             die "Bad line: $_";
78         }
79     }
80     close IN;
81 }
82
83 sub darcsget {
84     my $r_flags;
85     my $localpath;
86     my $remotepath;
87     my $path;
88     my $tag;
89
90     if (! grep /(?:--complete|--partial)/, @_) {
91         warning("adding --partial, to override use --complete");
92         $r_flags = [@_, "--partial"];
93     }
94     else {
95         $r_flags = \@_;
96     }
97
98     open IN, "< packages" or die "Can't open packages file";
99     while (<IN>) {
100         chomp;
101         if (/^([^ ]+) +(?:([^ ]+) +)?([^ ]+) +([^ ]+)$/) {
102             $localpath = $1;
103             $tag = defined($2) ? $2 : "";
104             $remotepath = $3;
105
106             if ($checked_out_tree) {
107                 $path = "$defaultrepo_base/$localpath";
108             }
109             else {
110                 $path = "$defaultrepo_base/$remotepath";
111             }
112
113             if (($tag eq "") || defined($tags{$tag})) {
114                 if (-d $localpath) {
115                     warning("$localpath already present; omitting");
116                 }
117                 else {
118                     darcs (@$r_flags, $path, $localpath);
119                 }
120             }
121         }
122         elsif (! /^(#.*)?$/) {
123             die "Bad line: $_";
124         }
125     }
126     close IN;
127 }
128
129 sub main {
130     if (! -d "_darcs" || ! -d "compiler") {
131         die "error: darcs-all must be run from the top level of the ghc tree."
132     }
133
134     while ($#_ ne -1) {
135         my $arg = shift;
136         # We handle -q here as well as lower down as we need to skip over it
137         # if it comes before the darcs command
138         if ($arg eq "-q") {
139             $verbose = 1;
140         }
141         elsif ($arg eq "-s") {
142             $verbose = 0;
143         }
144         # --dph says we grab the dph libs with 'get'.
145         # It has no effect on the other commands.
146         elsif ($arg eq "--dph") {
147             $tags{"dph"} = 1;
148         }
149         # --extra says we grab the extra libs with 'get'.
150         # It has no effect on the other commands.
151         elsif ($arg eq "--extra") {
152             $tags{"extralibs"} = 1;
153         }
154         # --nofib tells get to also grab the nofib repo.
155         # It has no effect on the other commands.
156         elsif ($arg eq "--nofib") {
157             $tags{"nofib"} = 1;
158         }
159         # --testsuite tells get to also grab the testsuite repo.
160         # It has no effect on the other commands.
161         elsif ($arg eq "--testsuite") {
162             $tags{"testsuite"} = 1;
163         }
164         else {
165             unshift @_, $arg;
166             if (grep /^-q$/, @_) {
167                 $verbose = 1;
168             }
169             last;
170         }
171     }
172
173     if ($#_ eq -1) {
174         die "What do you want to do?";
175     }
176     my $command = $_[0];
177     if ($command eq "get") {
178         darcsget @_;
179     }
180     else {
181         if ($command =~ /^(?:w|wh|wha|what|whats|whatsn|whatsne|whatsnew)$/) {
182             # Hack around whatsnew failing if there are no changes
183             $ignore_failure = 1;
184         }
185         darcsall @_;
186     }
187 }
188
189 main(@ARGV);
190