summaryrefslogtreecommitdiffstats
path: root/bin/git/git-hub
blob: 953aa9cf769dc48be1bc2db027cad41c646ba97b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#!/usr/bin/env perl
use strict;
use warnings;

package App::Git::Hub;

use File::pushd;
use List::MoreUtils qw(any);
use Net::GitHub::V2;

sub git {
    if (wantarray) {
        chomp(my @ret = qx{git $_[0]});
        return @ret;
    }
    else {
        chomp(my $ret = qx{git $_[0]});
        return $ret;
    }
}

sub new {
    my $class = shift;
    my @argv = @_;

    bless {
        argv  => \@argv,
    }, $class;
}

sub user  { shift->_github->login }
sub argv  { @{ shift->{argv} }    }

sub shift_argv {
    my $self = shift;
    return shift @{ $self->{argv} };
}

sub run {
    my $self = shift;

    my $command = $self->shift_argv;
    $self->usage unless defined $command;
    $self->run_command($command);
}

sub usage {
    my @subcommands = do { no strict 'refs'; grep { /^run_command_/ } keys %{ __PACKAGE__ . '::' } };
    die "usage: git hub <subcommand> [args...]\n"
      . "subcommands:\n"
      . "  " . (join "\n  ", map { s/^run_command_//; $_ } @subcommands)
      . "\n";
}

sub run_command {
    my $self = shift;
    my ($command) = @_;

    my $meth = "run_command_$command";
    if ($self->can($meth)) {
        return $self->$meth($self->argv);
    }
    else {
        $self->usage;
    }
}

sub run_command_create {
    my $self = shift;
    my ($name, $description, $url) = @_;
    die "usage: git hub create <name> <description> [url]"
        unless @_ >= 2;

    $self->_github(repo => $name)->repos->create(
        $name, $description, $url || '', 1
    );
}

sub run_command_delete {
    my $self = shift;
    die "usage: git hub delete [-f] <name>"
        unless @_ == 1 || @_ == 2;

    my $force;
    if ($_[0] eq '-f') {
        $force = 1;
        shift @_;
    }

    my ($name) = @_;

    warn "not actually deleting $name (-f not set)\n" unless $force;

    my $response = $self->_github(repo => $name)->repos->delete(
        $force ? ({confirm => 1}) : ()
    );

    if ($response->{error}) {
        die "error deleting repository $name: $response->{error}\n";
    }
}

sub run_command_fork {
    my $self = shift;
    my ($owner, $repo) = @_;
    die "usage: git hub fork <owner> <repo>"
        unless @_ == 2;

    $self->_github(owner => $owner, repo => $repo)->repos->fork;
}

sub run_command_clone {
    my $self = shift;
    die "usage: git hub clone [--fork] [owner] <repo>"
        unless @_ == 1 || @_ == 2 || @_ == 3;

    my $fork;
    if ($_[0] eq '--fork') {
        shift @_;
        $fork = 1;
    }

    if (@_ == 1) {
        my ($repo) = @_;
        my $user = $self->user;
        git "clone git\@github.com:$user/$repo.git";
    }
    else {
        my ($owner, $repo) = @_;
        if ($fork) {
            $self->run_command_fork($owner, $repo);
            $self->run_command_clone($repo);
            {
                my $d = pushd($repo);
                $self->run_command_add($owner, $repo);
            }
        }
        else {
            git "clone git://github.com/$owner/$repo.git";
        }
    }
}

sub run_command_list {
    my $self = shift;
    die "usage: git hub list [-v] [owner]"
        unless @_ <= 2;

    my $verbose;
    if (@_ > 0 && $_[0] eq '-v') {
        $verbose = 1;
        shift @_;
    }

    my $owner = defined $_[0] ? $_[0] : $self->user;

    print for map {
              $verbose
                  ? ("$_->{name}:\n"
                    . ($_->{description} ? "  $_->{description}\n" : '')
                    . ($_->{homepage}    ? "  $_->{homepage}\n"    : ''))
                  : "$_->{name}\n"
          } @{ $self->_github(owner => $owner, repo => '')->repos->list };
}

sub run_command_pullreq {
    my $self = shift;
    my ($owner, $repo, $title, $branch, $body) = @_;
    die "usage: git hub pullreq <owner> <repo> <title> [branch] [description]"
        unless @_ == 3 || @_ == 4 || @_ == 5;

    # XXX: should default to the current branch
    $branch ||= 'master';

    $self->_github(owner => $owner, repo => $repo)->pull_request->pull_request(
        title => $title,
        body  => $body,
        head  => $self->user . ':' . $branch
    );
}

sub run_command_add {
    my $self = shift;
    die "usage: git hub add [owner] <repo>"
        unless @_ == 1 || @_ == 2;
    my ($owner, $repo) = (@_ > 1 ? @_ : (undef, $_[0]));

    my $remote;
    if ($owner) {
        $remote = $owner;
        git "remote add $remote git://github.com/$owner/$repo.git";
    }
    else {
        $owner = $self->user;
        $remote = (any { /\borigin\b/ } git "remote") ? 'github' : 'origin';
        git "remote add $remote git\@github.com:$owner/$repo.git";
    }

    git "fetch $remote";
}

sub run_command_pull {
    my $self = shift;
    die "usage: git hub pull <num>"
        unless @_ == 1;
    my ($num) = @_;

    my $remote = (any { /\bgithub\b/ } git "remote") ? 'github' : 'origin';
    git "fetch $remote refs/pull/$num/head:pull-$num";
}

sub _github {
    my $self = shift;
    my %opts = @_;

    my $user = $ENV{USER};
    my $token;

    if (open my $login_info, '<', "$ENV{HOME}/.github") {
        $user  = <$login_info>;
        chomp $user;
        $token = <$login_info>;
        chomp $token;
    }

    Net::GitHub::V2->new(
        (defined $user
            ? (owner => $user, login => $user)
            : ()),
        (defined $token
            ? (token => $token)
            : ()),
        repo => 'something_not_real',
        %opts,
    );
}

package main;

App::Git::Hub->new(@ARGV)->run;