summaryrefslogtreecommitdiff
path: root/bin/git-pasteapply
blob: e4876b4c27b6607038b7ccbd663393151fb6d519 (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
#!/usr/bin/perl
# Copyright (C) 2015 The Qt Company Ltd.
# Contact: http://www.qt.io/licensing/
#
# You may use this file under the terms of the 3-clause BSD license.
# See the file LICENSE from this package for details.
#

use strict;
use WWW::Mechanize;
use Config;

unless (scalar @ARGV) {
    print "Usage: git pasteapply [options] <number> [<number>...]\n" .
        "Fetches the paste from pastebin.ca numbered <number> and applies\n" .
        "Options are passed directly to git am\n" .
        "\n" .
        "Useful options:\n" .
        " -s, --signoff         Add Signed-off-by: line to the commit message\n" .
        " -3, --3way            When the patch does not apply cleanly, fall back on 3-way merge\n" .
        " -p<n>                 Strip <n> elements of the paths (default 1)\n" .
        " --whitespace=<nowarn,warn,fix,error,error-all>\n";
    exit 0;
}
my @pastes;
my @args;

for (@ARGV) {
    if (m/^-/) {
        push @args, $_;
    } else {
        push @pastes, $_;
    }
}

open GIT_AM, "|-", "git", "am", @args, "-"
    or die "Cannot start git-am: $!";

my $www = WWW::Mechanize->new();
foreach my $paste (@pastes) {
    my $url = pastebin_url($paste);
    print "Applying $url\n";
    $www->get($url);
    my $content = $www->content();
    $content =~ s/\r\n/\n/g;
    print GIT_AM $content;
}

close GIT_AM;
exit $?;

sub pastebin_url($) {
    my $arg = $_[0];
    return "http://pastebin.ca/raw/$3"
        if ($arg =~ m,^(https?://)?(.*\.)?pastebin\.ca/([0-9]+),);
    return "http://pastebin.com/download.php?i=$3"
        if ($arg =~ m,^(https?://)?(.*\.)?pastebin.com/([a-zA-Z0-9]+)$,);

    return $arg if ($arg =~ m,^http://,); # Assume it's plain text...

    # No http:// prefix
    return "http://pastebin.ca/raw/$arg" if ($ENV{PASTEBIN} =~ /pastebin\.ca/);
    return "http://pastebin.com/download.php?i=$arg"; # Fallback, assume pastebin.com
}