Skip to content
This repository
Newer
Older
100644 167 lines (108 sloc) 3.211 kb
a67d1e45 »
2009-12-17 A command line utility to fork and clone from gitpan
1 #!/usr/bin/env perl
2
3 use strict;
4 use warnings;
5 use v5.10;
6
7 use autodie;
8 use Net::GitHub::V2::Repositories;
9 use Getopt::Long::Descriptive;
10
11 my @Options = (
12 [ "login=s" => "github.com username" ],
13 [ "token=s" => "github.com API token" ],
14 [ "clone!" => "don't clone after forking" ],
15 );
16
17 my %Commands = (
18 fork => \&command_fork,
19 clone => \&command_clone,
20 );
21
22 my($Opts, $Usage) = describe_options(
23 "usage: %c <command> %o",
24 @Options
25 );
26 $Opts->{clone} = 1 unless exists $Opts->{clone};
27
28 read_config_file($Opts);
29
30 main($Opts, @ARGV);
31
32 sub main {
33 my($options, @args) = @_;
34 my $command = shift @args;
35
36 my $func = get_command($command);
37 return $func->($options, @args);
38 }
39
40 sub get_command {
41 my $command = shift;
42
43 my $func = $Commands{$command};
44 die <<ERROR unless $func;
45 Unknown command $command.
46 Valid commands are @{[ join ", ", keys %Commands ]}.
47 ERROR
48
49 return $func;
50 }
51
52 sub command_fork {
53 my($opts, $repo) = @_;
54
55 die "You must supply your github login and token to fork.\n"
56 unless $opts->{login} and $opts->{token};
57
58 my $github = Net::GitHub::V2::Repositories->new(
59 login => $opts->{login},
60 token => $opts->{token},
61 owner => "gitpan",
62 repo => $repo,
63 );
64
65 my $result = $github->fork;
66 die <<ERROR if $result->{error};
67 Forking gitpan/$repo failed. Github had this to say:
68 $result->{error}
69 ERROR
70
71 say <<OUT;
72 Successful fork of gitpan/$repo to $opts->{login}/$repo.
73 New repository at $result->{url}
74 OUT
75
7b22fb5b »
2009-12-17 Checkout the private repository of a fork. Make --noclone work.
76 return unless $opts->{clone};
77
78 system "git", "clone", "git\@github.com:$opts->{login}/$repo";
a67d1e45 »
2009-12-17 A command line utility to fork and clone from gitpan
79 }
80
81 sub command_clone {
82 my($opts, $repo) = @_;
83
84 system "git", "clone", "git://github.com/gitpan/$repo.git";
85 }
86
87
88 sub read_config_file {
89 my $opts = shift;
90
91 my $file = "$ENV{HOME}/.gitpan";
92 return unless -r $file;
93
94 open my($fh), $file;
95 while(my $line = <$fh>) {
96 next if $line =~ /^#/;
97
98 chomp $line;
99 my($key, $value) = split /\s*:\s*/, $line, 2;
100
101 $opts->{$key} = $value unless exists $opts->{$key};
102 }
103 }
104
105
106 =head1 NAME
107
108 gitpan - Utility to access gitpan repositories
109
110 =head1 SYNOPSIS
111
112 gitpan clone <distribution>
113 gitpan fork <distribution>
114
115 =head1 DESCRIPTION
116
117 gitpan is a utility to automate some common things you'll want to do
118 with gitpan repositories (see L<http://github.com/gitpan>).
119
120 =head1 COMMANDS
121
122 =head2 clone
123
124 gitpan clone <distribution>
125
126 Does a git clone of the distribution. Little more than a thin wrapper around:
127
128 git clone git://github.com/gitpan/distribution
129
130 For example:
131
132 gitpan clone DateTime
133
134
135 =head2 fork
136
137 gitpan --login=<login> --token=<token> fork <distribution>
138
139 Makes a fork of a gitpan distribution and then clones it.
140
141 Both C<--login> and C<--token> necessary to create the fork on github.
142
143 If C<--noclone> is given
144
145 =head1 OPTIONS
146
147 Default options can be given in a .gitpan file in your home directory.
148
149 For example:
150
151 login: you
152 token: deadbeef
153
154 =head2 login
155
156 Your github login name.
157
158 =head2 token
159
160 Your github API authorization token. Its in L<https://github.com/account>.
161
162 =head2 noclone
163
164 Don't clone after forking.
165
166 =cut
Something went wrong with that request. Please try again.