]> git.madduck.net Git - code/myrepos.git/blob - webcheckout

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

All patches and comments are welcome. Please squash your changes to logical commits before using git-format-patch and git-send-email to patches@git.madduck.net. If you'd read over the Git project's submission guidelines and adhered to them, I'd be especially grateful.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

fix handling of repos w/o titles
[code/myrepos.git] / webcheckout
1 #!/usr/bin/perl
2
3 =head1 NAME
4
5 webcheckout - check out repositories referenced on a web page
6
7 =head1 SYNOPSIS
8
9 B<webcheckout> [options] url [destdir]
10
11 =head1 DESCRIPTION
12
13 B<webcheckout> downloads an url and parses it, looking for version control 
14 repositories referenced by the page. It checks out each repository into
15 a subdirectory of the current directory, using whatever VCS program is
16 appropriate for that repository (git, svn, etc).
17
18 The information about the repositories is embedded in the web page using
19 the rel=vcs microformat, which is documented at
20 <http://kitenet.net/~joey/rfc/rel-vcs/>.
21
22 If the optional destdir parameter is specified, VCS programs will be asked
23 to check out repositories into that directory. If there are multiple
24 repositories to check out, each will be checked out into a separate
25 subdirectory of the destdir.
26
27 =head1 OPTIONS
28
29 =over 4
30
31 =item -a, --auth
32
33 Prefer authenticated repositories. By default, webcheckout will use
34 anonymous repositories when possible. If you have an account that
35 allows you to use authenticated repositories, you might want to use this
36 option.
37
38 =item -n
39
40 Do not actually check anything out, just print out the commands that would
41 be run to check out the repositories.
42
43 =item -q
44
45 Quiet mode. Do not print out the commands being run. (The VCS commands
46 may still be noisy however.)
47
48 =back
49
50 =head1 AUTHOR
51
52 Copyright 2009 Joey Hess <joey@kitenet.net>
53
54 Licensed under the GNU GPL version 2 or higher.
55
56 This program is included in mr <http://kitenet.net/~joey/code/mr/>
57
58 =cut
59
60 use LWP::Simple;
61 use HTML::Parser;
62 use Getopt::Long;
63 use warnings;
64 use strict;
65
66 # What to download.
67 my $url;
68
69 # Controls whether to print what is being done.
70 my $quiet=0;
71
72 # Controls whether to actually check anything out.
73 my $noact=0;
74
75 # Controls whether to perfer repos that use authentication.
76 my $want_auth=0;
77
78 # Controls where to check out to. If not set, the vcs is allowed to
79 # decide.
80 my $destdir;
81
82 # how to perform checkouts
83 my %handlers=(
84         git => sub { doit("git", "clone", shift, $destdir) },
85         svn => sub { doit("svn", "checkout", shift, $destdir) },
86         bzr => sub { doit("bzr", "branch", shift, $destdir) },
87 );
88
89 # Regexps matching urls that are used for anonymous
90 # repository checkouts. The order is significant:
91 # urls matching earlier in the list are preferred over
92 # those matching later.
93 my @anon_urls=(
94         qr/^git:\/\//i,
95         qr/^bzr:\/\//i,
96         qr/^svn:\/\//i,
97         qr/^http:\/\//i, # generally the worst transport
98 );
99
100 sub getopts {
101         Getopt::Long::Configure("bundling", "no_permute");
102         my $result=GetOptions(
103                 "q|quiet" => \$quiet,
104                 "n|noact" => \$noact,
105                 "a|auth", => \$want_auth,
106         );
107         if (! $result || @ARGV < 1) {
108                 die "usage: webcheckout [options] url [destdir]\n";
109         }
110
111         $url=shift @ARGV;
112         $destdir=shift @ARGV;
113
114         if ($noact) {
115                 $quiet=0;
116         }
117 }
118
119 sub doit {
120         my @args=grep { defined } @_;
121         print join(" ", @args)."\n" unless $quiet;
122         return 0 if $noact;
123         return system(@args);
124 }
125
126 # Is repo a better than repo b?
127 sub better {
128         my ($a, $b)=@_;
129
130         my $firstanon=$b;
131         foreach my $r (@anon_urls) {
132                 if ($a->{href} =~ /$r/) {
133                         $firstanon=$a;
134                         last;
135                 }
136                 elsif ($b->{href} =~ /$r/) {
137                         $firstanon=$b;
138                         last;
139                 }
140         }
141
142         if ($want_auth) {
143                 return $firstanon != $a;
144         }
145         else {
146                 return $firstanon == $a;
147         }
148 }
149
150 # Eliminate duplicate repositories from list.
151 # Duplicate repositories have the same title, or the same href.
152 sub dedup {
153         my %seenhref;
154         my %bytitle;
155         my @others;
156         foreach my $repo (@_) {
157                 if (exists $repo->{title} &&
158                     length $repo->{title}) {
159                         if (exists $bytitle{$repo->{title}}) {
160                                 my $other=$bytitle{$repo->{title}};
161                                 next unless better($repo, $other);
162                                 delete $bytitle{$other->{title}}
163                         }
164
165                         if (! $seenhref{$repo->{href}}++) {
166                                 $bytitle{$repo->{title}}=$repo;
167                         }
168                 }
169                 else {
170                         push @others, $repo;
171                 }
172         }
173
174         return values %bytitle, @others;
175 }
176
177 sub parse {
178         my $page=shift;
179
180         my @ret;
181         my $parser=HTML::Parser->new(api_version => 3);
182         $parser->handler(start => sub {
183                 my $tagname=shift;
184                 my $attr=shift;
185                 return if lc $tagname ne 'link';
186                 return if ! exists $attr->{rel} || lc $attr->{rel} ne 'vcs';
187                 return if ! exists $attr->{href} || ! length $attr->{href};
188                 return if ! exists $attr->{type} || ! length $attr->{type};
189                 push @ret, $attr;
190         }, "tagname, attr");
191         $parser->parse($page);
192         $parser->eof;
193
194         return @ret;
195 }
196
197 getopts();
198
199 my $page=get($url);
200 if (! defined $page) {
201         die "failed to download $url\n";
202 }
203
204 my @repos=dedup(parse($page));
205 if (! @repos) {
206         die "no repositories found on $url\n";
207 }
208
209 if (defined $destdir && @repos > 1) {
210         # create subdirs of $destdir for the multiple repos
211         mkdir($destdir);
212         chdir($destdir) || die "failed to chdir to $destdir: $!";
213         $destdir=undef;
214 }
215
216 my $errors=0;
217 foreach my $repo (@repos) {
218         my $handler=$handlers{$repo->{type}};
219         if ($handler) {
220                 if ($handler->($repo->{href}) != 0) {
221                         print STDERR "failed to checkout ".$repo->{href}."\n";
222                         $errors++;
223                 }
224         }
225         else {
226                 print STDERR "unknown repository type ".$repo->{type}.
227                         " for ".$repo->{href}."\n";
228                 $errors++;
229         }
230 }
231 exit($errors > 0);
232
233 #use Data::Dumper;
234 #print Dumper(\@repos);