summaryrefslogtreecommitdiff
path: root/rb-patch
blob: 96dbae1a9ce0d4eb1fcbffbf3c7b6519189c699c (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
#!/usr/bin/perl -w

use strict;
use LWP::Simple;
use LWP::UserAgent;
use Getopt::Long;
use Pod::Usage;
use Digest::SHA1 qw /sha1_hex/;

my %opt = (
	diff_num => 1,
);

GetOptions(
        'review-num|review|r=i' => \$opt{review_num},
        'diff-num|diff|d=i' => \$opt{diff_num},
        'help|?' => \$opt{help},
        man => \$opt{man}
) or pod2usage(2);
pod2usage(1) if ($opt{help});
if (not defined $opt{review_num}) {
	print STDERR "$0: Error: review number (-r) is required\n";
	pod2usage(2);
}
pod2usage(-exitstatus => 0, -verbose => 2) if $opt{man};


my $BASE_URL="https://reviewboard.asterisk.org";


my $review_num = $opt{review_num};
my $diff_num = $opt{diff_num};
my $review_url = "$BASE_URL/r/$review_num";
my $diff_page_url = "$review_url/diff/$diff_num";
my $diff_url = "$diff_page_url/raw";

my $content = get($diff_page_url);
die "$0: Failed to get reviewboard page $diff_page_url\n" unless defined $content;

# Simple lines-based parsing will do hor now
my @page = split(/\n/, $content);
$content =~ m{<h1 id="summary" class="editable">([^<]*)</h1>};
my $summary = $1;
$content =~ m{<a id="submitter" href="([^"]*)">([^<]*)</a>};
my ($url, $name) = ($1, $2);
$content =~ m{<span id="branch" class="editable">([^<]*)</a>};
my $branch = $1;
$url = "$BASE_URL$url";

my $ua = LWP::UserAgent->new;
my $response = $ua->get($diff_url);

#my $raw_diff = get($diff_url);
die "$0: Failed to get raw diff $diff_url".($response->status_live)."\n"
	unless $response->is_success;
my $raw_diff = $response->content;
my $date = $response->header('Date');

my $checksum = sha1_hex("$review_num::$diff_num::$name::$date");
#my @head = head($diff_page_url);
##my $date = $head[2];
#my $date = join '>|<', @head;

# Fix patch-level from -p0 to -p1
# Alternatively you could use git-am -p0
$raw_diff =~ m{\n--- [^\n]*\s\(revision (\d+)\)};
my $revision = $1;
$raw_diff =~ s{\n--- }   {$&a/}g;
$raw_diff =~ s{\n\+\+\+ }{$&b/}g;

print 
"From $checksum $date
From: $name <$url>
Date: $date
Subject: [PATCH] $summary

Review No. $review_num. diff no. $diff_num.
Base branch: $branch, revision: $revision

<fill in further description>

$raw_diff

-- 
$0
"

__END__

=head1 NAME
rb-patch

=head1 SYNOPSIS

rb-patch -r I<NUM>

Options:
  -r --review  number of reivewboard review to fetch. Required.  
  --help       brief help message
  --man        full documentation

=head1 OPTIONS

=over

=item B<--review> I<num>

Fetch ReviewBoard review number I<num>. This parameter is required.

=item B<--help>

Print a brief help message and exits.

=item B<--man>

Prints the manual page and exits.

=back

=head1 DESCRIPTION

This script is intended, in the long run, to convert a review-board review
to a mailbox that could be merged into a git repository through git-am(1).

At the moment, however, it only fetches latest review. Use:

  git checkout -b test-branch
  ../asterisk-tools/rb-patch -r I<num> >patch
  git am patch

=cut