summaryrefslogtreecommitdiffstats
path: root/bin/git-pastebin
blob: 4b17373f336e3719a0c9d0d158d6457bc9cace96 (plain)
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
#!/usr/bin/perl
# Arguments are:
#  git pastebin [<commit>]
#

# Copyright (C) Thiago Macieira <thiago@kde.org>
# Additional fixes by Giuseppe D'Angelo <dangelog@gmail.com>
# This program is based on paste2pastebin.pl, that is
#   Copyright (C) Fred Blaise <chapeaurouge_at_madpenguin_dot_org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

use strict;
use LWP::UserAgent;
use WWW::Mechanize;

my $description;
my $content;
my $squash = -1;
my @committish;
my @fpargs;
my @pathargs;
my $dashdash = 0;

while (scalar @ARGV) {
    my $arg = shift @ARGV;

    if ($arg eq "-d" || $arg eq "--description") {
        die "Option $arg requires an argument" unless scalar @ARGV;
        $description = shift @ARGV;
    } elsif ($arg eq "-s" || $arg eq "--squash") {
        $squash = 1;
    } elsif ($arg eq "--no-squash") {
        $squash = 0;
    } elsif ($arg eq "--") {
        $dashdash = 1;
    } elsif ($arg =~ m/^-/) {
        push @fpargs, $arg;
    } elsif ($dashdash) {
        push @pathargs, $arg;
    } else {
        push @committish, $arg;
    }
}

# Squash by default if description was given
$squash = $description if ($squash == -1);

# Default revision is HEAD
push @committish, "HEAD" unless (scalar @committish);

# Prepend -- if pathargs isn't empty
unshift @pathargs, "--" if (scalar @pathargs);

# Try to parse this commit
my @revlist;
my $needsrevlist = 0;
open REVPARSE, "-|", "git", "rev-parse", @committish;
while (<REVPARSE>) {
    chomp;
    push @revlist, $_;
    $needsrevlist = 1 if (m/^\^/);
}
close REVPARSE;

if ($needsrevlist) {
    # Get the revision list then
    open REVLIST, "-|", "git", "rev-list", "--reverse", @revlist
        or die "Cannot run git-rev-list: $!";
    @revlist = ();
    while (<REVLIST>) {
        chomp;
        push @revlist, $_;
    }
    close REVLIST;
}

# Are we squashing?
if (scalar @revlist > 1 && $squash) {
    # Yes, this one is easy
    open FORMATPATCH, "-|", "git", "format-patch", "--stdout", @fpargs, @committish, @pathargs
        or die "Cannot run git format-patch: $!";

    while (<FORMATPATCH>) {
        $content .= $_;
    }
    close FORMATPATCH;

    submit($description, $content);
    exit(0);
}

# No, we're not squashing
# Iterate over the commits
for my $rev (@revlist) {
    $content = "";
    $description = "";

    # Get description
    open PRETTY, "-|", "git", "log", "--pretty=format:%s%n%b", "$rev^!"
        or die "Cannot get description for $rev: $!";
    while (<PRETTY>) {
        $description .= $_;
    }
    close PRETTY;

    # Get patch
    open FORMATPATCH, "-|", "git", "format-patch", "--stdout", @fpargs, "$rev^!", @pathargs
        or die "Cannot get patch for $rev: $!";
    while (<FORMATPATCH>) {
        $content .= $_;
    }
    close FORMATPATCH;

    submit($description, $content);
}

sub submit($$) {
    if ($ENV{PASTEBIN} =~ /pastebin\.ca/) {
        submit_pastebinca(@_);
    } elsif ($ENV{PASTEBIN} =~ /pastebin.com/) {
        submit_pastebincom(@_);
    } elsif (!$ENV{PASTEBIN}) {
        submit_default(@_);
    } else {
        die "Sorry, I don't know how to talk to $ENV{PASTEBIN}."
    }
}

sub submit_default($$) {
    submit_pastebincom(@_);
}

sub submit_pastebincom($$) {
    my ($description, $content) = @_;
    my $paste_subdomain;
    $paste_subdomain = $2 if ($ENV{PASTEBIN} =~ m,(https?://)?(.*)\.pastebin\.com,);

    my %fields = (
        paste_code      =>       $content,
        paste_subdomain =>       $paste_subdomain,
        paste_format    =>       'diff',
        paste_name      =>       $ENV{PASTEBIN_NAME}
    );

    my $agent = LWP::UserAgent->new();
    my $api = 'http://pastebin.com/api_public.php';

    my $reply = $agent->post($api, \%fields);

    die "Could not send paste: $!" unless ($reply->is_success);

    my $reply_content = $reply->decoded_content;

    # actually, pastebin.com is so dumb that it returns
    # HTTP 200 OK even in case of error; the content then contains an error message...
    die "Could not send paste: $reply_content" if ($reply_content =~ /^ERROR/);

    print $reply_content, "\n";
}

sub submit_pastebinca($$) {
    my ($description, $content) = @_;
    my %fields = (
        content         =>      $content,
        description     =>      $description,
        type            =>      '34', # Unfortunately, "Diff/Patch" is missing in the quiet interface
        expiry          =>      '1 month',
        name            =>      '',
    );
    my $www = WWW::Mechanize->new();

    my $api_key       = "1OIIB1/PYzkNe/azsjOFkm4iBe0414V0";
    my $pastebin_url  = "http://pastebin.ca/quiet-paste.php?api=$api_key";
    my $pastebin_root = "http://pastebin.ca";

    $www->get($pastebin_url);
    die "Cannot get the pastebin form: $!" unless ($www->success());

    $www->form_name('pasteform');
    $www->set_fields(%fields);
    $www->submit_form(button => 's');

    die "Could not send paste: $!" unless ($www->success());

    $content = $www->content();
    if($content =~ m/^SUCCESS:(.*)/) {
        print "$pastebin_root/$1\n";
    } else {
        print $content;
    }
}