summaryrefslogtreecommitdiff
path: root/rb-patch
blob: eeaa800348d92438957483feb39390a357b42f30 (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
#!/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 = (
);

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 $reviewboard_server ="reviewboard.asterisk.org"; 
my $base_url="https://reviewboard.asterisk.org";

my $diff_num;
my $diff_num_str = "";
if (defined $opt{diff_num}) {
	$diff_num = $opt{diff_num};
	print STDERR "$diff_num\n";
	$diff_num_str = "/$diff_num";
	exit 1;
}
my $review_num = $opt{review_num};
my $review_url = "$base_url/r/$review_num";
my $diff_page_url = "$review_url/diff$diff_num_str";
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="/users/([^/]*)/">([^<]*)</a>};
my ($user_name, $full_name) = ($1, $2);
$content =~ m{<span id="branch" class="editable">([^<]*)</span>};
my $branch = $1;
$content =~ m{var gRevision = '(\d+)';};
$diff_num = $1;

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}::${user_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: $full_name <$user_name\@$reviewboard_server>
Date: $date
Subject: [PATCH] $summary

Review: $review_num
Diff: $diff_num
Svn-Branch: $branch
Svn-Revision: $revision
Origin: $diff_page_url

<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.
  -d --diff    number of the diff to fetch. Default: last one.
  --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<--diff> I<num>

Fetch diff no. I<num> rather than the last one.

=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